-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.js
70 lines (59 loc) · 1.45 KB
/
utils.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
const cloudinary = require('cloudinary').v2;
const { snakeCase } = require('lodash');
const pluginPkg = require('./package.json');
const gatsbyPkg = require('gatsby/package.json');
const SDK_CODE = 'X';
const SDK_SEMVER = pluginPkg.version;
const TECH_VERSION = gatsbyPkg.version;
const DEFAULT_KEYS = [
'resourceType',
'prefix',
'tags',
'maxResults',
'type',
'context',
'resultsPerPage',
];
const DEFAULT_TYPE = 'upload';
const newCloudinary = (options) => {
cloudinary.config({
cloud_name: options.cloudName,
api_key: options.apiKey,
api_secret: options.apiSecret,
});
return cloudinary;
};
const getResourceOptions = (options) => {
let result = {};
DEFAULT_KEYS.forEach((key) => {
if (typeof options[key] !== 'undefined') {
result[snakeCase(key)] = options[key];
}
});
result.type = result.type || DEFAULT_TYPE;
if (!result.results_per_page) {
result.results_per_page = result.max_results;
}
return result;
};
const generateCloudinaryUrl = (
{ cloud_name, public_id, resource_type },
{ secure },
) => {
const url = cloudinary.url(public_id, {
resource_type: resource_type,
cloud_name: cloud_name,
secure: secure,
transformation: { quality: 'auto', fetch_format: 'auto' },
urlAnalytics: true,
sdkCode: SDK_CODE,
sdkSemver: SDK_SEMVER,
techVersion: TECH_VERSION,
});
return url;
};
module.exports = {
newCloudinary,
getResourceOptions,
generateCloudinaryUrl,
};