diff --git a/.changeset/odd-oranges-appear.md b/.changeset/odd-oranges-appear.md new file mode 100644 index 0000000000..7627332a7f --- /dev/null +++ b/.changeset/odd-oranges-appear.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": minor +--- + +Use File or Blob type for binary types diff --git a/.changeset/soft-snakes-notice.md b/.changeset/soft-snakes-notice.md new file mode 100644 index 0000000000..ebfd3a3f54 --- /dev/null +++ b/.changeset/soft-snakes-notice.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": minor +--- + +Check value instanceof Blob when using isBlob diff --git a/package-lock.json b/package-lock.json index 1d61ed53b5..484b2f153d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@hey-api/openapi-ts", - "version": "0.31.0", + "version": "0.31.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@hey-api/openapi-ts", - "version": "0.31.0", + "version": "0.31.1", "license": "MIT", "dependencies": { "@apidevtools/json-schema-ref-parser": "11.5.4", diff --git a/src/templates/core/functions/isBlob.hbs b/src/templates/core/functions/isBlob.hbs index f08b580127..83dd5b892b 100644 --- a/src/templates/core/functions/isBlob.hbs +++ b/src/templates/core/functions/isBlob.hbs @@ -1,13 +1,3 @@ export const isBlob = (value: any): value is Blob => { - return ( - value !== null && - typeof value === 'object' && - typeof value.type === 'string' && - typeof value.stream === 'function' && - typeof value.arrayBuffer === 'function' && - typeof value.constructor === 'function' && - typeof value.constructor.name === 'string' && - /^(Blob|File)$/.test(value.constructor.name) && - /^(Blob|File)$/.test(value[Symbol.toStringTag]) - ); + return value instanceof Blob; }; diff --git a/src/templates/partials/base.hbs b/src/templates/partials/base.hbs index 5afa5801c9..c6602ad805 100644 --- a/src/templates/partials/base.hbs +++ b/src/templates/partials/base.hbs @@ -1,9 +1,5 @@ {{~#equals base 'binary'~}} -{{~#equals @root.$config.client 'fetch'}}Blob{{/equals~}} -{{~#equals @root.$config.client 'xhr'}}Blob{{/equals~}} -{{~#equals @root.$config.client 'axios'}}Blob{{/equals~}} -{{~#equals @root.$config.client 'angular'}}Blob{{/equals~}} -{{~#equals @root.$config.client 'node'}}Blob{{/equals~}} +Blob | File {{~else~}} {{~#useDateType @root.$config format~}} Date diff --git a/test/__snapshots__/v2/core/request.ts.snap b/test/__snapshots__/v2/core/request.ts.snap index b8c84b846b..bf88d79e96 100644 --- a/test/__snapshots__/v2/core/request.ts.snap +++ b/test/__snapshots__/v2/core/request.ts.snap @@ -14,17 +14,7 @@ export const isStringWithValue = (value: unknown): value is string => { }; export const isBlob = (value: any): value is Blob => { - return ( - value !== null && - typeof value === 'object' && - typeof value.type === 'string' && - typeof value.stream === 'function' && - typeof value.arrayBuffer === 'function' && - typeof value.constructor === 'function' && - typeof value.constructor.name === 'string' && - /^(Blob|File)$/.test(value.constructor.name) && - /^(Blob|File)$/.test(value[Symbol.toStringTag]) - ); + return value instanceof Blob; }; export const isFormData = (value: unknown): value is FormData => { diff --git a/test/__snapshots__/v2/models.ts.snap b/test/__snapshots__/v2/models.ts.snap index fbf6e0c80f..9559cc8621 100644 --- a/test/__snapshots__/v2/models.ts.snap +++ b/test/__snapshots__/v2/models.ts.snap @@ -54,7 +54,7 @@ export type NonAsciiStringæøåÆØÅöôêÊ字符串 = string; /** * This is a simple file */ -export type SimpleFile = Blob; +export type SimpleFile = Blob | File; /** * This is a simple reference @@ -313,7 +313,7 @@ export type ModelWithReference = { */ export type ModelWithArray = { prop?: Array; - propWithFile?: Array; + propWithFile?: Array; propWithNumber?: Array; }; diff --git a/test/__snapshots__/v3/core/request.ts.snap b/test/__snapshots__/v3/core/request.ts.snap index b8c84b846b..bf88d79e96 100644 --- a/test/__snapshots__/v3/core/request.ts.snap +++ b/test/__snapshots__/v3/core/request.ts.snap @@ -14,17 +14,7 @@ export const isStringWithValue = (value: unknown): value is string => { }; export const isBlob = (value: any): value is Blob => { - return ( - value !== null && - typeof value === 'object' && - typeof value.type === 'string' && - typeof value.stream === 'function' && - typeof value.arrayBuffer === 'function' && - typeof value.constructor === 'function' && - typeof value.constructor.name === 'string' && - /^(Blob|File)$/.test(value.constructor.name) && - /^(Blob|File)$/.test(value[Symbol.toStringTag]) - ); + return value instanceof Blob; }; export const isFormData = (value: unknown): value is FormData => { diff --git a/test/__snapshots__/v3/models.ts.snap b/test/__snapshots__/v3/models.ts.snap index 1be5f0237d..5444e22118 100644 --- a/test/__snapshots__/v3/models.ts.snap +++ b/test/__snapshots__/v3/models.ts.snap @@ -54,7 +54,7 @@ export type NonAsciiStringæøåÆØÅöôêÊ字符串 = string; /** * This is a simple file */ -export type SimpleFile = Blob; +export type SimpleFile = Blob | File; /** * This is a simple reference @@ -367,7 +367,7 @@ export type ModelWithReference = { */ export type ModelWithArrayReadOnlyAndWriteOnly = { prop?: Array; - propWithFile?: Array; + propWithFile?: Array; propWithNumber?: Array; }; @@ -376,7 +376,7 @@ export type ModelWithArrayReadOnlyAndWriteOnly = { */ export type ModelWithArray = { prop?: Array; - propWithFile?: Array; + propWithFile?: Array; propWithNumber?: Array; }; diff --git a/test/__snapshots__/v3/services/FileResponseService.ts.snap b/test/__snapshots__/v3/services/FileResponseService.ts.snap index ca34124543..15f8525076 100644 --- a/test/__snapshots__/v3/services/FileResponseService.ts.snap +++ b/test/__snapshots__/v3/services/FileResponseService.ts.snap @@ -11,7 +11,7 @@ export class FileResponseService { * @returns binary Success * @throws ApiError */ - public static fileResponse(data: TDataFileResponse): CancelablePromise { + public static fileResponse(data: TDataFileResponse): CancelablePromise { const { id } = data; return __request(OpenAPI, { method: 'GET', diff --git a/test/__snapshots__/v3/services/MultipartService.ts.snap b/test/__snapshots__/v3/services/MultipartService.ts.snap index 7fe7862c55..be7e460838 100644 --- a/test/__snapshots__/v3/services/MultipartService.ts.snap +++ b/test/__snapshots__/v3/services/MultipartService.ts.snap @@ -5,7 +5,7 @@ import { request as __request } from '../core/request'; export type TDataMultipartRequest = { formData?: { - content?: Blob; + content?: Blob | File; data?: ModelWithString | null; }; }; @@ -29,7 +29,7 @@ export class MultipartService { * @throws ApiError */ public static multipartResponse(): CancelablePromise<{ - file?: Blob; + file?: Blob | File; metadata?: { foo?: string; bar?: string; diff --git a/test/__snapshots__/v3/services/UploadService.ts.snap b/test/__snapshots__/v3/services/UploadService.ts.snap index f6da46b238..22847f40c8 100644 --- a/test/__snapshots__/v3/services/UploadService.ts.snap +++ b/test/__snapshots__/v3/services/UploadService.ts.snap @@ -6,7 +6,7 @@ export type TDataUploadFile = { /** * Supply a file reference for upload */ - file: Blob; + file: Blob | File; }; export class UploadService { diff --git a/test/__snapshots__/v3_angular/core/request.ts.snap b/test/__snapshots__/v3_angular/core/request.ts.snap index b7553394dd..b07e376716 100644 --- a/test/__snapshots__/v3_angular/core/request.ts.snap +++ b/test/__snapshots__/v3_angular/core/request.ts.snap @@ -18,17 +18,7 @@ export const isStringWithValue = (value: unknown): value is string => { }; export const isBlob = (value: any): value is Blob => { - return ( - value !== null && - typeof value === 'object' && - typeof value.type === 'string' && - typeof value.stream === 'function' && - typeof value.arrayBuffer === 'function' && - typeof value.constructor === 'function' && - typeof value.constructor.name === 'string' && - /^(Blob|File)$/.test(value.constructor.name) && - /^(Blob|File)$/.test(value[Symbol.toStringTag]) - ); + return value instanceof Blob; }; export const isFormData = (value: unknown): value is FormData => { diff --git a/test/__snapshots__/v3_angular/models.ts.snap b/test/__snapshots__/v3_angular/models.ts.snap index 14e52e1c78..ad411322fd 100644 --- a/test/__snapshots__/v3_angular/models.ts.snap +++ b/test/__snapshots__/v3_angular/models.ts.snap @@ -54,7 +54,7 @@ export type NonAsciiStringæøåÆØÅöôêÊ字符串 = string; /** * This is a simple file */ -export type SimpleFile = Blob; +export type SimpleFile = Blob | File; /** * This is a simple reference @@ -297,7 +297,7 @@ export type ModelWithReference = { */ export type ModelWithArrayReadOnlyAndWriteOnly = { prop?: Array; - propWithFile?: Array; + propWithFile?: Array; propWithNumber?: Array; }; @@ -306,7 +306,7 @@ export type ModelWithArrayReadOnlyAndWriteOnly = { */ export type ModelWithArray = { prop?: Array; - propWithFile?: Array; + propWithFile?: Array; propWithNumber?: Array; }; diff --git a/test/__snapshots__/v3_angular/services/FileResponseService.ts.snap b/test/__snapshots__/v3_angular/services/FileResponseService.ts.snap index c068d26f1a..477a576127 100644 --- a/test/__snapshots__/v3_angular/services/FileResponseService.ts.snap +++ b/test/__snapshots__/v3_angular/services/FileResponseService.ts.snap @@ -19,7 +19,7 @@ export class FileResponseService { * @returns binary Success * @throws ApiError */ - public fileResponse(data: TDataFileResponse): Observable { + public fileResponse(data: TDataFileResponse): Observable { const { id } = data; return __request(OpenAPI, this.http, { method: 'GET', diff --git a/test/__snapshots__/v3_angular/services/MultipartService.ts.snap b/test/__snapshots__/v3_angular/services/MultipartService.ts.snap index ec18241ffc..6fc5e3a1e5 100644 --- a/test/__snapshots__/v3_angular/services/MultipartService.ts.snap +++ b/test/__snapshots__/v3_angular/services/MultipartService.ts.snap @@ -7,7 +7,7 @@ import { request as __request } from '../core/request'; export type TDataMultipartRequest = { formData?: { - content?: Blob; + content?: Blob | File; data?: ModelWithString | null; }; }; @@ -36,7 +36,7 @@ export class MultipartService { * @throws ApiError */ public multipartResponse(): Observable<{ - file?: Blob; + file?: Blob | File; metadata?: { foo?: string; bar?: string; diff --git a/test/__snapshots__/v3_angular/services/UploadService.ts.snap b/test/__snapshots__/v3_angular/services/UploadService.ts.snap index aa86ccb9e9..6bdb2979e3 100644 --- a/test/__snapshots__/v3_angular/services/UploadService.ts.snap +++ b/test/__snapshots__/v3_angular/services/UploadService.ts.snap @@ -9,7 +9,7 @@ export type TDataUploadFile = { /** * Supply a file reference for upload */ - file: Blob; + file: Blob | File; }; @Injectable({ diff --git a/test/__snapshots__/v3_axios/core/request.ts.snap b/test/__snapshots__/v3_axios/core/request.ts.snap index 8a1ff67ee9..b183a405cb 100644 --- a/test/__snapshots__/v3_axios/core/request.ts.snap +++ b/test/__snapshots__/v3_axios/core/request.ts.snap @@ -18,17 +18,7 @@ export const isStringWithValue = (value: unknown): value is string => { }; export const isBlob = (value: any): value is Blob => { - return ( - value !== null && - typeof value === 'object' && - typeof value.type === 'string' && - typeof value.stream === 'function' && - typeof value.arrayBuffer === 'function' && - typeof value.constructor === 'function' && - typeof value.constructor.name === 'string' && - /^(Blob|File)$/.test(value.constructor.name) && - /^(Blob|File)$/.test(value[Symbol.toStringTag]) - ); + return value instanceof Blob; }; export const isFormData = (value: unknown): value is FormData => { diff --git a/test/__snapshots__/v3_client/core/request.ts.snap b/test/__snapshots__/v3_client/core/request.ts.snap index b8c84b846b..bf88d79e96 100644 --- a/test/__snapshots__/v3_client/core/request.ts.snap +++ b/test/__snapshots__/v3_client/core/request.ts.snap @@ -14,17 +14,7 @@ export const isStringWithValue = (value: unknown): value is string => { }; export const isBlob = (value: any): value is Blob => { - return ( - value !== null && - typeof value === 'object' && - typeof value.type === 'string' && - typeof value.stream === 'function' && - typeof value.arrayBuffer === 'function' && - typeof value.constructor === 'function' && - typeof value.constructor.name === 'string' && - /^(Blob|File)$/.test(value.constructor.name) && - /^(Blob|File)$/.test(value[Symbol.toStringTag]) - ); + return value instanceof Blob; }; export const isFormData = (value: unknown): value is FormData => { diff --git a/test/__snapshots__/v3_client/models.ts.snap b/test/__snapshots__/v3_client/models.ts.snap index 7cf4303789..e9246c7156 100644 --- a/test/__snapshots__/v3_client/models.ts.snap +++ b/test/__snapshots__/v3_client/models.ts.snap @@ -54,7 +54,7 @@ export type NonAsciiStringæøåÆØÅöôêÊ字符串 = string; /** * This is a simple file */ -export type SimpleFile = Blob; +export type SimpleFile = Blob | File; /** * This is a simple reference @@ -367,7 +367,7 @@ export type ModelWithReference = { */ export type ModelWithArrayReadOnlyAndWriteOnly = { prop?: Array; - propWithFile?: Array; + propWithFile?: Array; propWithNumber?: Array; }; @@ -376,7 +376,7 @@ export type ModelWithArrayReadOnlyAndWriteOnly = { */ export type ModelWithArray = { prop?: Array; - propWithFile?: Array; + propWithFile?: Array; propWithNumber?: Array; }; diff --git a/test/__snapshots__/v3_client/services/FileResponseService.ts.snap b/test/__snapshots__/v3_client/services/FileResponseService.ts.snap index 1ae60f65e1..896c7509a2 100644 --- a/test/__snapshots__/v3_client/services/FileResponseService.ts.snap +++ b/test/__snapshots__/v3_client/services/FileResponseService.ts.snap @@ -12,7 +12,7 @@ export class FileResponseService { * @returns binary Success * @throws ApiError */ - public fileResponse(data: TDataFileResponse): CancelablePromise { + public fileResponse(data: TDataFileResponse): CancelablePromise { const { id } = data; return this.httpRequest.request({ method: 'GET', diff --git a/test/__snapshots__/v3_client/services/MultipartService.ts.snap b/test/__snapshots__/v3_client/services/MultipartService.ts.snap index b149f0acb2..afbf9e1651 100644 --- a/test/__snapshots__/v3_client/services/MultipartService.ts.snap +++ b/test/__snapshots__/v3_client/services/MultipartService.ts.snap @@ -4,7 +4,7 @@ import type { BaseHttpRequest } from '../core/BaseHttpRequest'; export type TDataMultipartRequest = { formData?: { - content?: Blob; + content?: Blob | File; data?: ModelWithString | null; }; }; @@ -30,7 +30,7 @@ export class MultipartService { * @throws ApiError */ public multipartResponse(): CancelablePromise<{ - file?: Blob; + file?: Blob | File; metadata?: { foo?: string; bar?: string; diff --git a/test/__snapshots__/v3_client/services/UploadService.ts.snap b/test/__snapshots__/v3_client/services/UploadService.ts.snap index 3d49884abc..6fe43caa60 100644 --- a/test/__snapshots__/v3_client/services/UploadService.ts.snap +++ b/test/__snapshots__/v3_client/services/UploadService.ts.snap @@ -5,7 +5,7 @@ export type TDataUploadFile = { /** * Supply a file reference for upload */ - file: Blob; + file: Blob | File; }; export class UploadService { diff --git a/test/__snapshots__/v3_enums_typescript/core/request.ts.snap b/test/__snapshots__/v3_enums_typescript/core/request.ts.snap index b8c84b846b..bf88d79e96 100644 --- a/test/__snapshots__/v3_enums_typescript/core/request.ts.snap +++ b/test/__snapshots__/v3_enums_typescript/core/request.ts.snap @@ -14,17 +14,7 @@ export const isStringWithValue = (value: unknown): value is string => { }; export const isBlob = (value: any): value is Blob => { - return ( - value !== null && - typeof value === 'object' && - typeof value.type === 'string' && - typeof value.stream === 'function' && - typeof value.arrayBuffer === 'function' && - typeof value.constructor === 'function' && - typeof value.constructor.name === 'string' && - /^(Blob|File)$/.test(value.constructor.name) && - /^(Blob|File)$/.test(value[Symbol.toStringTag]) - ); + return value instanceof Blob; }; export const isFormData = (value: unknown): value is FormData => { diff --git a/test/__snapshots__/v3_enums_typescript/models.ts.snap b/test/__snapshots__/v3_enums_typescript/models.ts.snap index 058bb14c3f..8a3a8146a8 100644 --- a/test/__snapshots__/v3_enums_typescript/models.ts.snap +++ b/test/__snapshots__/v3_enums_typescript/models.ts.snap @@ -54,7 +54,7 @@ export type NonAsciiStringæøåÆØÅöôêÊ字符串 = string; /** * This is a simple file */ -export type SimpleFile = Blob; +export type SimpleFile = Blob | File; /** * This is a simple reference @@ -333,7 +333,7 @@ export type ModelWithReference = { */ export type ModelWithArrayReadOnlyAndWriteOnly = { prop?: Array; - propWithFile?: Array; + propWithFile?: Array; propWithNumber?: Array; }; @@ -342,7 +342,7 @@ export type ModelWithArrayReadOnlyAndWriteOnly = { */ export type ModelWithArray = { prop?: Array; - propWithFile?: Array; + propWithFile?: Array; propWithNumber?: Array; }; diff --git a/test/__snapshots__/v3_enums_typescript/services/FileResponseService.ts.snap b/test/__snapshots__/v3_enums_typescript/services/FileResponseService.ts.snap index ca34124543..15f8525076 100644 --- a/test/__snapshots__/v3_enums_typescript/services/FileResponseService.ts.snap +++ b/test/__snapshots__/v3_enums_typescript/services/FileResponseService.ts.snap @@ -11,7 +11,7 @@ export class FileResponseService { * @returns binary Success * @throws ApiError */ - public static fileResponse(data: TDataFileResponse): CancelablePromise { + public static fileResponse(data: TDataFileResponse): CancelablePromise { const { id } = data; return __request(OpenAPI, { method: 'GET', diff --git a/test/__snapshots__/v3_enums_typescript/services/MultipartService.ts.snap b/test/__snapshots__/v3_enums_typescript/services/MultipartService.ts.snap index 7fe7862c55..be7e460838 100644 --- a/test/__snapshots__/v3_enums_typescript/services/MultipartService.ts.snap +++ b/test/__snapshots__/v3_enums_typescript/services/MultipartService.ts.snap @@ -5,7 +5,7 @@ import { request as __request } from '../core/request'; export type TDataMultipartRequest = { formData?: { - content?: Blob; + content?: Blob | File; data?: ModelWithString | null; }; }; @@ -29,7 +29,7 @@ export class MultipartService { * @throws ApiError */ public static multipartResponse(): CancelablePromise<{ - file?: Blob; + file?: Blob | File; metadata?: { foo?: string; bar?: string; diff --git a/test/__snapshots__/v3_enums_typescript/services/UploadService.ts.snap b/test/__snapshots__/v3_enums_typescript/services/UploadService.ts.snap index f6da46b238..22847f40c8 100644 --- a/test/__snapshots__/v3_enums_typescript/services/UploadService.ts.snap +++ b/test/__snapshots__/v3_enums_typescript/services/UploadService.ts.snap @@ -6,7 +6,7 @@ export type TDataUploadFile = { /** * Supply a file reference for upload */ - file: Blob; + file: Blob | File; }; export class UploadService { diff --git a/test/__snapshots__/v3_node/core/request.ts.snap b/test/__snapshots__/v3_node/core/request.ts.snap index af13375581..327d937529 100644 --- a/test/__snapshots__/v3_node/core/request.ts.snap +++ b/test/__snapshots__/v3_node/core/request.ts.snap @@ -17,17 +17,7 @@ export const isStringWithValue = (value: unknown): value is string => { }; export const isBlob = (value: any): value is Blob => { - return ( - value !== null && - typeof value === 'object' && - typeof value.type === 'string' && - typeof value.stream === 'function' && - typeof value.arrayBuffer === 'function' && - typeof value.constructor === 'function' && - typeof value.constructor.name === 'string' && - /^(Blob|File)$/.test(value.constructor.name) && - /^(Blob|File)$/.test(value[Symbol.toStringTag]) - ); + return value instanceof Blob; }; export const isFormData = (value: unknown): value is FormData => { diff --git a/test/__snapshots__/v3_options/core/request.ts.snap b/test/__snapshots__/v3_options/core/request.ts.snap index b8c84b846b..bf88d79e96 100644 --- a/test/__snapshots__/v3_options/core/request.ts.snap +++ b/test/__snapshots__/v3_options/core/request.ts.snap @@ -14,17 +14,7 @@ export const isStringWithValue = (value: unknown): value is string => { }; export const isBlob = (value: any): value is Blob => { - return ( - value !== null && - typeof value === 'object' && - typeof value.type === 'string' && - typeof value.stream === 'function' && - typeof value.arrayBuffer === 'function' && - typeof value.constructor === 'function' && - typeof value.constructor.name === 'string' && - /^(Blob|File)$/.test(value.constructor.name) && - /^(Blob|File)$/.test(value[Symbol.toStringTag]) - ); + return value instanceof Blob; }; export const isFormData = (value: unknown): value is FormData => { diff --git a/test/__snapshots__/v3_xhr/core/request.ts.snap b/test/__snapshots__/v3_xhr/core/request.ts.snap index a65416e7cd..4bf6110b40 100644 --- a/test/__snapshots__/v3_xhr/core/request.ts.snap +++ b/test/__snapshots__/v3_xhr/core/request.ts.snap @@ -14,17 +14,7 @@ export const isStringWithValue = (value: unknown): value is string => { }; export const isBlob = (value: any): value is Blob => { - return ( - value !== null && - typeof value === 'object' && - typeof value.type === 'string' && - typeof value.stream === 'function' && - typeof value.arrayBuffer === 'function' && - typeof value.constructor === 'function' && - typeof value.constructor.name === 'string' && - /^(Blob|File)$/.test(value.constructor.name) && - /^(Blob|File)$/.test(value[Symbol.toStringTag]) - ); + return value instanceof Blob; }; export const isFormData = (value: unknown): value is FormData => {