Skip to content

Commit 5ed063b

Browse files
Merge pull request #555 from cloudinary/add-query-params
enable passing query params to URLConfig
2 parents e056f37 + 0b8856d commit 5ed063b

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

__TESTS__/unit/url/url.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,21 @@ describe('Tests for URL configuration', () => {
9494

9595
expect(url).toEqual(`https://res.cloudinary.com/demo/image/upload/s--${signature}--/c_crop,w_100/sample`);
9696
});
97+
98+
it('Should include query params', function () {
99+
const image = createNewImage('sample', {cloudName: 'demo'}, {queryParams: {"_i": "abcde", "_z": 1234, "_t": false}});
100+
const url = image.toURL();
101+
expect(url).toEqual(`https://res.cloudinary.com/demo/image/upload/sample?_i=abcde&_z=1234&_t=false`);
102+
});
103+
104+
it('Should include query params with analytics', function () {
105+
const image = createNewImage('sample', {cloudName: 'demo'}, {analytics: true, queryParams: {"_i": "abcde"}});
106+
const analyticsOptions = {
107+
techVersion: '16.0.0',
108+
sdkCode: 'T',
109+
sdkSemver: '1.0.0'
110+
};
111+
const url = image.toURL({trackedAnalytics: analyticsOptions});
112+
expect(url).toEqual(`https://res.cloudinary.com/demo/image/upload/sample?_i=abcde&_a=ATAABAQ0`);
113+
});
97114
});

__TESTS__/unit/urlConfig.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ describe('It tests a combination of Cloudinary URL and Configuration', () => {
157157
.setSecure(true)
158158
.setShorten(true)
159159
.setSignUrl(true)
160-
.setUseRootPath(true);
160+
.setUseRootPath(true)
161+
.setQueryParams({foo: 'bar', baz: 111, dummy: true});
161162

162163
expect(conf.cname).toBe('foo');
163164
expect(conf.forceVersion).toBe(true);
@@ -167,5 +168,6 @@ describe('It tests a combination of Cloudinary URL and Configuration', () => {
167168
expect(conf.shorten).toBe(true);
168169
expect(conf.signUrl).toBe(true);
169170
expect(conf.useRootPath).toBe(true);
171+
expect(conf.queryParams).toEqual({foo: 'bar', baz: 111, dummy: true});
170172
});
171173
});

src/assets/CloudinaryFile.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,17 @@ class CloudinaryFile {
283283
.replace(/\?/g, '%3F')
284284
.replace(/=/g, '%3D');
285285

286+
const queryParams = new URLSearchParams(this.urlConfig.queryParams as Record<string, string>);
287+
286288
// urlConfig.analytics is true by default, has to be explicitly set to false to overwrite
287289
// Don't add analytics when publicId includes a '?' to not risk changing existing query params
288290
if (this.urlConfig.analytics !== false && !(publicID.includes('?'))) {
289-
return `${safeURL}?_a=${getSDKAnalyticsSignature(trackedAnalytics)}`;
291+
queryParams.set("_a", getSDKAnalyticsSignature(trackedAnalytics));
292+
}
293+
294+
const queryParamsString = queryParams.toString();
295+
if (queryParamsString) {
296+
return `${safeURL}?${queryParamsString}`;
290297
} else {
291298
return safeURL;
292299
}

src/config/URLConfig.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class URLConfig extends Config implements IURLConfig {
1313
useRootPath?: boolean;
1414
secure?: boolean;
1515
forceVersion?: boolean;
16+
queryParams?: Record<string, string | number | boolean>;
1617

1718
/**
1819
* @param {IURLConfig} userURLConfig
@@ -101,6 +102,14 @@ class URLConfig extends Config implements IURLConfig {
101102
this.forceVersion = value;
102103
return this;
103104
}
105+
106+
/**
107+
* @param params Sets additional params
108+
*/
109+
setQueryParams(params: Record<string, string | number | boolean>):this {
110+
this.queryParams = params;
111+
return this;
112+
}
104113
}
105114

106115
export default URLConfig;

src/config/interfaces/Config/IURLConfig.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @prop {boolean} [secure]
1414
* @prop {boolean} [forceVersion]
1515
* @prop {boolean} [analytics]
16+
* @prop {object} [queryParams]
1617
* @example
1718
* import Cloudinary from '@cloudinary/url-gen';
1819
* // The Cloudinary Instance accepts a URLConfig under the `url` key
@@ -93,6 +94,11 @@ interface IURLConfig {
9394
* Whether or not to force a version
9495
*/
9596
forceVersion?: boolean;
97+
98+
/**
99+
* Additional params to be added to the URL
100+
*/
101+
queryParams?: Record<string, string | number | boolean>
96102
}
97103

98104
export default IURLConfig;

src/internal/internalConstants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export const ALLOWED_URL_CONFIG = [
1616
'useRootPath',
1717
'secure',
1818
'forceVersion',
19-
'analytics'
19+
'analytics',
20+
'queryParams'
2021
];
2122

2223
/**

0 commit comments

Comments
 (0)