inngest / inngest-js

The developer platform for easily building reliable workflows with zero infrastructure for TypeScript & JavaScript
https://www.inngest.com/
GNU General Public License v3.0
414 stars 41 forks source link

Pass serve handler arguments to middleware to access req-only data #453

Closed jpwilliams closed 8 months ago

jpwilliams commented 8 months ago

Summary

In some cases, a user may want to access the incoming request objects in a way that doesn't affect how Inngest should handle the request. For example, in the case of utilizing Cloudflare AI on Cloudflare Workers, an Ai instance is created using the env object passed to the request.

Currently, arguments being passed to the request handler are used only by the serve handler and aren't exposed to the function. Part of the reason for this is that the serve handler is currently entirely separate from creating functions, so typing request arguments would be difficult.

To resolve this in this PR, we instead pass these arguments to middleware, where we can then assess the request arguments and add properties to our function's input. #383 is used as the problem to solve here.

With this PR, #383 would be resolved using the following middleware:

import { Ai } from "@cloudflare/ai";
import { InngestMiddleware } from "inngest";

interface Env {
  // If you set another name in wrangler.toml as the value for 'binding',
  // replace "AI" with the variable name you defined.
  AI: any;
}

export const cloudflareMiddleware = new InngestMiddleware({
  name: "foo",
  init: () => {
    return {
      onFunctionRun: ({ reqArgs }) => {
        const [, env] = reqArgs as [Request, Env];
        const ai = new Ai(env.AI);

        return {
          transformInput: () => {
            return { ctx: { ai } };
          },
        };
      },
    };
  },
});

This would then be used like so:

import { inngest } from "./client";

export default inngest.createFunction(
  { id: "hello-world" },
  { event: "demo/event.sent" },
  async ({ ai }) => {
    // `ai` is typed and can be used directly or within a step
    const response = await ai.run("@cf/meta/llama-2-7b-chat-int8", {
      prompt: "What is the origin of the phrase Hello, World",
    });
  }
);

Checklist

Related

changeset-bot[bot] commented 8 months ago

🦋 Changeset detected

Latest commit: 71302e41a30033922d9466e70f89b4d5dd24561a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ------- | ----- | | inngest | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR