From 22ccc377ce24f5227036bcee6807b2ba78bf6a95 Mon Sep 17 00:00:00 2001 From: solufa Date: Fri, 16 Apr 2021 18:21:33 +0900 Subject: [PATCH] fix: support binary format of response --- samples/freee/$api.ts | 4 ++-- samples/freee/api/$api.ts | 4 ++-- samples/freee/api/1/$api.ts | 4 ++-- samples/freee/api/1/journals/$api.ts | 4 ++-- samples/freee/api/1/journals/reports/$api.ts | 4 ++-- .../api/1/journals/reports/_id@number/download/index.ts | 1 + src/buildV3.ts | 7 +++++-- src/builderUtils/converters.ts | 5 +++-- 8 files changed, 19 insertions(+), 14 deletions(-) diff --git a/samples/freee/$api.ts b/samples/freee/$api.ts index cf51235f..642d6129 100644 --- a/samples/freee/$api.ts +++ b/samples/freee/$api.ts @@ -1749,7 +1749,7 @@ const api = ({ baseURL, fetch }: AspidaClient) => { * */ get: (option: { query: Methods22['get']['query'], config?: T }) => - fetch(prefix, `${prefix4}${PATH13}`, GET, option).send(), + fetch(prefix, `${prefix4}${PATH13}`, GET, option).blob(), /** *

概要

* @@ -1764,7 +1764,7 @@ const api = ({ baseURL, fetch }: AspidaClient) => { * */ $get: (option: { query: Methods22['get']['query'], config?: T }) => - fetch(prefix, `${prefix4}${PATH13}`, GET, option).send().then(r => r.body), + fetch(prefix, `${prefix4}${PATH13}`, GET, option).blob().then(r => r.body), $path: (option?: { method?: 'get'; query: Methods22['get']['query'] }) => `${prefix}${prefix4}${PATH13}${option && option.query ? `?${dataToURLString(option.query)}` : ''}` }, diff --git a/samples/freee/api/$api.ts b/samples/freee/api/$api.ts index 30a89aa0..b47ad930 100644 --- a/samples/freee/api/$api.ts +++ b/samples/freee/api/$api.ts @@ -1748,7 +1748,7 @@ const api = ({ baseURL, fetch }: AspidaClient) => { * */ get: (option: { query: Methods22['get']['query'], config?: T }) => - fetch(prefix, `${prefix3}${PATH13}`, GET, option).send(), + fetch(prefix, `${prefix3}${PATH13}`, GET, option).blob(), /** *

概要

* @@ -1763,7 +1763,7 @@ const api = ({ baseURL, fetch }: AspidaClient) => { * */ $get: (option: { query: Methods22['get']['query'], config?: T }) => - fetch(prefix, `${prefix3}${PATH13}`, GET, option).send().then(r => r.body), + fetch(prefix, `${prefix3}${PATH13}`, GET, option).blob().then(r => r.body), $path: (option?: { method?: 'get'; query: Methods22['get']['query'] }) => `${prefix}${prefix3}${PATH13}${option && option.query ? `?${dataToURLString(option.query)}` : ''}` }, diff --git a/samples/freee/api/1/$api.ts b/samples/freee/api/1/$api.ts index 7ac519ba..e296357a 100644 --- a/samples/freee/api/1/$api.ts +++ b/samples/freee/api/1/$api.ts @@ -1747,7 +1747,7 @@ const api = ({ baseURL, fetch }: AspidaClient) => { * */ get: (option: { query: Methods22['get']['query'], config?: T }) => - fetch(prefix, `${prefix2}${PATH13}`, GET, option).send(), + fetch(prefix, `${prefix2}${PATH13}`, GET, option).blob(), /** *

概要

* @@ -1762,7 +1762,7 @@ const api = ({ baseURL, fetch }: AspidaClient) => { * */ $get: (option: { query: Methods22['get']['query'], config?: T }) => - fetch(prefix, `${prefix2}${PATH13}`, GET, option).send().then(r => r.body), + fetch(prefix, `${prefix2}${PATH13}`, GET, option).blob().then(r => r.body), $path: (option?: { method?: 'get'; query: Methods22['get']['query'] }) => `${prefix}${prefix2}${PATH13}${option && option.query ? `?${dataToURLString(option.query)}` : ''}` }, diff --git a/samples/freee/api/1/journals/$api.ts b/samples/freee/api/1/journals/$api.ts index a7813376..bfe1e630 100644 --- a/samples/freee/api/1/journals/$api.ts +++ b/samples/freee/api/1/journals/$api.ts @@ -33,7 +33,7 @@ const api = ({ baseURL, fetch }: AspidaClient) => { * */ get: (option: { query: Methods1['get']['query'], config?: T }) => - fetch(prefix, `${prefix1}${PATH2}`, GET, option).send(), + fetch(prefix, `${prefix1}${PATH2}`, GET, option).blob(), /** *

概要

* @@ -48,7 +48,7 @@ const api = ({ baseURL, fetch }: AspidaClient) => { * */ $get: (option: { query: Methods1['get']['query'], config?: T }) => - fetch(prefix, `${prefix1}${PATH2}`, GET, option).send().then(r => r.body), + fetch(prefix, `${prefix1}${PATH2}`, GET, option).blob().then(r => r.body), $path: (option?: { method?: 'get'; query: Methods1['get']['query'] }) => `${prefix}${prefix1}${PATH2}${option && option.query ? `?${dataToURLString(option.query)}` : ''}` }, diff --git a/samples/freee/api/1/journals/reports/$api.ts b/samples/freee/api/1/journals/reports/$api.ts index 1cab0d09..f42bd28b 100644 --- a/samples/freee/api/1/journals/reports/$api.ts +++ b/samples/freee/api/1/journals/reports/$api.ts @@ -30,7 +30,7 @@ const api = ({ baseURL, fetch }: AspidaClient) => { * */ get: (option: { query: Methods0['get']['query'], config?: T }) => - fetch(prefix, `${prefix0}${PATH1}`, GET, option).send(), + fetch(prefix, `${prefix0}${PATH1}`, GET, option).blob(), /** *

概要

* @@ -45,7 +45,7 @@ const api = ({ baseURL, fetch }: AspidaClient) => { * */ $get: (option: { query: Methods0['get']['query'], config?: T }) => - fetch(prefix, `${prefix0}${PATH1}`, GET, option).send().then(r => r.body), + fetch(prefix, `${prefix0}${PATH1}`, GET, option).blob().then(r => r.body), $path: (option?: { method?: 'get'; query: Methods0['get']['query'] }) => `${prefix}${prefix0}${PATH1}${option && option.query ? `?${dataToURLString(option.query)}` : ''}` }, diff --git a/samples/freee/api/1/journals/reports/_id@number/download/index.ts b/samples/freee/api/1/journals/reports/_id@number/download/index.ts index 8fd8fdf1..dd4943c1 100644 --- a/samples/freee/api/1/journals/reports/_id@number/download/index.ts +++ b/samples/freee/api/1/journals/reports/_id@number/download/index.ts @@ -20,5 +20,6 @@ export type Methods = { } status: 200 + resBody: Blob } } diff --git a/src/buildV3.ts b/src/buildV3.ts index 4eb44c88..92ca9f94 100644 --- a/src/buildV3.ts +++ b/src/buildV3.ts @@ -188,9 +188,12 @@ export default (openapi: OpenAPIV3.Document) => { const res = target.responses[code] const ref = isRefObject(res) ? resolveResRef(openapi, res.$ref) : res + const content = + ref.content && + Object.entries(ref.content).find(([key]) => key.startsWith('application/'))?.[1] - if (ref.content?.['application/json']?.schema) { - const val = schema2value(ref.content['application/json'].schema) + if (content?.schema) { + const val = schema2value(content.schema, true) val && params.push({ name: 'resBody', diff --git a/src/builderUtils/converters.ts b/src/builderUtils/converters.ts index 8d8f69eb..a55787c1 100644 --- a/src/builderUtils/converters.ts +++ b/src/builderUtils/converters.ts @@ -94,7 +94,8 @@ const object2value = (obj: OpenAPIV3.NonArraySchemaObject): Prop[] => { export const BINARY_TYPE = 'File | ReadStream' export const schema2value = ( - schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | undefined + schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | undefined, + isResponse?: true ): PropValue | null => { if (!schema) return null @@ -123,7 +124,7 @@ export const schema2value = ( } else if (schema.properties || schema.additionalProperties) { value = object2value(schema) } else if (schema.format === 'binary') { - value = BINARY_TYPE + value = isResponse ? 'Blob' : BINARY_TYPE } else if (schema.type !== 'object') { value = { integer: 'number',