@cspell/cspell-service-bus
Advanced tools
Comparing version 6.2.4-alpha.0 to 6.3.0
@@ -1,23 +0,43 @@ | ||
import { IsARequest, RequestResponseType, ServiceRequest } from './request'; | ||
import { IsARequest, RequestResponseType, ServiceRequest, ServiceRequestFactory } from './request'; | ||
export interface Dispatcher { | ||
dispatch<R extends ServiceRequest>(request: R): R['__r']; | ||
dispatch<R extends ServiceRequest>(request: R): RequestResponseType<R>; | ||
} | ||
export declare class ServiceBus implements Dispatcher { | ||
readonly handlers: Handler[]; | ||
constructor(handlers: Handler[]); | ||
constructor(handlers?: Handler[]); | ||
addHandler(handler: HandlerFn, name: string, description?: string): void; | ||
addHandler(handler: Handler): void; | ||
dispatch<R extends ServiceRequest>(request: R): RequestResponseType<R>; | ||
defaultHandler(request: ServiceRequest): import("./request").ServiceResponseFailure<unknown>; | ||
protected reduceHandlers<R extends ServiceRequest>(handlers: readonly Handler[], request: R, dispatcher: Dispatcher, defaultHandler: HandleRequest): HandleRequest; | ||
} | ||
export declare function createServiceBus(handlers?: Handler[]): ServiceBus; | ||
export declare type HandleRequestFn<R extends ServiceRequest> = (request: R) => RequestResponseType<R>; | ||
export declare type HandleRequestFn<R extends ServiceRequest> = (request: R, next: HandleRequestKnown<R>, dispatch: Dispatcher) => RequestResponseType<R>; | ||
export interface HandleRequest { | ||
<R extends ServiceRequest>(request: R): RequestResponseType<any>; | ||
<R extends ServiceRequest>(request: R): any; | ||
} | ||
interface HandlerNext { | ||
export interface HandleRequestKnown<R extends ServiceRequest> { | ||
(request: R): RequestResponseType<R>; | ||
} | ||
export interface HandlerNext { | ||
(next: HandleRequest): HandleRequest; | ||
} | ||
export interface Handler { | ||
export interface HandlerFn { | ||
(dispatcher: Dispatcher): HandlerNext; | ||
} | ||
export declare function createRequestHandler<T extends ServiceRequest>(isA: IsARequest<T>, fn: HandleRequestFn<T>): Handler; | ||
export interface Handler { | ||
/** | ||
* Name of the Handler. | ||
* Useful for debugging and uncaught exceptions. | ||
*/ | ||
readonly name: string; | ||
/** | ||
* Optional description of the Handler. | ||
*/ | ||
readonly description?: string | undefined; | ||
readonly fn: HandlerFn; | ||
} | ||
export declare function createIsRequestHandlerFn<T extends ServiceRequest>(isA: IsARequest<T>, fn: HandleRequestFn<T>): HandlerFn; | ||
export declare function createIsRequestHandler<T extends ServiceRequest>(isA: IsARequest<T>, fn: HandleRequestFn<T>, name: string, description?: string): Handler; | ||
export declare function createRequestHandler<T extends ServiceRequest>(requestDef: ServiceRequestFactory<T>, fn: HandleRequestFn<T>, name?: string, description?: string): Handler; | ||
export declare class ErrorUnhandledRequest extends Error { | ||
@@ -32,3 +52,8 @@ readonly request: ServiceRequest; | ||
} | ||
export {}; | ||
export declare class UnhandledHandlerError extends Error { | ||
readonly handlerName: string; | ||
readonly handlerDescription: string | undefined; | ||
readonly cause: unknown; | ||
constructor(handlerName: string, handlerDescription: string | undefined, cause: unknown); | ||
} | ||
//# sourceMappingURL=bus.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ErrorServiceRequestDepthExceeded = exports.ErrorUnhandledRequest = exports.createRequestHandler = exports.createServiceBus = exports.ServiceBus = void 0; | ||
exports.UnhandledHandlerError = exports.ErrorServiceRequestDepthExceeded = exports.ErrorUnhandledRequest = exports.createRequestHandler = exports.createIsRequestHandler = exports.createIsRequestHandlerFn = exports.createServiceBus = exports.ServiceBus = void 0; | ||
const request_1 = require("./request"); | ||
const MAX_DEPTH = 10; | ||
class ServiceBus { | ||
constructor(handlers) { | ||
this.handlers = handlers; | ||
constructor(handlers = []) { | ||
this.handlers = []; | ||
handlers.forEach((h) => this.addHandler(h)); | ||
} | ||
addHandler(handler) { | ||
this.handlers.push(handler); | ||
addHandler(handler, name = 'anonymous', description) { | ||
const h = typeof handler === 'function' ? { fn: handler, name, description } : handler; | ||
const { fn, name: _name, description: _description } = h; | ||
this.handlers.push({ fn, name: _name, description: _description }); | ||
} | ||
dispatch(request) { | ||
let depth = 0; | ||
const dispatcher = { | ||
dispatch, | ||
}; | ||
const unhandledHandler = (request) => { | ||
return (0, request_1.createResponseFail)(request, new ErrorUnhandledRequest(request)); | ||
}; | ||
const handlers = this.handlers.reverse().map((m) => m(dispatcher)); | ||
const dispatcher = { dispatch }; | ||
const handler = this.reduceHandlers(this.handlers, request, dispatcher, this.defaultHandler); | ||
function dispatch(request) { | ||
@@ -27,4 +25,2 @@ ++depth; | ||
} | ||
const defaultHandler = unhandledHandler; | ||
const handler = handlers.reduce((next, h) => h(next), defaultHandler); | ||
const response = handler(request); | ||
@@ -36,2 +32,20 @@ --depth; | ||
} | ||
defaultHandler(request) { | ||
return (0, request_1.createResponseFail)(request, new ErrorUnhandledRequest(request)); | ||
} | ||
reduceHandlers(handlers, request, dispatcher, defaultHandler) { | ||
const _handlers = handlers.map((m) => ({ ...m, fn: m.fn(dispatcher) })); | ||
const handler = _handlers.reduce((next, h) => { | ||
const fn = h.fn(next); | ||
return (req) => { | ||
try { | ||
return fn(req); | ||
} | ||
catch (e) { | ||
return (0, request_1.createResponseFail)(request, new UnhandledHandlerError(h.name, h.description, e)); | ||
} | ||
}; | ||
}, defaultHandler); | ||
return handler; | ||
} | ||
} | ||
@@ -43,5 +57,17 @@ exports.ServiceBus = ServiceBus; | ||
exports.createServiceBus = createServiceBus; | ||
function createRequestHandler(isA, fn) { | ||
return (_service) => (next) => (request) => isA(request) ? fn(request) : next(request); | ||
function createIsRequestHandlerFn(isA, fn) { | ||
return (dispatcher) => (next) => (request) => isA(request) ? fn(request, next, dispatcher) : next(request); | ||
} | ||
exports.createIsRequestHandlerFn = createIsRequestHandlerFn; | ||
function createIsRequestHandler(isA, fn, name, description) { | ||
return { | ||
fn: createIsRequestHandlerFn(isA, fn), | ||
name, | ||
description, | ||
}; | ||
} | ||
exports.createIsRequestHandler = createIsRequestHandler; | ||
function createRequestHandler(requestDef, fn, name, description) { | ||
return createIsRequestHandler(requestDef.is, fn, name ?? requestDef.type, description); | ||
} | ||
exports.createRequestHandler = createRequestHandler; | ||
@@ -63,2 +89,11 @@ class ErrorUnhandledRequest extends Error { | ||
exports.ErrorServiceRequestDepthExceeded = ErrorServiceRequestDepthExceeded; | ||
class UnhandledHandlerError extends Error { | ||
constructor(handlerName, handlerDescription, cause) { | ||
super(`Unhandled Error in Handler: ${handlerName}`); | ||
this.handlerName = handlerName; | ||
this.handlerDescription = handlerDescription; | ||
this.cause = cause; | ||
} | ||
} | ||
exports.UnhandledHandlerError = UnhandledHandlerError; | ||
//# sourceMappingURL=bus.js.map |
@@ -1,3 +0,3 @@ | ||
export { ServiceRequest, ServiceRequestAsync, ServiceRequestSync } from './request'; | ||
export { ServiceRequest } from './request'; | ||
export { ServiceBus, createServiceBus } from './bus'; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createServiceBus = exports.ServiceBus = exports.ServiceRequestSync = exports.ServiceRequestAsync = void 0; | ||
exports.createServiceBus = exports.ServiceBus = exports.ServiceRequest = void 0; | ||
var request_1 = require("./request"); | ||
Object.defineProperty(exports, "ServiceRequestAsync", { enumerable: true, get: function () { return request_1.ServiceRequestAsync; } }); | ||
Object.defineProperty(exports, "ServiceRequestSync", { enumerable: true, get: function () { return request_1.ServiceRequestSync; } }); | ||
Object.defineProperty(exports, "ServiceRequest", { enumerable: true, get: function () { return request_1.ServiceRequest; } }); | ||
var bus_1 = require("./bus"); | ||
@@ -8,0 +7,0 @@ Object.defineProperty(exports, "ServiceBus", { enumerable: true, get: function () { return bus_1.ServiceBus; } }); |
@@ -1,6 +0,6 @@ | ||
export interface ServiceRequest<T = string, R = unknown> { | ||
export interface ServiceRequest<T extends string = string, R = unknown> { | ||
readonly type: T; | ||
__r?: ServiceResponseBase<R>; | ||
} | ||
export declare class BaseServiceRequest<T extends string, R> implements ServiceRequest<T, R> { | ||
declare class BaseServiceRequest<T extends string, R> implements ServiceRequest<T, R> { | ||
readonly type: T; | ||
@@ -10,11 +10,10 @@ readonly __r?: ServiceResponseBase<R>; | ||
} | ||
export declare class ServiceRequestSync<T extends string, R> extends BaseServiceRequest<T, R> { | ||
readonly sync = true; | ||
export declare class ServiceRequest<T extends string, R> extends BaseServiceRequest<T, R> { | ||
readonly type: T; | ||
constructor(type: T); | ||
} | ||
export declare class ServiceRequestAsync<T extends string, R> extends BaseServiceRequest<T, R> { | ||
constructor(type: T); | ||
} | ||
interface ServiceResponseBase<T> { | ||
___T?: T; | ||
value?: T; | ||
error?: Error | undefined; | ||
} | ||
@@ -43,3 +42,11 @@ export interface ServiceResponseSuccess<T> extends ServiceResponseBase<T> { | ||
}): (t: unknown) => t is T; | ||
export interface ServiceRequestFactory<R extends ServiceRequest, T extends string = R['type']> { | ||
type: T; | ||
is: (r: ServiceRequest | R) => r is R; | ||
create(...params: any[]): R; | ||
} | ||
export declare const __testing__: { | ||
BaseServiceRequest: typeof BaseServiceRequest; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=request.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isInstanceOfFn = exports.isServiceResponseFailure = exports.isServiceResponseSuccess = exports.createResponseFail = exports.createResponse = exports.ServiceRequestAsync = exports.ServiceRequestSync = exports.BaseServiceRequest = void 0; | ||
exports.__testing__ = exports.isInstanceOfFn = exports.isServiceResponseFailure = exports.isServiceResponseSuccess = exports.createResponseFail = exports.createResponse = exports.ServiceRequest = void 0; | ||
class BaseServiceRequest { | ||
@@ -9,16 +9,9 @@ constructor(type) { | ||
} | ||
exports.BaseServiceRequest = BaseServiceRequest; | ||
class ServiceRequestSync extends BaseServiceRequest { | ||
class ServiceRequest extends BaseServiceRequest { | ||
constructor(type) { | ||
super(type); | ||
this.sync = true; | ||
this.type = type; | ||
} | ||
} | ||
exports.ServiceRequestSync = ServiceRequestSync; | ||
class ServiceRequestAsync extends BaseServiceRequest { | ||
constructor(type) { | ||
super(type); | ||
} | ||
} | ||
exports.ServiceRequestAsync = ServiceRequestAsync; | ||
exports.ServiceRequest = ServiceRequest; | ||
function createResponse(value) { | ||
@@ -44,2 +37,5 @@ return { value }; | ||
exports.isInstanceOfFn = isInstanceOfFn; | ||
exports.__testing__ = { | ||
BaseServiceRequest, | ||
}; | ||
//# sourceMappingURL=request.js.map |
@@ -6,3 +6,3 @@ { | ||
}, | ||
"version": "6.2.4-alpha.0", | ||
"version": "6.3.0", | ||
"description": "A Library for connecting requests to services that can fulfill them.", | ||
@@ -78,3 +78,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "3509b9fb14722e573713ba1828af6da6088a24e8" | ||
"gitHead": "6b7b2a0e92f0b3c760dd3c3fd6bc8a8a873e1987" | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
21233
13
410
0
1