From bbf336a341661ce192c1359b93bff06a2dcd9198 Mon Sep 17 00:00:00 2001 From: "Chakhsu.Lau" Date: Thu, 18 Jan 2024 07:04:05 +0800 Subject: [PATCH] feat: client support init() --- package.json | 3 ++- src/client/index.ts | 11 +++++++++++ test/unaryCall.test.ts | 13 +++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index bfb0a7b..cde15ba 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/client/index.ts b/src/client/index.ts index aaeeaae..f7c3f14 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -21,9 +21,19 @@ export default class Clients { private _proxyClientMap: Map = 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) { @@ -98,5 +108,6 @@ export default class Clients { clear() { this._clientFactory.clear() this._proxyClientMap.clear() + this._initialized = false } } diff --git a/test/unaryCall.test.ts b/test/unaryCall.test.ts index ee7387d..1a6254b 100644 --- a/test/unaryCall.test.ts +++ b/test/unaryCall.test.ts @@ -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() }) })