Skip to content

Commit 4aa6fb3

Browse files
authored
improvement(scully): add routes validation
* 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
1 parent 28dab59 commit 4aa6fb3

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

scully/routerPlugins/addOptionalRoutesPlugin.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,18 @@ import {RoutesTypes, RouteTypes} from '../utils/interfacesandenums';
44
import {logError, yellow, logWarn} from '../utils/log';
55

66
export const addOptionalRoutes = async (routeList = [] as string[]): Promise<HandledRoute[]> => {
7-
const routesToGenerate = await routeList.reduce(
8-
async (result: Promise<HandledRoute[]>, cur: string) => {
9-
const x = await result;
10-
if (scullyConfig.routes[cur]) {
11-
const r = await routePluginHandler(cur);
12-
x.push(...r);
13-
} else if (cur.includes('/:')) {
14-
logWarn(`No configuration for route "${yellow(cur)}" found. Skipping`);
15-
} else {
16-
x.push({route: cur, type: RouteTypes.default});
17-
}
18-
return x;
19-
},
20-
Promise.resolve([] as HandledRoute[])
21-
);
7+
const routesToGenerate = await routeList.reduce(async (result: Promise<HandledRoute[]>, cur: string) => {
8+
const x = await result;
9+
if (scullyConfig.routes[cur]) {
10+
const r = await routePluginHandler(cur);
11+
x.push(...r);
12+
} else if (cur.includes('/:')) {
13+
logWarn(`No configuration for route "${yellow(cur)}" found. Skipping`);
14+
} else {
15+
x.push({route: cur, type: RouteTypes.default});
16+
}
17+
return x;
18+
}, Promise.resolve([] as HandledRoute[]));
2219

2320
return routesToGenerate;
2421
};
@@ -44,7 +41,19 @@ async function routePluginHandler(route: string): Promise<HandledRoute[]> {
4441
return [{route, type: RouteTypes.default}];
4542
}
4643
if (plugins.router[conf.type]) {
47-
return (plugins.router[conf.type](route, conf) as unknown) as HandledRoute[];
44+
const generatedRoutes = (await (plugins.router[conf.type](route, conf) as unknown)) as HandledRoute[];
45+
generatedRoutes.forEach(handledRoute => {
46+
if (!handledRoute.route.startsWith('/')) {
47+
logWarn(
48+
`The plugin '${
49+
conf.type
50+
}' needs to return handledRoutes with a route that starts with '/'. The route ${JSON.stringify(
51+
handledRoute
52+
)} is invalid.`
53+
);
54+
}
55+
});
56+
return generatedRoutes;
4857
}
4958
return [{route, type: RouteTypes.default}];
5059
}

0 commit comments

Comments
 (0)