Skip to content

Commit

Permalink
feat: change Blob to File | ReadStream
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Mar 14, 2021
1 parent 9d730c9 commit 990a31a
Show file tree
Hide file tree
Showing 22 changed files with 71 additions and 41 deletions.
4 changes: 3 additions & 1 deletion samples/freee/@types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable */
import type { ReadStream } from 'fs'

export type paymentParams = {
company_id: number
date: string
Expand Down Expand Up @@ -339,7 +341,7 @@ export type receiptCreateParams = {
company_id: number
description?: string
issue_date?: string
receipt: Blob
receipt: File | ReadStream
}

export type receiptUpdateParams = {
Expand Down
4 changes: 3 additions & 1 deletion samples/openapi/api/v1/parse/image/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable */
import type { ReadStream } from 'fs'

export type Methods = {
post: {
status: 200
Expand All @@ -10,7 +12,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
rightholder?: string
statusCopyright?: 'unknown' | 'cc' | 'licensed' | 'sublicensed'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../../../../../@types'

export type Methods = {
Expand All @@ -13,7 +14,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
caption?: string
headline?: string
imageId?: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../../../../../@types'

export type Methods = {
Expand All @@ -13,7 +14,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
caption?: string
headline?: string
published?: boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../../../../../@types'

export type Methods = {
Expand All @@ -13,7 +14,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
caption?: string
headline?: string
published?: boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../../../@types'

export type Methods = {
Expand All @@ -13,7 +14,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
caption?: string
headline?: string
imageId?: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../../../@types'

export type Methods = {
Expand All @@ -13,7 +14,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
caption?: string
headline?: string
published?: boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../../../@types'

export type Methods = {
Expand All @@ -13,7 +14,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
caption?: string
headline?: string
published?: boolean
Expand Down
3 changes: 2 additions & 1 deletion samples/openapi/api/v3/extension/audio/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../@types'

export type Methods = {
Expand All @@ -13,7 +14,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
}
}
}
3 changes: 2 additions & 1 deletion samples/openapi/api/v3/extension/image/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../@types'

export type Methods = {
Expand All @@ -15,7 +16,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../../../@types'

export type Methods = {
Expand All @@ -13,7 +14,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
caption?: string
headline?: string
text?: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../../../@types'

export type Methods = {
Expand All @@ -13,7 +14,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
caption?: string
headline?: string
text?: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../../@types'

export type Methods = {
Expand Down Expand Up @@ -28,9 +29,9 @@ export type Methods = {
quoteSource?: string
quotePersonHandle?: string
videoId?: number
video?: Blob
video?: File | ReadStream
audioId?: number
audio?: Blob
audio?: File | ReadStream
text?: string
headline?: string
quoteCreated?: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../../../@types'

export type Methods = {
Expand All @@ -13,7 +14,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
caption?: string
headline?: string
text?: string
Expand Down
3 changes: 2 additions & 1 deletion samples/openapi/api/v3/extension/video/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../@types'

export type Methods = {
Expand All @@ -15,7 +16,7 @@ export type Methods = {
reqFormat: FormData

reqBody: {
file: Blob
file: File | ReadStream
}
}
}
3 changes: 2 additions & 1 deletion samples/openapi/api/v3/user/profile/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../../@types'

export type Methods = {
Expand All @@ -10,7 +11,7 @@ export type Methods = {
reqBody: {
screenName: string
url: string
image: Blob
image: File | ReadStream
imageId: string
}
}
Expand Down
4 changes: 2 additions & 2 deletions samples/simple/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
return {
upload: {
post: (option: { body: Methods3['post']['reqBody'], query: Methods3['post']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods3['post']['status']>(prefix, `${prefix1}${PATH5}`, POST, option, 'Blob').send(),
fetch<void, BasicHeaders, Methods3['post']['status']>(prefix, `${prefix1}${PATH5}`, POST, option).send(),
$post: (option: { body: Methods3['post']['reqBody'], query: Methods3['post']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods3['post']['status']>(prefix, `${prefix1}${PATH5}`, POST, option, 'Blob').send().then(r => r.body),
fetch<void, BasicHeaders, Methods3['post']['status']>(prefix, `${prefix1}${PATH5}`, POST, option).send().then(r => r.body),
$path: (option?: { method: 'post'; query: Methods3['post']['query'] }) =>
`${prefix}${prefix1}${PATH5}${option && option.query ? `?${dataToURLString(option.query)}` : ''}`
}
Expand Down
4 changes: 2 additions & 2 deletions samples/simple/file/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
return {
upload: {
post: (option: { body: Methods0['post']['reqBody'], query: Methods0['post']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods0['post']['status']>(prefix, `${prefix0}${PATH1}`, POST, option, 'Blob').send(),
fetch<void, BasicHeaders, Methods0['post']['status']>(prefix, `${prefix0}${PATH1}`, POST, option).send(),
$post: (option: { body: Methods0['post']['reqBody'], query: Methods0['post']['query'], config?: T }) =>
fetch<void, BasicHeaders, Methods0['post']['status']>(prefix, `${prefix0}${PATH1}`, POST, option, 'Blob').send().then(r => r.body),
fetch<void, BasicHeaders, Methods0['post']['status']>(prefix, `${prefix0}${PATH1}`, POST, option).send().then(r => r.body),
$path: (option?: { method: 'post'; query: Methods0['post']['query'] }) =>
`${prefix}${prefix0}${PATH1}${option && option.query ? `?${dataToURLString(option.query)}` : ''}`
}
Expand Down
4 changes: 3 additions & 1 deletion samples/simple/file/_id@number/upload/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable */
import type { ReadStream } from 'fs'

export type Methods = {
post: {
query: {
path: string
}

status: 204
reqBody: Blob
reqBody: File | ReadStream
}
}
3 changes: 2 additions & 1 deletion samples/swagger/pet/_petId@number/uploadImage/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
import type { ReadStream } from 'fs'
import * as Types from '../../../@types'

export type Methods = {
Expand All @@ -9,7 +10,7 @@ export type Methods = {

reqBody: {
additionalMetadata: string
file: Blob
file: File | ReadStream
}
}
}
43 changes: 26 additions & 17 deletions src/buildV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,15 @@ export default (openapi: OpenAPIV3.Document) => {

if (methods.length) {
const methodsText = props2String(methods, '')
const hasBinary = methodsText.includes('File | ReadStream')
const hasTypes = /( |<)Types\./.test(methodsText)

return {
file,
methods: `/* eslint-disable */\n${
/( |<)Types\./.test(methodsText)
? `import * as Types from '${file.map(() => '').join('../')}@types'\n\n`
: ''
hasBinary ? "import type { ReadStream } from 'fs'\n" : ''
}${hasBinary && !hasTypes ? '\n' : ''}${
hasTypes ? `import * as Types from '${file.map(() => '').join('../')}@types'\n\n` : ''
}export type Methods = ${methodsText}\n`
}
} else {
Expand All @@ -278,23 +280,30 @@ export default (openapi: OpenAPIV3.Document) => {
.filter(file => file.methods)
)

const typesText =
parameters.length + schemas.length
? [
...parameters.map(p => ({
name: p.name,
text: typeof p.props === 'string' ? p.props : props2String(p.props, '')
})),
...schemas.map(s => ({
name: s.name,
text: value2String(s.value, '').replace(/\n {2}/g, '\n')
}))
]
.map(p => `\nexport type ${p.name} = ${p.text}\n`)
.join('')
.replace(/ Types\./g, ' ')
: null

return {
baseURL: openapi.servers?.[0]?.url || '',
types:
parameters.length + schemas.length
? `/* eslint-disable */${[
...parameters.map(p => ({
name: p.name,
text: typeof p.props === 'string' ? p.props : props2String(p.props, '')
})),
...schemas.map(s => ({
name: s.name,
text: value2String(s.value, '').replace(/\n {2}/g, '\n')
}))
]
.map(p => `\nexport type ${p.name} = ${p.text}\n`)
.join('')}`.replace(/ Types\./g, ' ')
: null,
typesText &&
`/* eslint-disable */${
typesText.includes('File | ReadStream') ? "\nimport type { ReadStream } from 'fs'\n" : ''
}${typesText}`,
files
}
}
2 changes: 1 addition & 1 deletion src/builderUtils/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const schema2value = (
} else if (schema.properties || schema.additionalProperties) {
value = object2value(schema)
} else if (schema.format === 'binary') {
value = 'Blob'
value = 'File | ReadStream'
} else if (schema.type !== 'object') {
value = {
integer: 'number',
Expand Down

0 comments on commit 990a31a

Please sign in to comment.