Skip to content

Commit b4ce1dc

Browse files
committed
change default transformation position to query to better handle wildcard purge. Update test cases to reflect this change. Bump major version to reflect breaking change.
1 parent fbd63f0 commit b4ce1dc

File tree

7 files changed

+52
-16
lines changed

7 files changed

+52
-16
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "imagekit-javascript",
3-
"version": "3.1.0",
3+
"version": "4.0.0",
44
"description": "Javascript SDK for using ImageKit.io in the browser",
55
"main": "dist/imagekit.cjs.js",
66
"module": "dist/imagekit.esm.js",

src/url/builder.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,19 @@ export const buildURL = (opts: UrlOptions & ImageKitOptions) => {
4545
return "";
4646
}
4747

48-
// if (opts.sdkVersion && opts.sdkVersion.trim() != "") {
49-
// urlObj.searchParams.append("ik-sdk-version", opts.sdkVersion.trim());
50-
// }
51-
5248
for (var i in opts.queryParameters) {
5349
urlObj.searchParams.append(i, String(opts.queryParameters[i]));
5450
}
5551

5652
var transformationString = constructTransformationString(opts.transformation);
5753

5854
if (transformationString && transformationString.length) {
59-
if (transformationUtils.addAsQueryParameter(opts) || isSrcParameterUsedForURL) {
60-
urlObj.searchParams.append(TRANSFORMATION_PARAMETER, transformationString);
61-
} else {
55+
if (!transformationUtils.addAsQueryParameter(opts) && !isSrcParameterUsedForURL) {
6256
urlObj.pathname = pathJoin([
6357
TRANSFORMATION_PARAMETER + transformationUtils.getChainTransformDelimiter() + transformationString,
6458
urlObj.pathname,
6559
]);
66-
}
60+
}
6761
}
6862

6963
if (urlEndpointPattern) {
@@ -72,6 +66,17 @@ export const buildURL = (opts: UrlOptions & ImageKitOptions) => {
7266
urlObj.pathname = pathJoin([urlObj.pathname]);
7367
}
7468

69+
if (transformationString && transformationString.length) {
70+
if(transformationUtils.addAsQueryParameter(opts) || isSrcParameterUsedForURL) {
71+
if(urlObj.searchParams.toString() !== "") { // In 12 node.js .size was not there. So, we need to check if it is an object or not.
72+
return `${urlObj.href}&${TRANSFORMATION_PARAMETER}=${transformationString}`;
73+
}
74+
else {
75+
return `${urlObj.href}?${TRANSFORMATION_PARAMETER}=${transformationString}`;
76+
}
77+
}
78+
}
79+
7580
return urlObj.href;
7681
};
7782

src/utils/transformation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import supportedTransforms from "../constants/supportedTransforms";
22
import { ImageKitOptions, TransformationPosition } from "../interfaces";
33

4-
const DEFAULT_TRANSFORMATION_POSITION: TransformationPosition = "path";
54
const QUERY_TRANSFORMATION_POSITION: TransformationPosition = "query";
6-
const VALID_TRANSFORMATION_POSITIONS = [DEFAULT_TRANSFORMATION_POSITION, QUERY_TRANSFORMATION_POSITION];
5+
const PATH_TRANSFORMATION_POSITION: TransformationPosition = "path";
6+
const DEFAULT_TRANSFORMATION_POSITION: TransformationPosition = QUERY_TRANSFORMATION_POSITION;
7+
const VALID_TRANSFORMATION_POSITIONS = [PATH_TRANSFORMATION_POSITION, QUERY_TRANSFORMATION_POSITION];
78
const CHAIN_TRANSFORM_DELIMITER: string = ":";
89
const TRANSFORM_DELIMITER: string = ",";
910
const TRANSFORM_KEY_VALUE_DELIMITER: string = "-";

test/data/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports.initializationParams = {
22
publicKey: "test_public_key",
33
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
4+
transformationPosition: "path",
45
}

test/url-generation/basic.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ describe("URL generation", function () {
4848
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg`);
4949
});
5050

51+
it("By default transformationPosition should be query", function () {
52+
var imagekitNew = new ImageKit({
53+
publicKey: "test_public_key",
54+
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
55+
});
56+
const url = imagekitNew.url({
57+
path: "/test_path.jpg",
58+
transformation: [{
59+
"height": "300",
60+
"width": "400"
61+
}]
62+
});
63+
expect(url).equal("https://ik.imagekit.io/test_url_endpoint/test_path.jpg?tr=h-300,w-400");
64+
});
65+
5166
it('should generate the URL without sdk version', function () {
5267
const ik = new ImageKit({ ...initializationParams, sdkVersion: "" })
5368

@@ -111,7 +126,7 @@ describe("URL generation", function () {
111126
}]
112127
});
113128

114-
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_path.jpg?tr=h-300%2Cw-400`);
129+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_path.jpg?tr=h-300,w-400`);
115130
});
116131

117132
it('should generate the correct URL with a valid src parameter and transformation', function () {
@@ -123,7 +138,7 @@ describe("URL generation", function () {
123138
}]
124139
});
125140

126-
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg?tr=h-300%2Cw-400`);
141+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg?tr=h-300,w-400`);
127142
});
128143

129144
it('should generate the correct URL with transformationPosition as query parameter when src is provided', function () {
@@ -136,7 +151,7 @@ describe("URL generation", function () {
136151
}]
137152
});
138153

139-
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg?tr=h-300%2Cw-400`);
154+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg?tr=h-300,w-400`);
140155
});
141156

142157
it('should merge query parameters correctly in the generated URL', function () {
@@ -149,7 +164,7 @@ describe("URL generation", function () {
149164
}]
150165
});
151166

152-
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg?t1=v1&t2=v2&t3=v3&tr=h-300%2Cw-400`);
167+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg?t1=v1&t2=v2&t3=v3&tr=h-300,w-400`);
153168
});
154169

155170

test/url-generation/overlay.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,4 +441,18 @@ describe("Overlay encoding test cases", function () {
441441
});
442442
expect(url).equal(`https://ik.imagekit.io/demo/tr:l-subtitle,ie-${encodeURIComponent(safeBtoa("sub.srt"))},l-end/sample.mp4`);
443443
});
444+
445+
it("Avoid double encoding when transformation string is in query params", function () {
446+
const url = imagekit.url({
447+
path: "/sample.jpg",
448+
transformation: [{
449+
overlay: {
450+
type: "text",
451+
text: "Minimal Text"
452+
}
453+
}],
454+
transformationPosition: "query"
455+
});
456+
expect(url).equal(`https://ik.imagekit.io/demo/sample.jpg?tr=l-text,i-Minimal%20Text,l-end`);
457+
});
444458
});

0 commit comments

Comments
 (0)