- Fixed test snapshots

This commit is contained in:
Ferdi Koomen 2020-04-27 01:27:28 +02:00
parent 1859e60416
commit cdc933b803

View File

@ -96,6 +96,7 @@ export interface RequestOptions {
query?: { [key: string]: any };
formData?: { [key: string]: any };
body?: any;
responseHeader?: string;
}
"
`;
@ -262,9 +263,9 @@ export async function request(options: Readonly<RequestOptions>): Promise<Result
try {
switch (OpenAPI.CLIENT) {
case 'xhr':
return await requestUsingXHR(url, request);
return await requestUsingXHR(url, request, options.responseHeader);
default:
return await requestUsingFetch(url, request);
return await requestUsingFetch(url, request, options.responseHeader);
}
} catch (error) {
return {
@ -312,25 +313,45 @@ async function parseBody(response: Response): Promise<any> {
return null;
}
/**
* Fetch the response header (if specified)
* @param response Response object from fetch
* @param responseHeader The name of the header to fetch
*/
function parseHeader(response: Response, responseHeader?: string): string | null {
if (responseHeader) {
const content = response.headers.get(responseHeader);
if (typeof content === 'string') {
return content;
}
}
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.
* This is a problem for some of the Docs content, since that requires UTF-16!
* @param url The url to request.
* @param request The request object, containing method, headers, body, etc.
* @param responseHeader The header we want to parse.
*/
export async function requestUsingFetch(url: string, request: Readonly<RequestInit>): Promise<Result> {
export async function requestUsingFetch(url: string, request: Readonly<RequestInit>, responseHeader?: string): Promise<Result> {
// Fetch response using fetch API.
const response = await fetch(url, request);
// Get content of response header or response body
const contentHeader = parseHeader(response, responseHeader);
const contentBody = await parseBody(response);
// Create result object.
return {
url,
ok: response.ok,
status: response.status,
statusText: response.statusText,
body: await parseBody(response),
body: contentHeader || contentBody,
};
}
"
@ -370,6 +391,21 @@ function parseBody(xhr: XMLHttpRequest): any {
return null;
}
/**
* Fetch the response header (if specified)
* @param xhr XHR request object
* @param responseHeader The name of the header to fetch
*/
function parseHeader(xhr: XMLHttpRequest, responseHeader?: string): string | null {
if (responseHeader) {
const content = xhr.getResponseHeader(responseHeader);
if (typeof content === 'string') {
return content;
}
}
return null;
}
/**
* Request content using the new legacy XMLHttpRequest API. This method is useful
* when we want to request UTF-16 content, since it natively supports loading UTF-16.
@ -377,8 +413,9 @@ function parseBody(xhr: XMLHttpRequest): any {
* content using JavaScript... And that is very very slow.
* @param url The url to request.
* @param request The request object, containing method, headers, body, etc.
* @param responseHeader The header we want to parse.
*/
export async function requestUsingXHR(url: string, request: Readonly<RequestInit>): Promise<Result> {
export async function requestUsingXHR(url: string, request: Readonly<RequestInit>, responseHeader?: string): Promise<Result> {
return new Promise(resolve => {
const xhr = new XMLHttpRequest();
@ -396,16 +433,19 @@ export async function requestUsingXHR(url: string, request: Readonly<RequestInit
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
// Get content of response header or response body
const contentHeader = parseHeader(xhr, responseHeader);
const contentBody = parseBody(xhr);
// Create result object.
const result: Result = {
url,
ok: isSuccess(xhr.status),
status: xhr.status,
statusText: xhr.statusText,
body: parseBody(xhr),
body: contentHeader || contentBody,
};
// Done!
resolve(result);
}
@ -2090,14 +2130,15 @@ import { OpenAPI } from '../core/OpenAPI';
export class HeaderService {
/**
* @result any Successful response
* @result string Successful response
* @throws ApiError
*/
public static async callWithResultFromHeader(): Promise<any> {
public static async callWithResultFromHeader(): Promise<string> {
const result = await __request({
method: 'post',
path: \`/api/v\${OpenAPI.VERSION}/header\`,
responseHeader: 'operation-location',
});
if (!result.ok) {
@ -2587,6 +2628,7 @@ export interface RequestOptions {
query?: { [key: string]: any };
formData?: { [key: string]: any };
body?: any;
responseHeader?: string;
}
"
`;
@ -2753,9 +2795,9 @@ export async function request(options: Readonly<RequestOptions>): Promise<Result
try {
switch (OpenAPI.CLIENT) {
case 'xhr':
return await requestUsingXHR(url, request);
return await requestUsingXHR(url, request, options.responseHeader);
default:
return await requestUsingFetch(url, request);
return await requestUsingFetch(url, request, options.responseHeader);
}
} catch (error) {
return {
@ -2803,25 +2845,45 @@ async function parseBody(response: Response): Promise<any> {
return null;
}
/**
* Fetch the response header (if specified)
* @param response Response object from fetch
* @param responseHeader The name of the header to fetch
*/
function parseHeader(response: Response, responseHeader?: string): string | null {
if (responseHeader) {
const content = response.headers.get(responseHeader);
if (typeof content === 'string') {
return content;
}
}
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.
* This is a problem for some of the Docs content, since that requires UTF-16!
* @param url The url to request.
* @param request The request object, containing method, headers, body, etc.
* @param responseHeader The header we want to parse.
*/
export async function requestUsingFetch(url: string, request: Readonly<RequestInit>): Promise<Result> {
export async function requestUsingFetch(url: string, request: Readonly<RequestInit>, responseHeader?: string): Promise<Result> {
// Fetch response using fetch API.
const response = await fetch(url, request);
// Get content of response header or response body
const contentHeader = parseHeader(response, responseHeader);
const contentBody = await parseBody(response);
// Create result object.
return {
url,
ok: response.ok,
status: response.status,
statusText: response.statusText,
body: await parseBody(response),
body: contentHeader || contentBody,
};
}
"
@ -2861,6 +2923,21 @@ function parseBody(xhr: XMLHttpRequest): any {
return null;
}
/**
* Fetch the response header (if specified)
* @param xhr XHR request object
* @param responseHeader The name of the header to fetch
*/
function parseHeader(xhr: XMLHttpRequest, responseHeader?: string): string | null {
if (responseHeader) {
const content = xhr.getResponseHeader(responseHeader);
if (typeof content === 'string') {
return content;
}
}
return null;
}
/**
* Request content using the new legacy XMLHttpRequest API. This method is useful
* when we want to request UTF-16 content, since it natively supports loading UTF-16.
@ -2868,8 +2945,9 @@ function parseBody(xhr: XMLHttpRequest): any {
* content using JavaScript... And that is very very slow.
* @param url The url to request.
* @param request The request object, containing method, headers, body, etc.
* @param responseHeader The header we want to parse.
*/
export async function requestUsingXHR(url: string, request: Readonly<RequestInit>): Promise<Result> {
export async function requestUsingXHR(url: string, request: Readonly<RequestInit>, responseHeader?: string): Promise<Result> {
return new Promise(resolve => {
const xhr = new XMLHttpRequest();
@ -2887,16 +2965,19 @@ export async function requestUsingXHR(url: string, request: Readonly<RequestInit
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
// Get content of response header or response body
const contentHeader = parseHeader(xhr, responseHeader);
const contentBody = parseBody(xhr);
// Create result object.
const result: Result = {
url,
ok: isSuccess(xhr.status),
status: xhr.status,
statusText: xhr.statusText,
body: parseBody(xhr),
body: contentHeader || contentBody,
};
// Done!
resolve(result);
}
@ -4710,14 +4791,15 @@ import { OpenAPI } from '../core/OpenAPI';
export class HeaderService {
/**
* @result any Successful response
* @result string Successful response
* @throws ApiError
*/
public static async callWithResultFromHeader(): Promise<any> {
public static async callWithResultFromHeader(): Promise<string> {
const result = await __request({
method: 'post',
path: \`/api/v\${OpenAPI.VERSION}/header\`,
responseHeader: 'operation-location',
});
if (!result.ok) {