Skip to content

Commit 43e382f

Browse files
committed
- Working on better File type handeling
1 parent 9ff09b6 commit 43e382f

28 files changed

+146
-61
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.5.0-alpha",
3+
"version": "0.5.0-beta",
44
"description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

src/openApi/v2/parser/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export enum PrimaryType {
2-
FILE = 'File',
2+
FILE = 'any', // File or Buffer?
33
OBJECT = 'any',
44
ARRAY = 'any',
55
BOOLEAN = 'boolean',

src/openApi/v2/parser/getMappedType.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getMappedType } from './getMappedType';
22

33
describe('getMappedType', () => {
44
it('should map types to the basics', () => {
5-
expect(getMappedType('File')).toEqual('File');
5+
expect(getMappedType('File')).toEqual('any');
66
expect(getMappedType('String')).toEqual('string');
77
expect(getMappedType('date')).toEqual('string');
88
expect(getMappedType('date-time')).toEqual('string');

src/openApi/v3/parser/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export enum PrimaryType {
2-
FILE = 'File',
2+
FILE = 'any', // File or Buffer?
33
OBJECT = 'any',
44
ARRAY = 'any[]',
55
BOOLEAN = 'boolean',

src/openApi/v3/parser/getMappedType.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getMappedType } from './getMappedType';
22

33
describe('getMappedType', () => {
44
it('should map types to the basics', () => {
5-
expect(getMappedType('File')).toEqual('File');
5+
expect(getMappedType('File')).toEqual('any');
66
expect(getMappedType('String')).toEqual('string');
77
expect(getMappedType('date')).toEqual('string');
88
expect(getMappedType('date-time')).toEqual('string');

src/templates/core/functions/getHeaders.hbs renamed to src/templates/core/fetch/getHeaders.hbs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ function getHeaders(options: ApiRequestOptions): Headers {
99
}
1010

1111
if (options.body) {
12-
if (options.body instanceof Blob) {
13-
if (options.body.type) {
14-
headers.append('Content-Type', options.body.type);
15-
}
16-
} else if (typeof options.body === 'string') {
12+
if (isBlob(options.body)) {
13+
headers.append('Content-Type', options.body.type || 'application/octet-stream');
14+
} else if (isString(options.body)) {
1715
headers.append('Content-Type', 'text/plain');
1816
} else {
1917
headers.append('Content-Type', 'application/json');

src/templates/core/fetch/getRequestBody.hbs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
33
return getFormData(options.formData);
44
}
55
if (options.body) {
6-
if (options.body instanceof Blob) {
7-
return options.body;
8-
} else if (typeof options.body === 'string') {
6+
if (isString(options.body) || isBlob(options.body)) {
97
return options.body;
108
} else {
119
return JSON.stringify(options.body);

src/templates/core/fetch/getResponseHeader.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function getResponseHeader(response: Response, responseHeader?: string): string | null {
22
if (responseHeader) {
33
const content = response.headers.get(responseHeader);
4-
if (typeof content === 'string') {
4+
if (isString(content)) {
55
return content;
66
}
77
}

src/templates/core/fetch/request.hbs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import { OpenAPI } from './OpenAPI';
88
{{>functions/isDefined}}
99

1010

11+
{{>functions/isString}}
12+
13+
14+
{{>functions/isBlob}}
15+
16+
1117
{{>functions/getQueryString}}
1218

1319

@@ -17,7 +23,7 @@ import { OpenAPI } from './OpenAPI';
1723
{{>functions/getFormData}}
1824

1925

20-
{{>functions/getHeaders}}
26+
{{>fetch/getHeaders}}
2127

2228

2329
{{>fetch/getRequestBody}}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function isBinary(value: any): value is Buffer | ArrayBuffer | ArrayBufferView {
2+
const isBuffer = Buffer.isBuffer(value);
3+
const isArrayBuffer = types.isArrayBuffer(value);
4+
const isArrayBufferView = types.isArrayBufferView(value);
5+
return isBuffer || isArrayBuffer || isArrayBufferView;
6+
}

0 commit comments

Comments
 (0)