BlairCurrey / trpc-koa-adapter

Add trpc to your koa server
https://www.npmjs.com/package/trpc-koa-adapter
MIT License
22 stars 5 forks source link

refactor resolveHTTPResponse to nodeHTTPRequestHandler #4

Closed BlairCurrey closed 1 year ago

BlairCurrey commented 1 year ago

nodeHTTPRequestHandler takes care of the headers which means we wont have to handle that in this middleware. It also handles additional things that this middleware is not currently handling. It calls resolveHTTPResponse (as this middleware currently does) under the hood. It is used by the first party express, nest, and standalone (node's http package) adapaters.

There are some potential downsides - in particular that it calls res.end(). This means we cant modify the responds after calling it. We also dont have access to the response of resolveHTTPResponse as we currently do. Additionally it appears to have some logic that assumes a default status of 200, whereas the default status of koa is 404 (https://github.com/trpc/trpc/blob/abc941152b71ff2d68c63156eb5a142174779261/packages/server/src/adapters/node-http/nodeHTTPRequestHandler.ts#L63). At the moment I think these can be worked around but if that turns out not to be the case then then the other option is to use resolveHTTPResponse and re-implement the parts of nodeHTTPRequestHandler that we need. Or, see about making changes to nodeHTTPRequestHandler in tRPC (such as overriding the default status code).