question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

There are a few options here… 🤔

  1. Add a standalone package that releases basic send, json, etc helpers

    send(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 a app = 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 the data 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);
    }
    
  2. 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:closed
  • Created 6 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
lukeedcommented, Jan 21, 2018

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.

4reactions
icebobcommented, Jan 13, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found