Skip to content

Commit

Permalink
fix: support binary format of response
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Apr 16, 2021
1 parent 1619112 commit 22ccc37
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions samples/freee/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
* </ul>
*/
get: (option: { query: Methods22['get']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods22['get']['status']>(prefix, `${prefix4}${PATH13}`, GET, option).send(),
fetch<Methods22['get']['resBody'], BasicHeaders, Methods22['get']['status']>(prefix, `${prefix4}${PATH13}`, GET, option).blob(),
/**
* <h2 id="">概要</h2>
*
Expand All @@ -1764,7 +1764,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
* </ul>
*/
$get: (option: { query: Methods22['get']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods22['get']['status']>(prefix, `${prefix4}${PATH13}`, GET, option).send().then(r => r.body),
fetch<Methods22['get']['resBody'], BasicHeaders, Methods22['get']['status']>(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)}` : ''}`
},
Expand Down
4 changes: 2 additions & 2 deletions samples/freee/api/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
* </ul>
*/
get: (option: { query: Methods22['get']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods22['get']['status']>(prefix, `${prefix3}${PATH13}`, GET, option).send(),
fetch<Methods22['get']['resBody'], BasicHeaders, Methods22['get']['status']>(prefix, `${prefix3}${PATH13}`, GET, option).blob(),
/**
* <h2 id="">概要</h2>
*
Expand All @@ -1763,7 +1763,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
* </ul>
*/
$get: (option: { query: Methods22['get']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods22['get']['status']>(prefix, `${prefix3}${PATH13}`, GET, option).send().then(r => r.body),
fetch<Methods22['get']['resBody'], BasicHeaders, Methods22['get']['status']>(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)}` : ''}`
},
Expand Down
4 changes: 2 additions & 2 deletions samples/freee/api/1/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
* </ul>
*/
get: (option: { query: Methods22['get']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods22['get']['status']>(prefix, `${prefix2}${PATH13}`, GET, option).send(),
fetch<Methods22['get']['resBody'], BasicHeaders, Methods22['get']['status']>(prefix, `${prefix2}${PATH13}`, GET, option).blob(),
/**
* <h2 id="">概要</h2>
*
Expand All @@ -1762,7 +1762,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
* </ul>
*/
$get: (option: { query: Methods22['get']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods22['get']['status']>(prefix, `${prefix2}${PATH13}`, GET, option).send().then(r => r.body),
fetch<Methods22['get']['resBody'], BasicHeaders, Methods22['get']['status']>(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)}` : ''}`
},
Expand Down
4 changes: 2 additions & 2 deletions samples/freee/api/1/journals/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
* </ul>
*/
get: (option: { query: Methods1['get']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods1['get']['status']>(prefix, `${prefix1}${PATH2}`, GET, option).send(),
fetch<Methods1['get']['resBody'], BasicHeaders, Methods1['get']['status']>(prefix, `${prefix1}${PATH2}`, GET, option).blob(),
/**
* <h2 id="">概要</h2>
*
Expand All @@ -48,7 +48,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
* </ul>
*/
$get: (option: { query: Methods1['get']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods1['get']['status']>(prefix, `${prefix1}${PATH2}`, GET, option).send().then(r => r.body),
fetch<Methods1['get']['resBody'], BasicHeaders, Methods1['get']['status']>(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)}` : ''}`
},
Expand Down
4 changes: 2 additions & 2 deletions samples/freee/api/1/journals/reports/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
* </ul>
*/
get: (option: { query: Methods0['get']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods0['get']['status']>(prefix, `${prefix0}${PATH1}`, GET, option).send(),
fetch<Methods0['get']['resBody'], BasicHeaders, Methods0['get']['status']>(prefix, `${prefix0}${PATH1}`, GET, option).blob(),
/**
* <h2 id="">概要</h2>
*
Expand All @@ -45,7 +45,7 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
* </ul>
*/
$get: (option: { query: Methods0['get']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods0['get']['status']>(prefix, `${prefix0}${PATH1}`, GET, option).send().then(r => r.body),
fetch<Methods0['get']['resBody'], BasicHeaders, Methods0['get']['status']>(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)}` : ''}`
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export type Methods = {
}

status: 200
resBody: Blob
}
}
7 changes: 5 additions & 2 deletions src/buildV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 3 additions & 2 deletions src/builderUtils/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 22ccc37

Please sign in to comment.