Skip to content

Commit

Permalink
Add generic function invocation event to all event schemas (#489)
Browse files Browse the repository at this point in the history
## Summary
<!-- Succinctly describe your change, providing context, what you've
changed, and why. -->

Fixes an issue where `inngest/function.invoked` was missing from the
list of internal events automatically returned when using
`GetEvents<typeof inngest, true>`.

Also slightly refactors how these internal events are added, ensuring we
can't forget them again.

## Checklist
<!-- Tick these items off as you progress. -->
<!-- If an item isn't applicable, ideally please strikeout the item by
wrapping it in "~~"" and suffix it with "N/A My reason for skipping
this." -->
<!-- e.g. "- [ ] ~~Added tests~~ N/A Only touches docs" -->

- [ ] ~~Added a [docs PR](https://github.com/inngest/website) that
references this PR~~ N/A Bug fix
- [x] Added unit/integration tests
- [x] Added changesets if applicable
  • Loading branch information
jpwilliams committed Feb 19, 2024
1 parent d982fc3 commit 244b6bd
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 24 deletions.
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

0 comments on commit 244b6bd

Please sign in to comment.