Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Jan 11, 2024

Conversation

jpwilliams
Copy link
Member

@jpwilliams jpwilliams commented Jan 9, 2024

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

  • Added a docs PR that references this PR
  • Added unit/integration tests
  • Added changesets if applicable

Related

@jpwilliams jpwilliams added the ✨ new New features, integrations, or exports label Jan 9, 2024
@jpwilliams jpwilliams self-assigned this Jan 9, 2024
Copy link

changeset-bot bot commented Jan 9, 2024

🦋 Changeset detected

Latest commit: 71302e4

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

@inngest-release-bot inngest-release-bot added the 📦 inngest Affects the `inngest` package label Jan 9, 2024
@jpwilliams jpwilliams merged commit be6eb2a into main Jan 11, 2024
34 checks passed
@jpwilliams jpwilliams deleted the feat/middleware-req-args branch January 11, 2024 12:31
jpwilliams pushed a commit that referenced this pull request Jan 11, 2024
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## inngest@3.9.0

### Minor Changes

- [#453](#453)
[`be6eb2a`](be6eb2a)
Thanks [@jpwilliams](https://github.com/jpwilliams)! - Pass `reqArgs` to
`onFunctionRun` middleware hook to be able to use request values within
an Inngest function

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 inngest Affects the `inngest` package ✨ new New features, integrations, or exports
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Inngest client & functions should provide the request object & env variables
3 participants