cloudinary-microurl
Version:
A tiny library to generate cloudinary URLs from options objects
48 lines (41 loc) • 919 B
JavaScript
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;
};