Skip to content

Commit

Permalink
docs: init
Browse files Browse the repository at this point in the history
  • Loading branch information
npmstudy committed Oct 26, 2023
1 parent 22dfcf5 commit db5e8fb
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,91 @@ rpc.fn('a', function (a: string) {
});
```

## 定义函数

```js
rpc.fn('a', function (a: string) {
return a;
});
```

## 命名空间

```js
rpc.fn('com.yourcompony.a', function (a: string) {
return a;
});
```

## Context

this === koa ctx

```js
rpc.fn('com.yourcompony.a', function (a: string) {
return return `${this.path} , ${a} `;
});
```

## 定制

server

```js
rpc.fn('com.yourcompony.a', function (a: string) {
if (this.method === 'post'){
return return `${this.path} , ${a} `;
}
});
```

client

```js
import { createClient } from '@tomrpc/client';

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

const res = await client.a('hello');
console.dir(res);
```

稍后会通过com.yourcompony.getXXXX或com.yourcompony.postXXXX实现。

## 生命周期

- beforeAll(中间件)
- beforeEach(中间件)
- beforeOne(普通函数)
- 函数执行
- afterOne(普通函数)
- afterEach(中间件)
- afterAll(中间件)


```js
import { createServer } from '@tomrpc/core';

const rpc = createServer({
beforeOne: function (ctx: any, key: string) {
console.log(ctx.path);
console.log(ctx.method);
console.log('beforeOne key=' + key);
},
});
```


## License

Expand Down

0 comments on commit db5e8fb

Please sign in to comment.