UNPKG

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