forked from contentstack/gatsby-source-contentstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-helper.js
More file actions
51 lines (40 loc) · 1.48 KB
/
Copy pathimage-helper.js
File metadata and controls
51 lines (40 loc) · 1.48 KB
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
'use strict';
var _require = require('url'),
URLSearchParams = _require.URLSearchParams; // Determine the proper file extension based on mime type
var mimeTypeExtensions = {
'image/jpeg': '.jpg',
'image/jpg': '.jpg',
'image/gif': '.gif',
'image/png': '.png',
'image/webp': '.webp'
}; // Supported image formats by contentstack image API
var validImageFormats = ['jpg', 'png', 'webp', 'gif'];
var isImage = function isImage(image) {
return !!mimeTypeExtensions[image === null || image === void 0 ? void 0 : image.content_type];
}; // Creates a Contentstack image url
var createUrl = function createUrl(imgUrl) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var queryParams = {
width: options.width,
height: options.height,
format: options.toFormat,
quality: options.quality,
crop: options.crop,
fit: options.fit,
trim: options.trim,
pad: options.pad,
'bg-color': options.background
};
var searchParams = new URLSearchParams();
for (var key in queryParams) {
if (typeof queryParams[key] !== 'undefined') {
var _queryParams$key;
searchParams.append(key, (_queryParams$key = queryParams[key]) !== null && _queryParams$key !== void 0 ? _queryParams$key : '');
}
}
return "".concat(imgUrl, "?").concat(searchParams.toString());
};
exports.mimeTypeExtensions = mimeTypeExtensions;
exports.validImageFormats = validImageFormats;
exports.isImage = isImage;
exports.createUrl = createUrl;