lukeed / polka

A micro web server so fast, it'll make you dance! :dancers:
MIT License
5.39k stars 172 forks source link

Package: Express-like Response Helpers #14

Open lukeed opened 6 years ago

lukeed commented 6 years ago

Ported from #5:

Add a middleware package that attaches helpers directly to the `res` object?

This would mimic Express, potentially offering all/most of them.

This is a low(er) priority, but would definitely be helpful for those who can't live without the Express built-ins. 😆

r0mflip commented 6 years ago

Potentially required built-ins (must needed) have to be listed...?

lukeed commented 6 years ago

Sorry, don't understand what you mean

r0mflip commented 6 years ago

IMHO a list of must needed Express built-ins need to be made. Can't implement all of them right? And the ones that are lighter vs heavier on perf.

guilhermeoc97 commented 5 years ago

Would this be implemented as a middleware that would go on an use statement?

Because I kinda have something I would like to flesh out a bit more and maybe include here.

lukeed commented 5 years ago

I have 85% of the response helpers done now :) right now, some of the "big" ones are their own packages (eg, send, sendfile, redirect) but the others are grouped together and available as their own exports. I think this will be a @polka/response package:

import { link, download } from '@polka/response';

polka()
  .get('*', (req, res) => {
    link(res, ...);
  })

These imports are just like the current "send" package. They operate independently/don't need to be used inside Polka at all.

I might also be re-exporting those bigger packages (send, redirect, etc) from this package too. Not sure yet.

Finally, and probably more importantly, there's a @polka/compat package that will auto-attach all response helpers so that you have a near-identical Express setup.

import compat from `@polka/compat`;

polka()
  .use(compat)
  .get((req, res) => {
    res.set('X-Foo', 'custom header');
    res.status(200).json({ foo: 123 });
  })

I haven't released his components yet in a @next tag yet simply because I'm not exactly sure how to subdivide all these behaviors in the sane way. I don't want to risk renaming items, moving functions, etc until things are a little more concrete

char commented 5 years ago

Both @polka/response and @polka/compat sound like really good ideas.

The only thing that's holding me back from switching to polka from express for old projects.

jhwheeler commented 4 years ago

What is the status of this, @lukeed? Really looking forward to this!

lukeed commented 4 years ago

Thanks! I'm trying to carve out a chunk of time so that I can finish the entire Polka 1.0 suite in a 1-2 week push. All the big pieces are done – just need some documentation and some more tests.

As for this issue specifically, some of the smaller utilities are not done but they may not be needed at all. The bigger pieces of this issue left TBD are cookies & fresh

jhwheeler commented 4 years ago

Glad to hear it! The main helper we need is res.redirect; is that included in 1.0?

lukeed commented 4 years ago

Yes :)

lukeed commented 3 years ago

@talentlessguy Please do not go around advertising your framework in issues. Good practice in general.

talentlessguy commented 3 years ago

@lukeed I'm sorry, I just thought these extensions would be useful for ppl who use polka and have to rewrite them from scratch