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

Expose EventSchemas in Inngest instances #657

Merged
merged 10 commits into from
Oct 21, 2024
5 changes: 5 additions & 0 deletions .changeset/mean-brooms-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"inngest": patch
---

Expose `EventSchemas` in `Inngest` instances
30 changes: 29 additions & 1 deletion packages/inngest/src/components/EventSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ export class EventSchemas<
[internalEvents.ScheduledTimer]: ScheduledTimerEventPayload;
}>,
> {
protected runtimeSchemas: Record<string, unknown> = {};

private addRuntimeSchemas(schemas: Record<string, unknown>) {
this.runtimeSchemas = {
...this.runtimeSchemas,
...schemas,
};
}

/**
* Use generated Inngest types to type events.
*/
Expand Down Expand Up @@ -343,7 +352,6 @@ export class EventSchemas<
* ```
*/
public fromZod<T extends ZodEventSchemas | LiteralZodEventSchemas>(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
schemas: T
): EventSchemas<
Combine<
Expand All @@ -353,6 +361,26 @@ export class EventSchemas<
>
>
> {
let runtimeSchemas: Record<string, unknown>;

if (Array.isArray(schemas)) {
runtimeSchemas = schemas.reduce((acc, schema) => {
const {
name: { value: name },
...rest
} = schema.shape;

return {
...acc,
[name]: rest,
};
}, {});
} else {
runtimeSchemas = schemas;
}

this.addRuntimeSchemas(runtimeSchemas);

return this;
}
}
4 changes: 4 additions & 0 deletions packages/inngest/src/components/Inngest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ export class Inngest<TClientOpts extends ClientOptions = ClientOptions> {
*/
private _mode!: Mode;

protected readonly schemas?: NonNullable<TClientOpts["schemas"]>;

get apiBaseUrl(): string | undefined {
return this._apiBaseUrl;
}
Expand Down Expand Up @@ -192,6 +194,7 @@ export class Inngest<TClientOpts extends ClientOptions = ClientOptions> {
logger = new DefaultLogger(),
middleware,
isDev,
schemas,
} = this.options;

if (!id) {
Expand All @@ -216,6 +219,7 @@ export class Inngest<TClientOpts extends ClientOptions = ClientOptions> {
mode: this.mode,
});

this.schemas = schemas;
this.loadModeEnvVars();

this.logger = logger;
Expand Down
Loading