Skip to content

Commit

Permalink
Merge pull request #1300 from fugufish/update_middleware_typings
Browse files Browse the repository at this point in the history
Update middleware typings
  • Loading branch information
icebob authored Dec 4, 2024
2 parents d894073 + c0bff39 commit bc706d8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
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.ServiceSchema) => 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
14 changes: 12 additions & 2 deletions test/typescript/hello-world/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import * as path from "path";
import { ServiceBroker } from "../../../";
import { ServiceBroker, Service } from "../../../";

const broker = new ServiceBroker({
logger: {
Expand Down Expand Up @@ -35,7 +35,17 @@ const broker = new ServiceBroker({
}
}
]
}
},
middlewares: [
{
localAction(this: Service, next, action) {
return ctx => {
this.logger.info("Local action middleware", action.name);
return next(ctx);
};
},
}
]
});

broker.loadService(path.join(__dirname, "greeter.service.ts"));
Expand Down

0 comments on commit bc706d8

Please sign in to comment.