diff --git a/src/types/fetch.ts b/src/types/fetch.ts index 58cca170c3..b54f32129d 100644 --- a/src/types/fetch.ts +++ b/src/types/fetch.ts @@ -1,6 +1,6 @@ import type { RouterMethod } from "h3"; import type { FetchRequest, FetchOptions, FetchResponse } from "ofetch"; -import type { Serialize, Simplify } from "./serialize"; +import type { Serialize } from "./serialize"; import type { MatchedRoutes } from "./utils"; // An interface to extend in a local project @@ -13,15 +13,12 @@ export type NitroFetchRequest = // eslint-disable-next-line @typescript-eslint/ban-types | (string & {}); +// TODO: re-enable Simplify export type MiddlewareOf< Route extends string, Method extends RouterMethod | "default" > = Method extends keyof InternalApi[MatchedRoutes] - ? Simplify< - Serialize< - Exclude][Method], Error | void> - > - > + ? Exclude][Method]>, Error | void> : never; export type TypedInternalResponse< diff --git a/test/fixture/api/param/[id].ts b/test/fixture/api/param/[id].ts index ecb0e117ba..a65b3fc6b9 100644 --- a/test/fixture/api/param/[id].ts +++ b/test/fixture/api/param/[id].ts @@ -1,3 +1,3 @@ export default eventHandler((event) => { - return event.context.params.id; + return event.context.params!.id; }); diff --git a/test/fixture/api/serialized/error.ts b/test/fixture/api/serialized/error.ts new file mode 100644 index 0000000000..6b795fdff7 --- /dev/null +++ b/test/fixture/api/serialized/error.ts @@ -0,0 +1,5 @@ +export default defineEventHandler(() => { + return createError({ + statusCode: 400, + }); +}); diff --git a/test/fixture/api/serialized/null.ts b/test/fixture/api/serialized/null.ts new file mode 100644 index 0000000000..3fd586da85 --- /dev/null +++ b/test/fixture/api/serialized/null.ts @@ -0,0 +1,3 @@ +export default defineEventHandler(() => { + return null; +}); diff --git a/test/fixture/api/serialized/void.ts b/test/fixture/api/serialized/void.ts new file mode 100644 index 0000000000..8756ea1b2d --- /dev/null +++ b/test/fixture/api/serialized/void.ts @@ -0,0 +1 @@ +export default defineEventHandler(() => {}); diff --git a/test/fixture/api/wildcard/[...param].ts b/test/fixture/api/wildcard/[...param].ts index f917e925b8..f769d76961 100644 --- a/test/fixture/api/wildcard/[...param].ts +++ b/test/fixture/api/wildcard/[...param].ts @@ -1,3 +1,3 @@ export default eventHandler((event) => { - return event.context.params.param as string; + return event.context.params!.param as string; }); diff --git a/test/fixture/routes/icon.png.ts b/test/fixture/routes/icon.png.ts index 6032bb4f70..0fef54dbda 100644 --- a/test/fixture/routes/icon.png.ts +++ b/test/fixture/routes/icon.png.ts @@ -6,7 +6,7 @@ export default eventHandler((event) => { event.res.end(buff); }); -function base64ToArray(base64) { +function base64ToArray(base64: string) { const str = atob(base64); const bytes = new Uint8Array(str.length); for (let i = 0; i < str.length; i++) { diff --git a/test/fixture/types.ts b/test/fixture/types.ts index 02ee1bb6dc..9aaaab49c6 100644 --- a/test/fixture/types.ts +++ b/test/fixture/types.ts @@ -14,7 +14,7 @@ describe("API routes", () => { const dynamicString: string = ""; it("generates types for middleware, unknown and manual typed routes", () => { - expectTypeOf($fetch("/")).toMatchTypeOf>(); // middleware + expectTypeOf($fetch("/")).toEqualTypeOf>(); // middleware expectTypeOf($fetch("/api/unknown")).toEqualTypeOf>(); expectTypeOf($fetch("/test")).toEqualTypeOf< Promise @@ -176,47 +176,57 @@ describe("API routes", () => { }); it("generates the correct type depending on the method used", () => { - expectTypeOf($fetch("/api/methods", { method: "get" })).toMatchTypeOf< + expectTypeOf($fetch("/api/methods", { method: "get" })).toEqualTypeOf< Promise<"Index get"> >(); - expectTypeOf($fetch("/api/methods", { method: "post" })).toMatchTypeOf< + expectTypeOf($fetch("/api/methods", { method: "post" })).toEqualTypeOf< Promise<"Index post"> >(); expectTypeOf( $fetch("/api/methods/default", { method: "GET" }) - ).toMatchTypeOf>(); + ).toEqualTypeOf>(); expectTypeOf( $fetch("/api/methods/default", { method: "PUT" }) - ).toMatchTypeOf>(); + ).toEqualTypeOf>(); expectTypeOf( $fetch("/api/methods/default", { method: "POST" }) - ).toMatchTypeOf>(); + ).toEqualTypeOf>(); }); it("generates types matching JSON serialization output", () => { - expectTypeOf($fetch("/api/serialized/date")).toMatchTypeOf< + expectTypeOf($fetch("/api/serialized/date")).toEqualTypeOf< Promise<{ createdAt: string; }> >(); - expectTypeOf($fetch("/api/serialized/function")).toMatchTypeOf< + expectTypeOf($fetch("/api/serialized/error")).toEqualTypeOf< + Promise + >(); + + expectTypeOf($fetch("/api/serialized/void")).toEqualTypeOf< + Promise + >(); + + expectTypeOf($fetch("/api/serialized/null")).toEqualTypeOf>(); + + expectTypeOf($fetch("/api/serialized/function")).toEqualTypeOf< Promise >(); - expectTypeOf($fetch("/api/serialized/map")).toMatchTypeOf< + expectTypeOf($fetch("/api/serialized/map")).toEqualTypeOf< Promise<{ foo: Record; }> >(); - expectTypeOf($fetch("/api/serialized/set")).toMatchTypeOf< + expectTypeOf($fetch("/api/serialized/set")).toEqualTypeOf< Promise<{ foo: Record; }> >(); - expectTypeOf($fetch("/api/serialized/tuple")).toMatchTypeOf< + expectTypeOf($fetch("/api/serialized/tuple")).toEqualTypeOf< Promise<[string, string]> >(); });