Skip to content

Commit

Permalink
add routeDefinerName to generator-options
Browse files Browse the repository at this point in the history
  • Loading branch information
omermecitoglu committed Sep 5, 2024
1 parent 34627b9 commit 09b5dd8
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 14 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const Page = async () => {
UserDTO,
NewUserDTO,
UserPatchDTO,
}, {
// options
});
return <ReactSwagger spec={spec} />;
};
Expand All @@ -58,9 +60,13 @@ export default Page;

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

| Property | Type | Description |
| ------------ | ------------------------------------------- | ---------------------------------------------------------------- |
| models | Record<string, [ZodType](https://zod.dev)> | An object where keys are model names and values are Zod schemas. |
| Property | Type | Description |
| ------------------------ | ------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| models | Record<string, [ZodType](https://zod.dev)> | An object where keys are model names and values are Zod schemas |
| options | Object | `(Optional)` An object to customize the functionality of the route definer |
| options.include | string[] | `(Optional)` An array of strings which specifies the routes will be included to the JSON output |
| options.exclude | string[] | `(Optional)` An array of strings which specifies the routes will be excluded from the JSON output |
| 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`) |

### Result

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@omer-x/next-openapi-json-generator",
"version": "0.3.1",
"version": "0.4.0",
"description": "a Next.js plugin to generate OpenAPI documentation from route handlers",
"keywords": [
"next.js",
Expand Down
4 changes: 2 additions & 2 deletions src/core/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ function safeEval(code: string, routePath: string) {
}
}

export async function getRouteExports(routePath: string, schemas: Record<string, unknown>) {
export async function getRouteExports(routePath: string, routeDefinerName: string, schemas: Record<string, unknown>) {
const content = await fs.readFile(routePath, "utf-8");
const code = transpile(content);
const code = transpile(content, routeDefinerName);
const fixedCode = Object.keys(schemas).reduce(injectSchemas, code);
(global as Record<string, unknown>).schemas = schemas;
const result = safeEval(fixedCode, routePath);
Expand Down
2 changes: 1 addition & 1 deletion src/core/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

export function verifyOptions(include: string[] = [], exclude: string[] = []) {
export function verifyOptions(include: string[], exclude: string[]) {
if (process.env.NODE_ENV === "development") {
for (const item of include) {
if (!item.endsWith("/route.ts")) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ function fixExports(code: string) {
return `${exportFixer1}\n${code}\n${exportFixer2}`;
}

export function transpile(rawCode: string) {
export function transpile(rawCode: string, routeDefinerName: string) {
const code = fixExports(removeImports(rawCode));
const parts = [
"import createRoute from '@omer-x/next-openapi-route-handler'",
`import ${routeDefinerName} from '@omer-x/next-openapi-route-handler';`,
"import z from 'zod';",
code,
];
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ import type { ZodType } from "zod";
type GeneratorOptions = {
include?: string[],
exclude?: string[],
routeDefinerName?: string,
};

export default async function generateOpenApiSpec(schemas: Record<string, ZodType>, options?: GeneratorOptions) {
const verifiedOptions = verifyOptions(options?.include, options?.exclude);
export default async function generateOpenApiSpec(schemas: Record<string, ZodType>, {
include: includeOption = [],
exclude: excludeOption = [],
routeDefinerName = "defineRoute",
}: GeneratorOptions = {}) {
const verifiedOptions = verifyOptions(includeOption, excludeOption);
const appFolderPath = await findAppFolderPath();
if (!appFolderPath) throw new Error("This is not a Next.js application!");
const routes = await getDirectoryItems(appFolderPath, "route.ts");
const verifiedRoutes = filterDirectoryItems(appFolderPath, routes, verifiedOptions.include, verifiedOptions.exclude);
const validRoutes: RouteRecord[] = [];
for (const route of verifiedRoutes) {
const exportedRouteHandlers = await getRouteExports(route, schemas);
const exportedRouteHandlers = await getRouteExports(route, routeDefinerName, schemas);
for (const [method, routeHandler] of Object.entries(exportedRouteHandlers)) {
if (!routeHandler || !routeHandler.apiData) continue;
validRoutes.push(createRouteRecord(
Expand Down

0 comments on commit 09b5dd8

Please sign in to comment.