Skip to content

Commit

Permalink
feat: add view,but need make view & fn plugable
Browse files Browse the repository at this point in the history
  • Loading branch information
npmstudy committed Oct 29, 2023
1 parent 0141081 commit 958c93a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 11 deletions.
22 changes: 20 additions & 2 deletions packages/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,33 @@ import { createApp } from './src/index';
buildin: {
serve: { enable: true, root: join(import.meta.url, '.', 'public'), opts: {} },
cors: { enable: true },
jwt: {
enable: true,
secret: 'shhhhhh',
debug: true,
unless: ['/public', '/a'],
},
view: {
enable: true,
root: join(import.meta.url, '.', 'view'),
opts: {
map: {
html: 'ejs',
},
},
},
},
beforeAll: async (ctx, next) => {
console.dir(ctx.jwt);
// console.dir(ctx.jwt);
await next();
},
});

// rpc.view vs rpc.fn变成插件
rpc.fn('a', function (a) {
return a;
console.dir(this.render);
// this.render('user', { user: { name: 'alfred' } });
return { a: a };
});

rpc.start();
Expand Down
1 change: 1 addition & 0 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@tomrpc/mount": "^1.0.0",
"debug": "^4.3.4",
"desm": "^1.3.0",
"ejs": "^3.1.9",
"flat": "^6.0.1",
"jsonwebtoken": "^9.0.2",
"koa-cors": "^0.0.16",
Expand Down
24 changes: 20 additions & 4 deletions packages/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,26 @@ const log = debug('@tomrpc/app');
// debug: true
// logLevel:
// }
interface ICors {
enable?: boolean | false;
opts?: object;
}
interface IServe {
enable?: boolean | false;
root: string;
opts: object;
opts?: object;
}
interface IView {
enable?: boolean | false;
root: string;
opts?: object;
}
interface IJwt {
enable?: boolean | false;
secret?: string;
issuer?: string;
debug: boolean;
unless?: Array<string>;
}
interface IConfig {
name: string | 'tomapp';
Expand All @@ -35,9 +51,9 @@ interface IConfig {
mount?: string;
buildin: {
serve?: IServe;
cors?: { enable: true };
view?: { enable: true };
jwt?: { enable: true };
cors?: ICors;
view?: IView;
jwt?: IJwt;
};
beforeAll: any;

Check warning on line 58 in packages/app/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Unexpected any. Specify a different type
}
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/mw/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import corsMiddleware from '@koa/cors';
export const cors = {
name: 'cors',
lifeCycle: 'load',
mw: (opts = {}) => {
mw: (cfg = { opts: {} }) => {
// console.dir(opts);
return corsMiddleware(opts);
return corsMiddleware(cfg.opts);
},
};
2 changes: 1 addition & 1 deletion packages/app/src/mw/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export const jwt = {
lifeCycle: 'load',
mw: (opts) => {
// console.dir(opts);
return koajwt(opts);
return koajwt(opts).unless({ path: opts.unless });
},
};
7 changes: 5 additions & 2 deletions packages/app/src/mw/view.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import views from 'koa-views';

export const view = {
name: 'view',
lifeCycle: 'load',
mw: async (ctx: any, next: any) => {
await next();
mw: (cfg) => {
// console.dir(opts);
return views(cfg.root, cfg.opts);
},
};
3 changes: 3 additions & 0 deletions packages/app/view/user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% if (user) { %>
<h2><%= user.name %></h2>
<% } %>

0 comments on commit 958c93a

Please sign in to comment.