Skip to content

Commit 6a504f3

Browse files
committed
feat: example strategy
1 parent f09861a commit 6a504f3

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

src/core/definer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { customErrorTypes } from "~/types/error";
2+
import type { ExampleStrategy } from "~/types/example";
23
import type { HttpMethod } from "~/types/http";
34
import type { RouteHandler, RouteMethodHandler } from "~/types/next";
45
import type { ResponseDefinition } from "~/types/response";
@@ -59,6 +60,7 @@ type RouteOptions<
5960
hander: RouteMethodHandler<PathParamsInput, Req, Res>
6061
) => RouteMethodHandler<PathParamsInput, Req, Res>,
6162
security?: OperationObject["security"],
63+
exampleStrategy?: ExampleStrategy,
6264
} & (RouteWithBody<RequestBodyInput, RequestBodyOutput> | RouteWithoutBody);
6365

6466
function defineRoute<

src/core/example.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { ExampleStrategy } from "~/types/example";
2+
import generateExample from "~/utils/generateExample";
3+
import type { ZodType } from "zod";
4+
5+
export function injectExample(strategy: ExampleStrategy, schema: ZodType<unknown> | string, isArray: boolean) {
6+
if (strategy === "none" || typeof schema === "string") return {};
7+
const isStrict = strategy === "strict-full" || strategy === "strict-ignore-optionals";
8+
const ignoreOptionals = strategy === "ignore-optional" || strategy === "strict-ignore-optionals";
9+
return {
10+
example: generateExample(isArray ? schema.array() : schema, ignoreOptionals, isStrict),
11+
};
12+
}

src/types/example.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type ExampleStrategy = "none" | "full" | "ignore-optional" | "strict-full" | "strict-ignore-optionals";

0 commit comments

Comments
 (0)