felixmosh / bull-board

🎯 Queue background jobs inspector
MIT License
2.34k stars 366 forks source link

Can AppControllerRoute's method field ever be array? #405

Closed brkn closed 2 years ago

brkn commented 2 years ago

I'm trying to understand how to write public setApiRoutes(bullBoardRoutes: AppControllerRoute[]) for an adopter.

Rabbit hole

the type definition for AppControllerRoute is like this:

export interface AppControllerRoute {
  method: HTTPMethod | HTTPMethod[];
  route: string | string[];

  handler(request?: BullBoardRequest): Promisify<ControllerHandlerReturnType>;
}

From my understanding this AppControllerRoute is only referenced at AppRouteDefs in the api package.

export type AppRouteDefs = {
  entryPoint: AppViewRoute;
  api: AppControllerRoute[];
};

But at the packages/api/src/routes.ts in appRoutes object, only place AppRouteDefs is referenced, method is never an array, it's always a single methodName string like: put, get.

Question

  1. Should I assume method is never an array even though it's typedef is method: HTTPMethod | HTTPMethod[]; ? Is this for backwards compatibility?

  2. Same question for typedefinition of route: string | string[]

felixmosh commented 2 years ago

You can check an existing implementation adapters, method field can be an array. https://github.com/felixmosh/bull-board/blob/master/packages/express/src/ExpressAdapter.ts#L55 https://github.com/felixmosh/bull-board/blob/master/packages/fastify/src/FastifyAdapter.ts#L57

method & route should be treated as an array of, but they can be single thing, or and array of things.

Tip, you can normalize this kind with [].concat(aThing), when aThing may be a string or string[], will produce [aThing] in both of the cases.

For example:

const route = "/some/route";
console.log([].concat(route)); // = ["/some/route"];

const routeAsArray = ["/some/route1", "/some/route2"];
console.log([].concat(routeAsArray)); // = ["/some/route1", "/some/route2"];
brkn commented 2 years ago

But setApiRoutes is only called by createBullBoard, does createBullBoard ever pass an array there? Is setApiRoutes supposed to be public to the adapter's end user?

felixmosh commented 2 years ago

I'm not sure what is you question, can you explain what are you trying to implement?

Assume the usage should be similar to this:

  1. User creates an instance of an XAdapter;
  2. Passes it to createBullBoard, which calls to all these methods.
const serverAdapter = new ExpressAdapter();
const queues = [new BullMQAdapter(queueMQ)];
const { addQueue, removeQueue, setQueues, replaceQueues } = createBullBoard({
  queues,
  serverAdapter,
});

So, createBullBoard calls to adapters setApiRoutes method with the relevant end points, the adapter responsibility is to adapt to specific node framework.

If you pay attention, createBullBoard is kinda of builder design pattern.

felixmosh commented 2 years ago

Did you managed to solve it?

brkn commented 2 years ago

No I've stopped working on an adonis adapter, for the reasons I've mentioned here

I will either A. Fork the https://github.com/Rocketseat/adonis-bull/tree/alpha/ to make it work somehow B. Share the Worker definitions via private npm package between 2 server repositories and run the dashboard on fastify.

felixmosh commented 2 years ago

Do you have any small repo boilerplate that uses adonis, maybe I will able to work on integration of it.

brkn commented 2 years ago

Here is the demo for adonis-bull package, which I'm not happy with at all tbh: https://github.com/brkn/adonis-bull-demo-app

Here is the adapter I've lost hope with: https://github.com/brkn/adonis-bull-board-adapter

felixmosh commented 2 years ago

Looks like there are too many constrains in adonis, I think that a better solution would use an existing adapters.