Skip to content

Commit

Permalink
feat: make cname, secureDestination and privateCdn configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
olavea committed Oct 6, 2023
1 parent f10ce2a commit 9f62f92
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 18 deletions.
1 change: 0 additions & 1 deletion demo/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ module.exports = {
context: true,
maxResults: 10,
prefix: process.env.CLOUDINARY_SOURCE_PREFIX,
secure: false,
},
},
{
Expand Down
35 changes: 32 additions & 3 deletions plugin/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@ exports.pluginOptionsSchema = ({ Joi }) => {
prefix: Joi.string(),
context: Joi.boolean(),
secure: Joi.boolean().default(true),
cname: Joi.string(),
secureDistribution: Joi.string(),
privateCdn: Joi.boolean().default(false),
});
};

const getNodeData = (gatsbyUtils, media, cloudName, secure) => {
const getNodeData = (
gatsbyUtils,
media,
cloudName,
secure,
cname,
secureDistribution,
privateCdn,
) => {
const { createNodeId, createContentDigest } = gatsbyUtils;

const url = generateCloudinaryUrl(media, {
secure: secure,
cname: cname,
secure_distribution: secureDistribution,
private_cdn: privateCdn,
});

return {
Expand Down Expand Up @@ -66,6 +80,9 @@ const createCloudinaryNodes = async (
resourceOptions,
cloudName,
secure,
cname,
secureDistribution,
privateCdn,
) => {
const { actions, reporter } = gatsbyUtils;
const { max_results, results_per_page } = resourceOptions;
Expand All @@ -83,7 +100,15 @@ const createCloudinaryNodes = async (
});

result.resources.forEach((resource) => {
const nodeData = getNodeData(gatsbyUtils, resource, cloudName, secure);
const nodeData = getNodeData(
gatsbyUtils,
resource,
cloudName,
secure,
cname,
secureDistribution,
privateCdn,
);
actions.createNode(nodeData);
});

Expand All @@ -109,7 +134,8 @@ const createCloudinaryNodes = async (
};

exports.sourceNodes = async (gatsbyUtils, pluginOptions) => {
const { cloudName, secure } = pluginOptions;
const { cloudName, secure, cname, secureDistribution, privateCdn } =
pluginOptions;
const cloudinary = newCloudinary(pluginOptions);
const resourceOptions = getResourceOptions(pluginOptions);

Expand All @@ -119,5 +145,8 @@ exports.sourceNodes = async (gatsbyUtils, pluginOptions) => {
resourceOptions,
cloudName,
secure,
cname,
secureDistribution,
privateCdn,
);
};
7 changes: 7 additions & 0 deletions plugin/gatsby-node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ describe('pluginOptionsSchema', () => {
prefix: 800,
context: '',
secure: 'not a boolean',
cname: 2,
secureDistribution: 3,
privateCdn: 'not a boolean',
};

const { isValid, errors } = await testPluginOptionsSchema(
Expand All @@ -50,6 +53,9 @@ describe('pluginOptionsSchema', () => {
`"prefix" must be a string`,
`"context" must be a boolean`,
`"secure" must be a boolean`,
`"cname" must be a string`,
`"secureDistribution" must be a string`,
`"privateCdn" must be a boolean`,
]);
});

Expand All @@ -69,6 +75,7 @@ describe('pluginOptionsSchema', () => {
resultsPerPage: 10,
tags: false,
secure: true,
privateCdn: false,
});
});
});
5 changes: 4 additions & 1 deletion plugin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const getResourceOptions = (options) => {

const generateCloudinaryUrl = (
{ cloud_name, public_id, resource_type },
{ secure },
{ secure, cname, secure_distribution, private_cdn },
) => {
const url = cloudinary.url(public_id, {
resource_type: resource_type,
Expand All @@ -58,6 +58,9 @@ const generateCloudinaryUrl = (
sdkCode: SDK_CODE,
sdkSemver: SDK_SEMVER,
techVersion: TECH_VERSION,
cname: cname,
secure_distribution: secure_distribution,
private_cdn: private_cdn,
});

return url;
Expand Down
113 changes: 100 additions & 13 deletions plugin/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,110 @@ describe('getResourceOptions', () => {

describe('generateCloudinaryUrl', () => {
const ANALYTICS_CODE = 'AXOrEGQ0';
describe('generates correct url for image', () => {
it('for secure is true', () => {
const asset = {
public_id: 'public-id',
cloud_name: 'cloud-name',
};

it('generates correct url for image (default)', () => {
const asset = {
public_id: 'public-id',
cloud_name: 'cloud-name',
};
const secureUrl = generateCloudinaryUrl(asset, { secure: true });
expect(secureUrl).toBe(
`https://res.cloudinary.com/cloud-name/image/upload/f_auto,q_auto/public-id?_a=${ANALYTICS_CODE}`,
);
});

const secureUrl = generateCloudinaryUrl(asset, { secure: true });
expect(secureUrl).toBe(
`https://res.cloudinary.com/cloud-name/image/upload/f_auto,q_auto/public-id?_a=${ANALYTICS_CODE}`,
);
const url = generateCloudinaryUrl(asset, { secure: false });
expect(url).toBe(
`http://res.cloudinary.com/cloud-name/image/upload/f_auto,q_auto/public-id?_a=${ANALYTICS_CODE}`,
);
it('for secure is false', () => {
const asset = {
public_id: 'public-id',
cloud_name: 'cloud-name',
};

const url = generateCloudinaryUrl(asset, { secure: false });
expect(url).toBe(
`http://res.cloudinary.com/cloud-name/image/upload/f_auto,q_auto/public-id?_a=${ANALYTICS_CODE}`,
);
});

it('for custom cname and secure is false', () => {
const asset = {
public_id: 'public-id',
cloud_name: 'cloud-name',
};

const url = generateCloudinaryUrl(asset, {
secure: false,
cname: 'example.com',
});
expect(url).toBe(
`http://example.com/cloud-name/image/upload/f_auto,q_auto/public-id?_a=${ANALYTICS_CODE}`,
);
});

it('for custom cname and secure is true', () => {
const asset = {
public_id: 'public-id',
cloud_name: 'cloud-name',
};

const url = generateCloudinaryUrl(asset, {
secure: true,
secure_distribution: 'example.com',
});
expect(url).toBe(
`https://example.com/cloud-name/image/upload/f_auto,q_auto/public-id?_a=${ANALYTICS_CODE}`,
);
});

it('for custom secure_distribution (cname) and secure is true', () => {
const asset = {
public_id: 'public-id',
cloud_name: 'cloud-name',
};

const url = generateCloudinaryUrl(asset, {
secure: true,
secure_distribution: 'example.com',
});
expect(url).toBe(
`https://example.com/cloud-name/image/upload/f_auto,q_auto/public-id?_a=${ANALYTICS_CODE}`,
);
});

it('for private_cdn and secure is true', () => {
const asset = {
public_id: 'public-id',
cloud_name: 'cloud-name',
};

const url = generateCloudinaryUrl(asset, {
secure: true,
private_cdn: true,
});
expect(url).toBe(
`https://cloud-name-res.cloudinary.com/image/upload/f_auto,q_auto/public-id?_a=${ANALYTICS_CODE}`,
);
});

it('for private_cdn and secure is false', () => {
const asset = {
public_id: 'public-id',
cloud_name: 'cloud-name',
};

const url = generateCloudinaryUrl(asset, {
secure: false,
private_cdn: true,
});
expect(url).toBe(
`http://cloud-name-res.cloudinary.com/image/upload/f_auto,q_auto/public-id?_a=${ANALYTICS_CODE}`,
);
});
});

// cname: ,
// secure_destination,
// private_cdn
it('generates correct url for video', () => {
const asset = {
public_id: 'public-id',
Expand Down

0 comments on commit 9f62f92

Please sign in to comment.