|
| 1 | +import { describe, it, expectTypeOf } from "vitest"; |
| 2 | +import type { QueryObject } from "ufo"; |
| 3 | +import { |
| 4 | + eventHandler, |
| 5 | + H3Event, |
| 6 | + getQuery, |
| 7 | + readBody, |
| 8 | + readValidatedBody, |
| 9 | + getValidatedQuery, |
| 10 | +} from "../src"; |
| 11 | + |
| 12 | +describe("types", () => { |
| 13 | + describe("eventHandler", () => { |
| 14 | + it("return type (inferred)", () => { |
| 15 | + const handler = eventHandler(() => { |
| 16 | + return { |
| 17 | + foo: "bar", |
| 18 | + }; |
| 19 | + }); |
| 20 | + const response = handler({} as H3Event); |
| 21 | + expectTypeOf(response).toEqualTypeOf<{ foo: string }>(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("return type (simple generic)", () => { |
| 25 | + const handler = eventHandler<string>(() => { |
| 26 | + return ""; |
| 27 | + }); |
| 28 | + const response = handler({} as H3Event); |
| 29 | + expectTypeOf(response).toEqualTypeOf<string>(); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe("readBody", () => { |
| 34 | + it("untyped", () => { |
| 35 | + eventHandler(async (event) => { |
| 36 | + const body = await readBody(event); |
| 37 | + // TODO: Default to unknown in next major version |
| 38 | + expectTypeOf(body).toBeAny(); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it("typed via generic", () => { |
| 43 | + eventHandler(async (event) => { |
| 44 | + const body = await readBody<string>(event); |
| 45 | + expectTypeOf(body).not.toBeAny(); |
| 46 | + expectTypeOf(body).toBeString(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it("typed via validator", () => { |
| 51 | + eventHandler(async (event) => { |
| 52 | + // eslint-disable-next-line unicorn/consistent-function-scoping |
| 53 | + const validator = (body: unknown) => body as { id: string }; |
| 54 | + const body = await readValidatedBody(event, validator); |
| 55 | + expectTypeOf(body).not.toBeAny(); |
| 56 | + expectTypeOf(body).toEqualTypeOf<{ id: string }>(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it("typed via event handler", () => { |
| 61 | + eventHandler<{ body: { id: string } }>(async (event) => { |
| 62 | + const body = await readBody(event); |
| 63 | + expectTypeOf(body).not.toBeAny(); |
| 64 | + expectTypeOf(body).toEqualTypeOf<{ id: string }>(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe("getQuery", () => { |
| 70 | + it("untyped", () => { |
| 71 | + eventHandler((event) => { |
| 72 | + const query = getQuery(event); |
| 73 | + expectTypeOf(query).not.toBeAny(); |
| 74 | + expectTypeOf(query).toEqualTypeOf<QueryObject>(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("typed via generic", () => { |
| 79 | + eventHandler((event) => { |
| 80 | + const query = getQuery<{ id: string }>(event); |
| 81 | + expectTypeOf(query).not.toBeAny(); |
| 82 | + expectTypeOf(query).toEqualTypeOf<{ id: string }>(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("typed via validator", () => { |
| 87 | + eventHandler(async (event) => { |
| 88 | + // eslint-disable-next-line unicorn/consistent-function-scoping |
| 89 | + const validator = (body: unknown) => body as { id: string }; |
| 90 | + const body = await getValidatedQuery(event, validator); |
| 91 | + expectTypeOf(body).not.toBeAny(); |
| 92 | + expectTypeOf(body).toEqualTypeOf<{ id: string }>(); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it("typed via event handler", () => { |
| 97 | + eventHandler<{ query: { id: string } }>((event) => { |
| 98 | + const query = getQuery(event); |
| 99 | + expectTypeOf(query).not.toBeAny(); |
| 100 | + expectTypeOf(query).toEqualTypeOf<{ id: string }>(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments