Skip to content

Commit

Permalink
feat: client support init()
Browse files Browse the repository at this point in the history
  • Loading branch information
chakhsu committed Jan 17, 2024
1 parent 0d412d7 commit bbf336a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grpcity",
"version": "2.1.2",
"version": "2.1.3",
"description": "A powerful and complete gRPC framework for Node.js",
"author": "Chakhsu.Lau",
"license": "MIT",
Expand Down Expand Up @@ -34,6 +34,7 @@
"scripts": {
"clear": "rimraf lib && rimraf coverage",
"build": "pnpm clear && tsc -P tsconfig.json",
"publish": "pnpm build && pnpm publish .",
"lint:prettier": "prettier --cache --check --ignore-path .gitignore --ignore-path .prettierignore .",
"prettier": "pnpm lint:prettier --write",
"prepare": "husky install",
Expand Down
11 changes: 11 additions & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ export default class Clients {
private _proxyClientMap: Map<string, any> = new Map()
private _clientFactory: ClientFactory
private _credentials: ClientsOptions['credentials']
private _initialized: boolean = false

constructor(loader: ProtoLoader, options: ClientsOptions) {
this._clientFactory = new ClientFactory(loader)
this.init(options)
this._initialized = true
}

init(options: ClientsOptions) {
if (this._initialized) {
return
}

const { services, channelOptions, credentials } = options

if (credentials) {
Expand Down Expand Up @@ -98,5 +108,6 @@ export default class Clients {
clear() {
this._clientFactory.clear()
this._proxyClientMap.clear()
this._initialized = false
}
}
13 changes: 13 additions & 0 deletions test/unaryCall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,19 @@ describe('gRPC Unary Call', () => {
expect(/address/i.test(err.message)).toBeTruthy
}

clients.init({
services: { 'helloworld.Greeter': addr }
})

const greeterClient = clients.get('helloworld.Greeter')
const { status, metadata, peer, response } = await greeterClient.sayGreet({ name: 'grpcity' })

expect(typeof response).toBe('object')
expect(response.message).toBe('hello, grpcity')
expect(status.code).toBe(0)
expect(metadata.get('x-service-path')[0]).toBe('/helloworld.Greeter/SayGreet')
expect(typeof peer).toBe('string')

await server.shutdown()
})
})

0 comments on commit bbf336a

Please sign in to comment.