-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.ts
More file actions
43 lines (36 loc) · 1.24 KB
/
utils.ts
File metadata and controls
43 lines (36 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Region, params } from './types';
export function getHostforRegion(region: Region = Region.US, host?: string): string {
if (host) return host;
let url = 'cdn.contentstack.io';
if (region !== Region.US) {
url = region.toString().toLowerCase() + '-cdn.contentstack.com';
}
return url;
}
/**
* Checks if the code is running in a browser environment
* @returns {boolean} True if running in browser, false otherwise
*/
export function isBrowser() {
return (typeof window !== "undefined");
}
/**
* Encodes query parameters recursively, handling nested objects
* @param {params} params - Query parameters object to encode
* @returns {params} Encoded query parameters object
*/
export function encodeQueryParams(params: params): params {
const encodedParams: params = {};
for (const [key, value] of Object.entries(params)) {
if (typeof value === 'string') {
encodedParams[key] = encodeURIComponent(value);
} else if (typeof value === 'object' && value !== null) {
// Handle nested objects recursively
encodedParams[key] = encodeQueryParams(value as params);
} else {
// Keep non-string values as is (numbers, booleans, etc.)
encodedParams[key] = value;
}
}
return encodedParams;
}