-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,83 @@ | ||
import { describe, it, expectTypeOf } from "vitest"; | ||
import { | ||
eventHandler, | ||
H3Event, | ||
readBody, | ||
getQuery, | ||
readTypedBody, | ||
} from "../src"; | ||
|
||
describe("types for event handlers", () => { | ||
it("return type test", () => { | ||
const handler = eventHandler(() => { | ||
return { | ||
foo: "bar", | ||
}; | ||
}); | ||
|
||
expectTypeOf(handler({} as H3Event)).toEqualTypeOf< | ||
{ foo: string } | Promise<{ foo: string }> | ||
>(); | ||
}); | ||
// import type { QueryObject } from "ufo"; | ||
import { eventHandler, H3Event, getQuery, readBody } from "../src"; | ||
|
||
it("input type test", () => { | ||
eventHandler<{ body: { id: string } }>(async (event) => { | ||
const body = await readTypedBody(event); | ||
expectTypeOf(body).toEqualTypeOf<{ id: string }>(); | ||
expectTypeOf(getQuery(event)).toBeUnknown(); | ||
type MaybePromise<T> = T | Promise<T>; | ||
|
||
return null; | ||
describe("types", () => { | ||
describe("eventHandler", () => { | ||
it("return type (inferred)", () => { | ||
const handler = eventHandler(() => { | ||
return { | ||
foo: "bar", | ||
}; | ||
}); | ||
expectTypeOf(handler({} as H3Event)).toEqualTypeOf< | ||
MaybePromise<{ foo: string }> | ||
>(); | ||
}); | ||
|
||
eventHandler<{ query: { id: string } }>((event) => { | ||
const query = getQuery(event); | ||
expectTypeOf(query).toEqualTypeOf<{ id: string }>(); | ||
|
||
return null; | ||
it("return type (simple generic)", () => { | ||
const handler = eventHandler<string>(() => { | ||
return ""; | ||
}); | ||
expectTypeOf(handler({} as H3Event)).toEqualTypeOf< | ||
MaybePromise<string> | ||
>(); | ||
}); | ||
}); | ||
|
||
it("allows backwards compatible generic for eventHandler definition", () => { | ||
const handler = eventHandler<string>(() => { | ||
return ""; | ||
describe("readBody", () => { | ||
it("untyped", () => { | ||
eventHandler(async (event) => { | ||
const body = await readBody(event); | ||
// For backwards compatibility - this should likely become `unknown` in future | ||
expectTypeOf(body).toBeAny(); | ||
}); | ||
}); | ||
|
||
it("typed via generic", () => { | ||
eventHandler(async (event) => { | ||
const body = await readBody<string>(event); | ||
expectTypeOf(body).not.toBeAny(); | ||
expectTypeOf(body).toBeString(); | ||
}); | ||
}); | ||
|
||
it("typed via event handler", () => { | ||
eventHandler<{ body: { id: string } }>(async (event) => { | ||
const body = await readBody(event); | ||
expectTypeOf(body).not.toBeAny(); | ||
expectTypeOf(body).toEqualTypeOf<{ id: string }>(); | ||
}); | ||
}); | ||
expectTypeOf(handler({} as H3Event)).toEqualTypeOf< | ||
string | Promise<string> | ||
>(); | ||
}); | ||
|
||
// For backwards compatibility - this should likely become `unknown` in future | ||
it("input types aren't applied when omitted", () => { | ||
eventHandler(async (event) => { | ||
const body = await readBody(event); | ||
expectTypeOf(body).toBeAny(); | ||
expectTypeOf(getQuery(event)).toBeAny(); | ||
describe("getQuery", () => { | ||
it("untyped", () => { | ||
eventHandler((event) => { | ||
const query = getQuery(event); | ||
// TODO: It should be QueryObject to avoid breaking changes! | ||
expectTypeOf(query).toBeAny(); | ||
// expectTypeOf(query).not.toBeAny(); | ||
// expectTypeOf(query).toEqualTypeOf<QueryObject>(); | ||
}); | ||
}); | ||
|
||
it("typed via generic", () => { | ||
eventHandler((event) => { | ||
const query = getQuery<{ id: string }>(event); | ||
expectTypeOf(query).not.toBeAny(); | ||
expectTypeOf(query).toEqualTypeOf<{ id: string }>(); | ||
}); | ||
}); | ||
|
||
return null; | ||
it("typed via event handler", () => { | ||
eventHandler<{ query: { id: string } }>((event) => { | ||
const query = getQuery(event); | ||
expectTypeOf(query).not.toBeAny(); | ||
expectTypeOf(query).toEqualTypeOf<{ id: string }>(); | ||
}); | ||
}); | ||
}); | ||
}); |