From c97746b9a635dbf2771bfaf307375666a72d5ddc Mon Sep 17 00:00:00 2001 From: npmstudy Date: Thu, 26 Oct 2023 15:44:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=A0=B9=E6=8D=AE=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=90=8D=E7=A1=AE=E5=AE=9Aget/post=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 29 ++++++++++++++++++++++++++++- example/src/app.ts | 14 ++++++++++++++ packages/client/src/client.ts | 2 ++ packages/client/src/proxy.ts | 22 ++++++++++++++++++++-- packages/client/src/utils.ts | 30 ++++++++++++++++++++++++++++++ packages/client/test.ts | 24 ++++++++++++++++++++++++ packages/core/src/index.ts | 33 +++++++++++++++++++++++++++++++-- packages/core/src/utils.ts | 31 +++++++++++++++++++++++++++++++ 8 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 packages/client/src/utils.ts diff --git a/README.md b/README.md index 23e8874..21fa271 100644 --- a/README.md +++ b/README.md @@ -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 +` ## 生命周期 diff --git a/example/src/app.ts b/example/src/app.ts index 9de6977..f255608 100644 --- a/example/src/app.ts +++ b/example/src/app.ts @@ -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; }); diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 74c041a..5b1afed 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -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; } } diff --git a/packages/client/src/proxy.ts b/packages/client/src/proxy.ts index bfdace5..ff30e23 100644 --- a/packages/client/src/proxy.ts +++ b/packages/client/src/proxy.ts @@ -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) { @@ -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); }, }; diff --git a/packages/client/src/utils.ts b/packages/client/src/utils.ts new file mode 100644 index 0000000..6404732 --- /dev/null +++ b/packages/client/src/utils.ts @@ -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', + ]; +} diff --git a/packages/client/test.ts b/packages/client/test.ts index b1aaf01..d9044f9 100644 --- a/packages/client/test.ts +++ b/packages/client/test.ts @@ -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(); diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 49bb481..78d31a8 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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'; @@ -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), diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index 759e35d..fd9264e 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -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', + ]; +}