-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathurl.ts
33 lines (27 loc) · 987 Bytes
/
url.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
/*
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
*/
const hasOwn = Object.prototype.hasOwnProperty;
export type Params = Partial<Record<string, string | number>>;
export function pathToFunc(
pathPattern: string,
options?: { charEncoding?: "percent" | "none" },
): (params?: Params) => string {
const paramRE = /\{([a-zA-Z0-9_]+?)\}/g;
return function buildURLPath(params: Record<string, unknown> = {}): string {
return pathPattern.replace(paramRE, function (_, placeholder) {
if (!hasOwn.call(params, placeholder)) {
throw new Error(`Parameter '${placeholder}' is required`);
}
const value = params[placeholder];
if (typeof value !== "string" && typeof value !== "number") {
throw new Error(
`Parameter '${placeholder}' must be a string or number`,
);
}
return options?.charEncoding === "percent"
? encodeURIComponent(`${value}`)
: `${value}`;
});
};
}