Skip to content

Commit ffd7139

Browse files
committed
add isArray to response-definition
1 parent 9aebf97 commit ffd7139

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

src/core/content.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import zodToJsonSchema from "zod-to-json-schema";
22
import type { RequestBodyObject } from "@omer-x/openapi-types/request-body";
3+
import type { SchemaObject } from "@omer-x/openapi-types/schema";
34
import type { ZodType } from "zod";
45

5-
export function resolveContent(source?: ZodType<unknown> | string) {
6+
function resolveSchema(source: ZodType<unknown> | string, isArray: boolean = false): SchemaObject {
7+
if (typeof source === "string") {
8+
const refObject = { $ref: `#/components/schemas/${source}` } as SchemaObject;
9+
if (isArray) {
10+
return { type: "array", items: refObject } as SchemaObject;
11+
}
12+
return refObject;
13+
}
14+
return zodToJsonSchema(isArray ? source.array() : source, { target: "openApi3" }) as SchemaObject;
15+
}
16+
17+
export function resolveContent(source?: ZodType<unknown> | string, isArray: boolean = false) {
618
if (!source) return undefined;
719
return {
820
"application/json": {
9-
schema: typeof source === "string" ? {
10-
$ref: `#/components/schemas/${source}`,
11-
} : zodToJsonSchema(source, {
12-
target: "openApi3",
13-
}),
21+
schema: resolveSchema(source, isArray),
1422
},
1523
} as RequestBodyObject["content"];
1624
}

src/core/responses.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ export function addBadRequest(queryParams?: unknown, requestBody?: unknown) {
1111

1212
export function bundleResponses(collection: Record<string, ResponseDefinition>) {
1313
return Object.keys(collection).reduce((result, key) => {
14+
const response = collection[key];
1415
return {
1516
...result,
16-
[key]: {
17-
...collection[key],
18-
content: resolveContent(collection[key]?.content),
19-
},
17+
[key]: !response ? undefined : {
18+
description: response.description,
19+
content: resolveContent(response.content, response.isArray),
20+
} satisfies ResponseObject,
2021
};
2122
}, {}) as ResponsesObject;
2223
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type RouteOptions<Method, PathParams, QueryParams, RequestBody> = {
3636
} & (RouteWithBody<RequestBody> | RouteWithoutBody);
3737

3838
export default function createRoute<M extends HttpMethod, PP, QP, RB>(input: RouteOptions<M, PP, QP, RB>) {
39-
const handler: RouteMethodHandler<RB, PP> = async (request, props) => {
39+
const handler: RouteMethodHandler<PP> = async (request, props) => {
4040
try {
4141
const { searchParams } = new URL(request.url);
4242
const queryParams = parseSearchParams(searchParams, input.queryParams);
@@ -75,5 +75,5 @@ export default function createRoute<M extends HttpMethod, PP, QP, RB>(input: Rou
7575
responses: responses,
7676
};
7777

78-
return { [input.method]: handler } as RouteHandler<M, RB, PP>;
78+
return { [input.method]: handler } as RouteHandler<M, PP>;
7979
}

src/types/next.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import type { HttpMethod } from "./http";
2-
import type { FixedRequest } from "./request";
32
import type { OperationObject } from "@omer-x/openapi-types/operation";
43

54
type RouteHandlerProps<PathParams> = {
65
params?: PathParams,
76
};
87

9-
export type RouteMethodHandler<RequestBody, PathParams> = ((
10-
request: FixedRequest<RequestBody>,
8+
export type RouteMethodHandler<PathParams> = ((
9+
request: Request,
1110
props: RouteHandlerProps<PathParams>
1211
) => Promise<Response>) & {
1312
apiData?: OperationObject,
1413
};
1514

16-
export type RouteHandler<HM extends HttpMethod, RequestBody, PathParams> = {
17-
[key in HM]: RouteMethodHandler<RequestBody, PathParams>;
15+
export type RouteHandler<HM extends HttpMethod, PathParams> = {
16+
[key in HM]: RouteMethodHandler<PathParams>;
1817
};

src/types/response.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import type { ZodType } from "zod";
33
export type ResponseDefinition = {
44
description: string,
55
content?: ZodType | string,
6+
isArray?: boolean,
67
};

0 commit comments

Comments
 (0)