56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
export type UrlParams = {
|
|
[name: string]: any
|
|
}
|
|
|
|
type ParamValuePair = [string, string];
|
|
|
|
export function query(params: UrlParams = { }) {
|
|
function *simplify(name: string, param: any): IterableIterator<ParamValuePair> {
|
|
if (typeof param === 'string') {
|
|
yield [ name, param ];
|
|
} else if (typeof param === 'number') {
|
|
yield [ name, param.toString() ];
|
|
} else if (param instanceof Array) {
|
|
for (let entry of param) {
|
|
yield* simplify(`${name}[]`, entry);
|
|
}
|
|
} else if (typeof param === "object") {
|
|
for (let [key, entry] of Object.entries(param)) {
|
|
yield* simplify(`${name}[${key}]`, entry);
|
|
}
|
|
}
|
|
}
|
|
|
|
let simplified: ParamValuePair[] = [];
|
|
for (const [key, entry] of Object.entries(params)) {
|
|
for (const pair of simplify(key, entry)) {
|
|
simplified.push(pair);
|
|
}
|
|
}
|
|
|
|
return Object.values(simplified).map(entry => entry.map(encodeURIComponent).join('=')).join('&');
|
|
}
|
|
|
|
export function prepare(url: string, params: UrlParams = { }) {
|
|
const regex = /\{([\w-]+)\}/gi;
|
|
|
|
let group;
|
|
while (group = regex.exec(url)) {
|
|
const name = group[1];
|
|
|
|
url = url.replace(new RegExp(`\{${name}\}`, 'gi'), params[name]);
|
|
delete params[name];
|
|
}
|
|
|
|
return Object.keys(params).length > 0 ? `${url}?${query(params)}` : url;
|
|
}
|
|
|
|
export default {
|
|
departures: '/{provider}/departures',
|
|
stops: {
|
|
all: '/{provider}/stops',
|
|
search: '/{provider}/stops/search',
|
|
get: '/{provider}/stop/{id}'
|
|
},
|
|
prepare
|
|
} |