grpc-base-client
Advanced tools
Comparing version 1.2.0 to 1.3.0
import { ChannelOptions } from '@grpc/grpc-js'; | ||
import { Options as PackageOptions } from '@grpc/proto-loader'; | ||
export interface ClientConfig { | ||
import { GrpcMiddlewares, GrpcServiceDefinition } from './types'; | ||
export interface ClientConfig<TService extends GrpcServiceDefinition = any> { | ||
url: string; | ||
@@ -12,3 +13,4 @@ protoFile: string; | ||
grpcOptions?: Partial<ChannelOptions>; | ||
middlewares?: GrpcMiddlewares<TService>; | ||
} | ||
//# sourceMappingURL=client-config.d.ts.map |
@@ -0,9 +1,10 @@ | ||
import { GrpcServiceDefinition } from './types'; | ||
import { ClientConfig } from './client-config'; | ||
import { ClientPool } from './client-pool'; | ||
export declare class Client<T> { | ||
export declare class Client<T extends GrpcServiceDefinition<keyof T>> { | ||
private packageDefinition; | ||
private grpcInstance; | ||
readonly config: ClientConfig; | ||
readonly config: ClientConfig<T>; | ||
poolPosition?: number; | ||
constructor(config: ClientConfig, poolService?: typeof ClientPool); | ||
constructor(config: ClientConfig<T>, poolService?: typeof ClientPool); | ||
getInstance(): T; | ||
@@ -10,0 +11,0 @@ private createClient; |
@@ -1,4 +0,5 @@ | ||
export { Client } from './client'; | ||
export { ClientPool } from './client-pool'; | ||
export { ClientConfig } from './client-config'; | ||
export * from './client'; | ||
export * from './client-config'; | ||
export * from './client-pool'; | ||
export * from './types'; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ClientPool = exports.Client = void 0; | ||
var client_1 = require("./client"); | ||
Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_1.Client; } }); | ||
var client_pool_1 = require("./client-pool"); | ||
Object.defineProperty(exports, "ClientPool", { enumerable: true, get: function () { return client_pool_1.ClientPool; } }); | ||
__exportStar(require("./client"), exports); | ||
__exportStar(require("./client-config"), exports); | ||
__exportStar(require("./client-pool"), exports); | ||
__exportStar(require("./types"), exports); |
@@ -1,13 +0,6 @@ | ||
import { CallOptions, Metadata } from '@grpc/grpc-js'; | ||
import { GrpcServiceClient } from './../types'; | ||
import { ServiceClient } from '@grpc/grpc-js/build/src/make-client'; | ||
import { ClientConfig } from '../client-config'; | ||
export interface UnaryCall<P> { | ||
(param: P, ...args: unknown[]): void; | ||
} | ||
export interface UnaryCallPromisify<P, R> { | ||
(param: P, deadline: Partial<CallOptions> | undefined): PromiseLike<R>; | ||
(param: P, metadata: Metadata, deadline: Partial<CallOptions> | undefined): PromiseLike<R>; | ||
} | ||
export declare function handlePanic(client: ServiceClient, config: ClientConfig): void; | ||
export declare function overloadServices(client: ServiceClient, config: ClientConfig): ServiceClient; | ||
export declare function overloadServices<TService extends GrpcServiceClient>(client: TService, config: ClientConfig): TService; | ||
//# sourceMappingURL=overload-services.d.ts.map |
@@ -6,2 +6,4 @@ "use strict"; | ||
const client_pool_1 = require("../client-pool"); | ||
const types_1 = require("../types"); | ||
const apply_middlewares_1 = require("./apply-middlewares"); | ||
function handlePanic(client, config) { | ||
@@ -25,32 +27,38 @@ client_pool_1.ClientPool.renewConnect(config.url, client); | ||
} | ||
function applyPanicHandling(client, serviceName, action, config) { | ||
try { | ||
client[serviceName] = ((...args) => { | ||
const stream = action.apply(client, args); | ||
stream.on('error', (error) => { | ||
if ((error === null || error === void 0 ? void 0 : error.code) === constants_1.Status.UNAVAILABLE) { | ||
handlePanic(client, config); | ||
} | ||
}); | ||
return stream; | ||
}); | ||
} | ||
catch (error) { | ||
console.log(serviceName); | ||
} | ||
} | ||
function applyPromisify(client, serviceName, action, config) { | ||
client[serviceName] = promisifyUnary(action.bind(client), client, config); | ||
} | ||
function overloadServices(client, config) { | ||
const services = Object.keys(client.__proto__); | ||
services.forEach((serviceName) => { | ||
const action = client[serviceName]; | ||
if (!action) { | ||
return; | ||
} | ||
const isUnary = !(action === null || action === void 0 ? void 0 : action.requestStream) && !(action === null || action === void 0 ? void 0 : action.responseStream); | ||
if (isUnary) { | ||
client[serviceName] = promisifyUnary(action.bind(client), client, config); | ||
} | ||
if ((action === null || action === void 0 ? void 0 : action.requestStream) || (action === null || action === void 0 ? void 0 : action.responseStream)) { | ||
try { | ||
client[serviceName] = (...args) => { | ||
const stream = action.apply(client, args); | ||
stream.on('error', (error) => { | ||
if ((error === null || error === void 0 ? void 0 : error.code) === constants_1.Status.UNAVAILABLE) { | ||
handlePanic(client, config); | ||
} | ||
}); | ||
return stream; | ||
}; | ||
for (const serviceName in client.__proto__) { | ||
if (client.__proto__.hasOwnProperty(serviceName)) { | ||
const action = client[serviceName]; | ||
if (action) { | ||
if ((0, types_1.isStreamCall)(action)) { | ||
applyPanicHandling(client, serviceName, action, config); | ||
} | ||
else { | ||
applyPromisify(client, serviceName, action, config); | ||
} | ||
} | ||
catch (error) { | ||
console.log(serviceName); | ||
} | ||
} | ||
}); | ||
} | ||
(0, apply_middlewares_1.applyMiddlewares)(client, config.middlewares); | ||
return client; | ||
} | ||
exports.overloadServices = overloadServices; |
{ | ||
"name": "grpc-base-client", | ||
"description": "This project is a Library base for construct gRPC clients", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"private": false, | ||
@@ -6,0 +6,0 @@ "author": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26057
24
340