builder.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  14. if (k2 === undefined) k2 = k;
  15. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  16. }) : (function(o, m, k, k2) {
  17. if (k2 === undefined) k2 = k;
  18. o[k2] = m[k];
  19. }));
  20. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  21. Object.defineProperty(o, "default", { enumerable: true, value: v });
  22. }) : function(o, v) {
  23. o["default"] = v;
  24. });
  25. var __importStar = (this && this.__importStar) || function (mod) {
  26. if (mod && mod.__esModule) return mod;
  27. var result = {};
  28. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  29. __setModuleDefault(result, mod);
  30. return result;
  31. };
  32. Object.defineProperty(exports, "__esModule", { value: true });
  33. exports.ImageUrlBuilder = void 0;
  34. var urlForImage_1 = __importStar(require("./urlForImage"));
  35. var validFits = ['clip', 'crop', 'fill', 'fillmax', 'max', 'scale', 'min'];
  36. var validCrops = ['top', 'bottom', 'left', 'right', 'center', 'focalpoint', 'entropy'];
  37. var validAutoModes = ['format'];
  38. function isSanityModernClientLike(client) {
  39. return client && 'config' in client ? typeof client.config === 'function' : false;
  40. }
  41. function isSanityClientLike(client) {
  42. return client && 'clientConfig' in client ? typeof client.clientConfig === 'object' : false;
  43. }
  44. function rewriteSpecName(key) {
  45. var specs = urlForImage_1.SPEC_NAME_TO_URL_NAME_MAPPINGS;
  46. for (var _i = 0, specs_1 = specs; _i < specs_1.length; _i++) {
  47. var entry = specs_1[_i];
  48. var specName = entry[0], param = entry[1];
  49. if (key === specName || key === param) {
  50. return specName;
  51. }
  52. }
  53. return key;
  54. }
  55. function urlBuilder(options) {
  56. // Did we get a modernish client?
  57. if (isSanityModernClientLike(options)) {
  58. // Inherit config from client
  59. var _a = options.config(), apiUrl = _a.apiHost, projectId = _a.projectId, dataset = _a.dataset;
  60. var apiHost = apiUrl || 'https://api.sanity.io';
  61. return new ImageUrlBuilder(null, {
  62. baseUrl: apiHost.replace(/^https:\/\/api\./, 'https://cdn.'),
  63. projectId: projectId,
  64. dataset: dataset,
  65. });
  66. }
  67. // Did we get a SanityClient?
  68. var client = options;
  69. if (isSanityClientLike(client)) {
  70. // Inherit config from client
  71. var _b = client.clientConfig, apiUrl = _b.apiHost, projectId = _b.projectId, dataset = _b.dataset;
  72. var apiHost = apiUrl || 'https://api.sanity.io';
  73. return new ImageUrlBuilder(null, {
  74. baseUrl: apiHost.replace(/^https:\/\/api\./, 'https://cdn.'),
  75. projectId: projectId,
  76. dataset: dataset,
  77. });
  78. }
  79. // Or just accept the options as given
  80. return new ImageUrlBuilder(null, options);
  81. }
  82. exports.default = urlBuilder;
  83. var ImageUrlBuilder = /** @class */ (function () {
  84. function ImageUrlBuilder(parent, options) {
  85. this.options = parent
  86. ? __assign(__assign({}, (parent.options || {})), (options || {})) : __assign({}, (options || {})); // Copy options
  87. }
  88. ImageUrlBuilder.prototype.withOptions = function (options) {
  89. var baseUrl = options.baseUrl || this.options.baseUrl;
  90. var newOptions = { baseUrl: baseUrl };
  91. for (var key in options) {
  92. if (options.hasOwnProperty(key)) {
  93. var specKey = rewriteSpecName(key);
  94. newOptions[specKey] = options[key];
  95. }
  96. }
  97. return new ImageUrlBuilder(this, __assign({ baseUrl: baseUrl }, newOptions));
  98. };
  99. // The image to be represented. Accepts a Sanity 'image'-document, 'asset'-document or
  100. // _id of asset. To get the benefit of automatic hot-spot/crop integration with the content
  101. // studio, the 'image'-document must be provided.
  102. ImageUrlBuilder.prototype.image = function (source) {
  103. return this.withOptions({ source: source });
  104. };
  105. // Specify the dataset
  106. ImageUrlBuilder.prototype.dataset = function (dataset) {
  107. return this.withOptions({ dataset: dataset });
  108. };
  109. // Specify the projectId
  110. ImageUrlBuilder.prototype.projectId = function (projectId) {
  111. return this.withOptions({ projectId: projectId });
  112. };
  113. // Specify background color
  114. ImageUrlBuilder.prototype.bg = function (bg) {
  115. return this.withOptions({ bg: bg });
  116. };
  117. // Set DPR scaling factor
  118. ImageUrlBuilder.prototype.dpr = function (dpr) {
  119. // A DPR of 1 is the default - so only include it if we have a different value
  120. return this.withOptions(dpr && dpr !== 1 ? { dpr: dpr } : {});
  121. };
  122. // Specify the width of the image in pixels
  123. ImageUrlBuilder.prototype.width = function (width) {
  124. return this.withOptions({ width: width });
  125. };
  126. // Specify the height of the image in pixels
  127. ImageUrlBuilder.prototype.height = function (height) {
  128. return this.withOptions({ height: height });
  129. };
  130. // Specify focal point in fraction of image dimensions. Each component 0.0-1.0
  131. ImageUrlBuilder.prototype.focalPoint = function (x, y) {
  132. return this.withOptions({ focalPoint: { x: x, y: y } });
  133. };
  134. ImageUrlBuilder.prototype.maxWidth = function (maxWidth) {
  135. return this.withOptions({ maxWidth: maxWidth });
  136. };
  137. ImageUrlBuilder.prototype.minWidth = function (minWidth) {
  138. return this.withOptions({ minWidth: minWidth });
  139. };
  140. ImageUrlBuilder.prototype.maxHeight = function (maxHeight) {
  141. return this.withOptions({ maxHeight: maxHeight });
  142. };
  143. ImageUrlBuilder.prototype.minHeight = function (minHeight) {
  144. return this.withOptions({ minHeight: minHeight });
  145. };
  146. // Specify width and height in pixels
  147. ImageUrlBuilder.prototype.size = function (width, height) {
  148. return this.withOptions({ width: width, height: height });
  149. };
  150. // Specify blur between 0 and 100
  151. ImageUrlBuilder.prototype.blur = function (blur) {
  152. return this.withOptions({ blur: blur });
  153. };
  154. ImageUrlBuilder.prototype.sharpen = function (sharpen) {
  155. return this.withOptions({ sharpen: sharpen });
  156. };
  157. // Specify the desired rectangle of the image
  158. ImageUrlBuilder.prototype.rect = function (left, top, width, height) {
  159. return this.withOptions({ rect: { left: left, top: top, width: width, height: height } });
  160. };
  161. // Specify the image format of the image. 'jpg', 'pjpg', 'png', 'webp'
  162. ImageUrlBuilder.prototype.format = function (format) {
  163. return this.withOptions({ format: format });
  164. };
  165. ImageUrlBuilder.prototype.invert = function (invert) {
  166. return this.withOptions({ invert: invert });
  167. };
  168. // Rotation in degrees 0, 90, 180, 270
  169. ImageUrlBuilder.prototype.orientation = function (orientation) {
  170. return this.withOptions({ orientation: orientation });
  171. };
  172. // Compression quality 0-100
  173. ImageUrlBuilder.prototype.quality = function (quality) {
  174. return this.withOptions({ quality: quality });
  175. };
  176. // Make it a download link. Parameter is default filename.
  177. ImageUrlBuilder.prototype.forceDownload = function (download) {
  178. return this.withOptions({ download: download });
  179. };
  180. // Flip image horizontally
  181. ImageUrlBuilder.prototype.flipHorizontal = function () {
  182. return this.withOptions({ flipHorizontal: true });
  183. };
  184. // Flip image vertically
  185. ImageUrlBuilder.prototype.flipVertical = function () {
  186. return this.withOptions({ flipVertical: true });
  187. };
  188. // Ignore crop/hotspot from image record, even when present
  189. ImageUrlBuilder.prototype.ignoreImageParams = function () {
  190. return this.withOptions({ ignoreImageParams: true });
  191. };
  192. ImageUrlBuilder.prototype.fit = function (value) {
  193. if (validFits.indexOf(value) === -1) {
  194. throw new Error("Invalid fit mode \"".concat(value, "\""));
  195. }
  196. return this.withOptions({ fit: value });
  197. };
  198. ImageUrlBuilder.prototype.crop = function (value) {
  199. if (validCrops.indexOf(value) === -1) {
  200. throw new Error("Invalid crop mode \"".concat(value, "\""));
  201. }
  202. return this.withOptions({ crop: value });
  203. };
  204. // Saturation
  205. ImageUrlBuilder.prototype.saturation = function (saturation) {
  206. return this.withOptions({ saturation: saturation });
  207. };
  208. ImageUrlBuilder.prototype.auto = function (value) {
  209. if (validAutoModes.indexOf(value) === -1) {
  210. throw new Error("Invalid auto mode \"".concat(value, "\""));
  211. }
  212. return this.withOptions({ auto: value });
  213. };
  214. // Specify the number of pixels to pad the image
  215. ImageUrlBuilder.prototype.pad = function (pad) {
  216. return this.withOptions({ pad: pad });
  217. };
  218. // Gets the url based on the submitted parameters
  219. ImageUrlBuilder.prototype.url = function () {
  220. return (0, urlForImage_1.default)(this.options);
  221. };
  222. // Alias for url()
  223. ImageUrlBuilder.prototype.toString = function () {
  224. return this.url();
  225. };
  226. return ImageUrlBuilder;
  227. }());
  228. exports.ImageUrlBuilder = ImageUrlBuilder;
  229. //# sourceMappingURL=builder.js.map