-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
urlHelpers.ts
291 lines (260 loc) · 8.81 KB
/
urlHelpers.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { OperationSpec, OperationArguments, QueryCollectionFormat } from "./interfaces";
import { getOperationArgumentValueFromParameter } from "./operationHelpers";
import { getPathStringFromParameter } from "./interfaceHelpers";
const CollectionFormatToDelimiterMap: { [key in QueryCollectionFormat]: string } = {
CSV: ",",
SSV: " ",
Multi: "Multi",
TSV: "\t",
Pipes: "|"
};
export function getRequestUrl(
baseUri: string,
operationSpec: OperationSpec,
operationArguments: OperationArguments,
fallbackObject: { [parameterName: string]: any }
): string {
const urlReplacements = calculateUrlReplacements(
operationSpec,
operationArguments,
fallbackObject
);
let requestUrl = replaceAll(baseUri, urlReplacements);
if (operationSpec.path) {
const path = replaceAll(operationSpec.path, urlReplacements);
// QUIRK: sometimes we get a path component like {nextLink}
// which may be a fully formed URL. In that case, we should
// ignore the baseUri.
if (isAbsoluteUrl(path)) {
requestUrl = path;
} else {
requestUrl = appendPath(requestUrl, path);
}
}
const { queryParams, sequenceParams } = calculateQueryParameters(
operationSpec,
operationArguments,
fallbackObject
);
requestUrl = appendQueryParams(requestUrl, queryParams, sequenceParams);
return requestUrl;
}
function replaceAll(input: string, replacements: Map<string, string>): string {
let result = input;
for (const [searchValue, replaceValue] of replacements) {
result = result.split(searchValue).join(replaceValue);
}
return result;
}
function calculateUrlReplacements(
operationSpec: OperationSpec,
operationArguments: OperationArguments,
fallbackObject: { [parameterName: string]: any }
): Map<string, string> {
const result = new Map<string, string>();
if (operationSpec.urlParameters?.length) {
for (const urlParameter of operationSpec.urlParameters) {
let urlParameterValue: string = getOperationArgumentValueFromParameter(
operationArguments,
urlParameter,
fallbackObject
);
const parameterPathString = getPathStringFromParameter(urlParameter);
urlParameterValue = operationSpec.serializer.serialize(
urlParameter.mapper,
urlParameterValue,
parameterPathString
);
if (!urlParameter.skipEncoding) {
urlParameterValue = encodeURIComponent(urlParameterValue);
}
result.set(
`{${urlParameter.mapper.serializedName || parameterPathString}}`,
urlParameterValue
);
}
}
return result;
}
function isAbsoluteUrl(url: string): boolean {
return url.includes("://");
}
function appendPath(url: string, pathToAppend?: string): string {
if (!pathToAppend) {
return url;
}
const parsedUrl = new URL(url);
let newPath = parsedUrl.pathname;
if (!newPath.endsWith("/")) {
newPath = `${newPath}/`;
}
if (pathToAppend.startsWith("/")) {
pathToAppend = pathToAppend.substring(1);
}
const searchStart = pathToAppend.indexOf("?");
if (searchStart !== -1) {
const path = pathToAppend.substring(0, searchStart);
const search = pathToAppend.substring(searchStart + 1);
newPath = newPath + path;
if (search) {
parsedUrl.search = parsedUrl.search ? `${parsedUrl.search}&${search}` : search;
}
} else {
newPath = newPath + pathToAppend;
}
parsedUrl.pathname = newPath;
return parsedUrl.toString();
}
function calculateQueryParameters(
operationSpec: OperationSpec,
operationArguments: OperationArguments,
fallbackObject: { [parameterName: string]: any }
): {
queryParams: Map<string, string | string[]>;
sequenceParams: Set<string>;
} {
const result = new Map<string, string | string[]>();
const sequenceParams: Set<string> = new Set<string>();
if (operationSpec.queryParameters?.length) {
for (const queryParameter of operationSpec.queryParameters) {
if (queryParameter.mapper.type.name === "Sequence" && queryParameter.mapper.serializedName) {
sequenceParams.add(queryParameter.mapper.serializedName);
}
let queryParameterValue: string | string[] = getOperationArgumentValueFromParameter(
operationArguments,
queryParameter,
fallbackObject
);
if (
(queryParameterValue !== undefined && queryParameterValue !== null) ||
queryParameter.mapper.required
) {
queryParameterValue = operationSpec.serializer.serialize(
queryParameter.mapper,
queryParameterValue,
getPathStringFromParameter(queryParameter)
);
const delimiter = queryParameter.collectionFormat
? CollectionFormatToDelimiterMap[queryParameter.collectionFormat]
: "";
if (Array.isArray(queryParameterValue)) {
// replace null and undefined
queryParameterValue = queryParameterValue.map((item) => {
if (item === null || item === undefined) {
return "";
}
return item;
});
}
if (queryParameter.collectionFormat === "Multi" && queryParameterValue.length === 0) {
continue;
} else if (
Array.isArray(queryParameterValue) &&
(queryParameter.collectionFormat === "SSV" || queryParameter.collectionFormat === "TSV")
) {
queryParameterValue = queryParameterValue.join(delimiter);
}
if (!queryParameter.skipEncoding) {
if (Array.isArray(queryParameterValue)) {
queryParameterValue = queryParameterValue.map((item: string) => {
return encodeURIComponent(item);
});
} else {
queryParameterValue = encodeURIComponent(queryParameterValue);
}
}
// Join pipes and CSV *after* encoding, or the server will be upset.
if (
Array.isArray(queryParameterValue) &&
(queryParameter.collectionFormat === "CSV" || queryParameter.collectionFormat === "Pipes")
) {
queryParameterValue = queryParameterValue.join(delimiter);
}
result.set(
queryParameter.mapper.serializedName || getPathStringFromParameter(queryParameter),
queryParameterValue
);
}
}
}
return {
queryParams: result,
sequenceParams
};
}
function simpleParseQueryParams(queryString: string): Map<string, string | string[]> {
const result: Map<string, string | string[]> = new Map<string, string | string[]>();
if (!queryString || queryString[0] !== "?") {
return result;
}
// remove the leading ?
queryString = queryString.slice(1);
const pairs = queryString.split("&");
for (const pair of pairs) {
const [name, value] = pair.split("=", 2);
const existingValue = result.get(name);
if (existingValue) {
if (Array.isArray(existingValue)) {
existingValue.push(value);
} else {
result.set(name, [existingValue, value]);
}
} else {
result.set(name, value);
}
}
return result;
}
/** @internal */
export function appendQueryParams(
url: string,
queryParams: Map<string, string | string[]>,
sequenceParams: Set<string>
): string {
if (queryParams.size === 0) {
return url;
}
const parsedUrl = new URL(url);
// QUIRK: parsedUrl.searchParams will have their name/value pairs decoded, which
// can change their meaning to the server, such as in the case of a SAS signature.
// To avoid accidentally un-encoding a query param, we parse the key/values ourselves
const combinedParams = simpleParseQueryParams(parsedUrl.search);
for (const [name, value] of queryParams) {
const existingValue = combinedParams.get(name);
if (Array.isArray(existingValue)) {
if (Array.isArray(value)) {
existingValue.push(...value);
const valueSet = new Set(existingValue);
combinedParams.set(name, Array.from(valueSet));
} else {
existingValue.push(value);
}
} else if (existingValue) {
let newValue = value;
if (Array.isArray(value)) {
value.unshift(existingValue);
} else if (sequenceParams.has(name)) {
newValue = [existingValue, value];
}
combinedParams.set(name, newValue);
} else {
combinedParams.set(name, value);
}
}
const searchPieces: string[] = [];
for (const [name, value] of combinedParams) {
if (typeof value === "string") {
searchPieces.push(`${name}=${value}`);
} else {
// QUIRK: If we get an array of values, include multiple key/value pairs
for (const subValue of value) {
searchPieces.push(`${name}=${subValue}`);
}
}
}
// QUIRK: we have to set search manually as searchParams will encode comma when it shouldn't.
parsedUrl.search = searchPieces.length ? `?${searchPieces.join("&")}` : "";
return parsedUrl.toString();
}