From b7cd2469e5dd62eab1e0c1313b6f40645bb8a054 Mon Sep 17 00:00:00 2001 From: Johann Schopplich Date: Wed, 29 Nov 2023 10:26:27 +0100 Subject: [PATCH] refactor: OpenAPI interop default export --- eslint.config.js | 2 +- src/openapi.ts | 10 ++++++---- src/runtime/formData.ts | 12 ++++++------ src/runtime/openapi.ts | 18 +++++++++--------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index cbd8240..fb18115 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,6 +1,6 @@ import antfu from '@antfu/eslint-config' -export default antfu( +export default await antfu( { rules: { 'node/prefer-global/buffer': 'off', diff --git a/src/openapi.ts b/src/openapi.ts index 18e1c4f..beffbf2 100644 --- a/src/openapi.ts +++ b/src/openapi.ts @@ -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 = '' @@ -65,7 +65,9 @@ async function resolveSchema({ schema }: Endpoint): Promise( + m: T | Promise, +): Promise { + const resolved = await m + return (resolved as any).default || resolved } diff --git a/src/runtime/formData.ts b/src/runtime/formData.ts index fb7ca42..1e58d0d 100644 --- a/src/runtime/formData.ts +++ b/src/runtime/formData.ts @@ -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' ) } @@ -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' ) } diff --git a/src/runtime/openapi.ts b/src/runtime/openapi.ts index 4c6b122..f26a2ef 100644 --- a/src/runtime/openapi.ts +++ b/src/runtime/openapi.ts @@ -29,15 +29,15 @@ export type RequestBody = T extends { requestBody?: { content: infer Body } } headers: { 'content-type': 'application/octet-stream' } } : Record) - | (Body extends { 'application/json': infer Schema } - ? { body: Schema; headers?: { 'content-type'?: 'application/json' } } - : Record) - | (Body extends { 'application/x-www-form-urlencoded': any } - ? { - body: FormData - headers: { 'content-type': 'application/x-www-form-urlencoded' } - } - : Record) + | (Body extends { 'application/json': infer Schema } + ? { body: Schema, headers?: { 'content-type'?: 'application/json' } } + : Record) + | (Body extends { 'application/x-www-form-urlencoded': any } + ? { + body: FormData + headers: { 'content-type': 'application/x-www-form-urlencoded' } + } + : Record) : unknown export type RequestOptions<