42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
export function buildRoute(
|
|
route: any,
|
|
currentPath: string = '',
|
|
): {
|
|
[x: string]: {
|
|
POST?: Function;
|
|
GET?: Function;
|
|
PUT?: Function;
|
|
DELETE?: Function;
|
|
};
|
|
} {
|
|
let returnValue: { [x: string]: any } = {};
|
|
|
|
const keys = Object.keys(route);
|
|
for (let i in keys) {
|
|
const key = keys[i];
|
|
if (key === 'POST' || key === 'GET' || key === 'PUT' || key === 'DELETE' || key === 'variants') {
|
|
continue;
|
|
}
|
|
returnValue = {
|
|
...returnValue,
|
|
...buildRoute(route[key], `${currentPath}/${key}`),
|
|
};
|
|
}
|
|
|
|
if (route.variants || route.POST || route.GET || route.PUT || route.DELETE) {
|
|
const variants: string[] = route.variants ?? [];
|
|
const endpointDefinition = {
|
|
POST: route.POST,
|
|
GET: route.GET,
|
|
PUT: route.PUT,
|
|
DELETE: route.DELETE,
|
|
};
|
|
returnValue[currentPath] = endpointDefinition;
|
|
for (let key in variants) {
|
|
returnValue[`${currentPath}/${variants[key]}`] = endpointDefinition;
|
|
}
|
|
}
|
|
|
|
return returnValue;
|
|
}
|