Skip to content

Commit 62e20ee

Browse files
committed
feat: allow passing string to query params.
1 parent 0cef320 commit 62e20ee

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

src/assets/CloudinaryFile.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,41 @@ class CloudinaryFile {
288288
// 2. If we got an object we have to check if URLSearchParams is available, and only then use it
289289
// Beause we want to throw a meaningful Error in case it's not - we can wrap the below code in a try catch clause
290290
// 3. If it already was a string then we can skip the parsing but still we need to assign it to `queryParamsString`
291-
const queryParams = new URLSearchParams(this.urlConfig.queryParams as Record<string, string>);
292-
293-
// urlConfig.analytics is true by default, has to be explicitly set to false to overwrite
294-
// Don't add analytics when publicId includes a '?' to not risk changing existing query params
295-
if (this.urlConfig.analytics !== false && !(publicID.includes('?'))) {
296-
queryParams.set("_a", getSDKAnalyticsSignature(trackedAnalytics));
297-
}
291+
//
292+
293+
const shouldAddAnalytics = this.urlConfig.analytics !== false && !(publicID.includes('?'));
294+
295+
if (typeof(this.urlConfig.queryParams) === 'object'){ //#1
296+
297+
try { //#2
298+
const queryParams = new URLSearchParams(this.urlConfig.queryParams as Record<string, string>);
299+
// urlConfig.analytics is true by default, has to be explicitly set to false to overwrite
300+
// Don't add analytics when publicId includes a '?' to not risk changing existing query params
301+
302+
if (shouldAddAnalytics) {
303+
queryParams.set("_a", getSDKAnalyticsSignature(trackedAnalytics));
304+
}
305+
306+
const queryParamsString = queryParams.toString();
307+
308+
} catch(){
309+
//TODO: improve error message, consult with docs team.
310+
throw "Error: URLSearchParams is not available."; //#2
311+
}
312+
}
313+
314+
//if (typeof(this.urlConfig.queryParams) === 'string'){ //#3
315+
else{ //#3
316+
//assign to queryParamsString
317+
let queryParamsString = this.urlConfig.queryParams || '';
318+
319+
if (shouldAddAnalytics) {
320+
queryParams.set("_a", getSDKAnalyticsSignature(trackedAnalytics));
321+
queryParamsString += (queryParamsString.length > 0 ? "&" :"") + "_a=" + getSDKAnalyticsSignature(trackedAnalytics);
322+
}
323+
324+
}
298325

299-
const queryParamsString = queryParams.toString();
300326
if (queryParamsString) {
301327
return `${safeURL}?${queryParamsString}`;
302328
} else {

src/config/.URLConfig.ts.swp

12 KB
Binary file not shown.

src/config/URLConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class URLConfig extends Config implements IURLConfig {
1414
secure?: boolean;
1515
forceVersion?: boolean;
1616
// TODO: We need to change the type here to accept string
17-
queryParams?: Record<string, string | number | boolean>;
17+
queryParams?: Record<string, string | number | boolean> | string;
1818

1919
/**
2020
* @param {IURLConfig} userURLConfig
@@ -108,7 +108,7 @@ class URLConfig extends Config implements IURLConfig {
108108
* @param params Sets additional params
109109
*/
110110
// TODO: We need to change the type here to accept string
111-
setQueryParams(params: Record<string, string | number | boolean>):this {
111+
setQueryParams(params: Record<string, string | number | boolean> | string):this {
112112
this.queryParams = params;
113113
return this;
114114
}

src/config/interfaces/Config/IURLConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @prop {boolean} [forceVersion]
1515
* @prop {boolean} [analytics]
1616
* // TODO: This is just for generating docs - we need to change the type here to accept string
17-
* @prop {object} [queryParams]
17+
* @prop {object|string} [queryParams]
1818
* @example
1919
* import Cloudinary from '@cloudinary/url-gen';
2020
* // The Cloudinary Instance accepts a URLConfig under the `url` key
@@ -100,7 +100,7 @@ interface IURLConfig {
100100
* Additional params to be added to the URL
101101
*/
102102
// TODO: We need to change the type here to accept string
103-
queryParams?: Record<string, string | number | boolean>
103+
queryParams?: Record<string, string | number | boolean> | string
104104
}
105105

106106
export default IURLConfig;

0 commit comments

Comments
 (0)