Skip to content

Commit

Permalink
1.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
xXAvoraXx committed Jul 7, 2024
1 parent 692166f commit fea78ea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "umi-plugin-ssr-routes",
"author": "xXAvoraXx",
"version": "1.0.7",
"version": "1.0.8",
"main": "dist/cjs/index.js",
"types": "dist/cjs/index.d.ts",
"scripts": {
Expand Down
20 changes: 11 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ const DIR_NAME = ".";

export default (api: IApi) => {
const umiTmpDir = api.paths.absTmpPath;

api.addRuntimePluginKey(() => "ssrRoutes");

api.logger.info("Use ssr-routes plugin.");

api.describe({
key: "ssrRoutes",
config: {
schema(joi) {
return joi.object({
fetchRoutes: joi.function().required(),
apiPath: joi.string().required(),
requestLibPath: joi.string().required(),
responseType: joi.string().required(),
});
},
onChange: api.ConfigChangeType.regenerateTmpFiles,
Expand All @@ -35,11 +40,12 @@ export default (api: IApi) => {
return;
}

const { fetchRoutes } = api.userConfig?.ssrRoutes || {};
const { apiPath, requestLibPath, responseType } =
api.userConfig?.ssrRoutes || {};

if (!fetchRoutes) {
if (!apiPath && !requestLibPath && !responseType) {
api.logger.warn(
"Please configure ssrRoutes.fetchRoutes, otherwise plugin-ssr-routes will not work."
"Please configure ssrRoutes.apiPath, ssrRoutes.requestLibPath ssrRoutes.responseType, otherwise plugin-ssr-routes will not work."
);
return;
}
Expand Down Expand Up @@ -67,7 +73,7 @@ export default (api: IApi) => {

api.writeTmpFile({
path: `${DIR_NAME}/service.ts`,
content: getServiceContent(fetchRoutes),
content: getServiceContent(apiPath, requestLibPath, responseType),
});

api.writeTmpFile({
Expand All @@ -81,10 +87,6 @@ export default (api: IApi) => {
});
});

api.addRuntimePlugin({
fn: () => [join(umiTmpDir!, `${DIR_NAME}/runtime.tsx`)],
});

api.addTmpGenerateWatcherPaths(() => [
join(umiTmpDir!, `${DIR_NAME}/service.ts`),
]);
Expand Down
12 changes: 9 additions & 3 deletions src/utils/getServiceContent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
export default (fetchRoutes: () => Promise<RouteRaw[]>) => {
export default (apiPath: string, requestLibPath: string, responseType: string) => {
return `\
export async function getRoutersInfo() {
const fetchRoutes = ${fetchRoutes.toString()};
return await fetchRoutes();
const requestLib = require(${requestLibPath});
try {
const response = await requestLib.get(${apiPath});
const routes = response.${responseType};
return routes;
} catch (error) {
return [];
}
}
`;
};

0 comments on commit fea78ea

Please sign in to comment.