这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/odd-oranges-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": minor
---

Use File or Blob type for binary types
5 changes: 5 additions & 0 deletions .changeset/soft-snakes-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": minor
---

Check value instanceof Blob when using isBlob
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 1 addition & 11 deletions src/templates/core/functions/isBlob.hbs
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible to inline this call wherever it's used instead of having a separate function? It's so straightforward now that we don't need a wrapper function around it...

};
6 changes: 1 addition & 5 deletions src/templates/partials/base.hbs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 1 addition & 11 deletions test/__snapshots__/v2/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/v2/models.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -313,7 +313,7 @@ export type ModelWithReference = {
*/
export type ModelWithArray = {
prop?: Array<ModelWithString>;
propWithFile?: Array<Blob>;
propWithFile?: Array<Blob | File>;
propWithNumber?: Array<number>;
};

Expand Down
12 changes: 1 addition & 11 deletions test/__snapshots__/v3/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/v3/models.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -367,7 +367,7 @@ export type ModelWithReference = {
*/
export type ModelWithArrayReadOnlyAndWriteOnly = {
prop?: Array<ModelWithReadOnlyAndWriteOnly>;
propWithFile?: Array<Blob>;
propWithFile?: Array<Blob | File>;
propWithNumber?: Array<number>;
};

Expand All @@ -376,7 +376,7 @@ export type ModelWithArrayReadOnlyAndWriteOnly = {
*/
export type ModelWithArray = {
prop?: Array<ModelWithString>;
propWithFile?: Array<Blob>;
propWithFile?: Array<Blob | File>;
propWithNumber?: Array<number>;
};

Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/v3/services/FileResponseService.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class FileResponseService {
* @returns binary Success
* @throws ApiError
*/
public static fileResponse(data: TDataFileResponse): CancelablePromise<Blob> {
public static fileResponse(data: TDataFileResponse): CancelablePromise<Blob | File> {
const { id } = data;
return __request(OpenAPI, {
method: 'GET',
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/v3/services/MultipartService.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { request as __request } from '../core/request';

export type TDataMultipartRequest = {
formData?: {
content?: Blob;
content?: Blob | File;
data?: ModelWithString | null;
};
};
Expand All @@ -29,7 +29,7 @@ export class MultipartService {
* @throws ApiError
*/
public static multipartResponse(): CancelablePromise<{
file?: Blob;
file?: Blob | File;
metadata?: {
foo?: string;
bar?: string;
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/v3/services/UploadService.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type TDataUploadFile = {
/**
* Supply a file reference for upload
*/
file: Blob;
file: Blob | File;
};

export class UploadService {
Expand Down
12 changes: 1 addition & 11 deletions test/__snapshots__/v3_angular/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/v3_angular/models.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -297,7 +297,7 @@ export type ModelWithReference = {
*/
export type ModelWithArrayReadOnlyAndWriteOnly = {
prop?: Array<ModelWithReadOnlyAndWriteOnly>;
propWithFile?: Array<Blob>;
propWithFile?: Array<Blob | File>;
propWithNumber?: Array<number>;
};

Expand All @@ -306,7 +306,7 @@ export type ModelWithArrayReadOnlyAndWriteOnly = {
*/
export type ModelWithArray = {
prop?: Array<ModelWithString>;
propWithFile?: Array<Blob>;
propWithFile?: Array<Blob | File>;
propWithNumber?: Array<number>;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class FileResponseService {
* @returns binary Success
* @throws ApiError
*/
public fileResponse(data: TDataFileResponse): Observable<Blob> {
public fileResponse(data: TDataFileResponse): Observable<Blob | File> {
const { id } = data;
return __request(OpenAPI, this.http, {
method: 'GET',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { request as __request } from '../core/request';

export type TDataMultipartRequest = {
formData?: {
content?: Blob;
content?: Blob | File;
data?: ModelWithString | null;
};
};
Expand Down Expand Up @@ -36,7 +36,7 @@ export class MultipartService {
* @throws ApiError
*/
public multipartResponse(): Observable<{
file?: Blob;
file?: Blob | File;
metadata?: {
foo?: string;
bar?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type TDataUploadFile = {
/**
* Supply a file reference for upload
*/
file: Blob;
file: Blob | File;
};

@Injectable({
Expand Down
12 changes: 1 addition & 11 deletions test/__snapshots__/v3_axios/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
12 changes: 1 addition & 11 deletions test/__snapshots__/v3_client/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/v3_client/models.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -367,7 +367,7 @@ export type ModelWithReference = {
*/
export type ModelWithArrayReadOnlyAndWriteOnly = {
prop?: Array<ModelWithReadOnlyAndWriteOnly>;
propWithFile?: Array<Blob>;
propWithFile?: Array<Blob | File>;
propWithNumber?: Array<number>;
};

Expand All @@ -376,7 +376,7 @@ export type ModelWithArrayReadOnlyAndWriteOnly = {
*/
export type ModelWithArray = {
prop?: Array<ModelWithString>;
propWithFile?: Array<Blob>;
propWithFile?: Array<Blob | File>;
propWithNumber?: Array<number>;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class FileResponseService {
* @returns binary Success
* @throws ApiError
*/
public fileResponse(data: TDataFileResponse): CancelablePromise<Blob> {
public fileResponse(data: TDataFileResponse): CancelablePromise<Blob | File> {
const { id } = data;
return this.httpRequest.request({
method: 'GET',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { BaseHttpRequest } from '../core/BaseHttpRequest';

export type TDataMultipartRequest = {
formData?: {
content?: Blob;
content?: Blob | File;
data?: ModelWithString | null;
};
};
Expand All @@ -30,7 +30,7 @@ export class MultipartService {
* @throws ApiError
*/
public multipartResponse(): CancelablePromise<{
file?: Blob;
file?: Blob | File;
metadata?: {
foo?: string;
bar?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type TDataUploadFile = {
/**
* Supply a file reference for upload
*/
file: Blob;
file: Blob | File;
};

export class UploadService {
Expand Down
12 changes: 1 addition & 11 deletions test/__snapshots__/v3_enums_typescript/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Loading