From c8777e8ec418b4d409c589388735b75ac57501e6 Mon Sep 17 00:00:00 2001 From: Gabriel Massadas <5445926+G4brym@users.noreply.github.com> Date: Tue, 6 Feb 2024 11:54:52 +0000 Subject: [PATCH] Add option to define custom base router (#121) --- README.md | 2 +- src/openapi.ts | 18 +++++++++++------- src/types.ts | 9 +++------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 1a0555a..9cdc06d 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ export class TaskFetch extends OpenAPIRoute { }, responses: { '200': { - description: "Task fetched successfully", + description: 'Task fetched successfully', schema: { metaData: {}, task: Task, diff --git a/src/openapi.ts b/src/openapi.ts index 9a2a19a..5b58e50 100644 --- a/src/openapi.ts +++ b/src/openapi.ts @@ -66,7 +66,9 @@ export function OpenAPIRouter< }) } - const router = Router({ base: options?.base, routes: options?.routes }) + const routerToUse = options?.baseRouter || Router + + const router = routerToUse({ base: options?.base, routes: options?.routes }) const routerProxy: OpenAPIRouterType = new Proxy(router, { // @ts-expect-error (we're adding an expected prop "path" to the get) @@ -94,11 +96,9 @@ export function OpenAPIRouter< // Merge inner router definitions into outer router registry.merge(nestedRouter.registry) } else if (prop !== 'all') { - const parsedRoute = - (options?.base || '') + - route - .replace(/\/+(\/|$)/g, '$1') // strip double & trailing splash - .replace(/:(\w+)/g, '{$1}') // convert parameters into openapi compliant + const parsedRoute = ((options?.base || '') + route) + .replaceAll(/\/+(\/|$)/g, '$1') // strip double & trailing splash + .replaceAll(/:(\w+)/g, '{$1}') // convert parameters into openapi compliant // @ts-ignore let schema: RouteConfig = undefined @@ -139,7 +139,11 @@ export function OpenAPIRouter< // TODO: make sure this works params: z.object( params.reduce( - (obj, item) => Object.assign(obj, { [item]: z.string() }), + // matched parameters start with ':' so replace the first occurrence with nothing + (obj, item) => + Object.assign(obj, { + [item.replace(':', '')]: z.string(), + }), {} ) ), diff --git a/src/types.ts b/src/types.ts index 76587df..fd7ca6d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,10 +1,6 @@ import { RouteEntry } from 'itty-router' -import { OpenAPIObject } from 'openapi3-ts/oas31' -import { AnyZodObject, ZodType } from 'zod' -import { - ResponseConfig, - ZodRequestBody, -} from '@asteasolutions/zod-to-openapi/dist/openapi-registry' +import { ZodType } from 'zod' +import { ResponseConfig } from '@asteasolutions/zod-to-openapi/dist/openapi-registry' import { RouteConfig } from '@asteasolutions/zod-to-openapi' import { OpenAPIObjectConfigV31 } from '@asteasolutions/zod-to-openapi/dist/v3.1/openapi-generator' import { OpenAPIObjectConfig } from '@asteasolutions/zod-to-openapi/dist/v3.0/openapi-generator' @@ -20,6 +16,7 @@ export interface RouterOptions { raiseUnknownParameters?: boolean generateOperationIds?: boolean openapiVersion?: '3' | '3.1' + baseRouter?: any } export declare type RouteParameter = {