Skip to content

Commit

Permalink
feat: 根据方法名确定get/post请求
Browse files Browse the repository at this point in the history
  • Loading branch information
npmstudy committed Oct 26, 2023
1 parent e155839 commit c97746b
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 5 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,34 @@ const res = await client.a('hello');
console.dir(res);
```

稍后会通过com.yourcompony.getXXXX或com.yourcompony.postXXXX实现。
## 根据方法名确定get/post请求

```js

rpc.fn('getUsers', function (a: string) {
return {
a: a,
msg: 'getUsers',
};
});

rpc.fn('postUsers', function (a: string) {
return {
a: a,
msg: 'postUsers',
};
});



const res = await client.getUsers('hello');
const res = await client.postUsers('hello');

```

如果发送get请求`http://127.0.0.1:3000/postUsers?$p=[%22hello%22]
`,会返回`process fn:postUsers , you need send post request from client
`

## 生命周期

Expand Down
14 changes: 14 additions & 0 deletions example/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ const rpc = createServer({
},
});

rpc.fn('getUsers', function (a: string) {
return {
a: a,
msg: 'getUsers',
};
});

rpc.fn('postUsers', function (a: string) {
return {
a: a,
msg: 'postUsers',
};
});

rpc.fn('a', (a: string) => {
return a;
});
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export class TomClient {
body: JSON.stringify(r),
headers: { 'Content-Type': 'application/json' },
});

const data = await response.text();
log(data);
return data;
}
}
22 changes: 20 additions & 2 deletions packages/client/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import debug from 'debug';
const log = debug('@tomrpc/client');

import { TomClient } from '.';
import { getHttpMethods } from './utils';

export const defaultConfig = {
methodFilter: function (lastKey: string) {
Expand All @@ -12,10 +13,27 @@ export const defaultConfig = {
// console.dir(config);
const o = new TomClient(config);
const lastKey = key.split('.').pop();
const method = config.methodFilter(lastKey).toLowerCase();
let method = config.methodFilter(lastKey).toLowerCase();
const httpMethods = getHttpMethods();
log(key);

// getUser => get
// 优先级比methodFilter高
const supportMethods = [];
httpMethods.forEach(function (m) {
if (lastKey.indexOf(m) != -1) {
log(m);
supportMethods.push(m);
return m;
}
});
log('supportMethods');
log(supportMethods);

if (supportMethods.length > 0) method = supportMethods[0];
log(lastKey);
log(method);
const _p = [key, ...parms];
const _p = [key.replace('default.', ''), ...parms];
return await o[method](..._p);
},
};
Expand Down
30 changes: 30 additions & 0 deletions packages/client/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export function getHttpMethods() {
return [
'get',
'post',
'put',
'head',
'delete',
'options',
'trace',
'copy',
'lock',
'mkcol',
'move',
'purge',
'propfind',
'proppatch',
'unlock',
'report',
'mkactivity',
'checkout',
'merge',
'm-search',
'notify',
'subscribe',
'unsubscribe',
'patch',
'search',
'connect',
];
}
24 changes: 24 additions & 0 deletions packages/client/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,28 @@ const main = async () => {
// console.dir(b);
};

const post = async () => {
const client = createClient({
host: '127.0.0.1',
port: 3000,
// namespace: 'a',
// methodFilter: function (lastKey: string) {
// if (lastKey === 'a') {
// return 'post';
// } else {
// return 'get';
// }
// },
});

const res1 = await client.getUsers('hello getUsers');
console.dir(res1);
const res = await client.postUsers('hello postUsers');
console.dir(res);

// const b = await client.a('hello');
// console.dir(b);
};

main();
post();
33 changes: 31 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Koa from 'koa';
import compose from 'koa-compose';

import { mountMiddleware } from './core';
import { isArrowFunction } from './utils';
import { isArrowFunction, getHttpMethods } from './utils';

export const lib = () => 'lib';

Expand Down Expand Up @@ -81,7 +81,36 @@ export const createServer = function (config?: any) {
log('beforeOne');
const key = ctx.path.replace('/', '').split('/').join('.');
_cfg.beforeOne(ctx, key);
await next();

const lastKey = key.split('.').pop();
const httpMethods = getHttpMethods();

const supportMethods = [];
httpMethods.forEach(function (m) {
if (lastKey.indexOf(m) != -1) {
console.log(m);
supportMethods.push(m);
return m;
}
});
console.log(supportMethods);

if (supportMethods.length === 0) {
log('没有匹配到包含get/post等方法的函数');
await next();
} else if (ctx.method === supportMethods[0]) {
log('匹配到包含get/post等方法的函数');
await next();
} else {
log('匹配到包含get/post等方法的函数,但method不对');
ctx.body =
'process fn:' +
lastKey +
' , you need send ' +
supportMethods[0] +
' request from client';
}

log('beforeOne end');
},
mountMiddleware(this.rpcFunctions),
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,34 @@ export const isArrowFunction = (func) => {
}
return false;
};

export function getHttpMethods() {
return [
'get',
'post',
'put',
'head',
'delete',
'options',
'trace',
'copy',
'lock',
'mkcol',
'move',
'purge',
'propfind',
'proppatch',
'unlock',
'report',
'mkactivity',
'checkout',
'merge',
'm-search',
'notify',
'subscribe',
'unsubscribe',
'patch',
'search',
'connect',
];
}

0 comments on commit c97746b

Please sign in to comment.