-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueryParams.ts
162 lines (146 loc) · 5.18 KB
/
queryParams.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {
QueryParams,
PersistableQueryParams,
} from '@/shared/model/QueryParams';
import { IdApiQueryParams } from '../model/IdapiQueryParams';
import { AllRoutes } from '../model/Routes';
/**
* function to remove undefined, null, and empty string values from an object
* and covert the values to strings so they can be used in a query string using
* the URLSearchParams object
* @param obj Object to remove empty keys from
* @returns Object with empty keys removed and values converted to strings
*/
export const removeEmptyKeysFromObjectAndConvertValuesToString = (
obj: Record<string, unknown>,
) => {
const newObj: Record<string, string> = {};
Object.keys(obj).forEach((key) => {
const asStr = obj[key]?.toString();
if (asStr) {
// eslint-disable-next-line functional/immutable-data -- we have to mutate the object to convert the values to strings
newObj[key] = asStr;
}
});
return newObj;
};
/**
* @param params QueryParams object with all query parameters
* @returns PersistableQueryParams - object with query parameters to persist between requests
*/
export const getPersistableQueryParams = (
params: QueryParams,
): PersistableQueryParams => ({
returnUrl: params.returnUrl,
clientId: params.clientId,
ref: params.ref,
refViewId: params.refViewId,
listName: params.listName,
componentEventParams: params.componentEventParams,
fromURI: params.fromURI,
appClientId: params.appClientId,
useOktaClassic: params.useOktaClassic,
usePasscodeSignIn: params.usePasscodeSignIn,
signInCurrentView: params.signInCurrentView,
});
/**
* @param params QueryParams object with all query parameters
* @returns PersistableQueryParams - object with query parameters to persist between requests but without the appClientId and fromURI params which are Okta specific
*/
export const getPersistableQueryParamsWithoutOktaParams = (
params: QueryParams,
): PersistableQueryParams => {
const persistableQueryParams = getPersistableQueryParams(params);
// eslint-disable-next-line functional/immutable-data -- we mutate the object to remove values
delete persistableQueryParams.appClientId;
// eslint-disable-next-line functional/immutable-data -- we mutate the object to remove values
delete persistableQueryParams.fromURI;
return persistableQueryParams;
};
/**
*
* @param path a path segment of a url that is typed as part of RoutePaths
* @param params QueryParams - any query params to be added to the path
* @param overrides Any query parameter overrides
* @returns string
* This takes a non-typed url string and adds query parameters
* This should only be used when you have a path that is dynamically generated ie from another input parameter
* You should use addQueryParamsToPath for all typed internal application urls
*/
export const addQueryParamsToPath = (
path: AllRoutes,
params: QueryParams,
overrides?: Partial<QueryParams>,
): string => {
return addQueryParamsToUntypedPath(path, params, overrides);
};
/**
*
* @param path a path segment of a url that is not typed as part of RoutePaths
* @param params QueryParams - any query params to be added to the path
* @param overrides Any query parameter overrides
* @returns string
* This takes a non-typed url string and adds query parameters
* This should only be used when you have a path that is dynamically generated ie from another input parameter
* You should use addQueryParamsToPath for all typed internal application urls
*/
export const addQueryParamsToUntypedPath = (
path: string,
params: QueryParams,
overrides?: Partial<QueryParams>,
): string => {
const divider = path.includes('?') ? '&' : '?';
const searchParams = new URLSearchParams(
removeEmptyKeysFromObjectAndConvertValuesToString({
...getPersistableQueryParams(params),
...overrides,
}),
);
searchParams.sort();
return `${path}${divider}${searchParams.toString()}`;
};
/**
*
* @param path a path segment of a url
* @param params QueryParams - any query params to be added to the path
* @param overrides Any query parameter overrides
* @returns string
* addApiQueryParamsToPath is for adding query params to an API path
* These parameters are not filtered, so this function is slighly different to addQueryParamsToUntypedPath
*/
export const addApiQueryParamsToPath = (
path: string,
params: IdApiQueryParams,
overrides?: Partial<QueryParams>,
): string => {
const divider = path.includes('?') ? '&' : '?';
const searchParams = new URLSearchParams(
removeEmptyKeysFromObjectAndConvertValuesToString({
...params,
...overrides,
}),
);
searchParams.sort();
return `${path}${divider}${searchParams.toString()}`;
};
/**
*
* @param params QueryParams - any query params to be added to the path
* @param overrides Any query parameter overrides
* @returns string
* buildQueryParamsString is for building a Gateway compatible query string
* These parameters are are filtered, so only allowed parameters can be added.
*/
export const buildQueryParamsString = (
params: QueryParams,
overrides?: Partial<QueryParams>,
): string => {
const searchParams = new URLSearchParams(
removeEmptyKeysFromObjectAndConvertValuesToString({
...getPersistableQueryParams(params),
...overrides,
}),
);
searchParams.sort();
return `?${searchParams.toString()}`;
};