grpc-base-client
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -33,4 +33,8 @@ import { CallOptions, Metadata, ClientReadableStream, ClientWritableStream, Client } from '@grpc/grpc-js'; | ||
} | ||
export declare type DefaultGrpcMiddleware = (params: Parameters<OtherGrpcParams>) => void; | ||
export declare type GrpcMethodMiddleware<TService extends GrpcServiceDefinition<KeyType>, k extends keyof TService> = (params: Parameters<FullGrpcParams<TService, k>>) => void; | ||
export interface MiddlewareResult { | ||
onEnd?(error: unknown | undefined): void; | ||
onItem?<T>(result: T): T; | ||
} | ||
export declare type DefaultGrpcMiddleware = (params: Parameters<OtherGrpcParams>) => void | MiddlewareResult; | ||
export declare type GrpcMethodMiddleware<TService extends GrpcServiceDefinition<KeyType>, k extends keyof TService> = (params: Parameters<FullGrpcParams<TService, k>>) => void | MiddlewareResult; | ||
export declare type GrpcMiddlewares<TService extends GrpcServiceDefinition<KeyType>> = { | ||
@@ -37,0 +41,0 @@ [k in keyof TService]: GrpcMethodMiddleware<TService, k>[]; |
@@ -6,2 +6,11 @@ "use strict"; | ||
const grpc_js_1 = require("@grpc/grpc-js"); | ||
function treatMiddlewareResult(result, onEnds, onItems) { | ||
if (result) { | ||
const { onEnd, onItem } = result; | ||
if (onEnd) | ||
onEnds.push(onEnd.bind(result)); | ||
if (onItem) | ||
onItems.push(onItem.bind(result)); | ||
} | ||
} | ||
function callMiddlewaresFactory(defaultMiddlewares, methodMiddlewares = []) { | ||
@@ -12,5 +21,7 @@ if (!methodMiddlewares.length && !defaultMiddlewares.length) { | ||
return (params) => { | ||
const onEnds = []; | ||
const onItems = []; | ||
const [, ...others] = params; | ||
for (const middleware of defaultMiddlewares) { | ||
middleware(others); | ||
treatMiddlewareResult(middleware(others), onEnds, onItems); | ||
} | ||
@@ -20,4 +31,5 @@ params[1] = others[0]; | ||
for (const middleware of methodMiddlewares) { | ||
middleware(params); | ||
treatMiddlewareResult(middleware(params), onEnds, onItems); | ||
} | ||
return { onItems, onEnds }; | ||
}; | ||
@@ -37,2 +49,62 @@ } | ||
} | ||
function endingWithError(onEnds, err) { | ||
for (const onEnd of onEnds) { | ||
onEnd(err); | ||
} | ||
} | ||
function endingWithoutError(ended, onEnds) { | ||
if (!ended) { | ||
for (const onEnd of onEnds) { | ||
onEnd(undefined); | ||
} | ||
} | ||
} | ||
function procOnItem(onItems, result) { | ||
for (const onItem of onItems) { | ||
result = onItem(result); | ||
} | ||
return result; | ||
} | ||
async function wrapPromise(promise, onEnds, onItems) { | ||
let ended = false; | ||
try { | ||
return procOnItem(onItems, await promise); | ||
} | ||
catch (err) { | ||
ended = true; | ||
endingWithError(onEnds, err); | ||
} | ||
finally { | ||
endingWithoutError(ended, onEnds); | ||
} | ||
} | ||
async function* wrapIterable(iterable, onEnds, onItems) { | ||
let ended = false; | ||
try { | ||
if (onItems.length) { | ||
for await (const item of iterable) { | ||
yield procOnItem(onItems, item); | ||
} | ||
} | ||
else { | ||
yield* iterable; | ||
} | ||
} | ||
catch (err) { | ||
ended = true; | ||
endingWithError(onEnds, err); | ||
} | ||
finally { | ||
endingWithoutError(ended, onEnds); | ||
} | ||
} | ||
function runWrapped(rawCall, client, newArgs, onEnds, onItems) { | ||
const result = rawCall.apply(client, newArgs); | ||
if (result[Symbol.asyncIterator]) { | ||
const iterable = wrapIterable(result, onEnds, onItems); | ||
iterable.onMetadata = result.onMetadata; | ||
return iterable; | ||
} | ||
return wrapPromise(result, onEnds, onItems); | ||
} | ||
function applyMiddlewares(client, serviceMiddlewares, MetadataClass) { | ||
@@ -51,4 +123,4 @@ var _a; | ||
const newArgs = getNewParameters(args, MetadataClass); | ||
callMiddlewares.call(serviceMiddlewares, newArgs); | ||
return rawCall.apply(client, newArgs); | ||
const { onEnds, onItems } = callMiddlewares.call(serviceMiddlewares, newArgs); | ||
return runWrapped(rawCall, client, newArgs, onEnds, onItems); | ||
}); | ||
@@ -55,0 +127,0 @@ } |
{ | ||
"name": "grpc-base-client", | ||
"description": "This project is a Library base for construct gRPC clients", | ||
"version": "2.0.0", | ||
"version": "2.1.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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31018
466