Skip to content

Commit

Permalink
improvement(scully): add routes validation
Browse files Browse the repository at this point in the history
* improvement(scully): add routes validation

When plugins generate routes for scully, we need to validate that the handledRoutes.route start with
/. In this change we show a warning.

* style(scully): format with prettier
  • Loading branch information
Villanuevand authored Jan 4, 2020
1 parent 28dab59 commit 4aa6fb3
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions scully/routerPlugins/addOptionalRoutesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@ import {RoutesTypes, RouteTypes} from '../utils/interfacesandenums';
import {logError, yellow, logWarn} from '../utils/log';

export const addOptionalRoutes = async (routeList = [] as string[]): Promise<HandledRoute[]> => {
const routesToGenerate = await routeList.reduce(
async (result: Promise<HandledRoute[]>, cur: string) => {
const x = await result;
if (scullyConfig.routes[cur]) {
const r = await routePluginHandler(cur);
x.push(...r);
} else if (cur.includes('/:')) {
logWarn(`No configuration for route "${yellow(cur)}" found. Skipping`);
} else {
x.push({route: cur, type: RouteTypes.default});
}
return x;
},
Promise.resolve([] as HandledRoute[])
);
const routesToGenerate = await routeList.reduce(async (result: Promise<HandledRoute[]>, cur: string) => {
const x = await result;
if (scullyConfig.routes[cur]) {
const r = await routePluginHandler(cur);
x.push(...r);
} else if (cur.includes('/:')) {
logWarn(`No configuration for route "${yellow(cur)}" found. Skipping`);
} else {
x.push({route: cur, type: RouteTypes.default});
}
return x;
}, Promise.resolve([] as HandledRoute[]));

return routesToGenerate;
};
Expand All @@ -44,7 +41,19 @@ async function routePluginHandler(route: string): Promise<HandledRoute[]> {
return [{route, type: RouteTypes.default}];
}
if (plugins.router[conf.type]) {
return (plugins.router[conf.type](route, conf) as unknown) as HandledRoute[];
const generatedRoutes = (await (plugins.router[conf.type](route, conf) as unknown)) as HandledRoute[];
generatedRoutes.forEach(handledRoute => {
if (!handledRoute.route.startsWith('/')) {
logWarn(
`The plugin '${
conf.type
}' needs to return handledRoutes with a route that starts with '/'. The route ${JSON.stringify(
handledRoute
)} is invalid.`
);
}
});
return generatedRoutes;
}
return [{route, type: RouteTypes.default}];
}

0 comments on commit 4aa6fb3

Please sign in to comment.