UNPKG

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