diff --git a/packages/client/README.md b/packages/client/README.md index 3ab49b0..76c2f18 100644 --- a/packages/client/README.md +++ b/packages/client/README.md @@ -29,6 +29,31 @@ rpc.fn('a', (a: string) => { }); ``` +## 返回类型 + +方法的第二个参数是返回类型,默认返回json。 + +返回json + +```js +const res = await client.a('hello'); +console.dir(res); + +or + +const res = await client.a('hello','json'); +console.dir(res); + +``` + +返回text,只有1种办法 + +```js +const res = await client.a('hello','text'); +console.dir(res); +``` + + ## 扩展 默认用 get 方法,如果想用 post,可以通过 methodFilter 来配置 @@ -51,8 +76,6 @@ const client = createClient({ 比如a.a,你的lastKey就是a,这里把namespace去掉,更简单。 - - ## test ``` diff --git a/packages/client/src/__tests__/index.test.ts b/packages/client/src/__tests__/index.test.ts index 6776ebb..f1b268d 100644 --- a/packages/client/src/__tests__/index.test.ts +++ b/packages/client/src/__tests__/index.test.ts @@ -41,22 +41,12 @@ describe('lib', () => { }, }); - // const request = supertest(rpc.callback()); - it('should GET return json', async () => { rpc.start(30001); const client = createClient({ host: '127.0.0.1', port: 30001, - // namespace: 'a', - // methodFilter: function (lastKey: string) { - // if (lastKey === 'a') { - // return 'post'; - // } else { - // return 'get'; - // } - // }, }); // console.dir(client); @@ -73,42 +63,26 @@ describe('lib', () => { it('should GET return text', async () => { rpc.start(30002); - // const res = await request2.get('/api/a?$p=["hello"]'); - // expect(res.type).toEqual('application/json'); - // expect(res.status).toEqual(200); - // expect(res.body).toEqual({ a: 'hello' }); const client = createClient({ host: '127.0.0.1', port: 30002, - // namespace: 'a', - // methodFilter: function (lastKey: string) { - // if (lastKey === 'a') { - // return 'post'; - // } else { - // return 'get'; - // } - // }, }); // console.dir(client); - const res1 = await client.b('hello', 'text'); + const res1 = await client.text('hello', 'text'); // const res1 = await req.json(); console.dir(res1); expect(res1).toBe('hello'); }); - it.only('should POST return json', async () => { - rpc.start(30002); - // const res = await request2.get('/api/a?$p=["hello"]'); - // expect(res.type).toEqual('application/json'); - // expect(res.status).toEqual(200); - // expect(res.body).toEqual({ a: 'hello' }); + it('should POST return json', async () => { + rpc.start(30003); const client = createClient({ host: '127.0.0.1', - port: 30002, + port: 30003, // namespace: 'a', methodFilter: function (lastKey: string) { if (lastKey === 'b') { diff --git a/packages/client/src/proxy.ts b/packages/client/src/proxy.ts index b11a51d..bb15e5f 100644 --- a/packages/client/src/proxy.ts +++ b/packages/client/src/proxy.ts @@ -1,9 +1,9 @@ import debug from 'debug'; -const log = console.dir; //debug('@tomrpc/client'); +const log = debug('@tomrpc/client'); import { TomClient } from '.'; -import { getHttpMethods, mergeDeep } from './utils'; +import { getHttpMethods } from './utils'; export const defaultConfig = { methodFilter: function (lastKey: string) { diff --git a/packages/client/src/utils.ts b/packages/client/src/utils.ts index 4c6e2a7..6404732 100644 --- a/packages/client/src/utils.ts +++ b/packages/client/src/utils.ts @@ -28,35 +28,3 @@ export function getHttpMethods() { 'connect', ]; } - -/** - * Simple object check. - * @param item - * @returns {boolean} - */ -export function isObject(item) { - return item && typeof item === 'object' && !Array.isArray(item); -} - -/** - * Deep merge two objects. - * @param target - * @param ...sources - */ -export function mergeDeep(target, ...sources) { - if (!sources.length) return target; - const source = sources.shift(); - - if (isObject(target) && isObject(source)) { - for (const key in source) { - if (isObject(source[key])) { - if (!target[key]) Object.assign(target, { [key]: {} }); - mergeDeep(target[key], source[key]); - } else { - Object.assign(target, { [key]: source[key] }); - } - } - } - - return mergeDeep(target, ...sources); -}