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

Allow for configuration via serve #629

Open goszczynskip opened 3 months ago

goszczynskip commented 3 months ago

In a monorepo setup where Inngest functions live in a separate package, there's no solution to pass runtime configuration to functions.

apps
 └── nextjs
     └── app
         └── api
             └── inngest
                 └── route.tsx (serve is called there)
packages
 └── inngest
     └── functions
         └── myfunction.ts
     └── index.ts (middleware is setup here)

The Inngest package is a dependency of the Nextjs app. So it shouldn't import exported values from it since it will introduce cycle.

My proposed solution

I want to pass custom configuration data via serve to the client init function so I can set up the parametrized context. This follows the same context creation logic proposed by tRPC. Example from create-t3-turbo

// nextjs api route
import { inngest } from "@acme/inngest" // custom package in monorepo
import { functions } from "@acme/inngest/functions"

serve({
  client: inngest,
  functions,
  ctx: inngest.createContext({
     myProp: "myValue"
  })
})
// packages/inngest

interface InitialContext {
  myValue: string
}

const i = new Inngest<InitialContext>({
  schemas: new EventSchemas().fromRecord<Events>()
})

const middleware = i.createMiddleware({
  name: "Test Middleware",
  // here come values from serve
  init({ client, ctx }) {
    return {
       ...
    };
  };
});

export const inngest = i.use(middleware)

Describe alternatives you've considered

I considered accessing process.env.ENV_VAR directly in inngest package. This is suboptimal due to the lack of type safety. Also, this limits me only to process.env usage.

linear[bot] commented 3 months ago

INN-3240 Allow for configuration via serve