Skip to content

Commit

Permalink
define response locals type
Browse files Browse the repository at this point in the history
and some minor type definition adjustments
  • Loading branch information
paed01 committed Jul 22, 2023
1 parent 8bd1dc9 commit 5456923
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
49 changes: 29 additions & 20 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Request, Response, NextFunction, Handler, Router } from 'express'
import { Engine, BpmnEngineOptions, BpmnEngineExecutionState, BpmnMessage } from 'bpmn-engine'
import { Request, Response, NextFunction, Router, Locals } from 'express';
import { ParamsDictionary } from 'express-serve-static-core';
import { Engine, BpmnEngineOptions, BpmnEngineExecutionState, BpmnMessage } from 'bpmn-engine';
import { LRUCache } from 'lru-cache';
import { Broker } from 'smqp';

Expand Down Expand Up @@ -83,6 +84,8 @@ export interface PostponedActivity extends BpmnMessage {
executing?: BpmnMessage[];
}

type ExecutionListener = BpmnEngineOptions['listener'];

export class Engines {
constructor(options: BpmnEngineMiddlewareOptions);
adapter: IStorageAdapter;
Expand Down Expand Up @@ -144,13 +147,19 @@ export interface SignalRequestBody {
[x: string]: any;
}

type tokenParam = {
token: string,
};
interface TokenParam extends ParamsDictionary {
token: string;
}

interface TokenActivityIdParam extends TokenParam {
activityId: string;
}

type activityIdParam = {
activityId: string,
};
export interface EngineResponseLocals extends Locals {
engines: Engines;
adapter: IStorageAdapter;
listener: ExecutionListener;
}

export class BpmnEngineMiddleware {
readonly adapter?: IStorageAdapter;
Expand All @@ -159,37 +168,37 @@ export class BpmnEngineMiddleware {
constructor(options?: { adapter?: IStorageAdapter, engines?: Engines, engineOptions?: BpmnEngineOptions });
init(req: Request, res: Response, next: NextFunction): void;
/** Add adapter, engines, and app engine listener to res.locals */
addEngineLocals(req: Request, res: Response, next: NextFunction): void;
addEngineLocals(req: Request, res: Response<undefined, EngineResponseLocals>, next: NextFunction): void;
/** GET (*)?/version */
getVersion(req: Request, res: Response<{version: string}>, next: NextFunction): void;
getVersion(req: Request, res: Response<{version: string}>, next: NextFunction): Promise<void>;
/** GET (*)?/deployment */
getDeployment(req: Request, res: Response<{name: string}>, next: NextFunction): Promise<void>;
/** POST (*)?/deployment/create */
create(req: Request<any, CreateRequestBody, CreateResponseBody>, res: Response<CreateResponseBody>, next: NextFunction): Promise<void>;
/** POST (*)?/process-definition/:deploymentName/start */
start(req: Request<{deploymentName: string}, StartRequestBody, ExecutionInstance>, res: Response<ExecutionInstance>, next: NextFunction): Promise<void>;
start(req: Request<{deploymentName: string}, StartRequestBody, ExecutionInstance>, res: Response<ExecutionInstance, EngineResponseLocals>, next: NextFunction): Promise<void>;
/** GET (*)?/running */
getRunning(req: Request, res: Response<RunningResult>, next: NextFunction): Promise<void>;
/** GET (*)?/status/:token */
getStatusByToken(req: Request<tokenParam>, res: Response<EngineStatus>, next: NextFunction): Promise<void>;
getStatusByToken(req: Request<TokenParam>, res: Response<EngineStatus>, next: NextFunction): Promise<void>;
/** GET (*)?/status/:token/:activityId */
getActivityStatus(req: Request<tokenParam & activityIdParam>, res: Response<PostponedActivity>, next: NextFunction): Promise<void>;
getActivityStatus(req: Request<TokenActivityIdParam>, res: Response<PostponedActivity, EngineResponseLocals>, next: NextFunction): Promise<void>;
/** POST (*)?/resume/:token */
resumeByToken(req: Request<tokenParam>, res: Response, next: NextFunction): Promise<void>;
resumeByToken(req: Request<TokenParam>, res: Response<EngineStatus, EngineResponseLocals>, next: NextFunction): Promise<void>;
/** POST (*)?/signal/:token */
signalActivity(req: Request<tokenParam, any, SignalRequestBody>, res: Response<EngineStatus>, next: NextFunction): Promise<void>;
signalActivity(req: Request<TokenParam, EngineStatus, SignalRequestBody>, res: Response<EngineStatus, EngineResponseLocals>, next: NextFunction): Promise<void>;
/** POST (*)?/cancel/:token */
cancelActivity(req: Request<tokenParam, any, SignalRequestBody>, res: Response<EngineStatus>, next: NextFunction): Promise<void>;
cancelActivity(req: Request<TokenParam, EngineStatus, SignalRequestBody>, res: Response<EngineStatus, EngineResponseLocals>, next: NextFunction): Promise<void>;
/** POST (*)?/fail/:token */
failActivity(req: Request<tokenParam, any, SignalRequestBody>, res: Response<EngineStatus>, next: NextFunction): Promise<void>;
failActivity(req: Request<TokenParam, EngineStatus, SignalRequestBody>, res: Response<EngineStatus, EngineResponseLocals>, next: NextFunction): Promise<void>;
/** GET (*)?/state/:token */
getStateByToken(req: Request<tokenParam>, res: Response<EngineState>, next: NextFunction): Promise<void>;
getStateByToken(req: Request<TokenParam>, res: Response<EngineState>, next: NextFunction): Promise<void>;
/** DELETE (*)?/state/:token */
deleteStateByToken(req: Request<tokenParam>, res: Response, next: NextFunction): Promise<void>;
deleteStateByToken(req: Request<TokenParam>, res: Response, next: NextFunction): Promise<void>;
/** DELETE (*)?/internal/stop */
internalStopAll(req: Request, res: Response, next: NextFunction): void;
/** DELETE (*)?/internal/stop/:token */
internalStopByToken(req: Request<tokenParam>, res: Response, next: NextFunction): void;
internalStopByToken(req: Request<TokenParam>, res: Response, next: NextFunction): void;
}

interface MiddlewareReturnType extends Router {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bpmn-middleware",
"version": "0.0.7",
"version": "0.0.8",
"description": "BPMN engine express middleware",
"type": "module",
"main": "dist/main.cjs",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ BpmnEngineMiddleware.prototype.getStatusByToken = async function getStatusByToke
BpmnEngineMiddleware.prototype.getActivityStatus = async function getActivityStatus(req, res, next) {
try {
const { token, activityId } = req.params;
const postponed = await this.engines.getPostponed(token, new BpmnPrefixListener(req.app));
const postponed = await this.engines.getPostponed(token, res.locals.listener);
const activity = postponed.find((p) => p.id === activityId);

if (!activity) throw new HttpError(`Token ${token} has no running activity with id ${activityId}`, 400);
Expand Down

0 comments on commit 5456923

Please sign in to comment.