Skip to content

Commit

Permalink
Fix requiring enum value when using an internal event as a trigger (#479
Browse files Browse the repository at this point in the history
)

## Summary
<!-- Succinctly describe your change, providing context, what you've
changed, and why. -->

No longer require an enum value like `internalEvents.FunctionFailed` if
wanting to use an internal event as a trigger.

## 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

## Related
<!-- A space for any related links, issues, or PRs. -->
<!-- Linear issues are autolinked. -->
<!-- e.g. - INN-123 -->
<!-- GitHub issues/PRs can be linked using shorthand. -->
<!-- e.g. "- inngest/inngest#123" -->
<!-- Feel free to remove this section if there are no applicable related
links.-->
- Fixes #454
  • Loading branch information
jpwilliams committed Feb 12, 2024
1 parent 52398dc commit 1b2eaed
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-mice-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"inngest": patch
---

Fix requiring enum value when using an internal event as a trigger
6 changes: 4 additions & 2 deletions packages/inngest/etc/inngest.api.md

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

36 changes: 35 additions & 1 deletion packages/inngest/src/components/EventSchemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventSchemas } from "@local/components/EventSchemas";
import { Inngest, type GetEvents } from "@local/components/Inngest";
import { type internalEvents } from "@local/helpers/consts";
import { type IsAny } from "@local/helpers/types";
import { type EventPayload } from "@local/types";
import { type EventPayload, type FailureEventPayload } from "@local/types";
import { assertType, type IsEqual } from "type-plus";
import { z } from "zod";

Expand All @@ -26,6 +26,40 @@ describe("EventSchemas", () => {
assertType<IsEqual<Expected, Actual>>(true);
});

test("providing no schemas keeps all types generic", () => {
const inngest = new Inngest({
id: "test",
eventKey: "test-key-123",
});

inngest.createFunction({ id: "test" }, { event: "foo" }, ({ event }) => {
assertType<string>(event.name);
assertType<IsAny<typeof event.data>>(true);
});
});

test("can use internal string literal types as triggers if any event schemas are defined", () => {
const schemas = new EventSchemas();

const inngest = new Inngest({
id: "test",
schemas,
eventKey: "test-key-123",
});

inngest.createFunction(
{ id: "test" },
{ event: "inngest/function.failed" },
({ event }) => {
assertType<
| `${internalEvents.FunctionInvoked}`
| `${internalEvents.FunctionFailed}`
>(event.name);
assertType<FailureEventPayload["data"]>(event.data);
}
);
});

describe("fromRecord", () => {
test("sets types based on input", () => {
const schemas = new EventSchemas().fromRecord<{
Expand Down
9 changes: 6 additions & 3 deletions packages/inngest/src/components/EventSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { type Simplify } from "type-fest";
import { type internalEvents } from "../helpers/consts";
import {
type FnFailedEventName,
type FnFinishedEventName,
} from "../helpers/consts";
import { type IsEmptyObject, type IsStringLiteral } from "../helpers/types";
import type * as z from "../helpers/validators/zod";
import {
Expand Down Expand Up @@ -217,8 +220,8 @@ export type Combine<
*/
export class EventSchemas<
S extends Record<string, EventPayload> = {
[internalEvents.FunctionFailed]: FailureEventPayload;
[internalEvents.FunctionFinished]: FinishedEventPayload;
[FnFailedEventName]: FailureEventPayload;
[FnFinishedEventName]: FinishedEventPayload;
},
> {
/**
Expand Down
8 changes: 8 additions & 0 deletions packages/inngest/src/helpers/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ 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

0 comments on commit 1b2eaed

Please sign in to comment.