TS-RPC
使用方法
ts-rpc.json
{
"client": {
"baseUrl": "127.0.0.1:3000"
},
"server": {
"scanDir": ["server/*.ts"],
"metaOutDir": "./"
}
}
服务端
import { bindKoa, RPCService, RPCMethod } from '@hughfenghen/ts-rpc/server'
bindKoa(User)
@RPCService()
class User {
@RPCMethod()
getInfoById(id: string): { name: string, age: number, avatar: string } {
return {
name: '22',
age: 18,
avatar: '<image url>'
}
}
}
客户端
import { createRetmoteService } from '@hughfenghen/ts-rpc/client'
const rs = createRetmoteService({
baseUrl: "127.0.0.1:3000",
agent: (req: Request): Response => {
}
})
const userInfo = await rs.User.getInfoById('<user id>')
console.log(userInfo)
运行 demo
git clone git@github.com:hughfenghen/ts-rpc.git
cd demo && yarn
yarn server
新开 shell 窗口,yarn client