UNPKG

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