Skip to content

Commit

Permalink
chore: refact
Browse files Browse the repository at this point in the history
  • Loading branch information
npmstudy committed Dec 5, 2023
1 parent dcb7783 commit 1a6a5e3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 66 deletions.
27 changes: 25 additions & 2 deletions packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 来配置
Expand All @@ -51,8 +76,6 @@ const client = createClient({

比如a.a,你的lastKey就是a,这里把namespace去掉,更简单。



## test

```
Expand Down
34 changes: 4 additions & 30 deletions packages/client/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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') {
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/proxy.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
32 changes: 0 additions & 32 deletions packages/client/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 1a6a5e3

Please sign in to comment.