Skip to content

Commit

Permalink
chore: refact
Browse files Browse the repository at this point in the history
  • Loading branch information
npmstudy committed Nov 5, 2023
1 parent 3dff5b6 commit ed5c616
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 280 deletions.
2 changes: 1 addition & 1 deletion packages/core2/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Demo from './demo';
import Fn from './src/fn';
import { Fn } from './src/fn';
import RpcServer from './src/server';

const rpc = new RpcServer({});
Expand Down
4 changes: 2 additions & 2 deletions packages/core2/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import request from 'supertest';
import { describe, expect, it } from 'vitest';

import { lib } from '..';
// import { lib } from '..';

describe('lib', () => {
it('should render lib', () => {
expect(lib()).toBe('lib');
expect('lib').toBe('lib');
});
});

Expand Down
12 changes: 8 additions & 4 deletions packages/core2/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export default class Plugable implements Strategy {
this.load.push(this.getMiddleware());
}

getConfig(ctx) {
return ctx[this.name];
}

proxy() {
return async (ctx, next) => {
console.dir('proxy prefix=' + this.prefix);
Expand All @@ -65,11 +69,11 @@ export default class Plugable implements Strategy {
}

getMiddleware() {
console.dir('getMiddleware');
// console.dir('getMiddleware');
const pre = this.pre();
const process = this.process();
const after = this.after();
return compose([pre, process, after]);
const post = this.post();
return compose([pre, process, post]);
}

pre() {
Expand All @@ -79,7 +83,7 @@ export default class Plugable implements Strategy {
console.dir('pre end');
};
}
after() {
post() {
return async (ctx, next) => {
console.dir('after');
await next();
Expand Down
32 changes: 0 additions & 32 deletions packages/core2/src/core.ts

This file was deleted.

84 changes: 57 additions & 27 deletions packages/core2/src/fn.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import debug from 'debug';

import Plugable from './base';
import { mountMiddleware } from './core';
import { isArrowFunction, getHttpMethods } from './utils';
const log = debug('@tomrpc/core');

export default class Fn extends Plugable {
export class Fn extends Plugable {
// public name: string;

constructor(cfg?: any) {
Expand All @@ -15,38 +14,69 @@ export default class Fn extends Plugable {
this.prefix = '/api';
}
fn(key, fn) {
console.dir('=this.config=');
console.dir(this.config);
// console.dir('=this.config=');
// console.dir(this.config);
if (!this.config['functions']) this.config['functions'] = {};
this.config['functions'][key] = fn;
// if (Object.entries(items)) {
// // for (const [name, fn] of Object.entries(items)) {
// // if (isArrowFunction(fn)) {
// // console.log(
// // `this.rpcFunctions[${name}] is arrow function, please use ctx as param, not this`
// // );
// // }
// // if (this.rpcFunctions[name]) {
// // log(`add ${name}: ${fn}`);
// // console.log(`this.rpcFunctions[${name}] exisit`);
// // }
// // this.rpcFunctions[name] = fn;
// // }
// } else {
// this.config['functions'].push(items);
// }
}
add(items) {
for (const [name, fn] of Object.entries(items)) {
if (isArrowFunction(fn)) {
console.log(
`this.rpcFunctions[${name}] is arrow function, please use ctx as param, not this`
);
}
if (this.config['functions'][name]) {
log(`add ${name}: ${fn}`);
console.log(`this.rpcFunctions[${name}] exisit`);
}
// this.rpcFunctions[name] = fn;
this.config['functions'][name] = fn;
}
}

process() {
console.dir("this.config['functions']");
console.dir(this);
console.dir(this.serverConfig);
return mountMiddleware(this);
console.dir('process');
return this.mount();
}

mount() {
return async (ctx, next) => {
const prefix = this.prefix;
const routers = this.config['functions'];
log(routers);
const path = ctx.path.replace(prefix, '');
log('mountMiddleware' + ctx.path);
const key = '/' + path.replace('/', '').split('/').join('.');

if (!['POST', 'PUT', 'PATCH'].includes(ctx.method) && !ctx.query.$p) {
console.log('not match $p param, no process');
ctx.body = 'not match $p param, no process';
} else {
const param = ctx.method === 'POST' ? ctx.request.body : JSON.parse(ctx.query.$p);

log(key);
log(param);
log(routers[key]);

if (routers[key]) {
const args = [...param, ctx];
// console.dir(args);
const result = routers[key].apply(ctx, args);
// console.dir(result);
ctx.body = result;
} else {
const msg = JSON.stringify(ctx, null, 4);
ctx.body = ` not match path ${ctx.path} \n ctx = ${msg}`;
await next();
}
//
}
};
}
pre() {
return async (ctx, next) => {
console.log('beforeOne');
// console.log('pre');
const key = ctx.path.replace('/', '').split('/').join('.');
// this.config.beforeOne(ctx, key);

Expand All @@ -56,15 +86,15 @@ export default class Fn extends Plugable {
const supportMethods = [];
httpMethods.forEach(function (m) {
if (lastKey.indexOf(m) != -1) {
console.log(m);
// console.log(m);
supportMethods.push(m);
return m;
}
});
// console.log(supportMethods);

if (supportMethods.length === 0) {
console.log('没有匹配到包含get/post等方法的函数');
console.log(ctx.path + ',没有匹配到包含get/post等开头的函数');
await next();
} else if (ctx.method === supportMethods[0]) {
log('匹配到包含get/post等方法的函数');
Expand Down
Loading

0 comments on commit ed5c616

Please sign in to comment.