Skip to content

Commit

Permalink
refactor: OpenAPI interop default export
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Nov 29, 2023
1 parent c75032a commit b7cd246
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import antfu from '@antfu/eslint-config'

export default antfu(
export default await antfu(
{
rules: {
'node/prefer-global/buffer': 'off',
Expand Down
10 changes: 6 additions & 4 deletions src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function generateTypes(
throw new Error('Failed to generate OpenAPI types')
})

const openAPITS = await resolveOpenAPIImports()
const openAPITS = await interopDefault(import('openapi-typescript'))
const schemas = await Promise.all(
Object.entries(endpoints).map(async ([id, endpoint]) => {
let types = ''
Expand Down Expand Up @@ -65,7 +65,9 @@ async function resolveSchema({ schema }: Endpoint): Promise<string | URL | OpenA
return schema!
}

async function resolveOpenAPIImports() {
const openAPITS = await import('openapi-typescript')
return openAPITS.default || openAPITS
async function interopDefault<T>(
m: T | Promise<T>,
): Promise<T extends { default: infer U } ? U : T> {
const resolved = await m
return (resolved as any).default || resolved
}
12 changes: 6 additions & 6 deletions src/runtime/formData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export function isFormData(obj: unknown): obj is FormData {
export function isSerializedFormData(obj: unknown): obj is SerializedFormData {
return (
typeof obj === 'object'
&& obj !== null
&& '__type' in obj
&& obj.__type === 'form-data'
&& obj !== null
&& '__type' in obj
&& obj.__type === 'form-data'
)
}

Expand Down Expand Up @@ -65,9 +65,9 @@ export async function objectToFormData(obj: SerializedFormData) {
function isSerializedBlob(obj: unknown): obj is SerializedBlob {
return (
typeof obj === 'object'
&& obj !== null
&& '__type' in obj
&& obj.__type === 'blob'
&& obj !== null
&& '__type' in obj
&& obj.__type === 'blob'
)
}

Expand Down
18 changes: 9 additions & 9 deletions src/runtime/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ export type RequestBody<T> = T extends { requestBody?: { content: infer Body } }
headers: { 'content-type': 'application/octet-stream' }
}
: Record<string, any>)
| (Body extends { 'application/json': infer Schema }
? { body: Schema; headers?: { 'content-type'?: 'application/json' } }
: Record<string, any>)
| (Body extends { 'application/x-www-form-urlencoded': any }
? {
body: FormData
headers: { 'content-type': 'application/x-www-form-urlencoded' }
}
: Record<string, any>)
| (Body extends { 'application/json': infer Schema }
? { body: Schema, headers?: { 'content-type'?: 'application/json' } }
: Record<string, any>)
| (Body extends { 'application/x-www-form-urlencoded': any }
? {
body: FormData
headers: { 'content-type': 'application/x-www-form-urlencoded' }
}
: Record<string, any>)
: unknown

export type RequestOptions<
Expand Down

0 comments on commit b7cd246

Please sign in to comment.