Skip to content

Commit

Permalink
Add option to define custom base router (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored Feb 6, 2024
1 parent 665b38e commit c8777e8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class TaskFetch extends OpenAPIRoute {
},
responses: {
'200': {
description: "Task fetched successfully",
description: 'Task fetched successfully',
schema: {
metaData: {},
task: Task,
Expand Down
18 changes: 11 additions & 7 deletions src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RouteType, Args> = new Proxy(router, {
// @ts-expect-error (we're adding an expected prop "path" to the get)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
}),
{}
)
),
Expand Down
9 changes: 3 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -20,6 +16,7 @@ export interface RouterOptions {
raiseUnknownParameters?: boolean
generateOperationIds?: boolean
openapiVersion?: '3' | '3.1'
baseRouter?: any
}

export declare type RouteParameter = {
Expand Down

0 comments on commit c8777e8

Please sign in to comment.