UNPKG

4.47 kBJavaScriptView Raw
1/**
2 * Assign a value to a nested object
3 * @function putNestedValue
4 * @param params the parent object - this argument will be modified!
5 * @param key key in the form nested[innerkey]
6 * @param value the value to assign
7 * @return the modified params object
8 */
9const url = require('url');
10const extend = require("lodash/extend");
11const isObject = require("lodash/isObject");
12const isString = require("lodash/isString");
13const isUndefined = require("lodash/isUndefined");
14const isEmpty = require("lodash/isEmpty");
15const entries = require('./utils/entries');
16
17let cloudinary_config = void 0;
18
19/**
20 * Sets a value in an object using a nested key
21 * @param {object} params The object to assign the value in.
22 * @param {string} key The key of the value. A period is used to denote inner keys.
23 * @param {*} value The value to set.
24 * @returns {object} The params argument.
25 * @example
26 * let o = {foo: {bar: 1}};
27 * putNestedValue(o, 'foo.bar', 2); // {foo: {bar: 2}}
28 * putNestedValue(o, 'foo.inner.key', 'this creates an inner object');
29 * // {{foo: {bar: 2}, inner: {key: 'this creates an inner object'}}}
30 */
31function putNestedValue(params, key, value) {
32 let chain = key.split(/[\[\]]+/).filter(i => i.length);
33 let outer = params;
34 let lastKey = chain.pop();
35 for (let j = 0; j < chain.length; j++) {
36 let innerKey = chain[j];
37 let inner = outer[innerKey];
38 if (inner == null) {
39 inner = {};
40 outer[innerKey] = inner;
41 }
42 outer = inner;
43 }
44 outer[lastKey] = value;
45 return params;
46}
47
48function parseCloudinaryConfigFromEnvURL(ENV_STR) {
49 let conf = {};
50
51 let uri = url.parse(ENV_STR, true);
52
53 if (uri.protocol === 'cloudinary:') {
54 conf = Object.assign({}, conf, {
55 cloud_name: uri.host,
56 api_key: uri.auth && uri.auth.split(":")[0],
57 api_secret: uri.auth && uri.auth.split(":")[1],
58 private_cdn: uri.pathname != null,
59 secure_distribution: uri.pathname && uri.pathname.substring(1)
60 });
61 } else if (uri.protocol === 'account:') {
62 conf = Object.assign({}, conf, {
63 account_id: uri.host,
64 provisioning_api_key: uri.auth && uri.auth.split(":")[0],
65 provisioning_api_secret: uri.auth && uri.auth.split(":")[1]
66 });
67 }
68
69 return conf;
70}
71
72function extendCloudinaryConfigFromQuery(ENV_URL, confToExtend = {}) {
73 let uri = url.parse(ENV_URL, true);
74 if (uri.query != null) {
75 entries(uri.query).forEach(([key, value]) => putNestedValue(confToExtend, key, value));
76 }
77}
78
79function extendCloudinaryConfig(parsedConfig, confToExtend = {}) {
80 entries(parsedConfig).forEach(([key, value]) => {
81 if (value !== undefined) {
82 confToExtend[key] = value;
83 }
84 });
85
86 return confToExtend;
87}
88
89module.exports = function (new_config, new_value) {
90 if ((cloudinary_config == null) || new_config === true) {
91 if (cloudinary_config == null) {
92 cloudinary_config = {};
93 } else {
94 Object.keys(cloudinary_config).forEach(key => delete cloudinary_config[key]);
95 }
96
97 let CLOUDINARY_ENV_URL = process.env.CLOUDINARY_URL;
98 let CLOUDINARY_ENV_ACCOUNT_URL = process.env.CLOUDINARY_ACCOUNT_URL;
99 let CLOUDINARY_API_PROXY = process.env.CLOUDINARY_API_PROXY;
100
101 if (CLOUDINARY_ENV_URL && !CLOUDINARY_ENV_URL.toLowerCase().startsWith('cloudinary://')) {
102 throw new Error("Invalid CLOUDINARY_URL protocol. URL should begin with 'cloudinary://'");
103 }
104 if (CLOUDINARY_ENV_ACCOUNT_URL && !CLOUDINARY_ENV_ACCOUNT_URL.toLowerCase().startsWith('account://')) {
105 throw new Error("Invalid CLOUDINARY_ACCOUNT_URL protocol. URL should begin with 'account://'");
106 }
107 if (!isEmpty(CLOUDINARY_API_PROXY)) {
108 extendCloudinaryConfig({ api_proxy: CLOUDINARY_API_PROXY }, cloudinary_config);
109 }
110
111 [CLOUDINARY_ENV_URL, CLOUDINARY_ENV_ACCOUNT_URL].forEach((ENV_URL) => {
112 if (ENV_URL) {
113 let parsedConfig = parseCloudinaryConfigFromEnvURL(ENV_URL);
114 extendCloudinaryConfig(parsedConfig, cloudinary_config);
115 // Provide Query support in ENV url cloudinary://key:secret@test123?foo[bar]=value
116 // expect(cloudinary_config.foo.bar).to.eql('value')
117 extendCloudinaryConfigFromQuery(ENV_URL, cloudinary_config);
118 }
119 });
120 }
121 if (!isUndefined(new_value)) {
122 cloudinary_config[new_config] = new_value;
123 } else if (isString(new_config)) {
124 return cloudinary_config[new_config];
125 } else if (isObject(new_config)) {
126 extend(cloudinary_config, new_config);
127 }
128 return cloudinary_config;
129};