- Added support for node-fetch 3.0.0 (breaking change)

This commit is contained in:
Ferdi Koomen 2021-09-27 22:40:53 +02:00
parent 17d3c74afa
commit 75c335a24a
7 changed files with 29 additions and 33 deletions

View File

@ -1,6 +1,6 @@
{
"name": "openapi-typescript-codegen",
"version": "0.9.3",
"version": "0.10.0",
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
"author": "Ferdi Koomen",
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

View File

@ -29,7 +29,7 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
if (options.body) {
if (options.mediaType) {
headers.append('Content-Type', options.mediaType);
} else if (isBinary(options.body)) {
} else if (isBlob(options.body)) {
headers.append('Content-Type', 'application/octet-stream');
} else if (isString(options.body)) {
headers.append('Content-Type', 'text/plain');

View File

@ -5,7 +5,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
if (options.body) {
if (options.mediaType?.includes('/json')) {
return JSON.stringify(options.body)
} else if (isString(options.body) || isBinary(options.body)) {
} else if (isString(options.body) || isBlob(options.body)) {
return options.body;
} else {
return JSON.stringify(options.body);

View File

@ -18,7 +18,7 @@ import { OpenAPI } from './OpenAPI';
{{>functions/isStringWithValue}}
{{>functions/isBinary}}
{{>functions/isBlob}}
{{>functions/getQueryString}}

View File

@ -1,7 +1,7 @@
{{~#equals base 'File'~}}
{{~#equals @root.httpClient 'fetch'}}Blob{{/equals~}}
{{~#equals @root.httpClient 'xhr'}}Blob{{/equals~}}
{{~#equals @root.httpClient 'node'}}Buffer | ArrayBuffer | ArrayBufferView{{/equals~}}
{{~#equals @root.httpClient 'node'}}Blob{{/equals~}}
{{~else~}}
{{{base}}}
{{~/equals~}}

View File

@ -163,22 +163,20 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
const token = await resolve(options, OpenAPI.TOKEN);
const username = await resolve(options, OpenAPI.USERNAME);
const password = await resolve(options, OpenAPI.PASSWORD);
const defaultHeaders = await resolve(options, OpenAPI.HEADERS);
const additionalHeaders = await resolve(options, OpenAPI.HEADERS);
const filteredHeaders = Object.entries({
const defaultHeaders = Object.entries({
Accept: 'application/json',
...defaultHeaders,
...additionalHeaders,
...options.headers,
}).reduce((acc, [headerKey, headerValue]) => {
if (typeof headerValue !== 'undefined') {
return {
...acc,
[headerKey]: headerValue,
};
}
return acc;
}, {});
const headers = new Headers(filteredHeaders);
})
.filter(([key, value]) => isDefined(value))
.reduce((headers, [key, value]) => ({
...headers,
[key]: value,
}), {});
const headers = new Headers(defaultHeaders);
if (isStringWithValue(token)) {
headers.append('Authorization', \`Bearer \${token}\`);
@ -2527,22 +2525,20 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
const token = await resolve(options, OpenAPI.TOKEN);
const username = await resolve(options, OpenAPI.USERNAME);
const password = await resolve(options, OpenAPI.PASSWORD);
const defaultHeaders = await resolve(options, OpenAPI.HEADERS);
const additionalHeaders = await resolve(options, OpenAPI.HEADERS);
const filteredHeaders = Object.entries({
const defaultHeaders = Object.entries({
Accept: 'application/json',
...defaultHeaders,
...additionalHeaders,
...options.headers,
}).reduce((acc, [headerKey, headerValue]) => {
if (typeof headerValue !== 'undefined') {
return {
...acc,
[headerKey]: headerValue,
};
}
return acc;
}, {});
const headers = new Headers(filteredHeaders);
})
.filter(([key, value]) => isDefined(value))
.reduce((headers, [key, value]) => ({
...headers,
[key]: value,
}), {});
const headers = new Headers(defaultHeaders);
if (isStringWithValue(token)) {
headers.append('Authorization', \`Bearer \${token}\`);

View File

@ -13,7 +13,7 @@ async function generateV2() {
exportSchemas: true,
exportModels: true,
exportServices: true,
request: './test/custom/request.ts',
// request: './test/custom/request.ts',
});
}
@ -28,7 +28,7 @@ async function generateV3() {
exportSchemas: true,
exportModels: true,
exportServices: true,
request: './test/custom/request.ts',
// request: './test/custom/request.ts',
});
}