Skip to content

Commit

Permalink
add test fixture, and revert endsWith to regex
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewuu committed Sep 11, 2024
1 parent fae8967 commit 7cc9bc7
Show file tree
Hide file tree
Showing 10 changed files with 338 additions and 360 deletions.
5 changes: 4 additions & 1 deletion src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ const createRoutes = async (

for (const [path, pathItem] of pathEntries) {
const namespace = getNamespace(path, paths)

const route = await createRoute(namespace, path, pathItem, context)

const existingRoute = routeMap.get(route.path)
Expand Down Expand Up @@ -318,7 +319,9 @@ const getNamespace = (path: string, paths: OpenapiPaths): string | null => {

// An endpoint is a route that ends without further paths. i.e., ends in
// a letter (not slash).
const endpoints = pathKeys.filter((key) => !key.endsWith('/'))
const endpoints = pathKeys.filter((key) => {
return new RegExp(`^/${[...namespace, part].join('/')}/\\w+$`).test(key)
})

if (endpoints.length === 0) {
namespace.push(part)
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/types/code-sample-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,22 @@ export default [
},
},
},
{
title: 'List planes',
description: 'This is the wya to get all plans',
request: {
path: '/transport/air/planes/list',
parameters: {},
},
response: {
body: {
planes: [
{
plane_id: '9d3163f9-9185-40d3-a0ce-a03d3c7ce402',
name: 'Woosh',
},
],
},
},
},
]
48 changes: 48 additions & 0 deletions test/fixtures/types/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ export default {
},
required: ['foo_id', 'name'],
},
plane: {
type: 'object',
description: 'A plane resource.',
properties: {
plane_id: {
description: 'plane id',
format: 'uuid',
type: 'string',
},
name: {
description: 'Planej name',
type: 'string',
},
},
required: ['plane_id', 'name'],
},
},
},
paths: {
Expand Down Expand Up @@ -157,5 +173,37 @@ export default {
'x-title': 'List foos',
},
},
'/transport/air/planes/list': {
get: {
operationId: 'planesListGet',
responses: {
200: {
content: {
'application/json': {
schema: {
properties: {
ok: { type: 'boolean' },
planes: {
items: { $ref: '#/components/schemas/plane' },
type: 'array',
},
},
required: ['planes', 'ok'],
type: 'object',
},
},
},
description: 'List all planes.',
},
400: { description: 'Bad Request' },
401: { description: 'Unauthorized' },
},
security: [],
summary: '/transport/air/planes/list',
tags: ['/transport/air/planes'],
'x-response-key': 'planes',
'x-title': 'List planes',
},
},
},
}
7 changes: 7 additions & 0 deletions test/fixtures/types/route-specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,12 @@ export const routes = {
jsonResponse: z.object({
foos: z.array(schemas.foo),
}),
'/transport/air/planes/list': {
auth: 'none',
methods: ['GET'],
jsonResponse: z.object({
planes: z.array(schemas.plane),
}),
},
},
} as const
14 changes: 14 additions & 0 deletions test/fixtures/types/route-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ export interface Routes {
}
}
}
'/transport/air/planes/list': {
route: '/transport/air/planes/list'
method: 'GET'
queryParams: Record<string, unknown>
jsonBody: Record<string, unknown>
commonParams: Record<string, unknown>
formData: Record<string, unknown>
jsonResponse: {
planes: {
plane_id: string
name: string
}
}
}
}

export type RouteResponse<Path extends keyof Routes> =
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/types/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export const foo = z.object({
undocumented_prop: z.string().optional(),
nullable_property: z.string().optional().nullable(),
})

export const plane = z.object({
plane_id: z.string().uuid(),
name: z.string(),
})
Loading

0 comments on commit 7cc9bc7

Please sign in to comment.