Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update middleware typings #1300

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/middleware.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { ActionHandler, ActionSchema, EventSchema } from "./service";
import type { ActionHandler, ActionSchema, EventSchema, EventSchemaHandler, ServiceMethod } from "./service";
import type { CallingOptions } from "./service-broker";
import type Service = require("./service");
import type ServiceBroker = require("./service-broker");
import type Service from "./service";
import type ServiceBroker from "./service-broker";
import Transporters from "./transporters";
import * as Transit from "./transit";

declare namespace MiddlewareHandler {
export type CallMiddlewareHandler = (
Expand All @@ -10,15 +12,38 @@ declare namespace MiddlewareHandler {
opts: CallingOptions
) => Promise<any>;

export type Middleware = {
[name: string]:
| ((handler: ActionHandler, action: ActionSchema) => any)
| ((handler: ActionHandler, event: EventSchema) => any)
| ((handler: ActionHandler) => any)
| ((service: Service) => any)
| ((broker: ServiceBroker) => any)
| ((handler: CallMiddlewareHandler) => CallMiddlewareHandler);
};
export interface Middleware {
name?: string;
created?: (broker: ServiceBroker) => void;
localAction?: (next: ActionHandler, action: ActionSchema) => ActionHandler;
remoteAction?: (next: ActionHandler, action: ActionSchema) => ActionHandler;
localEvent?: (next: EventSchemaHandler, event: EventSchema) => EventSchemaHandler;
remoteEvent?: (next: EventSchemaHandler, event: EventSchema) => EventSchemaHandler;
localMethod?: (next: ServiceMethod, method: ServiceMethod) => ServiceMethod;
createService?: (next: ServiceBroker['createService']) => ServiceBroker['createService'];
registerLocalService?: (next: ServiceBroker['registerLocalService']) => ServiceBroker['registerLocalService'];
destroyService?: (next: ServiceBroker['destroyService']) => ServiceBroker['destroyService'];
call?: (next: ServiceBroker['call']) => ServiceBroker['call'];
mcall?: (next: ServiceBroker['mcall']) => ServiceBroker['mcall'];
emit?: (next: ServiceBroker['emit']) => ServiceBroker['emit'];
broadcast?: (next: ServiceBroker['broadcast']) => ServiceBroker['broadcast'];
broadcastLocal?: (next: ServiceBroker['broadcastLocal']) => ServiceBroker['broadcastLocal'];
serviceCreating?: (service: Service, schema: Service.Schema) => void;
serviceCreated?: (service: Service) => void;
serviceStarting?: (service: Service) => Promise<void>;
serviceStarted?: (service: Service) => Promise<void>;
serviceStopping?: (service: Service) => Promise<void>;
serviceStopped?: (service: Service) => Promise<void>;
starting?: (broker: ServiceBroker) => Promise<void>;
started?: (broker: ServiceBroker) => Promise<void>;
stopping?: (broker: ServiceBroker) => Promise<void>;
stopped?: (broker: ServiceBroker) => Promise<void>;
transitPublish?(next: Transit['publish']): Transit['publish'];
transitMessageHandler?(next: Transit['messageHandler']): Transit['messageHandler'];
transporterSend?(next: Transporters.Base['send']): Transporters.Base['send'];
transporterReceive?(next: Transporters.Base['receive']): Transporters.Base['receive'];
newLogEntry?(type: string, args: unknown[], bindings: unknown): void;
}

export type MiddlewareInit = (broker: ServiceBroker) => Middleware;
export interface MiddlewareCallHandlerOptions {
Expand Down
1 change: 1 addition & 0 deletions src/service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ declare namespace Service {
[key: string]: ActionSchema | ActionHandler | boolean;
} & ThisType<Service<S>>;

export type ServiceMethod = (...args: any[]) => any & ThisType<Service>;
export type ServiceMethods = { [key: string]: (...args: any[]) => any } & ThisType<Service>;

export interface ServiceDependency {
Expand Down