mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Added default parser in case of missing content header
This commit is contained in:
parent
df4538df1f
commit
769529bfbd
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "openapi-typescript-codegen",
|
||||
"version": "0.1.11",
|
||||
"version": "0.1.12",
|
||||
"description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.",
|
||||
"author": "Ferdi Koomen",
|
||||
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",
|
||||
|
||||
@ -5,6 +5,32 @@
|
||||
|
||||
import { Result } from './Result';
|
||||
|
||||
/**
|
||||
* Try to parse the content for any response status code.
|
||||
* We check the "Content-Type" header to see if we need to parse the
|
||||
* content as json or as plain text.
|
||||
* @param response Response object from fetch
|
||||
*/
|
||||
async function parseBody(response: Response): Promise<any> {
|
||||
|
||||
try {
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
return await response.json();
|
||||
|
||||
default:
|
||||
return await response.text();
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request content using the new Fetch API. This is the default API that is used and
|
||||
* is create for all JSON, XML and text objects. However it is limited to UTF-8.
|
||||
@ -18,33 +44,11 @@ export async function requestUsingFetch(url: string, request: Readonly<RequestIn
|
||||
const response = await fetch(url, request);
|
||||
|
||||
// Create result object.
|
||||
const result: Result = {
|
||||
return {
|
||||
url,
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
body: null,
|
||||
body: await parseBody(response),
|
||||
};
|
||||
|
||||
// Try to parse the content for any response status code.
|
||||
// We check the "Content-Type" header to see if we need to parse the
|
||||
// content as json or as plain text.
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
result.body = await response.json();
|
||||
break;
|
||||
|
||||
case 'text/plain':
|
||||
case 'text/xml':
|
||||
case 'text/xml; charset=utf-8':
|
||||
case 'text/xml; charset=utf-16':
|
||||
result.body = await response.text();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -3,8 +3,34 @@
|
||||
/* eslint-disable */
|
||||
/* prettier-ignore */
|
||||
|
||||
import { Result } from './Result';
|
||||
import { isSuccess } from './isSuccess';
|
||||
import { Result } from './Result';
|
||||
|
||||
/**
|
||||
* Try to parse the content for any response status code.
|
||||
* We check the "Content-Type" header to see if we need to parse the
|
||||
* content as json or as plain text.
|
||||
* @param xhr XHR request object
|
||||
*/
|
||||
function parseBody(xhr: XMLHttpRequest): any {
|
||||
try {
|
||||
const contentType = xhr.getResponseHeader('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
return JSON.parse(xhr.responseText);
|
||||
|
||||
default:
|
||||
return xhr.responseText;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request content using the new legacy XMLHttpRequest API. This method is useful
|
||||
@ -38,28 +64,9 @@ export async function requestUsingXHR(url: string, request: Readonly<RequestInit
|
||||
ok: isSuccess(xhr.status),
|
||||
status: xhr.status,
|
||||
statusText: xhr.statusText,
|
||||
body: null,
|
||||
body: parseBody(xhr),
|
||||
};
|
||||
|
||||
// Try to parse the content for any response status code.
|
||||
// We check the "Content-Type" header to see if we need to parse the
|
||||
// content as json or as plain text.
|
||||
const contentType = xhr.getResponseHeader('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
result.body = JSON.parse(xhr.responseText);
|
||||
break;
|
||||
|
||||
case 'text/plain':
|
||||
case 'text/xml':
|
||||
case 'text/xml; charset=utf-8':
|
||||
case 'text/xml; charset=utf-16':
|
||||
result.body = xhr.responseText;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Done!
|
||||
resolve(result);
|
||||
|
||||
@ -280,6 +280,32 @@ exports[`generation v2 file(./test/result/v2/core/requestUsingFetch.ts): ./test/
|
||||
|
||||
import { Result } from './Result';
|
||||
|
||||
/**
|
||||
* Try to parse the content for any response status code.
|
||||
* We check the \\"Content-Type\\" header to see if we need to parse the
|
||||
* content as json or as plain text.
|
||||
* @param response Response object from fetch
|
||||
*/
|
||||
async function parseBody(response: Response): Promise<any> {
|
||||
|
||||
try {
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
return await response.json();
|
||||
|
||||
default:
|
||||
return await response.text();
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request content using the new Fetch API. This is the default API that is used and
|
||||
* is create for all JSON, XML and text objects. However it is limited to UTF-8.
|
||||
@ -293,35 +319,13 @@ export async function requestUsingFetch(url: string, request: Readonly<RequestIn
|
||||
const response = await fetch(url, request);
|
||||
|
||||
// Create result object.
|
||||
const result: Result = {
|
||||
return {
|
||||
url,
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
body: null,
|
||||
body: await parseBody(response),
|
||||
};
|
||||
|
||||
// Try to parse the content for any response status code.
|
||||
// We check the \\"Content-Type\\" header to see if we need to parse the
|
||||
// content as json or as plain text.
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
result.body = await response.json();
|
||||
break;
|
||||
|
||||
case 'text/plain':
|
||||
case 'text/xml':
|
||||
case 'text/xml; charset=utf-8':
|
||||
case 'text/xml; charset=utf-16':
|
||||
result.body = await response.text();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
"
|
||||
`;
|
||||
@ -332,8 +336,34 @@ exports[`generation v2 file(./test/result/v2/core/requestUsingXHR.ts): ./test/re
|
||||
/* eslint-disable */
|
||||
/* prettier-ignore */
|
||||
|
||||
import { Result } from './Result';
|
||||
import { isSuccess } from './isSuccess';
|
||||
import { Result } from './Result';
|
||||
|
||||
/**
|
||||
* Try to parse the content for any response status code.
|
||||
* We check the \\"Content-Type\\" header to see if we need to parse the
|
||||
* content as json or as plain text.
|
||||
* @param xhr XHR request object
|
||||
*/
|
||||
function parseBody(xhr: XMLHttpRequest): any {
|
||||
try {
|
||||
const contentType = xhr.getResponseHeader('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
return JSON.parse(xhr.responseText);
|
||||
|
||||
default:
|
||||
return xhr.responseText;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request content using the new legacy XMLHttpRequest API. This method is useful
|
||||
@ -367,28 +397,9 @@ export async function requestUsingXHR(url: string, request: Readonly<RequestInit
|
||||
ok: isSuccess(xhr.status),
|
||||
status: xhr.status,
|
||||
statusText: xhr.statusText,
|
||||
body: null,
|
||||
body: parseBody(xhr),
|
||||
};
|
||||
|
||||
// Try to parse the content for any response status code.
|
||||
// We check the \\"Content-Type\\" header to see if we need to parse the
|
||||
// content as json or as plain text.
|
||||
const contentType = xhr.getResponseHeader('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
result.body = JSON.parse(xhr.responseText);
|
||||
break;
|
||||
|
||||
case 'text/plain':
|
||||
case 'text/xml':
|
||||
case 'text/xml; charset=utf-8':
|
||||
case 'text/xml; charset=utf-16':
|
||||
result.body = xhr.responseText;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Done!
|
||||
resolve(result);
|
||||
@ -2431,6 +2442,32 @@ exports[`generation v3 file(./test/result/v3/core/requestUsingFetch.ts): ./test/
|
||||
|
||||
import { Result } from './Result';
|
||||
|
||||
/**
|
||||
* Try to parse the content for any response status code.
|
||||
* We check the \\"Content-Type\\" header to see if we need to parse the
|
||||
* content as json or as plain text.
|
||||
* @param response Response object from fetch
|
||||
*/
|
||||
async function parseBody(response: Response): Promise<any> {
|
||||
|
||||
try {
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
return await response.json();
|
||||
|
||||
default:
|
||||
return await response.text();
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request content using the new Fetch API. This is the default API that is used and
|
||||
* is create for all JSON, XML and text objects. However it is limited to UTF-8.
|
||||
@ -2444,35 +2481,13 @@ export async function requestUsingFetch(url: string, request: Readonly<RequestIn
|
||||
const response = await fetch(url, request);
|
||||
|
||||
// Create result object.
|
||||
const result: Result = {
|
||||
return {
|
||||
url,
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
body: null,
|
||||
body: await parseBody(response),
|
||||
};
|
||||
|
||||
// Try to parse the content for any response status code.
|
||||
// We check the \\"Content-Type\\" header to see if we need to parse the
|
||||
// content as json or as plain text.
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
result.body = await response.json();
|
||||
break;
|
||||
|
||||
case 'text/plain':
|
||||
case 'text/xml':
|
||||
case 'text/xml; charset=utf-8':
|
||||
case 'text/xml; charset=utf-16':
|
||||
result.body = await response.text();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
"
|
||||
`;
|
||||
@ -2483,8 +2498,34 @@ exports[`generation v3 file(./test/result/v3/core/requestUsingXHR.ts): ./test/re
|
||||
/* eslint-disable */
|
||||
/* prettier-ignore */
|
||||
|
||||
import { Result } from './Result';
|
||||
import { isSuccess } from './isSuccess';
|
||||
import { Result } from './Result';
|
||||
|
||||
/**
|
||||
* Try to parse the content for any response status code.
|
||||
* We check the \\"Content-Type\\" header to see if we need to parse the
|
||||
* content as json or as plain text.
|
||||
* @param xhr XHR request object
|
||||
*/
|
||||
function parseBody(xhr: XMLHttpRequest): any {
|
||||
try {
|
||||
const contentType = xhr.getResponseHeader('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
return JSON.parse(xhr.responseText);
|
||||
|
||||
default:
|
||||
return xhr.responseText;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request content using the new legacy XMLHttpRequest API. This method is useful
|
||||
@ -2518,28 +2559,9 @@ export async function requestUsingXHR(url: string, request: Readonly<RequestInit
|
||||
ok: isSuccess(xhr.status),
|
||||
status: xhr.status,
|
||||
statusText: xhr.statusText,
|
||||
body: null,
|
||||
body: parseBody(xhr),
|
||||
};
|
||||
|
||||
// Try to parse the content for any response status code.
|
||||
// We check the \\"Content-Type\\" header to see if we need to parse the
|
||||
// content as json or as plain text.
|
||||
const contentType = xhr.getResponseHeader('Content-Type');
|
||||
if (contentType) {
|
||||
switch (contentType.toLowerCase()) {
|
||||
case 'application/json':
|
||||
case 'application/json; charset=utf-8':
|
||||
result.body = JSON.parse(xhr.responseText);
|
||||
break;
|
||||
|
||||
case 'text/plain':
|
||||
case 'text/xml':
|
||||
case 'text/xml; charset=utf-8':
|
||||
case 'text/xml; charset=utf-16':
|
||||
result.body = xhr.responseText;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Done!
|
||||
resolve(result);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user