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

Fix client allowing extraneous properties upon creation #367

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/inngest/etc/inngest.api.md

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

17 changes: 9 additions & 8 deletions packages/inngest/src/components/EventSchemas.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { EventSchemas } from "@local/components/EventSchemas";
import { Inngest, type GetEvents } from "@local/components/Inngest";
import { type GetEvents, type Inngest } from "@local/components/Inngest";
import { type IsAny } from "@local/helpers/types";
import { type EventPayload } from "@local/types";
import { assertType, type IsEqual } from "type-plus";
import { z } from "zod";
import { createClient } from "../test/helpers";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Schemas<T extends EventSchemas<any>> = GetEvents<
Expand Down Expand Up @@ -659,7 +660,7 @@ describe("EventSchemas", () => {
"test.event": { data: { a: string } };
}>();

const inngest = new Inngest({
const inngest = createClient({
id: "test",
schemas,
eventKey: "test-key-123",
Expand All @@ -682,7 +683,7 @@ describe("EventSchemas", () => {
"test.event": { data: any };
}>();

const inngest = new Inngest({
const inngest = createClient({
id: "test",
schemas,
eventKey: "test-key-123",
Expand All @@ -707,7 +708,7 @@ describe("EventSchemas", () => {
"test.event2": { data: { foo: string } };
}>();

const inngest = new Inngest({
const inngest = createClient({
id: "test",
schemas,
eventKey: "test-key-123",
Expand Down Expand Up @@ -735,7 +736,7 @@ describe("EventSchemas", () => {
"test.event2": { data: { bar: boolean } };
}>();

const inngest = new Inngest({
const inngest = createClient({
id: "test",
schemas,
eventKey: "test-key-123",
Expand Down Expand Up @@ -766,7 +767,7 @@ describe("EventSchemas", () => {
"test.event2": { data: any };
}>();

const inngest = new Inngest({
const inngest = createClient({
id: "test",
schemas,
eventKey: "test-key-123",
Expand Down Expand Up @@ -795,7 +796,7 @@ describe("EventSchemas", () => {
"test.event2": { data: { foo: string } };
}>();

const inngest = new Inngest({
const inngest = createClient({
id: "test",
schemas,
eventKey: "test-key-123",
Expand Down Expand Up @@ -840,7 +841,7 @@ describe("EventSchemas", () => {

const schemas = new EventSchemas().fromUnion<TestEvent | TestEvent2>();

const inngest = new Inngest({
const inngest = createClient({
id: "test",
schemas,
eventKey: "test-key-123",
Expand Down
14 changes: 13 additions & 1 deletion packages/inngest/src/components/Inngest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,18 @@ describe("send", () => {
});

describe("types", () => {
describe("instantiation", () => {
test("disallows setting unknown config property with object literal", () => {
// @ts-expect-error Unknown property
const _fn = () => new Inngest({ id: "test", baseURL: "bar" });
});

test("allows setting unknown config property with variable", () => {
const options = { id: "test", baseURL: "bar" };
const _fn = () => new Inngest(options);
});
});

describe("no custom types", () => {
const inngest = createClient({ id: "test", eventKey: testEventKey });

Expand Down Expand Up @@ -734,7 +746,7 @@ describe("createFunction", () => {
});

describe("helper types", () => {
const inngest = new Inngest({
const inngest = createClient({
id: "test",
schemas: new EventSchemas().fromRecord<{
foo: { data: { foo: string } };
Expand Down
21 changes: 12 additions & 9 deletions packages/inngest/src/components/Inngest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type Exact } from "type-fest";
import { InngestApi } from "../api/api";
import {
defaultDevServerHost,
Expand Down Expand Up @@ -147,15 +148,17 @@ export class Inngest<TOpts extends ClientOptions = ClientOptions> {
* });
* ```
*/
constructor({
id,
eventKey,
baseUrl,
fetch,
env,
logger = new DefaultLogger(),
middleware,
}: TOpts) {
constructor(options: Exact<ClientOptions, TOpts>) {
const {
id,
eventKey,
baseUrl,
fetch,
env,
logger = new DefaultLogger(),
middleware,
} = options as ClientOptions;

if (!id) {
// TODO PrettyError
throw new Error("An `id` must be passed to create an Inngest instance.");
Expand Down
4 changes: 2 additions & 2 deletions packages/inngest/src/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
import { Inngest } from "@local";
import { Inngest } from "@local/components/Inngest";
import { type ServeHandlerOptions } from "@local/components/InngestCommHandler";
import { envKeys, headerKeys, queryKeys } from "@local/helpers/consts";
import { type Env } from "@local/helpers/env";
Expand Down Expand Up @@ -461,7 +461,7 @@ export const testFramework = (
const ret = await run(
[
{
client: new Inngest({ id: "Test", env: "FOO" }),
client: createClient({ id: "Test", env: "FOO" }),
functions: [],
},
],
Expand Down