Skip to content

Commit

Permalink
fix(types): don't simplify type of serialized return (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Mar 13, 2023
1 parent 21b2d65 commit b167c62
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 20 deletions.
9 changes: 3 additions & 6 deletions src/types/fetch.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Route>]
? Simplify<
Serialize<
Exclude<InternalApi[MatchedRoutes<Route>][Method], Error | void>
>
>
? Exclude<Serialize<InternalApi[MatchedRoutes<Route>][Method]>, Error | void>
: never;

export type TypedInternalResponse<
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/api/param/[id].ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default eventHandler((event) => {
return event.context.params.id;
return event.context.params!.id;
});
5 changes: 5 additions & 0 deletions test/fixture/api/serialized/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default defineEventHandler(() => {
return createError({
statusCode: 400,
});
});
3 changes: 3 additions & 0 deletions test/fixture/api/serialized/null.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default defineEventHandler(() => {
return null;
});
1 change: 1 addition & 0 deletions test/fixture/api/serialized/void.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default defineEventHandler(() => {});
2 changes: 1 addition & 1 deletion test/fixture/api/wildcard/[...param].ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default eventHandler((event) => {
return event.context.params.param as string;
return event.context.params!.param as string;
});
2 changes: 1 addition & 1 deletion test/fixture/routes/icon.png.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
32 changes: 21 additions & 11 deletions test/fixture/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("API routes", () => {
const dynamicString: string = "";

it("generates types for middleware, unknown and manual typed routes", () => {
expectTypeOf($fetch("/")).toMatchTypeOf<Promise<unknown>>(); // middleware
expectTypeOf($fetch("/")).toEqualTypeOf<Promise<unknown>>(); // middleware
expectTypeOf($fetch("/api/unknown")).toEqualTypeOf<Promise<unknown>>();
expectTypeOf($fetch<TestResponse>("/test")).toEqualTypeOf<
Promise<TestResponse>
Expand Down Expand Up @@ -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<Promise<"Default route">>();
).toEqualTypeOf<Promise<"Default route">>();
expectTypeOf(
$fetch("/api/methods/default", { method: "PUT" })
).toMatchTypeOf<Promise<"Default route">>();
).toEqualTypeOf<Promise<"Default route">>();
expectTypeOf(
$fetch("/api/methods/default", { method: "POST" })
).toMatchTypeOf<Promise<"Default override">>();
).toEqualTypeOf<Promise<"Default override">>();
});

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<unknown>
>();

expectTypeOf($fetch("/api/serialized/void")).toEqualTypeOf<
Promise<unknown>
>();

expectTypeOf($fetch("/api/serialized/null")).toEqualTypeOf<Promise<null>>();

expectTypeOf($fetch("/api/serialized/function")).toEqualTypeOf<
Promise<object>
>();

expectTypeOf($fetch("/api/serialized/map")).toMatchTypeOf<
expectTypeOf($fetch("/api/serialized/map")).toEqualTypeOf<
Promise<{
foo: Record<string, never>;
}>
>();

expectTypeOf($fetch("/api/serialized/set")).toMatchTypeOf<
expectTypeOf($fetch("/api/serialized/set")).toEqualTypeOf<
Promise<{
foo: Record<string, never>;
}>
>();

expectTypeOf($fetch("/api/serialized/tuple")).toMatchTypeOf<
expectTypeOf($fetch("/api/serialized/tuple")).toEqualTypeOf<
Promise<[string, string]>
>();
});
Expand Down

0 comments on commit b167c62

Please sign in to comment.