Skip to content

Commit

Permalink
Change typings path, related to #289
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed Jun 2, 2021
1 parent 60740d7 commit 8a2d055
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 78 deletions.
43 changes: 13 additions & 30 deletions packages/express/src/ExpressAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import express, {
Express,
NextFunction,
Request,
Response,
Router,
} from 'express';
import express, { Express, NextFunction, Request, Response, Router } from 'express';
import {
AppControllerRoute,
AppViewRoute,
BullBoardQueues,
ControllerHandlerReturnType,
HTTPMethod,
IServerAdapter,
} from '@bull-board/api/typings/app';
} from '@bull-board/api/dist/typings/app';
import { wrapAsync } from './helpers/wrapAsync';

export class ExpressAdapter implements IServerAdapter {
private readonly app: Express;
private basePath = '';
private bullBoardQueues: BullBoardQueues | undefined;
private errorHandler:
| ((error: Error) => ControllerHandlerReturnType)
| undefined;
private errorHandler: ((error: Error) => ControllerHandlerReturnType) | undefined;

constructor() {
this.app = express();
Expand All @@ -32,10 +24,7 @@ export class ExpressAdapter implements IServerAdapter {
return this;
}

public setStaticPath(
staticsRoute: string,
staticsPath: string
): ExpressAdapter {
public setStaticPath(staticsRoute: string, staticsPath: string): ExpressAdapter {
this.app.use(staticsRoute, express.static(staticsPath));

return this;
Expand All @@ -46,18 +35,14 @@ export class ExpressAdapter implements IServerAdapter {
return this;
}

public setErrorHandler(
handler: (error: Error) => ControllerHandlerReturnType
) {
public setErrorHandler(handler: (error: Error) => ControllerHandlerReturnType) {
this.errorHandler = handler;
return this;
}

public setApiRoutes(routes: AppControllerRoute[]): ExpressAdapter {
if (!this.errorHandler) {
throw new Error(
`Please call 'setErrorHandler' before using 'registerPlugin'`
);
throw new Error(`Please call 'setErrorHandler' before using 'registerPlugin'`);
} else if (!this.bullBoardQueues) {
throw new Error(`Please call 'setQueues' before using 'registerPlugin'`);
}
Expand All @@ -82,16 +67,14 @@ export class ExpressAdapter implements IServerAdapter {
)
);

router.use(
(err: Error, _req: Request, res: Response, next: NextFunction) => {
if (!this.errorHandler) {
return next();
}

const response = this.errorHandler(err);
return res.status(response.status as 500).send(response.body);
router.use((err: Error, _req: Request, res: Response, next: NextFunction) => {
if (!this.errorHandler) {
return next();
}
);

const response = this.errorHandler(err);
return res.status(response.status as 500).send(response.body);
});

this.app.use(router);
return this;
Expand Down
61 changes: 15 additions & 46 deletions packages/fastify/src/FastifyAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
BullBoardQueues,
ControllerHandlerReturnType,
IServerAdapter,
} from '@bull-board/api/typings/app';
} from '@bull-board/api/dist/typings/app';
import { FastifyInstance } from 'fastify';
import pointOfView from 'point-of-view';

Expand All @@ -20,25 +20,18 @@ type FastifyRouteDef = {
export class FastifyAdapter implements IServerAdapter {
private basePath = '';
private bullBoardQueues: BullBoardQueues | undefined;
private errorHandler:
| ((error: Error) => ControllerHandlerReturnType)
| undefined;
private errorHandler: ((error: Error) => ControllerHandlerReturnType) | undefined;
private statics: { path: string; route: string } | undefined;
private viewPath: string | undefined;
private entryRoute:
| { method: HTTPMethods; routes: string[]; filename: string }
| undefined;
private entryRoute: { method: HTTPMethods; routes: string[]; filename: string } | undefined;
private apiRoutes: Array<FastifyRouteDef> | undefined;

public setBasePath(path: string): FastifyAdapter {
this.basePath = path;
return this;
}

public setStaticPath(
staticsRoute: string,
staticsPath: string
): FastifyAdapter {
public setStaticPath(staticsRoute: string, staticsPath: string): FastifyAdapter {
this.statics = { route: staticsRoute, path: staticsPath };

return this;
Expand All @@ -49,27 +42,19 @@ export class FastifyAdapter implements IServerAdapter {
return this;
}

public setErrorHandler(
handler: (error: Error) => ControllerHandlerReturnType
) {
public setErrorHandler(handler: (error: Error) => ControllerHandlerReturnType) {
this.errorHandler = handler;
return this;
}

public setApiRoutes(routes: AppControllerRoute[]): FastifyAdapter {
this.apiRoutes = routes.reduce((result, routeRaw) => {
const routes = Array.isArray(routeRaw.route)
? routeRaw.route
: [routeRaw.route];
const methods = Array.isArray(routeRaw.method)
? routeRaw.method
: [routeRaw.method];
const routes = Array.isArray(routeRaw.route) ? routeRaw.route : [routeRaw.route];
const methods = Array.isArray(routeRaw.method) ? routeRaw.method : [routeRaw.method];

routes.forEach((route) => {
result.push({
method: methods.map((method) =>
method.toUpperCase()
) as unknown as HTTPMethods,
method: methods.map((method) => method.toUpperCase()) as unknown as HTTPMethods,
route,
handler: routeRaw.handler,
});
Expand Down Expand Up @@ -98,35 +83,19 @@ export class FastifyAdapter implements IServerAdapter {
}

public registerPlugin() {
return (
fastify: FastifyInstance,
_opts: { basePath: string },
next: (err?: Error) => void
) => {
return (fastify: FastifyInstance, _opts: { basePath: string }, next: (err?: Error) => void) => {
if (!this.statics) {
throw new Error(
`Please call 'setStaticPath' before using 'registerPlugin'`
);
throw new Error(`Please call 'setStaticPath' before using 'registerPlugin'`);
} else if (!this.entryRoute) {
throw new Error(
`Please call 'setEntryRoute' before using 'registerPlugin'`
);
throw new Error(`Please call 'setEntryRoute' before using 'registerPlugin'`);
} else if (!this.viewPath) {
throw new Error(
`Please call 'setViewsPath' before using 'registerPlugin'`
);
throw new Error(`Please call 'setViewsPath' before using 'registerPlugin'`);
} else if (!this.apiRoutes) {
throw new Error(
`Please call 'setApiRoutes' before using 'registerPlugin'`
);
throw new Error(`Please call 'setApiRoutes' before using 'registerPlugin'`);
} else if (!this.bullBoardQueues) {
throw new Error(
`Please call 'setQueues' before using 'registerPlugin'`
);
throw new Error(`Please call 'setQueues' before using 'registerPlugin'`);
} else if (!this.errorHandler) {
throw new Error(
`Please call 'setErrorHandler' before using 'registerPlugin'`
);
throw new Error(`Please call 'setErrorHandler' before using 'registerPlugin'`);
}

fastify.register(pointOfView, {
Expand Down
2 changes: 1 addition & 1 deletion packages/hapi/src/HapiAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
BullBoardQueues,
ControllerHandlerReturnType,
IServerAdapter,
} from '@bull-board/api/typings/app';
} from '@bull-board/api/dist/typings/app';
import { PluginBase, PluginPackage } from '@hapi/hapi';
import Vision from '@hapi/vision';
import Inert from '@hapi/inert';
Expand Down
2 changes: 1 addition & 1 deletion packages/koa/src/KoaAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
BullBoardQueues,
ControllerHandlerReturnType,
IServerAdapter,
} from '@bull-board/api/typings/app';
} from '@bull-board/api/dist/typings/app';

import Koa from 'koa';
import Router from 'koa-router';
Expand Down

0 comments on commit 8a2d055

Please sign in to comment.