From 622cd25ff9959132b32de340b96d7d16c819cfa1 Mon Sep 17 00:00:00 2001 From: Jack Williams <1736957+jpwilliams@users.noreply.github.com> Date: Fri, 24 Jan 2025 11:52:23 +0000 Subject: [PATCH 1/7] Add and use `Inngest.Like` for public APIs --- packages/inngest/src/components/Inngest.ts | 33 ++++++++++++++++--- .../src/components/InngestCommHandler.ts | 6 ++-- .../inngest/src/components/connect/index.ts | 4 +-- packages/inngest/src/fastify.ts | 2 +- packages/inngest/src/helpers/errors.ts | 2 +- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/packages/inngest/src/components/Inngest.ts b/packages/inngest/src/components/Inngest.ts index a9dc6a3ec..e21927531 100644 --- a/packages/inngest/src/components/Inngest.ts +++ b/packages/inngest/src/components/Inngest.ts @@ -98,7 +98,9 @@ export type EventsFromOpts = * * @public */ -export class Inngest { +export class Inngest + implements Inngest.Like +{ /** * The ID of this instance, most commonly a reference to the application it * resides in. @@ -757,11 +759,32 @@ export const builtInMiddleware = (( */ export namespace Inngest { /** - * Represents any `Inngest` instance, regardless of generics and - * inference. + * Represents any `Inngest` instance, regardless of generics and inference. + * + * Prefer use of `Inngest.Like` where possible to ensure compatibility with + * multiple versions. */ export type Any = Inngest; + /** + * References any `Inngest` instance across library versions, useful for use + * in public APIs to ensure compatibility with multiple versions. + * + * Prefer use of `Inngest.Any` internally and `Inngest.Like` for public APIs. + */ + export interface Like { + readonly id: string; + apiBaseUrl: string | undefined; + eventBaseUrl: string | undefined; + env: string | null; + buildId?: string | undefined; + + setEnvVars(env?: Record): this; + setEventKey(eventKey: string): void; + send(payload: unknown, options?: { env?: string }): Promise; + createFunction: Inngest.CreateFunction; + } + export type CreateFunction = < TMiddleware extends InngestMiddleware.Stack, TTrigger extends SingleOrArray< @@ -843,7 +866,7 @@ export namespace Inngest { */ export type GetStepTools< // eslint-disable-next-line @typescript-eslint/no-explicit-any - TInngest extends Inngest, + TInngest extends Inngest.Any, TTrigger extends keyof GetEvents & string = keyof GetEvents & string, > = GetFunctionInput extends { step: infer TStep } @@ -989,5 +1012,5 @@ export type GetEvents< * @public */ // eslint-disable-next-line @typescript-eslint/no-explicit-any -export type ClientOptionsFromInngest> = +export type ClientOptionsFromInngest = TInngest extends Inngest ? U : ClientOptions; diff --git a/packages/inngest/src/components/InngestCommHandler.ts b/packages/inngest/src/components/InngestCommHandler.ts index b95f0bf52..790273741 100644 --- a/packages/inngest/src/components/InngestCommHandler.ts +++ b/packages/inngest/src/components/InngestCommHandler.ts @@ -79,7 +79,7 @@ export interface ServeHandlerOptions extends RegisterOptions { /** * The `Inngest` instance used to declare all functions. */ - client: Inngest.Any; + client: Inngest.Like; /** * An array of the functions to serve and register with Inngest. @@ -122,7 +122,7 @@ interface InngestCommHandlerOptions< * receiving events from the same service, as you can reuse a single * definition of Inngest. */ - client: Inngest.Any; + client: Inngest.Like; /** * An array of the functions to serve and register with Inngest. @@ -365,7 +365,7 @@ export class InngestCommHandler< } this.frameworkName = options.frameworkName; - this.client = options.client; + this.client = options.client as Inngest.Any; this.id = options.id || this.client.id; this.handler = options.handler as Handler; diff --git a/packages/inngest/src/components/connect/index.ts b/packages/inngest/src/components/connect/index.ts index 30bc5d5a4..a3dbc6b8d 100644 --- a/packages/inngest/src/components/connect/index.ts +++ b/packages/inngest/src/components/connect/index.ts @@ -470,11 +470,11 @@ class WebSocketWorkerConnection implements WorkerConnection { } export const connect = async ( - inngest: Inngest.Any, + inngest: Inngest.Like, options: ConnectHandlerOptions // eslint-disable-next-line @typescript-eslint/require-await ): Promise => { - const conn = new WebSocketWorkerConnection(inngest, options); + const conn = new WebSocketWorkerConnection(inngest as Inngest.Any, options); await conn.connect(); diff --git a/packages/inngest/src/fastify.ts b/packages/inngest/src/fastify.ts index 33d5c1cb5..9a2f977af 100644 --- a/packages/inngest/src/fastify.ts +++ b/packages/inngest/src/fastify.ts @@ -70,7 +70,7 @@ import { type RegisterOptions, type SupportedFrameworkName } from "./types.js"; export const frameworkName: SupportedFrameworkName = "fastify"; type InngestPluginOptions = { - client: Inngest.Any; + client: Inngest.Like; functions: InngestFunction.Any[]; options?: RegisterOptions; }; diff --git a/packages/inngest/src/helpers/errors.ts b/packages/inngest/src/helpers/errors.ts index 0d6b06b02..e7bca4e82 100644 --- a/packages/inngest/src/helpers/errors.ts +++ b/packages/inngest/src/helpers/errors.ts @@ -463,7 +463,7 @@ export const fixEventKeyMissingSteps = [ `Pass a key to the \`new Inngest()\` constructor using the \`${ "eventKey" satisfies keyof ClientOptions }\` option`, - `Use \`inngest.${"setEventKey" satisfies keyof Inngest}()\` at runtime`, + `Use \`inngest.${"setEventKey" satisfies keyof Inngest.Like}()\` at runtime`, ]; /** From 4871f330972efdf97dd6526e9ef6a93babd48f1c Mon Sep 17 00:00:00 2001 From: Jack Williams <1736957+jpwilliams@users.noreply.github.com> Date: Fri, 24 Jan 2025 11:57:28 +0000 Subject: [PATCH 2/7] Add and use `InngestFunction.Like` for public APIs --- packages/inngest/src/components/InngestCommHandler.ts | 2 +- packages/inngest/src/components/InngestFunction.ts | 10 +++++++++- packages/inngest/src/components/connect/types.ts | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/inngest/src/components/InngestCommHandler.ts b/packages/inngest/src/components/InngestCommHandler.ts index 790273741..1bf4d07a2 100644 --- a/packages/inngest/src/components/InngestCommHandler.ts +++ b/packages/inngest/src/components/InngestCommHandler.ts @@ -84,7 +84,7 @@ export interface ServeHandlerOptions extends RegisterOptions { /** * An array of the functions to serve and register with Inngest. */ - functions: readonly InngestFunction.Any[]; + functions: readonly InngestFunction.Like[]; } export interface InternalServeHandlerOptions extends ServeHandlerOptions { diff --git a/packages/inngest/src/components/InngestFunction.ts b/packages/inngest/src/components/InngestFunction.ts index 8181feeaf..9c1eb9cc4 100644 --- a/packages/inngest/src/components/InngestFunction.ts +++ b/packages/inngest/src/components/InngestFunction.ts @@ -46,7 +46,8 @@ export class InngestFunction< TTriggers extends InngestFunction.Trigger< TriggersFromClient >[] = InngestFunction.Trigger>[], -> { +> implements InngestFunction.Like +{ static stepId = "step"; static failureSuffix = "-failure"; @@ -282,6 +283,13 @@ export namespace InngestFunction { any >; + export interface Like { + opts: Options; + id(prefix?: string): string; + name: string; + description?: string; + } + /** * A user-friendly method of specifying a trigger for an Inngest function. * diff --git a/packages/inngest/src/components/connect/types.ts b/packages/inngest/src/components/connect/types.ts index 44b9d3649..16179b555 100644 --- a/packages/inngest/src/components/connect/types.ts +++ b/packages/inngest/src/components/connect/types.ts @@ -5,7 +5,7 @@ export interface ConnectHandlerOptions extends RegisterOptions { /** * An array of the functions to serve and register with Inngest. */ - functions: readonly InngestFunction.Any[]; + functions: readonly InngestFunction.Like[]; instanceId: string; maxConcurrency?: number; From b6371a78fcf7791285a6a76bd85eac159b6e7bb5 Mon Sep 17 00:00:00 2001 From: Jack Williams <1736957+jpwilliams@users.noreply.github.com> Date: Fri, 24 Jan 2025 13:50:10 +0000 Subject: [PATCH 3/7] Use `InngestFunction.Like` in comm handler opts and connect --- packages/inngest/src/components/InngestCommHandler.ts | 4 ++-- packages/inngest/src/components/connect/index.ts | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/inngest/src/components/InngestCommHandler.ts b/packages/inngest/src/components/InngestCommHandler.ts index 1bf4d07a2..48e3c32d9 100644 --- a/packages/inngest/src/components/InngestCommHandler.ts +++ b/packages/inngest/src/components/InngestCommHandler.ts @@ -127,7 +127,7 @@ interface InngestCommHandlerOptions< /** * An array of the functions to serve and register with Inngest. */ - functions: readonly InngestFunction.Any[]; + functions: readonly InngestFunction.Like[]; /** * The `handler` is the function that will be called with your framework's @@ -380,7 +380,7 @@ export class InngestCommHandler< ); // Ensure we filter any undefined functions in case of missing imports. - this.rawFns = options.functions.filter(Boolean); + this.rawFns = options.functions.filter(Boolean) as InngestFunction.Any[]; if (this.rawFns.length !== options.functions.length) { // TODO PrettyError diff --git a/packages/inngest/src/components/connect/index.ts b/packages/inngest/src/components/connect/index.ts index a3dbc6b8d..e851c0de9 100644 --- a/packages/inngest/src/components/connect/index.ts +++ b/packages/inngest/src/components/connect/index.ts @@ -1,4 +1,4 @@ -import { InngestCommHandler } from "inngest"; +import { InngestCommHandler, type InngestFunction } from "inngest"; import { ulid } from "ulid"; import { headerKeys, queryKeys } from "../../helpers/consts.js"; import { allProcessEnv, getPlatformName } from "../../helpers/env.js"; @@ -112,7 +112,8 @@ class WebSocketWorkerConnection implements WorkerConnection { }; const functions: Array = this.options.functions.flatMap( - (f) => f["getConfig"](new URL("http://example.com")) // refactor; base URL shouldn't be optional here; we likely need to fetch a different kind of config + (f) => + (f as InngestFunction.Any)["getConfig"](new URL("http://example.com")) // refactor; base URL shouldn't be optional here; we likely need to fetch a different kind of config ); const data: connectionEstablishData = { From 39ad139cd6e1c9b3710e74e0334934537c7e8de9 Mon Sep 17 00:00:00 2001 From: Jack Williams <1736957+jpwilliams@users.noreply.github.com> Date: Fri, 24 Jan 2025 13:52:35 +0000 Subject: [PATCH 4/7] Remove unused code --- packages/inngest/src/helpers/functions.test.ts | 8 -------- packages/inngest/src/helpers/functions.ts | 1 - 2 files changed, 9 deletions(-) diff --git a/packages/inngest/src/helpers/functions.test.ts b/packages/inngest/src/helpers/functions.test.ts index 11a65114d..977577e3f 100644 --- a/packages/inngest/src/helpers/functions.test.ts +++ b/packages/inngest/src/helpers/functions.test.ts @@ -1,8 +1,6 @@ -import { InngestFunction } from "@local/components/InngestFunction"; import { ExecutionVersion } from "@local/components/execution/InngestExecution"; import { parseFnData, type FnData } from "@local/helpers/functions"; import { type EventPayload } from "@local/types"; -import { createClient } from "../test/helpers"; const randomstr = (): string => { return (Math.random() + 1).toString(36).substring(2); @@ -71,12 +69,6 @@ describe("#parseFnData", () => { }, ]; - const fn = new InngestFunction( - createClient({ id: "test-client" }), - { id: "test-fn", triggers: [{ event: "test-event" }] }, - () => "test-return" - ); - specs.forEach((test) => { it(test.name, () => { if (test.isOk) { diff --git a/packages/inngest/src/helpers/functions.ts b/packages/inngest/src/helpers/functions.ts index 99fa0a907..6f16c9828 100644 --- a/packages/inngest/src/helpers/functions.ts +++ b/packages/inngest/src/helpers/functions.ts @@ -1,7 +1,6 @@ import { ZodError, z } from "zod"; import { type InngestApi } from "../api/api.js"; import { stepsSchemas } from "../api/schema.js"; -import { type InngestFunction } from "../components/InngestFunction.js"; import { ExecutionVersion, PREFERRED_EXECUTION_VERSION, From 524d773a484633efc95e9acc155c604fa60c5d83 Mon Sep 17 00:00:00 2001 From: Jack Williams <1736957+jpwilliams@users.noreply.github.com> Date: Thu, 30 Jan 2025 13:33:06 +0000 Subject: [PATCH 5/7] Remove complex types from `Like` types --- packages/inngest/src/components/Inngest.ts | 5 ----- packages/inngest/src/components/InngestFunction.ts | 2 -- 2 files changed, 7 deletions(-) diff --git a/packages/inngest/src/components/Inngest.ts b/packages/inngest/src/components/Inngest.ts index e21927531..3ea244268 100644 --- a/packages/inngest/src/components/Inngest.ts +++ b/packages/inngest/src/components/Inngest.ts @@ -778,11 +778,6 @@ export namespace Inngest { eventBaseUrl: string | undefined; env: string | null; buildId?: string | undefined; - - setEnvVars(env?: Record): this; - setEventKey(eventKey: string): void; - send(payload: unknown, options?: { env?: string }): Promise; - createFunction: Inngest.CreateFunction; } export type CreateFunction = < diff --git a/packages/inngest/src/components/InngestFunction.ts b/packages/inngest/src/components/InngestFunction.ts index 9c1eb9cc4..014269c01 100644 --- a/packages/inngest/src/components/InngestFunction.ts +++ b/packages/inngest/src/components/InngestFunction.ts @@ -284,8 +284,6 @@ export namespace InngestFunction { >; export interface Like { - opts: Options; - id(prefix?: string): string; name: string; description?: string; } From 41745741e171e3305c75306b1087d1828cd79417 Mon Sep 17 00:00:00 2001 From: Jack Williams <1736957+jpwilliams@users.noreply.github.com> Date: Thu, 30 Jan 2025 13:33:19 +0000 Subject: [PATCH 6/7] Revert to using `Inngest.Any` for local checks --- packages/inngest/src/helpers/errors.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/inngest/src/helpers/errors.ts b/packages/inngest/src/helpers/errors.ts index e7bca4e82..c0dc592bf 100644 --- a/packages/inngest/src/helpers/errors.ts +++ b/packages/inngest/src/helpers/errors.ts @@ -463,7 +463,7 @@ export const fixEventKeyMissingSteps = [ `Pass a key to the \`new Inngest()\` constructor using the \`${ "eventKey" satisfies keyof ClientOptions }\` option`, - `Use \`inngest.${"setEventKey" satisfies keyof Inngest.Like}()\` at runtime`, + `Use \`inngest.${"setEventKey" satisfies keyof Inngest.Any}()\` at runtime`, ]; /** From e8f8b435d9486b7ed26528a303493782721555d4 Mon Sep 17 00:00:00 2001 From: Jack Williams Date: Thu, 30 Jan 2025 13:43:55 +0000 Subject: [PATCH 7/7] Create late-cups-smell.md --- .changeset/late-cups-smell.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/late-cups-smell.md diff --git a/.changeset/late-cups-smell.md b/.changeset/late-cups-smell.md new file mode 100644 index 000000000..da553d527 --- /dev/null +++ b/.changeset/late-cups-smell.md @@ -0,0 +1,5 @@ +--- +"inngest": patch +--- + +`serve()` and `connect()` now have looser typing for `client` and `functions`, resulting in easier use of multiple `inngest` packages in a single process