From 09b5dd857ebe7b63aa1300cabf03fdeb46a4c400 Mon Sep 17 00:00:00 2001 From: Omer Mecitoglu Date: Thu, 5 Sep 2024 10:23:18 +0700 Subject: [PATCH] add routeDefinerName to generator-options --- README.md | 12 +++++++++--- package-lock.json | 4 ++-- package.json | 2 +- src/core/next.ts | 4 ++-- src/core/options.ts | 2 +- src/core/transpile.ts | 4 ++-- src/index.ts | 11 ++++++++--- 7 files changed, 25 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f9b7c6a..7076e3f 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ const Page = async () => { UserDTO, NewUserDTO, UserPatchDTO, + }, { + // options }); return ; }; @@ -58,9 +60,13 @@ export default Page; The `generateOpenApiSpec` function takes an object with the following properties: -| Property | Type | Description | -| ------------ | ------------------------------------------- | ---------------------------------------------------------------- | -| models | Record | An object where keys are model names and values are Zod schemas. | +| Property | Type | Description | +| ------------------------ | ------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| models | Record | 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 diff --git a/package-lock.json b/package-lock.json index eed24ba..823eb4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@omer-x/next-openapi-json-generator", - "version": "0.3.1", + "version": "0.4.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@omer-x/next-openapi-json-generator", - "version": "0.3.1", + "version": "0.4.0", "license": "MIT", "dependencies": { "@omer-x/package-metadata": "^0.1.2", diff --git a/package.json b/package.json index b59bc80..58c15a5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/core/next.ts b/src/core/next.ts index f67d67d..dd1770f 100644 --- a/src/core/next.ts +++ b/src/core/next.ts @@ -32,9 +32,9 @@ function safeEval(code: string, routePath: string) { } } -export async function getRouteExports(routePath: string, schemas: Record) { +export async function getRouteExports(routePath: string, routeDefinerName: string, schemas: Record) { 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).schemas = schemas; const result = safeEval(fixedCode, routePath); diff --git a/src/core/options.ts b/src/core/options.ts index d77d75b..7b956e2 100644 --- a/src/core/options.ts +++ b/src/core/options.ts @@ -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")) { diff --git a/src/core/transpile.ts b/src/core/transpile.ts index 7c57850..0991335 100644 --- a/src/core/transpile.ts +++ b/src/core/transpile.ts @@ -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, ]; diff --git a/src/index.ts b/src/index.ts index c8b9b9a..d7e4b0d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,17 +10,22 @@ import type { ZodType } from "zod"; type GeneratorOptions = { include?: string[], exclude?: string[], + routeDefinerName?: string, }; -export default async function generateOpenApiSpec(schemas: Record, options?: GeneratorOptions) { - const verifiedOptions = verifyOptions(options?.include, options?.exclude); +export default async function generateOpenApiSpec(schemas: Record, { + 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(