unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.83k stars 491 forks source link

Azure functions: Progamming Model V4 #1103

Open mcremer-able opened 1 year ago

mcremer-able commented 1 year ago

Describe the feature

Azure relased a new model to programm azure functions. version

Supports a flexible file structure and code-centric approach to triggers and bindings

This would allow use to give our apps a bigger surface area and also use more advanced features of azure functions At the moment we are just redirecting everything to a single function with a custom function.json

{
  "entryPoint": "handle",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "{*url}",
      "methods": ["delete", "get", "head", "options", "patch", "post", "put"]
    },
    { "type": "http", "direction": "out", "name": "res" }
  ]
}

Model V4

The new style would allow us to dynamically define endpoints and handlers in a single(or multiple) main.js that imports from a function folder.

const { app } = require('@azure/functions');
const nitroapp = require('./functions/app')
const fileload= require('./functions/static')

app.http('httpTrigger1', {
    methods: ['POST', 'PATCH'],
    route:'/api/'
    handler: async (req, context) => {
        return nitroapp.localCall({
          url: req.url,
          headers: req.headers,
          method: req.method,
          body: req.body,
    })
    }
});
app.http('httpTrigger1', {
    methods: ['POST', 'PATCH'],
    route:'/static/'
    handler: async (req, context) => {
        return fileload.localCall({
          url: req.url,
          headers: req.headers,
          method: req.method,
          body: req.body,
    })
    }
});

Pros

Extra

This could also adress https://github.com/Azure/azure-functions-host/issues/293 In https://github.com/unjs/nitro/blob/8e06f2e4a65c6de0d2f2f3a5ef156ae3821ed60a/src/runtime/entries/azure-functions.ts#L12

Work required

Additional information

pi0 commented 1 year ago

/cc @danielroe

danielroe commented 1 year ago

This seems like a good move, although we probably should start it off as a separate preset/entry while it's still in preview and only supports Node 18.x.