Skip to content

Commit

Permalink
fix(generator): fix bff and server generator (web-infra-dev#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Oct 26, 2021
1 parent 523349c commit bda98a1
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 16 deletions.
44 changes: 34 additions & 10 deletions packages/generator/generators/bff-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const handleTemplateFile = async (
process.exit(1);
}

let updateInfo = {};
let updateInfo: Record<string, string> = {};

if (framework === Framework.Express || framework === Framework.Koa) {
updateInfo = {
Expand All @@ -71,6 +71,29 @@ const handleTemplateFile = async (
};
}

if (framework === Framework.Nest) {
updateInfo = {
'dependencies.@nestjs/core': `^${await getPackageVersion(
'@nestjs/core',
)}`,
'dependencies.@nestjs/common': `^${await getPackageVersion(
'@nestjs/common',
)}`,
};
if (bffType === BFFType.Func) {
updateInfo['dependencies.express'] = `^${await getPackageVersion(
'express',
)}`;
}
} else {
updateInfo = {
...updateInfo,
[`dependencies.${framework as string}`]: `^${await getPackageVersion(
framework as string,
)}`,
};
}

await jsonAPI.update(
context.materials.default.get(path.join(appDir, 'package.json')),
{
Expand All @@ -85,9 +108,6 @@ const handleTemplateFile = async (
}`]: `^${await getPackageVersion(
`@modern-js/plugin-${framework as string}`,
)}`,
[`dependencies.${framework as string}`]: `^${await getPackageVersion(
framework as string,
)}`,
...updateInfo,
},
},
Expand Down Expand Up @@ -139,11 +159,15 @@ const handleTemplateFile = async (
.replace('.handlebars', ``),
);
await appApi.forgeTemplate(
`templates/function/app/${framework as string}.handlebars`,
`templates/function/app/${language}/${
framework as string
}.${language}.handlebars`,
undefined,
resourceKey =>
resourceKey.replace(
`templates/function/app/${framework as string}.handlebars`,
`templates/function/app/${language}/${
framework as string
}.${language}.handlebars`,
`api/_app.${language}`,
),
);
Expand All @@ -170,15 +194,15 @@ const handleTemplateFile = async (
await appApi.forgeTemplate(
`templates/framework/app/${framework as string}/**/*`,
resourceKey =>
framework === Framework.Egg ? resourceKey.includes(language) : true,
framework === Framework.Egg || framework === Framework.Koa
? resourceKey.includes(language)
: true,
resourceKey =>
resourceKey
.replace(`templates/framework/app/${framework as string}/`, 'api/')
.replace(
'.handlebars',
framework === Framework.Egg || framework === Framework.Nest
? ''
: `.${language}`,
framework === Framework.Express ? `.${language}` : '',
),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default function traceMiddleware() {
return async (ctx, next) => {
await next();
console.info(`access url: ${ctx.req.url}`);
console.info(`access url: ${ctx.url}`);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Context } from "egg";
export default function traceMiddleware() {
return async (ctx: Context, next: any) => {
await next();
console.info(`access url: ${ctx.req.url}`);
console.info(`access url: ${ctx.url}`);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Koa, { Context } from 'koa'
import koaBody from 'koa-body'

const app = new Koa();
app.use(async (ctx: Context, next) => {
console.info(`access url: ${ctx.url}`);
await next();
});

// ensure use koa
app.use(koaBody());

export default app;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { hook } from "@modern-js/runtime/server";

export default hook(({ addMiddleware }) => {
addMiddleware(() => async (ctx, next) => {
console.info(`access url: ${ctx.url}`);
await next();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Context } from "egg";

export default hook(({ addMiddleware }) => {
addMiddleware(() => async (ctx: Context, next: any) => {
console.info(`access url: ${ctx.req.url}`);
console.info(`access url: ${ctx.url}`);
await next();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { hook } from '@modern-js/runtime/server';
import { Request, Response, NextFunction } from 'express';

export default hook(({ addMiddleware }) => {
addMiddleware(async (req: Request, res: Response, next: NextFunction) => {
console.info(`access url: ${req.url}`);
next();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { hook } from '@modern-js/runtime/server';
import { Context, Next } from 'koa';

export default hook(({ addMiddleware }) => {
addMiddleware(async (ctx: Context, next: Next) => {
console.info(`access url: ${ctx.url}`);
await next();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {
Module,
NestMiddleware,
} from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
async use(req: any, res: any, next: any) {
console.info(`access url: ${req.url}`);
next();
use(req: Request, res: Response, next: NextFunction) {
console.info(`access url: ${req.url}`);
next();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const handleTemplateFile = async (
'dependencies.@nestjs/common': `^${await getPackageVersion(
'@nestjs/common',
)}`,
'dependencies.express': `^${await getPackageVersion('express')}`,
};
} else {
updateInfo = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Module,
NestMiddleware,
} from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
Expand Down

0 comments on commit bda98a1

Please sign in to comment.