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