-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcloudinaryUrl.js
47 lines (41 loc) · 919 Bytes
/
cloudinaryUrl.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
const TYPES = {
fetch_format: "f",
crop: "c",
effect: "e",
flags: "fl",
gravity: "g",
height: "h",
radius: "r",
quality: "q",
width: "w",
dpr: "dpr"
};
module.exports = (id, options = {}) => {
if (!options.cloud_name) throw Error("options.cloud_name required");
const scheme = options.secure ? "https" : "http";
const source = options.source || "upload";
const keys = Object.keys(options);
const urlParams = keys
.map(key => {
const prefix = TYPES[key];
const value = options[key];
if (prefix) {
return `${prefix}_${value}`;
}
})
.filter(Boolean)
.join(",");
const version = options.version && `v${options.version}`;
const url = [
`${scheme}://res.cloudinary.com`,
encodeURIComponent(options.cloud_name),
"image",
source,
urlParams,
version,
id
]
.filter(Boolean)
.join("/");
return url;
};