Skip to content

Commit f96705b

Browse files
enable passing query params to URLConfig
1 parent e056f37 commit f96705b

File tree

6 files changed

+40
-3
lines changed

6 files changed

+40
-3
lines changed

__TESTS__/unit/url/url.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,16 @@ 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}});
100+
const url = image.toURL();
101+
expect(url).toEqual(`https://res.cloudinary.com/demo/image/upload/sample?_i=abcde&_z=1234`);
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 url = image.toURL();
107+
expect(url).toEqual(`https://res.cloudinary.com/demo/image/upload/sample?_i=abcde&_a=E`);
108+
});
97109
});

__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'});
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'});
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: Record<string, string | number> = this.urlConfig.queryParams || {};
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._a = getSDKAnalyticsSignature(trackedAnalytics);
292+
}
293+
294+
const queryParamsStr = `${Object.entries(queryParams).map(([key, value]) => `${key}=${value}`).join('&')}`;
295+
if (queryParamsStr) {
296+
return `${safeURL}?${queryParamsStr}`;
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>
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 query params
108+
*/
109+
setQueryParams(params: Record<string, string | number>):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} [additionalQueryParams]
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 query params to be added to the URL
100+
*/
101+
queryParams?: Record<string, string | number>
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)