next-cloudinary 2.2.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ import { ImageProps } from 'next/image';
2
+ import { ImageOptions } from '@cloudinary-util/url-loader';
3
+
4
+ type CldImageProps = Omit<ImageProps, 'src'> & ImageOptions & {
5
+ src: string;
6
+ preserveTransformations?: boolean;
7
+ };
8
+ declare const CldImage: (props: CldImageProps) => JSX.Element;
9
+
10
+ interface CldOgImageProps {
11
+ alt: string;
12
+ crop?: string;
13
+ excludeTags?: Array<string>;
14
+ gravity?: string;
15
+ height: string | number;
16
+ src: string;
17
+ twitterTitle?: string;
18
+ width: string | number;
19
+ }
20
+ declare const CldOgImage: ({ excludeTags, twitterTitle, ...props }: CldOgImageProps) => JSX.Element;
21
+
22
+ interface CldUploadWidgetPropsOptions {
23
+ uploadSignature?: Function;
24
+ }
25
+ interface CldUploadWidgetPropsChildren {
26
+ cloudinary: any;
27
+ widget: any;
28
+ open: Function;
29
+ }
30
+ interface CldUploadWidgetProps {
31
+ children?: ({ cloudinary, widget, open }: CldUploadWidgetPropsChildren) => JSX.Element;
32
+ onUpload?: Function;
33
+ options?: CldUploadWidgetPropsOptions;
34
+ signatureEndpoint?: URL | RequestInfo;
35
+ uploadPreset?: string;
36
+ }
37
+ declare const CldUploadWidget: ({ children, onUpload, options, signatureEndpoint, uploadPreset, }: CldUploadWidgetProps) => JSX.Element;
38
+
39
+ interface CldUploadButtonProps extends Omit<CldUploadWidgetProps, 'children'> {
40
+ children?: JSX.Element | string | Array<JSX.Element | string>;
41
+ onClick?: Function;
42
+ }
43
+ declare const CldUploadButton: ({ children, onClick, onUpload, options, signatureEndpoint, uploadPreset, ...props }: CldUploadButtonProps) => JSX.Element;
44
+
45
+ interface CloudinaryLoaderCldOptions {
46
+ heightResize?: string | number;
47
+ widthResize?: string | number;
48
+ }
49
+ interface CloudinaryLoaderLoaderOptions {
50
+ height?: string | number;
51
+ width?: string | number;
52
+ }
53
+ interface CloudinaryLoader {
54
+ loaderOptions: CloudinaryLoaderLoaderOptions;
55
+ imageProps: ImageProps;
56
+ cldOptions: CloudinaryLoaderCldOptions;
57
+ cldConfig?: object;
58
+ }
59
+ declare function cloudinaryLoader({ loaderOptions, imageProps, cldOptions, cldConfig }: CloudinaryLoader): string;
60
+
61
+ export { CldImage, CldImageProps, CldOgImage, CldOgImageProps, CldUploadButton, CldUploadButtonProps, CldUploadWidget, CldUploadWidgetProps, CldUploadWidgetPropsOptions, CloudinaryLoader, CloudinaryLoaderCldOptions, CloudinaryLoaderLoaderOptions, cloudinaryLoader };
package/dist/index.js ADDED
@@ -0,0 +1,438 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __objRest = (source, exclude) => {
26
+ var target = {};
27
+ for (var prop in source)
28
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
+ target[prop] = source[prop];
30
+ if (source != null && __getOwnPropSymbols)
31
+ for (var prop of __getOwnPropSymbols(source)) {
32
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
+ target[prop] = source[prop];
34
+ }
35
+ return target;
36
+ };
37
+ var __export = (target, all) => {
38
+ for (var name in all)
39
+ __defProp(target, name, { get: all[name], enumerable: true });
40
+ };
41
+ var __copyProps = (to, from, except, desc) => {
42
+ if (from && typeof from === "object" || typeof from === "function") {
43
+ for (let key of __getOwnPropNames(from))
44
+ if (!__hasOwnProp.call(to, key) && key !== except)
45
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
46
+ }
47
+ return to;
48
+ };
49
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
50
+ // If the importer is in node compatibility mode or this is not an ESM
51
+ // file that has been converted to a CommonJS file using a Babel-
52
+ // compatible transform (i.e. "__esModule" has not been set), then set
53
+ // "default" to the CommonJS "module.exports" for node compatibility.
54
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
55
+ mod
56
+ ));
57
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
58
+ var __async = (__this, __arguments, generator) => {
59
+ return new Promise((resolve, reject) => {
60
+ var fulfilled = (value) => {
61
+ try {
62
+ step(generator.next(value));
63
+ } catch (e) {
64
+ reject(e);
65
+ }
66
+ };
67
+ var rejected = (value) => {
68
+ try {
69
+ step(generator.throw(value));
70
+ } catch (e) {
71
+ reject(e);
72
+ }
73
+ };
74
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
75
+ step((generator = generator.apply(__this, __arguments)).next());
76
+ });
77
+ };
78
+
79
+ // src/index.ts
80
+ var src_exports = {};
81
+ __export(src_exports, {
82
+ CldImage: () => CldImage_default,
83
+ CldOgImage: () => CldOgImage_default,
84
+ CldUploadButton: () => CldUploadButton_default,
85
+ CldUploadWidget: () => CldUploadWidget_default,
86
+ cloudinaryLoader: () => cloudinaryLoader
87
+ });
88
+ module.exports = __toCommonJS(src_exports);
89
+
90
+ // src/components/CldImage/CldImage.tsx
91
+ var import_react = require("react");
92
+ var import_image = __toESM(require("next/image"));
93
+ var import_util = require("@cloudinary-util/util");
94
+ var import_url_loader2 = require("@cloudinary-util/url-loader");
95
+
96
+ // src/lib/cloudinary.ts
97
+ function pollForProcessingImage(options) {
98
+ return __async(this, null, function* () {
99
+ const { src } = options;
100
+ try {
101
+ yield new Promise((resolve, reject) => {
102
+ fetch(src).then((res) => {
103
+ if (!res.ok) {
104
+ reject(res);
105
+ return;
106
+ }
107
+ resolve(res);
108
+ });
109
+ });
110
+ } catch (e) {
111
+ if (e.status === 423) {
112
+ return yield pollForProcessingImage(options);
113
+ }
114
+ return false;
115
+ }
116
+ return true;
117
+ });
118
+ }
119
+
120
+ // src/loaders/cloudinary-loader.ts
121
+ var import_url_loader = require("@cloudinary-util/url-loader");
122
+
123
+ // src/constants/analytics.ts
124
+ var import_package = __toESM(require("next/package.json"));
125
+
126
+ // package.json
127
+ var package_default = {
128
+ name: "next-cloudinary",
129
+ version: "2.3.1",
130
+ main: "./dist/index.js",
131
+ module: "./dist/index.mjs",
132
+ types: "./dist/index.d.ts",
133
+ source: "src/index.ts",
134
+ scripts: {
135
+ build: "tsup src/index.ts --format esm,cjs --dts --external react",
136
+ dev: "tsup src/index.ts --format esm,cjs --watch --dts --external react",
137
+ prepublishOnly: "cp ../README.md . && yarn build",
138
+ postpublish: "rm ./README.md",
139
+ test: "jest"
140
+ },
141
+ dependencies: {
142
+ "@cloudinary-util/url-loader": "^1.4.0",
143
+ "@cloudinary-util/util": "^1.2.0",
144
+ "@cloudinary/url-gen": "^1.8.6"
145
+ },
146
+ devDependencies: {
147
+ "@babel/core": "^7.19.6",
148
+ "@babel/preset-env": "^7.19.4",
149
+ "@types/jest": "^29.2.0",
150
+ "@types/react": "^18.0.28",
151
+ "@types/react-dom": "^18.0.11",
152
+ "babel-jest": "^29.2.2",
153
+ dotenv: "^16.0.3",
154
+ jest: "^29.2.2",
155
+ "jest-environment-jsdom": "^29.2.2",
156
+ "ts-jest": "^29.0.3",
157
+ tsup: "^6.6.3",
158
+ typescript: "^4.9.4"
159
+ },
160
+ peerDependencies: {
161
+ next: "^12 || ^13",
162
+ react: "^17 || ^18"
163
+ }
164
+ };
165
+
166
+ // src/constants/analytics.ts
167
+ var NEXT_CLOUDINARY_ANALYTICS_ID = "V";
168
+ var NEXT_CLOUDINARY_VERSION = package_default.version;
169
+ var NEXT_VERSION = import_package.default.version;
170
+
171
+ // src/loaders/cloudinary-loader.ts
172
+ function cloudinaryLoader({ loaderOptions, imageProps, cldOptions, cldConfig = {} }) {
173
+ const options = __spreadValues(__spreadValues({}, imageProps), cldOptions);
174
+ options.width = typeof options.width === "string" ? parseInt(options.width) : options.width;
175
+ options.height = typeof options.height === "string" ? parseInt(options.height) : options.height;
176
+ if (typeof (loaderOptions == null ? void 0 : loaderOptions.width) === "number" && typeof options.width === "number" && loaderOptions.width !== options.width) {
177
+ const multiplier = loaderOptions.width / options.width;
178
+ options.widthResize = loaderOptions.width;
179
+ if (typeof options.height === "number") {
180
+ options.heightResize = Math.round(options.height * multiplier);
181
+ }
182
+ }
183
+ return (0, import_url_loader.constructCloudinaryUrl)({
184
+ // @ts-expect-error
185
+ options,
186
+ config: __spreadValues({
187
+ cloud: {
188
+ cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME
189
+ }
190
+ }, cldConfig),
191
+ analytics: {
192
+ sdkCode: NEXT_CLOUDINARY_ANALYTICS_ID,
193
+ sdkSemver: NEXT_CLOUDINARY_VERSION,
194
+ techVersion: NEXT_VERSION,
195
+ feature: ""
196
+ }
197
+ });
198
+ }
199
+
200
+ // src/components/CldImage/CldImage.tsx
201
+ var import_jsx_runtime = require("react/jsx-runtime");
202
+ "use client";
203
+ var CldImage = (props) => {
204
+ const CLD_OPTIONS = [
205
+ "deliveryType",
206
+ "preserveTransformations"
207
+ ];
208
+ import_url_loader2.transformationPlugins.forEach(({ props: props2 = [] }) => {
209
+ props2.forEach((prop) => {
210
+ if (CLD_OPTIONS.includes(prop)) {
211
+ throw new Error(`Option ${prop} already exists!`);
212
+ }
213
+ CLD_OPTIONS.push(prop);
214
+ });
215
+ });
216
+ const imageProps = {
217
+ alt: props.alt,
218
+ src: props.src
219
+ };
220
+ Object.keys(props).filter((key) => !CLD_OPTIONS.includes(key)).forEach((key) => imageProps[key] = props[key]);
221
+ const defaultImgKey = Object.keys(imageProps).map((key) => `${key}:${imageProps[key]}`).join(";");
222
+ const [imgKey, setImgKey] = (0, import_react.useState)(defaultImgKey);
223
+ const cldOptions = {};
224
+ CLD_OPTIONS.forEach((key) => {
225
+ if (props[key]) {
226
+ cldOptions[key] = props[key] || void 0;
227
+ }
228
+ });
229
+ if (props.preserveTransformations) {
230
+ try {
231
+ const transformations = (0, import_util.getTransformations)(props.src);
232
+ cldOptions.rawTransformations = [...transformations.flat(), ...props.rawTransformations || []];
233
+ } catch (e) {
234
+ console.warn(`Failed to preserve transformations: ${e.message}`);
235
+ }
236
+ }
237
+ function handleOnError(options) {
238
+ return __async(this, null, function* () {
239
+ const result = yield pollForProcessingImage({ src: options.target.src });
240
+ if (result) {
241
+ setImgKey(`${defaultImgKey};${Date.now()}`);
242
+ }
243
+ });
244
+ }
245
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
246
+ import_image.default,
247
+ __spreadProps(__spreadValues({}, imageProps), {
248
+ loader: (loaderOptions) => cloudinaryLoader({ loaderOptions, imageProps, cldOptions }),
249
+ onError: handleOnError
250
+ }),
251
+ imgKey
252
+ );
253
+ };
254
+ var CldImage_default = CldImage;
255
+
256
+ // src/components/CldOgImage/CldOgImage.tsx
257
+ var import_head = __toESM(require("next/head"));
258
+ var import_url_loader3 = require("@cloudinary-util/url-loader");
259
+ var import_jsx_runtime2 = require("react/jsx-runtime");
260
+ var IMAGE_WIDTH = 2400;
261
+ var IMAGE_HEIGHT = 1200;
262
+ var TWITTER_CARD = "summary_large_image";
263
+ var CldOgImage = (_a) => {
264
+ var _b = _a, { excludeTags = [], twitterTitle } = _b, props = __objRest(_b, ["excludeTags", "twitterTitle"]);
265
+ const options = __spreadProps(__spreadValues({}, props), {
266
+ alt: props.alt,
267
+ crop: props.crop || "fill",
268
+ gravity: props.gravity || "center",
269
+ height: props.height || IMAGE_HEIGHT,
270
+ src: props.src,
271
+ width: props.width || IMAGE_WIDTH
272
+ });
273
+ const ogImageUrl = (0, import_url_loader3.constructCloudinaryUrl)({
274
+ options,
275
+ config: {
276
+ cloud: {
277
+ cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME
278
+ }
279
+ },
280
+ analytics: {
281
+ sdkCode: NEXT_CLOUDINARY_ANALYTICS_ID,
282
+ sdkSemver: NEXT_CLOUDINARY_VERSION,
283
+ techVersion: NEXT_VERSION,
284
+ feature: ""
285
+ }
286
+ });
287
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_head.default, { children: [
288
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("meta", { property: "og:image", content: ogImageUrl }),
289
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("meta", { property: "og:image:secure_url", content: ogImageUrl }),
290
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("meta", { property: "og:image:width", content: `${options.width}` }),
291
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("meta", { property: "og:image:height", content: `${options.height}` }),
292
+ options.alt && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("meta", { property: "og:image:alt", content: options.alt }),
293
+ !excludeTags.includes("twitter:title") && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("meta", { property: "twitter:title", content: twitterTitle || " " }),
294
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("meta", { property: "twitter:card", content: TWITTER_CARD }),
295
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("meta", { property: "twitter:image", content: ogImageUrl })
296
+ ] });
297
+ };
298
+ var CldOgImage_default = CldOgImage;
299
+
300
+ // src/components/CldUploadWidget/CldUploadWidget.tsx
301
+ var import_script = __toESM(require("next/script"));
302
+
303
+ // src/lib/util.ts
304
+ function triggerOnIdle(callback) {
305
+ if (window && "requestIdleCallback" in window) {
306
+ return requestIdleCallback(callback);
307
+ }
308
+ return setTimeout(() => callback(), 1);
309
+ }
310
+
311
+ // src/components/CldUploadWidget/CldUploadWidget.tsx
312
+ var import_jsx_runtime3 = require("react/jsx-runtime");
313
+ var cloudinary;
314
+ var widget;
315
+ var CldUploadWidget = ({
316
+ children,
317
+ onUpload,
318
+ options,
319
+ signatureEndpoint,
320
+ uploadPreset
321
+ }) => {
322
+ const signed = !!signatureEndpoint;
323
+ function handleOnLoad() {
324
+ if (!cloudinary) {
325
+ cloudinary = window.cloudinary;
326
+ }
327
+ triggerOnIdle(() => {
328
+ if (!widget) {
329
+ widget = createWidget();
330
+ }
331
+ });
332
+ }
333
+ function generateSignature(callback, paramsToSign) {
334
+ if (typeof signatureEndpoint === "undefined") {
335
+ throw Error("Failed to generate signature: signatureEndpoint undefined.");
336
+ }
337
+ fetch(signatureEndpoint, {
338
+ method: "POST",
339
+ body: JSON.stringify({
340
+ paramsToSign
341
+ })
342
+ }).then((r) => r.json()).then(({ signature }) => {
343
+ callback(signature);
344
+ });
345
+ }
346
+ function createWidget() {
347
+ const uploadOptions = __spreadValues({
348
+ cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME,
349
+ uploadPreset: uploadPreset || process.env.NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET,
350
+ apiKey: process.env.NEXT_PUBLIC_CLOUDINARY_API_KEY
351
+ }, options);
352
+ if (signed) {
353
+ uploadOptions.uploadSignature = generateSignature;
354
+ if (!uploadOptions.apiKey) {
355
+ return new Error("Signed Upload needs apiKey!");
356
+ }
357
+ }
358
+ return cloudinary == null ? void 0 : cloudinary.createUploadWidget(
359
+ uploadOptions,
360
+ function(error, result) {
361
+ if ((error || result.event === "success") && typeof onUpload === "function") {
362
+ onUpload(error, result, widget);
363
+ }
364
+ }
365
+ );
366
+ }
367
+ function open() {
368
+ if (!widget) {
369
+ widget = createWidget();
370
+ }
371
+ widget && widget.open();
372
+ }
373
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
374
+ typeof children === "function" && children({
375
+ cloudinary,
376
+ widget,
377
+ open
378
+ }),
379
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
380
+ import_script.default,
381
+ {
382
+ id: `cloudinary-${Math.floor(Math.random() * 100)}`,
383
+ src: "https://widget.cloudinary.com/v2.0/global/all.js",
384
+ onLoad: handleOnLoad,
385
+ onError: (e) => console.error(`Failed to load Cloudinary: ${e.message}`)
386
+ }
387
+ )
388
+ ] });
389
+ };
390
+ var CldUploadWidget_default = CldUploadWidget;
391
+
392
+ // src/components/CldUploadButton/CldUploadButton.tsx
393
+ var import_jsx_runtime4 = require("react/jsx-runtime");
394
+ var CldUploadButton = (_a) => {
395
+ var _b = _a, {
396
+ children,
397
+ onClick,
398
+ onUpload,
399
+ options,
400
+ signatureEndpoint,
401
+ uploadPreset
402
+ } = _b, props = __objRest(_b, [
403
+ "children",
404
+ "onClick",
405
+ "onUpload",
406
+ "options",
407
+ "signatureEndpoint",
408
+ "uploadPreset"
409
+ ]);
410
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
411
+ CldUploadWidget_default,
412
+ {
413
+ onUpload,
414
+ options,
415
+ signatureEndpoint,
416
+ uploadPreset,
417
+ children: ({ open }) => {
418
+ function handleOnClick(e) {
419
+ e.preventDefault();
420
+ open();
421
+ if (typeof onClick === "function") {
422
+ onClick(e);
423
+ }
424
+ }
425
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("button", __spreadProps(__spreadValues({}, props), { onClick: handleOnClick, children: children || "Upload" }));
426
+ }
427
+ }
428
+ ) });
429
+ };
430
+ var CldUploadButton_default = CldUploadButton;
431
+ // Annotate the CommonJS export names for ESM import in node:
432
+ 0 && (module.exports = {
433
+ CldImage,
434
+ CldOgImage,
435
+ CldUploadButton,
436
+ CldUploadWidget,
437
+ cloudinaryLoader
438
+ });