Skip to content

Commit 09b5dd8

Browse files
committed
add routeDefinerName to generator-options
1 parent 34627b9 commit 09b5dd8

File tree

7 files changed

+25
-14
lines changed

7 files changed

+25
-14
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ const Page = async () => {
4747
UserDTO,
4848
NewUserDTO,
4949
UserPatchDTO,
50+
}, {
51+
// options
5052
});
5153
return <ReactSwagger spec={spec} />;
5254
};
@@ -58,9 +60,13 @@ export default Page;
5860

5961
The `generateOpenApiSpec` function takes an object with the following properties:
6062

61-
| Property | Type | Description |
62-
| ------------ | ------------------------------------------- | ---------------------------------------------------------------- |
63-
| models | Record<string, [ZodType](https://zod.dev)> | An object where keys are model names and values are Zod schemas. |
63+
| Property | Type | Description |
64+
| ------------------------ | ------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
65+
| models | Record<string, [ZodType](https://zod.dev)> | An object where keys are model names and values are Zod schemas |
66+
| options | Object | `(Optional)` An object to customize the functionality of the route definer |
67+
| options.include | string[] | `(Optional)` An array of strings which specifies the routes will be included to the JSON output |
68+
| options.exclude | string[] | `(Optional)` An array of strings which specifies the routes will be excluded from the JSON output |
69+
| options.routeDefinerName | string | `(Optional)` Name of the function that was exported from the [`Next OpenAPI Route Handler`](https://www.npmjs.com/package/@omer-x/next-openapi-route-handler) (Default: `defineRoute`) |
6470

6571
### Result
6672

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@omer-x/next-openapi-json-generator",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"description": "a Next.js plugin to generate OpenAPI documentation from route handlers",
55
"keywords": [
66
"next.js",

src/core/next.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ function safeEval(code: string, routePath: string) {
3232
}
3333
}
3434

35-
export async function getRouteExports(routePath: string, schemas: Record<string, unknown>) {
35+
export async function getRouteExports(routePath: string, routeDefinerName: string, schemas: Record<string, unknown>) {
3636
const content = await fs.readFile(routePath, "utf-8");
37-
const code = transpile(content);
37+
const code = transpile(content, routeDefinerName);
3838
const fixedCode = Object.keys(schemas).reduce(injectSchemas, code);
3939
(global as Record<string, unknown>).schemas = schemas;
4040
const result = safeEval(fixedCode, routePath);

src/core/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console */
22

3-
export function verifyOptions(include: string[] = [], exclude: string[] = []) {
3+
export function verifyOptions(include: string[], exclude: string[]) {
44
if (process.env.NODE_ENV === "development") {
55
for (const item of include) {
66
if (!item.endsWith("/route.ts")) {

src/core/transpile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ function fixExports(code: string) {
1111
return `${exportFixer1}\n${code}\n${exportFixer2}`;
1212
}
1313

14-
export function transpile(rawCode: string) {
14+
export function transpile(rawCode: string, routeDefinerName: string) {
1515
const code = fixExports(removeImports(rawCode));
1616
const parts = [
17-
"import createRoute from '@omer-x/next-openapi-route-handler'",
17+
`import ${routeDefinerName} from '@omer-x/next-openapi-route-handler';`,
1818
"import z from 'zod';",
1919
code,
2020
];

src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ import type { ZodType } from "zod";
1010
type GeneratorOptions = {
1111
include?: string[],
1212
exclude?: string[],
13+
routeDefinerName?: string,
1314
};
1415

15-
export default async function generateOpenApiSpec(schemas: Record<string, ZodType>, options?: GeneratorOptions) {
16-
const verifiedOptions = verifyOptions(options?.include, options?.exclude);
16+
export default async function generateOpenApiSpec(schemas: Record<string, ZodType>, {
17+
include: includeOption = [],
18+
exclude: excludeOption = [],
19+
routeDefinerName = "defineRoute",
20+
}: GeneratorOptions = {}) {
21+
const verifiedOptions = verifyOptions(includeOption, excludeOption);
1722
const appFolderPath = await findAppFolderPath();
1823
if (!appFolderPath) throw new Error("This is not a Next.js application!");
1924
const routes = await getDirectoryItems(appFolderPath, "route.ts");
2025
const verifiedRoutes = filterDirectoryItems(appFolderPath, routes, verifiedOptions.include, verifiedOptions.exclude);
2126
const validRoutes: RouteRecord[] = [];
2227
for (const route of verifiedRoutes) {
23-
const exportedRouteHandlers = await getRouteExports(route, schemas);
28+
const exportedRouteHandlers = await getRouteExports(route, routeDefinerName, schemas);
2429
for (const [method, routeHandler] of Object.entries(exportedRouteHandlers)) {
2530
if (!routeHandler || !routeHandler.apiData) continue;
2631
validRoutes.push(createRouteRecord(

0 commit comments

Comments
 (0)