Skip to content

Commit 8252ce0

Browse files
fix: improve schema resolution on Windows
1 parent b8d4850 commit 8252ce0

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/openapi.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { OpenAPI3, OpenAPITSOptions } from 'openapi-typescript'
22
import type { EndpointConfiguration } from './module'
3+
import { pathToFileURL } from 'node:url'
34
import { useNuxt } from '@nuxt/kit'
4-
import { resolve } from 'pathe'
5+
import { isAbsolute, resolve } from 'pathe'
56
import { pascalCase } from 'scule'
67

78
/** @deprecated Hooks should be used instead */
@@ -193,26 +194,27 @@ export type operations = Record<string, never>
193194
}
194195

195196
async function resolveSchema(id: string, { schema }: SchemaEndpoint): Promise<string | URL | OpenAPI3> {
196-
const nuxt = useNuxt()
197-
198197
if (typeof schema === 'function') {
199198
console.warn(`[nuxt-api-party] Passing a function to "apiParty.endpoints.${id}.schema" is deprecated. Use the "api-party:extend" hook instead.`)
200199
return await schema()
201200
}
202201

203-
if (typeof schema === 'string' && !isValidUrl(schema))
204-
return new URL(resolve(nuxt.options.rootDir, schema), import.meta.url)
202+
if (typeof schema === 'string') {
203+
if (/^https?:\/\//i.test(schema))
204+
return schema
205205

206-
return schema!
207-
}
206+
if (schema.startsWith('file://'))
207+
return new URL(schema)
208208

209-
function isValidUrl(url: string) {
210-
try {
211-
return Boolean(new URL(url))
212-
}
213-
catch {
214-
return false
209+
const nuxt = useNuxt()
210+
const resolvedPath = isAbsolute(schema)
211+
? schema
212+
: resolve(nuxt.options.rootDir, schema)
213+
214+
return pathToFileURL(resolvedPath)
215215
}
216+
217+
return schema!
216218
}
217219

218220
async function interopDefault<T>(

0 commit comments

Comments
 (0)