Skip to content
Merged
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
24 changes: 14 additions & 10 deletions packages/server/src/express/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ export interface MiddlewareOptions extends AdapterBaseOptions {
* Callback for getting a PrismaClient for the given request
*/
getPrisma: (req: Request, res: Response) => unknown | Promise<unknown>;

/**
* This option is used to enable/disable the option to manage the response
* by the middleware. If set to true, the middleware will not send the
* response and the user will be responsible for sending the response.
* Controls if the middleware directly sends a response. If set to false,
* the response is stored in the `res.locals` object and then the middleware
* calls the `next()` function to pass the control to the next middleware.
* Subsequent middleware or request handlers need to make sure to send
* a response.
*
* Defaults to false;
* Defaults to true;
*/
manageCustomResponse?: boolean;
sendResponse?: boolean;
}

/**
Expand All @@ -43,9 +46,9 @@ const factory = (options: MiddlewareOptions): Handler => {

return async (request, response, next) => {
const prisma = (await options.getPrisma(request, response)) as DbClientContract;
const { manageCustomResponse } = options;
const { sendResponse } = options;

if (manageCustomResponse && !prisma) {
if (sendResponse === false && !prisma) {
throw new Error('unable to get prisma from request context');
}

Expand All @@ -69,7 +72,7 @@ const factory = (options: MiddlewareOptions): Handler => {
}
query = buildUrlQuery(rawQuery, useSuperJson);
} catch {
if (manageCustomResponse) {
if (sendResponse === false) {
throw new Error('invalid query parameters');
}
return response.status(400).json(marshalToObject({ message: 'invalid query parameters' }, useSuperJson));
Expand All @@ -86,7 +89,8 @@ const factory = (options: MiddlewareOptions): Handler => {
zodSchemas,
logger: options.logger,
});
if (manageCustomResponse) {
if (sendResponse === false) {
// attach response and pass control to the next middleware
response.locals = {
status: r.status,
body: r.body,
Expand All @@ -95,7 +99,7 @@ const factory = (options: MiddlewareOptions): Handler => {
}
return response.status(r.status).json(marshalToObject(r.body, useSuperJson));
} catch (err) {
if (manageCustomResponse) {
if (sendResponse === false) {
throw err;
}
return response
Expand Down
2 changes: 1 addition & 1 deletion packages/server/tests/adapter/express.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ describe('Express adapter tests - rest handler with customMiddleware', () => {
modelMeta,
zodSchemas,
handler: RESTAPIHandler({ endpoint: 'http://localhost/api' }),
manageCustomResponse: true,
sendResponse: false,
})
);

Expand Down