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

Allow wildcard event typing with .fromRecord() #823

Merged
merged 4 commits into from
Jan 30, 2025

Conversation

jpwilliams
Copy link
Member

@jpwilliams jpwilliams commented Jan 29, 2025

Summary

Adds the ability to provide better wildcard event typing in .fromRecord() by changing how name is assigned and overwritten when forming EventSchemas.

The following schema is now valid:

export const schemas = new EventSchemas().fromRecord<{
  "app/blog.post.*":
    | {
        name: "app/blog.post.created";
        data: {
          postId: string;
          authorId: string;
          createdAt: string;
        };
      }
    | {
        name: "app/blog.post.published";
        data: {
          postId: string;
          authorId: string;
          publishedAt: string;
        };
      };
}>();

When creating a function, this allows you to appropriately type narrow the event to pull out the correct data:

inngest.createFunction(
  { id: "my-fn" },
  { event: "app/blog.post.*" },
  async ({ event }) => {
    if (event.name === "app/blog.post.created") {
      console.log("Blog post created at:", event.data.createdAt);
    } else if (event.name === "app/blog.post.published") {
      console.log("Blog post published at:", event.data.publishedAt);
    }
  }
);

Checklist

  • Added a docs PR that references this PR
  • Added unit/integration tests
  • Added changesets if applicable

Related

Carefully navigates preserving the clashing name type checks,
only allowing the wildcard functionality if a `*` is present.
@jpwilliams jpwilliams added the ⬆️ improvement Performance, reliability, or usability improvements label Jan 29, 2025
@jpwilliams jpwilliams requested a review from djfarrelly January 29, 2025 10:34
@jpwilliams jpwilliams self-assigned this Jan 29, 2025
Copy link

changeset-bot bot commented Jan 29, 2025

🦋 Changeset detected

Latest commit: 443a121

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
inngest Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@inngest-release-bot inngest-release-bot added the 📦 inngest Affects the `inngest` package label Jan 29, 2025
Copy link
Member

@djfarrelly djfarrelly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

@jpwilliams jpwilliams merged commit f1d2385 into main Jan 30, 2025
53 checks passed
@jpwilliams jpwilliams deleted the allow-record-wildcards branch January 30, 2025 15:39
jpwilliams pushed a commit that referenced this pull request Jan 30, 2025
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @inngest/[email protected]

### Patch Changes

- [#816](#816)
[`fadd94a`](fadd94a)
Thanks [@joelhooks](https://github.com/joelhooks)! - Add Deepseek
support

## [email protected]

### Patch Changes

- [#817](#817)
[`446be1b`](446be1b)
Thanks [@jpwilliams](https://github.com/jpwilliams)! - `serve()` and
`connect()` now have looser typing for `client` and `functions`,
resulting in easier use of multiple `inngest` packages in a single
process

- [#823](#823)
[`f1d2385`](f1d2385)
Thanks [@jpwilliams](https://github.com/jpwilliams)! - Allow wildcard
event typing with `.fromRecord()`

    The following schema is now valid:

    ```ts
    export const schemas = new EventSchemas().fromRecord<{
      "app/blog.post.*":
        | {
            name: "app/blog.post.created";
            data: {
              postId: string;
              authorId: string;
              createdAt: string;
            };
          }
        | {
            name: "app/blog.post.published";
            data: {
              postId: string;
              authorId: string;
              publishedAt: string;
            };
          };
    }>();
    ```

When creating a function, this allows you to appropriately type narrow
the event to pull out the correct data:

    ```ts
    inngest.createFunction(
      { id: "my-fn" },
      { event: "app/blog.post.*" },
      async ({ event }) => {
        if (event.name === "app/blog.post.created") {
          console.log("Blog post created at:", event.data.createdAt);
        } else if (event.name === "app/blog.post.published") {
console.log("Blog post published at:", event.data.publishedAt);
        }
      },
    );
    ```

- [#825](#825)
[`661ed7b`](661ed7b)
Thanks [@jpwilliams](https://github.com/jpwilliams)! - If no `functions`
are provided to `inngest.connect()`, it will now use any functions that
have been created with the client instead

- Updated dependencies
\[[`fadd94a`](fadd94a)]:
    -   @inngest/[email protected]

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⬆️ improvement Performance, reliability, or usability improvements 📦 inngest Affects the `inngest` package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants