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

Add generic function invocation event to all event schemas #489

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-phones-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"inngest": patch
---

Add generic function invocation event to all event schemas
21 changes: 14 additions & 7 deletions packages/inngest/etc/inngest.api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/inngest/src/components/EventSchemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ describe("EventSchemas", () => {

type Expected =
| `${internalEvents.FunctionFailed}`
| `${internalEvents.FunctionFinished}`;
| `${internalEvents.FunctionFinished}`
| `${internalEvents.FunctionInvoked}`;

type Actual = Schemas<typeof schemas>[keyof Schemas<
typeof schemas
Expand Down
35 changes: 27 additions & 8 deletions packages/inngest/src/components/EventSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { type Simplify } from "type-fest";
import {
type FnFailedEventName,
type FnFinishedEventName,
} from "../helpers/consts";
import { type internalEvents } from "../helpers/consts";
import { type IsEmptyObject, type IsStringLiteral } from "../helpers/types";
import type * as z from "../helpers/validators/zod";
import {
type EventPayload,
type FailureEventPayload,
type FinishedEventPayload,
type InvokedEventPayload,
} from "../types";

/**
Expand All @@ -33,6 +31,26 @@ export type StandardEventSchema = {
*/
export type StandardEventSchemas = Record<string, StandardEventSchema>;

/**
* Asserts that the given type `T` contains a mapping for all internal events.
*
* Usage of this ensures that we never forget about an internal event in schemas
* when adding new ones.
*
* It also ensures that the mapped name is not the enum type, as this would
* require a user to use the enum type to access the event schema to declare
* triggers, where we want to allow them to use the string literal.
*
* @public
*/
export type AssertInternalEventPayloads<
T extends Record<internalEvents, EventPayload>,
> = {
[K in keyof T as `${K & string}`]: Simplify<
Omit<T[K], "name"> & { name: `${K & string}` }
>;
};

/**
* A string error used to highlight to a user that they have a clashing name
* between the event name and the key of the event schema.
Expand Down Expand Up @@ -219,10 +237,11 @@ export type Combine<
* @public
*/
export class EventSchemas<
S extends Record<string, EventPayload> = {
[FnFailedEventName]: FailureEventPayload;
[FnFinishedEventName]: FinishedEventPayload;
},
S extends Record<string, EventPayload> = AssertInternalEventPayloads<{
[internalEvents.FunctionFailed]: FailureEventPayload;
[internalEvents.FunctionFinished]: FinishedEventPayload;
[internalEvents.FunctionInvoked]: InvokedEventPayload;
}>,
> {
/**
* Use generated Inngest types to type events.
Expand Down
8 changes: 0 additions & 8 deletions packages/inngest/src/helpers/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,6 @@ export enum internalEvents {
FunctionFinished = "inngest/function.finished",
}

/**
* Accessing enum values as literals in some TypeScript types can be difficult,
* so we also manually create the string values here.
*/
export const FnFailedEventName = `${internalEvents.FunctionFailed}`;
export const FnInvokedEventName = `${internalEvents.FunctionInvoked}`;
export const FnFinishedEventName = `${internalEvents.FunctionFinished}`;

export const logPrefix = chalk.magenta.bold("[Inngest]");

export const debugPrefix = "inngest";
Expand Down
1 change: 1 addition & 0 deletions packages/inngest/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {
EventSchemas,
type AssertInternalEventPayloads,
type Combine,
type LiteralZodEventSchema,
type StandardEventSchemaToPayload,
Expand Down
12 changes: 12 additions & 0 deletions packages/inngest/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ export type FinishedEventPayload = {
);
};

/**
* The payload for any generic function invocation event. In practice, the event
* data will be more specific to the function being invoked.
*
* @public
*/
export type InvokedEventPayload = Simplify<
Omit<EventPayload, "name"> & {
name: `${internalEvents.FunctionInvoked}`;
}
>;

/**
* Unique codes for the different types of operation that can be sent to Inngest
* from SDK step functions.
Expand Down
Loading