next-cloudinary 1.10.0 → 1.12.0
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.
- package/README.md +1 -0
- package/dist/next-cloudinary.js +100 -65
- package/dist/next-cloudinary.js.map +1 -1
- package/dist/next-cloudinary.modern.mjs +99 -69
- package/dist/next-cloudinary.modern.mjs.map +1 -1
- package/dist/next-cloudinary.module.js +99 -65
- package/dist/next-cloudinary.module.js.map +1 -1
- package/dist/next-cloudinary.umd.js +103 -69
- package/dist/next-cloudinary.umd.js.map +1 -1
- package/package.json +6 -3
- package/tsconfig.json +26 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useState, useRef } from 'react';
|
|
2
2
|
import Image from 'next/image';
|
|
3
3
|
import { Cloudinary } from '@cloudinary/url-gen';
|
|
4
|
+
import nextPkg from 'next/package.json';
|
|
4
5
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
5
6
|
import Head from 'next/head';
|
|
6
7
|
import Script from 'next/script';
|
|
@@ -885,19 +886,64 @@ var zoompanPlugin = {
|
|
|
885
886
|
plugin: plugin
|
|
886
887
|
};
|
|
887
888
|
|
|
889
|
+
const NEXT_CLOUDINARY_ANALYTICS_ID = 'V';
|
|
890
|
+
|
|
891
|
+
var name = "next-cloudinary";
|
|
892
|
+
var version = "1.12.0";
|
|
893
|
+
var main = "dist/next-cloudinary.umd.js";
|
|
894
|
+
var module = "dist/next-cloudinary.module.js";
|
|
895
|
+
var source = "src/index.ts";
|
|
896
|
+
var types = "dist/index.d.ts";
|
|
897
|
+
var scripts = {
|
|
898
|
+
build: "microbundle --no-compress --jsx React.createElement --jsxFragment React.Fragment --jsxImportSource react",
|
|
899
|
+
dev: "microbundle watch --no-compress --jsx React.createElement --jsxFragment React.Fragment --jsxImportSource react",
|
|
900
|
+
prepublishOnly: "cp ../README.md . && yarn build",
|
|
901
|
+
postpublish: "rm ./README.md",
|
|
902
|
+
test: "jest"
|
|
903
|
+
};
|
|
904
|
+
var dependencies = {
|
|
905
|
+
"@cloudinary/url-gen": "^1.8.6"
|
|
906
|
+
};
|
|
907
|
+
var devDependencies = {
|
|
908
|
+
"@babel/core": "^7.19.6",
|
|
909
|
+
"@babel/preset-env": "^7.19.4",
|
|
910
|
+
"@types/jest": "^29.2.0",
|
|
911
|
+
"babel-jest": "^29.2.2",
|
|
912
|
+
dotenv: "^16.0.3",
|
|
913
|
+
jest: "^29.2.2",
|
|
914
|
+
"jest-environment-jsdom": "^29.2.2",
|
|
915
|
+
microbundle: "^0.15.1",
|
|
916
|
+
"ts-jest": "^29.0.3",
|
|
917
|
+
typescript: "^4.9.4"
|
|
918
|
+
};
|
|
919
|
+
var peerDependencies = {
|
|
920
|
+
next: "^12 || ^13",
|
|
921
|
+
react: "^17 || ^18"
|
|
922
|
+
};
|
|
923
|
+
var pkg = {
|
|
924
|
+
name: name,
|
|
925
|
+
version: version,
|
|
926
|
+
main: main,
|
|
927
|
+
module: module,
|
|
928
|
+
"umd:main": "dist/next-cloudinary.umd.js",
|
|
929
|
+
source: source,
|
|
930
|
+
types: types,
|
|
931
|
+
scripts: scripts,
|
|
932
|
+
dependencies: dependencies,
|
|
933
|
+
devDependencies: devDependencies,
|
|
934
|
+
peerDependencies: peerDependencies
|
|
935
|
+
};
|
|
936
|
+
|
|
888
937
|
const transformationPlugins = [
|
|
889
938
|
// Background Removal must always come first
|
|
890
939
|
removeBackgroundPlugin, croppingPlugin, effectsPlugin, overlaysPlugin, namedTransformationsPlugin, underlaysPlugin, zoompanPlugin,
|
|
891
940
|
// Raw transformations needs to be last simply to make sure
|
|
892
941
|
// it's always expected to applied the same way
|
|
893
|
-
|
|
894
942
|
rawTransformationsPlugin];
|
|
895
943
|
let cld;
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
* constructCloudinaryUrl
|
|
944
|
+
/**
|
|
945
|
+
* constructCloudinaryUrl
|
|
899
946
|
*/
|
|
900
|
-
|
|
901
947
|
function constructCloudinaryUrl({
|
|
902
948
|
options,
|
|
903
949
|
config
|
|
@@ -906,52 +952,53 @@ function constructCloudinaryUrl({
|
|
|
906
952
|
cld = new Cloudinary(_extends({
|
|
907
953
|
cloud: {
|
|
908
954
|
cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME
|
|
909
|
-
},
|
|
910
|
-
url: {
|
|
911
|
-
// Used to avoid issues with SSR particularly for the blurred placeholder
|
|
912
|
-
analytics: false
|
|
913
955
|
}
|
|
914
956
|
}, config));
|
|
915
957
|
}
|
|
916
|
-
const publicId = getPublicId(options.src);
|
|
958
|
+
const publicId = getPublicId(options == null ? void 0 : options.src);
|
|
917
959
|
const cldImage = cld.image(publicId);
|
|
918
960
|
transformationPlugins.forEach(({
|
|
919
961
|
plugin
|
|
920
962
|
}) => {
|
|
963
|
+
// @ts-ignore
|
|
921
964
|
const {
|
|
922
965
|
options: pluginOptions
|
|
923
966
|
} = plugin({
|
|
924
967
|
cldImage,
|
|
925
968
|
options
|
|
926
969
|
}) || {};
|
|
927
|
-
if (pluginOptions != null && pluginOptions.format) {
|
|
970
|
+
if (pluginOptions != null && pluginOptions.format && options) {
|
|
928
971
|
options.format = pluginOptions.format;
|
|
929
972
|
}
|
|
930
|
-
if (pluginOptions != null && pluginOptions.width) {
|
|
973
|
+
if (pluginOptions != null && pluginOptions.width && options) {
|
|
931
974
|
options.resize = {
|
|
932
975
|
width: pluginOptions == null ? void 0 : pluginOptions.width
|
|
933
976
|
};
|
|
934
977
|
}
|
|
935
978
|
});
|
|
936
|
-
if (options.resize) {
|
|
979
|
+
if (options != null && options.resize) {
|
|
937
980
|
const {
|
|
938
981
|
width,
|
|
939
982
|
crop = 'scale'
|
|
940
983
|
} = options.resize;
|
|
941
984
|
cldImage.effect(`c_${crop},w_${width}`);
|
|
942
985
|
}
|
|
943
|
-
return cldImage.setDeliveryType(options.deliveryType || 'upload').format(options.format || 'auto').delivery(`q_${options.quality || 'auto'}`).toURL(
|
|
986
|
+
return cldImage.setDeliveryType((options == null ? void 0 : options.deliveryType) || 'upload').format((options == null ? void 0 : options.format) || 'auto').delivery(`q_${(options == null ? void 0 : options.quality) || 'auto'}`).toURL({
|
|
987
|
+
trackedAnalytics: {
|
|
988
|
+
sdkCode: NEXT_CLOUDINARY_ANALYTICS_ID,
|
|
989
|
+
sdkSemver: pkg.version,
|
|
990
|
+
techVersion: nextPkg.version
|
|
991
|
+
}
|
|
992
|
+
});
|
|
944
993
|
}
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
*
|
|
948
|
-
*
|
|
949
|
-
*
|
|
950
|
-
*
|
|
951
|
-
*
|
|
952
|
-
* @return {string} The images public id
|
|
994
|
+
/**
|
|
995
|
+
* Retrieves the public id of a cloudiary image url. If no url is recognized it returns the parameter it self.
|
|
996
|
+
* If it's recognized that is a url and it's not possible to get the public id, it warns the user.
|
|
997
|
+
*
|
|
998
|
+
* @param {string} src The cloudiary url or public id.
|
|
999
|
+
*
|
|
1000
|
+
* @return {string} The images public id
|
|
953
1001
|
*/
|
|
954
|
-
|
|
955
1002
|
function getPublicId(src) {
|
|
956
1003
|
if (typeof src !== 'string') {
|
|
957
1004
|
throw new Error(`Invalid src of type ${typeof src}`);
|
|
@@ -971,11 +1018,9 @@ function getPublicId(src) {
|
|
|
971
1018
|
}
|
|
972
1019
|
return src;
|
|
973
1020
|
}
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
* createPlaceholderUrl
|
|
1021
|
+
/**
|
|
1022
|
+
* createPlaceholderUrl
|
|
977
1023
|
*/
|
|
978
|
-
|
|
979
1024
|
function createPlaceholderUrl({
|
|
980
1025
|
src,
|
|
981
1026
|
placeholder = true,
|
|
@@ -1000,11 +1045,9 @@ function createPlaceholderUrl({
|
|
|
1000
1045
|
config
|
|
1001
1046
|
});
|
|
1002
1047
|
}
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
* pollForProcessingImage
|
|
1048
|
+
/**
|
|
1049
|
+
* pollForProcessingImage
|
|
1006
1050
|
*/
|
|
1007
|
-
|
|
1008
1051
|
async function pollForProcessingImage(options) {
|
|
1009
1052
|
const {
|
|
1010
1053
|
src
|
|
@@ -1066,23 +1109,21 @@ const CldImage = props => {
|
|
|
1066
1109
|
CLD_OPTIONS.push(prop);
|
|
1067
1110
|
});
|
|
1068
1111
|
});
|
|
1069
|
-
|
|
1070
1112
|
// Construct the base Image component props by filtering out Cloudinary-specific props
|
|
1071
|
-
|
|
1072
|
-
|
|
1113
|
+
const imageProps = {
|
|
1114
|
+
alt: props.alt,
|
|
1115
|
+
src: props.src
|
|
1116
|
+
};
|
|
1073
1117
|
Object.keys(props).filter(key => !CLD_OPTIONS.includes(key)).forEach(key => imageProps[key] = props[key]);
|
|
1074
1118
|
const defaultImgKey = Object.keys(imageProps).map(key => `${key}:${imageProps[key]}`).join(';');
|
|
1075
1119
|
const [imgKey, setImgKey] = useState(defaultImgKey);
|
|
1076
|
-
|
|
1077
1120
|
// Construct Cloudinary-specific props by looking for values for any of the supported prop keys
|
|
1078
|
-
|
|
1079
1121
|
const cldOptions = {};
|
|
1080
1122
|
CLD_OPTIONS.forEach(key => {
|
|
1081
1123
|
if (props[key]) {
|
|
1082
1124
|
cldOptions[key] = props[key];
|
|
1083
1125
|
}
|
|
1084
1126
|
});
|
|
1085
|
-
|
|
1086
1127
|
// If we see a placeholder option, configure a Cloudinary-based URL.
|
|
1087
1128
|
// The resulting image will always be blurred per Next.js, so we're
|
|
1088
1129
|
// limited on options for placeholders.
|
|
@@ -1091,7 +1132,6 @@ const CldImage = props => {
|
|
|
1091
1132
|
// an Image component prop as opposed to simply the URL
|
|
1092
1133
|
//
|
|
1093
1134
|
// https://nextjs.org/docs/api-reference/next/image#blurdataurl
|
|
1094
|
-
|
|
1095
1135
|
if (imageProps.placeholder) {
|
|
1096
1136
|
const publicId = getPublicId(props.src);
|
|
1097
1137
|
imageProps.blurDataURL = createPlaceholderUrl({
|
|
@@ -1104,13 +1144,12 @@ const CldImage = props => {
|
|
|
1104
1144
|
}
|
|
1105
1145
|
if (props.src && props.preserveTransformations) {
|
|
1106
1146
|
const transformations = getTransformations(props.src, props.preserveTransformations);
|
|
1107
|
-
imageProps.rawTransformations
|
|
1147
|
+
const rawTransformations = imageProps.rawTransformations ? [...imageProps.rawTransformations] : [];
|
|
1148
|
+
imageProps.rawTransformations = [...rawTransformations, ...transformations];
|
|
1108
1149
|
}
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
* handleOnError
|
|
1150
|
+
/**
|
|
1151
|
+
* handleOnError
|
|
1112
1152
|
*/
|
|
1113
|
-
|
|
1114
1153
|
async function handleOnError(options) {
|
|
1115
1154
|
const result = await pollForProcessingImage({
|
|
1116
1155
|
src: options.target.src
|
|
@@ -1143,15 +1182,14 @@ const CldOgImage = _ref => {
|
|
|
1143
1182
|
width: props.width || IMAGE_WIDTH,
|
|
1144
1183
|
height: props.height || IMAGE_HEIGHT,
|
|
1145
1184
|
crop: props.crop || 'fill',
|
|
1146
|
-
gravity: props.gravity || 'center'
|
|
1185
|
+
gravity: props.gravity || 'center',
|
|
1186
|
+
alt: props.alt
|
|
1147
1187
|
});
|
|
1148
1188
|
const ogImageUrl = constructCloudinaryUrl({
|
|
1149
1189
|
options
|
|
1150
1190
|
});
|
|
1151
|
-
|
|
1152
1191
|
// We need to include the tags within the Next.js Head component rather than
|
|
1153
1192
|
// direcly adding them inside of the Head otherwise we get unexpected results
|
|
1154
|
-
|
|
1155
1193
|
return /*#__PURE__*/jsxs(Head, {
|
|
1156
1194
|
children: [/*#__PURE__*/jsx("meta", {
|
|
1157
1195
|
property: "og:image",
|
|
@@ -1191,12 +1229,10 @@ const CldUploadWidget = ({
|
|
|
1191
1229
|
const cloudinary = useRef();
|
|
1192
1230
|
const widget = useRef();
|
|
1193
1231
|
const signed = !!signatureEndpoint;
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
*
|
|
1197
|
-
* @description Makes a request to an endpoint to sign Cloudinary parameters as part of widget creation
|
|
1232
|
+
/**
|
|
1233
|
+
* generateSignature
|
|
1234
|
+
* @description Makes a request to an endpoint to sign Cloudinary parameters as part of widget creation
|
|
1198
1235
|
*/
|
|
1199
|
-
|
|
1200
1236
|
function generateSignature(callback, paramsToSign) {
|
|
1201
1237
|
fetch(signatureEndpoint, {
|
|
1202
1238
|
method: "POST",
|
|
@@ -1209,19 +1245,16 @@ const CldUploadWidget = ({
|
|
|
1209
1245
|
callback(signature);
|
|
1210
1246
|
});
|
|
1211
1247
|
}
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
*
|
|
1215
|
-
* @description Creates a new instance of the Cloudinary widget and stores in a ref
|
|
1248
|
+
/**
|
|
1249
|
+
* createWidget
|
|
1250
|
+
* @description Creates a new instance of the Cloudinary widget and stores in a ref
|
|
1216
1251
|
*/
|
|
1217
|
-
|
|
1218
1252
|
function createWidget() {
|
|
1219
1253
|
var _cloudinary$current;
|
|
1220
1254
|
// When creating a signed upload, you need to provide both your Cloudinary API Key
|
|
1221
1255
|
// as well as a signature generator function that will sign any paramters
|
|
1222
1256
|
// either on page load or during the upload process. Read more about signed uploads at:
|
|
1223
1257
|
// https://cloudinary.com/documentation/upload_widget#signed_uploads
|
|
1224
|
-
|
|
1225
1258
|
const uploadOptions = _extends({
|
|
1226
1259
|
cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME,
|
|
1227
1260
|
uploadPreset: uploadPreset || process.env.NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET,
|
|
@@ -1229,13 +1262,12 @@ const CldUploadWidget = ({
|
|
|
1229
1262
|
}, options);
|
|
1230
1263
|
if (signed) {
|
|
1231
1264
|
uploadOptions.uploadSignature = generateSignature;
|
|
1232
|
-
|
|
1233
1265
|
// No need for apiSecret here because of api/sign-cloudinary-params
|
|
1234
|
-
|
|
1235
1266
|
if (!uploadOptions.apiKey) {
|
|
1236
1267
|
return new Error("Signed Upload needs apiKey!");
|
|
1237
1268
|
}
|
|
1238
1269
|
}
|
|
1270
|
+
// @ts-ignore
|
|
1239
1271
|
return (_cloudinary$current = cloudinary.current) == null ? void 0 : _cloudinary$current.createUploadWidget(uploadOptions, function (error, result) {
|
|
1240
1272
|
// The callback is a bit more chatty than failed or success so
|
|
1241
1273
|
// only trigger when one of those are the case. You can additionally
|
|
@@ -1246,25 +1278,23 @@ const CldUploadWidget = ({
|
|
|
1246
1278
|
}
|
|
1247
1279
|
});
|
|
1248
1280
|
}
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
* open
|
|
1252
|
-
* @description When triggered, uses the current widget instance to open the upload modal
|
|
1281
|
+
/**
|
|
1282
|
+
* open
|
|
1283
|
+
* @description When triggered, uses the current widget instance to open the upload modal
|
|
1253
1284
|
*/
|
|
1254
|
-
|
|
1255
1285
|
function open() {
|
|
1256
1286
|
if (!(widget != null && widget.current)) {
|
|
1257
1287
|
widget.current = createWidget();
|
|
1258
1288
|
}
|
|
1289
|
+
// @ts-ignore
|
|
1259
1290
|
(widget == null ? void 0 : widget.current) && widget.current.open();
|
|
1260
1291
|
}
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
*
|
|
1264
|
-
* @description Stores the Cloudinary window instance to a ref when the widget script loads
|
|
1292
|
+
/**
|
|
1293
|
+
* handleOnLoad
|
|
1294
|
+
* @description Stores the Cloudinary window instance to a ref when the widget script loads
|
|
1265
1295
|
*/
|
|
1266
|
-
|
|
1267
1296
|
function handleOnLoad() {
|
|
1297
|
+
// @ts-ignore
|
|
1268
1298
|
cloudinary.current = window.cloudinary;
|
|
1269
1299
|
}
|
|
1270
1300
|
return /*#__PURE__*/jsxs(Fragment, {
|