Skip to content

Commit

Permalink
feat: Generate all routes and resources (#132)
Browse files Browse the repository at this point in the history
* feat: General all routes and resources

* Update types

* ci: Generate code

---------

Co-authored-by: Seam Bot <devops@getseam.com>
  • Loading branch information
razor-x and seambot authored Nov 13, 2024
1 parent 22835a5 commit 1c8eb8b
Show file tree
Hide file tree
Showing 7 changed files with 13,179 additions and 5,656 deletions.
9 changes: 5 additions & 4 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
Expand Up @@ -73,7 +73,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@seamapi/types": "1.292.2",
"@seamapi/types": "1.294.1",
"@types/node": "^20.8.10",
"ava": "^6.0.1",
"c8": "^10.1.2",
Expand Down
60 changes: 17 additions & 43 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,63 +271,38 @@ export const createBlueprint = async (
// TODO: Move openapi to TypesModuleSchema
const openapi = typesModule.openapi as Openapi

const isFakeData = openapi.info.title === 'Foo'
const targetPaths = ['/acs', '/events', '/thermostats']
const targetSchemas = [
'acs_access_group',
'acs_credential',
'acs_credential_pool',
'acs_credential_provisioning_automation',
'acs_entrance',
'acs_system',
'acs_user',
'event',
'climate_preset',
'thermostat_schedule',
]

const context = {
codeSampleDefinitions,
formatCode,
}

return {
title: openapi.info.title,
routes: await createRoutes(openapi.paths, isFakeData, targetPaths, context),
resources: createResources(
openapi.components.schemas,
isFakeData,
targetSchemas,
),
routes: await createRoutes(openapi.paths, context),
resources: createResources(openapi.components.schemas),
}
}

const createRoutes = async (
paths: OpenapiPaths,
isFakeData: boolean,
targetPaths: string[],
context: Context,
): Promise<Route[]> => {
const routeMap = new Map<string, Route>()

for (const targetPath of targetPaths) {
const pathEntries = Object.entries(paths).filter(
([path]) => isFakeData || path.startsWith(targetPath),
)
const pathEntries = Object.entries(paths)

for (const [path, pathItem] of pathEntries) {
const namespace = getNamespace(path, paths)
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)
if (existingRoute != null) {
existingRoute.endpoints.push(...route.endpoints)
continue
}
const route = await createRoute(namespace, path, pathItem, context)

routeMap.set(route.path, route)
const existingRoute = routeMap.get(route.path)
if (existingRoute != null) {
existingRoute.endpoints.push(...route.endpoints)
continue
}

routeMap.set(route.path, route)
}

const routes = Array.from(routeMap.values())
Expand Down Expand Up @@ -698,12 +673,9 @@ const createParameter = (

const createResources = (
schemas: Openapi['components']['schemas'],
isFakeData: boolean,
targetSchemas: string[],
): Record<string, Resource> => {
return Object.entries(schemas)
.filter(([schemaName]) => isFakeData || targetSchemas.includes(schemaName))
.reduce<Record<string, Resource>>((acc, [schemaName, schema]) => {
return Object.entries(schemas).reduce<Record<string, Resource>>(
(acc, [schemaName, schema]) => {
if (
typeof schema === 'object' &&
schema !== null &&
Expand All @@ -727,7 +699,9 @@ const createResources = (
}
}
return acc
}, {})
},
{},
)
}

const createResponse = (
Expand Down
Loading

0 comments on commit 1c8eb8b

Please sign in to comment.