Skip to content

Commit

Permalink
fix: re-add support for openapi-typescript 5 and 6 (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmess1221 authored May 13, 2024
1 parent 78f30a3 commit bef1297
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 87 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@
"defu": "^6.1.4",
"ofetch": "^1.3.4",
"ohash": "^1.1.3",
"openapi-typescript": "^7.0.0-next.10",
"openapi-typescript-helpers": "^0.0.8",
"pathe": "^1.1.2",
"scule": "^1.3.0",
"ufo": "^1.5.3"
},
"peerDependencies": {
"openapi-typescript": "^5 || ^6 || ^7"
},
"devDependencies": {
"@nuxt/eslint-config": "^0.3.9",
"@nuxt/module-builder": "^0.5.5",
Expand Down
86 changes: 2 additions & 84 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions src/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { resolve } from 'pathe'
import { useNuxt } from '@nuxt/kit'
import type { OpenAPI3, OpenAPITSOptions } from 'openapi-typescript'
import openAPITS, { astToString } from 'openapi-typescript'
import type { ApiEndpoint } from './module'

declare module 'openapi-typescript' {
// exists in openapi-typescript@7
function _astToString(ast: unknown): string
export const astToString: typeof _astToString | undefined
}

export async function generateDeclarationTypes(
endpoints: Record<string, ApiEndpoint>,
globalOpenAPIOptions: OpenAPITSOptions,
Expand All @@ -29,15 +36,18 @@ async function generateSchemaTypes(options: {
openAPITSOptions?: OpenAPITSOptions
},
) {
const { default: openAPITS, astToString } = await import('openapi-typescript')
const schema = await resolveSchema(options.endpoint)

try {
const ast = await openAPITS(schema, {
...options.openAPITSOptions,
...options.endpoint.openAPITS,
})
return astToString(ast)
if (typeof ast !== 'string') {
// openapi-typescript 7
return astToString!(ast)
}
return ast
}
catch (error) {
console.error(`Failed to generate types for ${options.id}`)
Expand Down

0 comments on commit bef1297

Please sign in to comment.