Skip to content

Commit 9b1aa87

Browse files
committed
fix: type error for non-string path params
1 parent 9056e8a commit 9b1aa87

4 files changed

Lines changed: 15 additions & 11 deletions

File tree

src/core/definer.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { customErrorTypes } from "~/types/error";
22
import type { HttpMethod } from "~/types/http";
33
import type { RouteHandler, RouteMethodHandler } from "~/types/next";
4+
import type { FixPathParams } from "~/types/path-params";
45
import type { ResponseCollection } from "~/types/response";
56
import { parseRequestBody, resolveRequestBody } from "./body";
67
import { resolveParams } from "./params";
@@ -62,8 +63,8 @@ type RouteOptions<
6263
issues?: ZodIssue[],
6364
) => Res,
6465
middleware?: (
65-
hander: RouteMethodHandler<PathParamsInput, Req, Res>,
66-
) => RouteMethodHandler<PathParamsInput, Req, Res>,
66+
hander: RouteMethodHandler<FixPathParams<PathParamsInput>, Req, Res>,
67+
) => RouteMethodHandler<FixPathParams<PathParamsInput>, Req, Res>,
6768
security?: OperationObject["security"],
6869
} & (RouteWithBody<RequestBodyInput, RequestBodyOutput> | RouteWithoutBody);
6970

@@ -80,12 +81,12 @@ function defineRoute<
8081
ResDef extends Record<string, unknown>,
8182
>(input: RouteOptions<
8283
M, PPI, PPO, QPI, QPO, RBI, RBO, MwReq, MwRes, ResDef
83-
>) {
84-
const handler: RouteMethodHandler<PPI, MwReq, MwRes> = async (request, context) => {
84+
>): RouteHandler<M, FixPathParams<PPI>, MwReq, MwRes> {
85+
const handler: RouteMethodHandler<FixPathParams<PPI>, MwReq, MwRes> = async (request, context) => {
8586
try {
8687
const { searchParams } = new URL(request.url);
8788
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
88-
const nextSegmentParams = context ? (await context.params) : undefined;
89+
const nextSegmentParams = context ? (await context.params) as PPI : undefined;
8990
const pathParams = parsePathParams(nextSegmentParams, input.pathParams) as PPO;
9091
const queryParams = parseSearchParams(searchParams, input.queryParams) as QPO;
9192
const body = await parseRequestBody(request, input.method, input.requestBody, input.hasFormData) as RBO;
@@ -154,10 +155,10 @@ function defineRoute<
154155
if (input.middleware) {
155156
const alteredHandler = input.middleware(handler);
156157
alteredHandler.apiData = handler.apiData;
157-
return { [input.method]: alteredHandler } as RouteHandler<M, PPI, MwReq, MwRes>;
158+
return { [input.method]: alteredHandler } as RouteHandler<M, FixPathParams<PPI>, MwReq, MwRes>;
158159
}
159160

160-
return { [input.method]: handler } as RouteHandler<M, PPI, MwReq, MwRes>;
161+
return { [input.method]: handler } as RouteHandler<M, FixPathParams<PPI>, MwReq, MwRes>;
161162
}
162163

163164
export default defineRoute;

src/core/path-params.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { safeParse } from "./zod-error-handler";
22
import type { ZodError, ZodType } from "zod";
33

4-
export default function parsePathParams<I, O>(source?: I, schema?: ZodType<O, I>) {
4+
export default function parsePathParams<I, O>(source?: NoInfer<I>, schema?: ZodType<O, I>) {
55
if (!schema) return null;
66
if (!source) throw new Error("UNNECESSARY_PATH_PARAMS");
77
try {

src/types/next.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { HttpMethod } from "./http";
22
import type { OperationObject } from "@omer-x/openapi-types/operation";
33

4-
type RouteHandlerContext<PathParams> = {
4+
type RouteHandlerContext<PathParams extends Record<string, string>> = {
55
params: Promise<PathParams>,
66
};
77

8-
export type RouteMethodHandler<PathParamsInput, Req, Res> = ((
8+
export type RouteMethodHandler<PathParamsInput extends Record<string, string>, Req, Res> = ((
99
request: Req,
1010
context: RouteHandlerContext<PathParamsInput>,
1111
) => Promise<Res>) & {
@@ -14,7 +14,7 @@ export type RouteMethodHandler<PathParamsInput, Req, Res> = ((
1414

1515
export type RouteHandler<
1616
HM extends HttpMethod,
17-
PathParamsInput,
17+
PathParamsInput extends Record<string, string>,
1818
Req,
1919
Res,
2020
> = Record<

src/types/path-params.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type FixPathParams<T> = {
2+
[K in keyof T]: string;
3+
};

0 commit comments

Comments
 (0)