Tidied up Bruno files. Implemented builder utility to have more powerful route definitions.
This commit is contained in:
41
src/utilities/routeBuilder.ts
Normal file
41
src/utilities/routeBuilder.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user