Giter VIP home page Giter VIP logo

Comments (3)

ejizba avatar ejizba commented on June 17, 2024 1

Discussed during standup today. We will add a Response class, but we will not provide res as a parameter

from azure-functions-nodejs-library.

ejizba avatar ejizba commented on June 17, 2024

My feeling is that a res object is more important in frameworks that rely on callbacks, where you frequently get a bunch of references to objects (err, req, res, etc.) passed as arguments. By contrast, I think a goal of the async/await pattern is to make inputs & outputs more intuitive and IMO res is clearly an output. We could mix & match qualities of both, but that gets confusing fast. For example, if I modify the body on the res argument, but return a different object from the promise, which takes priority?

What if we provide a Response class, but don't pass res as an argument? Aka your second example code would look like this:

app.get('route', (_, req) => {
  const res = new Response({ body: { foo: 'bar' } });
  if (req.headers.has('accepts')) {
    const accepts = req.headers.get('accepts');
    if (accepts.indexOf('application/json')) {
      res.headers.add('content-type', 'application/json');
    }
  }
  return res;
});

I've been modeling after undici a lot and they do have both ResponseInit (a barebones/flexible object) and Response (a more opinionated/helpful class), so this would be consistent. Of course users would be free to use this new class or just create their own.

from azure-functions-nodejs-library.

aaronpowell avatar aaronpowell commented on June 17, 2024

As I think about it more, I do think that the "res-as-param" design might be better addressed at a higher-level wrapper over the library, as that's the sort of thing that might be better tied to a framework like Express/Fastify/etc. and not really core to Functions.

Accepting the Response object as the return value from a Function might be a good idea, as it's clear that this is pretty low level, so using the raw types might be beneficial there. Would also save on having to write type definitions!

from azure-functions-nodejs-library.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.