Response helpers
See original GitHub issueThere are a few options here… 🤔
-
Add a standalone package that releases basic
send
,json
, etc helperssend(res, code, data);
This would mean removing
app.send
from the core server, which isn’t too bad since it’s only accessible if you define aapp = polka()
ahead of your route handler(s).A) Do we want to be explicit & require everything to be set?
send(res, code=200, data='', headers={}) { res.writeHead(code, headers); res.end(data || STATUS_CODES[code]); }
B) Or do we want to infer
Content-Type
based on thedata
type (zeit/micro
does this)?const TYPE = 'Content-Type'; const LENGTH = 'Content-Length'; send(res, code=200, data='', headers={}) { let len, type=headers[TYPE]; data = data || STATUS_CODES[code]; // pseudo-code if (data instanceof Stream) { type = type || 'application/octet-stream'; } else if (data instanceof Buffer) { len = data.length; type = type || 'application/octet-stream'; } else if (typeof data === 'object') { data = JSON.stringify(data); len = data.length; type = 'application/json'; } else { type = type || 'text/plain'; len = data.length; } // update headers[TYPE] = type; headers[LENGTH] = len; // write res.writeHead(code, headers); res.end(data); }
-
Add a middleware package that attaches helpers directly to the
res
object?This would mimic Express, potentially offering all/most of them.
Please vote 🙏
Please know that all options would live as external packages and not inside Polka directly.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
API Routes: Response Helpers - Next.js
API Routes include a set of Express.js-like methods for the response to help you creating new API endpoints. Learn how it works here....
Read more >Next.js - Response Helpers - Tutorialspoint
Next.js - Response Helpers, res object have express.js like helper methods to ease development to create services.
Read more >next-response-helpers - npm
Helper functions for NextJS API and SSR responses. Latest version: 0.2.0, last published: 4 months ago. Start using next-response-helpers in ...
Read more >How to handle API Errors Using Response Helpers in Next.js?
Let's see how to manage these errors using Next's Response Helpers. Next.js. It is a web framework built on top of React.js. Next....
Read more >Module: Rack::Response::Helpers - RubyDoc.info
Instance Method Summary collapse ... Add a header that may have multiple values. ... Specify that the content should be cached. ... Get...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Okay, so first of all, thank you all for your feedback! 😃
I’ve decided to release sub-packages that you all can install a la carte, depending on your values and needs.
Option 1A exists as
@polka/send
Option 1B exists as
@polka/send-type
Option 2 doesn’t exist yet, but I’ll open an issue to track it.
Thank you again for all your help! 🙏 I’m going to try and release a few more sub-packages before releasing everything as a
0.3.0
.If you want to stay a minimal server, I think the A is better. And maybe you can create a middleware which implements the B logic.