Skip to content

Commit 2200b67

Browse files
committed
refactor: resolveParams with example strategy
1 parent 6a504f3 commit 2200b67

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/core/definer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ function defineRoute<
110110
};
111111

112112
const parameters = [
113-
...resolveParams("path", input.pathParams),
114-
...resolveParams("query", input.queryParams),
113+
...resolveParams("path", input.exampleStrategy ?? "none", input.pathParams),
114+
...resolveParams("query", input.exampleStrategy ?? "none", input.queryParams),
115115
];
116116

117117
const responses = bundleResponses(input.responses);

src/core/params.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import type { ParameterObject } from "@omer-x/openapi-types/parameter";
55

66
describe("resolveParams", () => {
77
it("should return an empty array if source is not provided", () => {
8-
const result = resolveParams("query", undefined);
8+
const result = resolveParams("query", "none", undefined);
99
expect(result).toEqual([]);
1010
});
1111

1212
it("should throw an error if the schema does not have properties", () => {
1313
const zodSchema = z.string();
1414

15-
expect(() => resolveParams("query", zodSchema)).toThrow("Invalid object schema");
15+
expect(() => resolveParams("query", "none", zodSchema)).toThrow("Invalid object schema");
1616
});
1717

1818
it("should resolve parameters for a valid Zod object schema", () => {
@@ -21,7 +21,7 @@ describe("resolveParams", () => {
2121
includeDetails: z.boolean().optional().describe("Whether to include user details"),
2222
});
2323

24-
const result = resolveParams("query", zodSchema);
24+
const result = resolveParams("query", "none", zodSchema);
2525

2626
const expectedParams: ParameterObject[] = [
2727
{
@@ -55,7 +55,7 @@ describe("resolveParams", () => {
5555
includeDetails: z.boolean().optional().describe("Whether to include user details"),
5656
});
5757

58-
const result = resolveParams("query", zodSchema);
58+
const result = resolveParams("query", "none", zodSchema);
5959

6060
const expectedParams: ParameterObject[] = [
6161
{

src/core/params.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import type { ExampleStrategy } from "~/types/example";
2+
import { injectExample } from "./example";
13
import convertToOpenAPI from "./zod-to-openapi";
24
import type { ParameterObject } from "@omer-x/openapi-types/parameter";
35
import type { ZodType } from "zod";
46

5-
export function resolveParams(kind: ParameterObject["in"], source?: ZodType<unknown>) {
7+
export function resolveParams(kind: ParameterObject["in"], exampleStrategy: ExampleStrategy, source?: ZodType<unknown>) {
68
if (!source) return [];
79
const schema = convertToOpenAPI(source, false);
810
if (!("properties" in schema)) throw new Error("Invalid object schema");
@@ -13,5 +15,6 @@ export function resolveParams(kind: ParameterObject["in"], source?: ZodType<unkn
1315
required: schema.required?.includes(name) ?? false,
1416
description: definition.description,
1517
schema: definition,
18+
...injectExample(exampleStrategy, source, false),
1619
}));
1720
}

0 commit comments

Comments
 (0)