[Typescript] Improve exception for unknown responses (#10361)

* Clean up tests

* Change pets to async-await

* Use different pets for each test

* Workaround multiple petstore server instances

* Add test for unknown error response code

* Remove call duplication

Running the petstore server locally fixes all issues.

* Make body of unexpected error responses available

* Regenerate all consolidated typescript samples

* Remove json heuristic for unknown error responses
This commit is contained in:
Bodo Graumann 2021-09-24 23:53:37 +02:00 committed by GitHub
parent 476a82cb75
commit be3bd2e6c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 2465 additions and 490 deletions

View File

@ -200,7 +200,7 @@ export class {{classname}}ResponseProcessor {
return body;
{{/is2xx}}
{{^is2xx}}
throw new ApiException<{{{dataType}}}>({{code}}, body);
throw new ApiException<{{{dataType}}}>({{code}}, "{{message}}", body);
{{/is2xx}}
{{/dataType}}
{{^dataType}}
@ -208,7 +208,7 @@ export class {{classname}}ResponseProcessor {
return;
{{/is2xx}}
{{^is2xx}}
throw new ApiException<string>(response.httpStatusCode, "{{message}}");
throw new ApiException<undefined>(response.httpStatusCode, "{{message}}", undefined);
{{/is2xx}}
{{/dataType}}
}
@ -233,8 +233,7 @@ export class {{classname}}ResponseProcessor {
{{/returnType}}
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | {{{fileContentDataType}}} | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
{{/operation}}

View File

@ -8,7 +8,7 @@
*
*/
export class ApiException<T> extends Error {
public constructor(public code: number, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + JSON.stringify(body))
public constructor(public code: number, message: string, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + message + "\nBody: " + JSON.stringify(body))
}
}

View File

@ -287,6 +287,22 @@ export class ResponseContext {
{{/node}}
{{/platforms}}
}
/**
* Use a heuristic to get a body of unknown data structure.
* Return as string if possible, otherwise as binary.
*/
public getBodyAsAny(): Promise<string | {{{fileContentDataType}}} | undefined> {
try {
return this.body.text();
} catch {}
try {
return this.body.binary();
} catch {}
return Promise.resolve(undefined);
}
}
export interface HttpLibrary {

View File

@ -134,8 +134,7 @@ export class DefaultApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -160,8 +159,7 @@ export class DefaultApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -186,8 +184,7 @@ export class DefaultApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -8,7 +8,7 @@
*
*/
export class ApiException<T> extends Error {
public constructor(public code: number, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + JSON.stringify(body))
public constructor(public code: number, message: string, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + message + "\nBody: " + JSON.stringify(body))
}
}

View File

@ -201,6 +201,22 @@ export class ResponseContext {
});
}
}
/**
* Use a heuristic to get a body of unknown data structure.
* Return as string if possible, otherwise as binary.
*/
public getBodyAsAny(): Promise<string | Blob | undefined> {
try {
return this.body.text();
} catch {}
try {
return this.body.binary();
} catch {}
return Promise.resolve(undefined);
}
}
export interface HttpLibrary {

View File

@ -402,7 +402,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid input");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid input", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -414,8 +414,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -428,7 +427,7 @@ export class PetApiResponseProcessor {
public async deletePet(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid pet value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid pet value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -436,8 +435,7 @@ export class PetApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -457,7 +455,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid status value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid status value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -469,8 +467,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -490,7 +487,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid tag value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid tag value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -502,8 +499,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -523,10 +519,10 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Pet not found");
throw new ApiException<undefined>(response.httpStatusCode, "Pet not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -538,8 +534,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -559,13 +554,13 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Pet not found");
throw new ApiException<undefined>(response.httpStatusCode, "Pet not found", undefined);
}
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Validation exception");
throw new ApiException<undefined>(response.httpStatusCode, "Validation exception", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -577,8 +572,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -591,7 +585,7 @@ export class PetApiResponseProcessor {
public async updatePetWithForm(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid input");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid input", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -599,8 +593,7 @@ export class PetApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -629,8 +622,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -145,10 +145,10 @@ export class StoreApiResponseProcessor {
public async deleteOrder(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Order not found");
throw new ApiException<undefined>(response.httpStatusCode, "Order not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -156,8 +156,7 @@ export class StoreApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -186,8 +185,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -207,10 +205,10 @@ export class StoreApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Order not found");
throw new ApiException<undefined>(response.httpStatusCode, "Order not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -222,8 +220,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -243,7 +240,7 @@ export class StoreApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid Order");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid Order", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -255,8 +252,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -333,7 +333,7 @@ export class UserApiResponseProcessor {
public async createUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -341,8 +341,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -355,7 +354,7 @@ export class UserApiResponseProcessor {
public async createUsersWithArrayInput(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -363,8 +362,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -377,7 +375,7 @@ export class UserApiResponseProcessor {
public async createUsersWithListInput(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -385,8 +383,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -399,10 +396,10 @@ export class UserApiResponseProcessor {
public async deleteUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -410,8 +407,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -431,10 +427,10 @@ export class UserApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -446,8 +442,7 @@ export class UserApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -467,7 +462,7 @@ export class UserApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username/password supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username/password supplied", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -479,8 +474,7 @@ export class UserApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -493,7 +487,7 @@ export class UserApiResponseProcessor {
public async logoutUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -501,8 +495,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -515,10 +508,10 @@ export class UserApiResponseProcessor {
public async updateUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid user supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid user supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -526,8 +519,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -8,7 +8,7 @@
*
*/
export class ApiException<T> extends Error {
public constructor(public code: number, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + JSON.stringify(body))
public constructor(public code: number, message: string, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + message + "\nBody: " + JSON.stringify(body))
}
}

View File

@ -187,6 +187,22 @@ export class ResponseContext {
const fileName = this.getParsedHeader("content-disposition")["filename"] || "";
return { data, name: fileName };
}
/**
* Use a heuristic to get a body of unknown data structure.
* Return as string if possible, otherwise as binary.
*/
public getBodyAsAny(): Promise<string | Buffer | undefined> {
try {
return this.body.text();
} catch {}
try {
return this.body.binary();
} catch {}
return Promise.resolve(undefined);
}
}
export interface HttpLibrary {

View File

@ -400,7 +400,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid input");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid input", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -412,8 +412,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -426,7 +425,7 @@ export class PetApiResponseProcessor {
public async deletePet(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid pet value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid pet value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -434,8 +433,7 @@ export class PetApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -455,7 +453,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid status value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid status value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -467,8 +465,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -488,7 +485,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid tag value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid tag value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -500,8 +497,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -521,10 +517,10 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Pet not found");
throw new ApiException<undefined>(response.httpStatusCode, "Pet not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -536,8 +532,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -557,13 +552,13 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Pet not found");
throw new ApiException<undefined>(response.httpStatusCode, "Pet not found", undefined);
}
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Validation exception");
throw new ApiException<undefined>(response.httpStatusCode, "Validation exception", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -575,8 +570,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -589,7 +583,7 @@ export class PetApiResponseProcessor {
public async updatePetWithForm(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid input");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid input", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -597,8 +591,7 @@ export class PetApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -627,8 +620,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -143,10 +143,10 @@ export class StoreApiResponseProcessor {
public async deleteOrder(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Order not found");
throw new ApiException<undefined>(response.httpStatusCode, "Order not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -154,8 +154,7 @@ export class StoreApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -184,8 +183,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -205,10 +203,10 @@ export class StoreApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Order not found");
throw new ApiException<undefined>(response.httpStatusCode, "Order not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -220,8 +218,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -241,7 +238,7 @@ export class StoreApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid Order");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid Order", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -253,8 +250,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -331,7 +331,7 @@ export class UserApiResponseProcessor {
public async createUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -339,8 +339,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -353,7 +352,7 @@ export class UserApiResponseProcessor {
public async createUsersWithArrayInput(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -361,8 +360,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -375,7 +373,7 @@ export class UserApiResponseProcessor {
public async createUsersWithListInput(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -383,8 +381,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -397,10 +394,10 @@ export class UserApiResponseProcessor {
public async deleteUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -408,8 +405,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -429,10 +425,10 @@ export class UserApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -444,8 +440,7 @@ export class UserApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -465,7 +460,7 @@ export class UserApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username/password supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username/password supplied", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -477,8 +472,7 @@ export class UserApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -491,7 +485,7 @@ export class UserApiResponseProcessor {
public async logoutUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -499,8 +493,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -513,10 +506,10 @@ export class UserApiResponseProcessor {
public async updateUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid user supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid user supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -524,8 +517,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -8,7 +8,7 @@
*
*/
export class ApiException<T> extends Error {
public constructor(public code: number, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + JSON.stringify(body))
public constructor(public code: number, message: string, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + message + "\nBody: " + JSON.stringify(body))
}
}

View File

@ -219,6 +219,22 @@ export class ResponseContext {
});
}
}
/**
* Use a heuristic to get a body of unknown data structure.
* Return as string if possible, otherwise as binary.
*/
public getBodyAsAny(): Promise<string | Blob | undefined> {
try {
return this.body.text();
} catch {}
try {
return this.body.binary();
} catch {}
return Promise.resolve(undefined);
}
}
export interface HttpLibrary {

View File

@ -405,7 +405,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid input");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid input", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -417,8 +417,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -431,7 +430,7 @@ export class PetApiResponseProcessor {
public async deletePet(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid pet value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid pet value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -439,8 +438,7 @@ export class PetApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -460,7 +458,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid status value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid status value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -472,8 +470,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -493,7 +490,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid tag value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid tag value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -505,8 +502,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -526,10 +522,10 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Pet not found");
throw new ApiException<undefined>(response.httpStatusCode, "Pet not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -541,8 +537,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -562,13 +557,13 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Pet not found");
throw new ApiException<undefined>(response.httpStatusCode, "Pet not found", undefined);
}
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Validation exception");
throw new ApiException<undefined>(response.httpStatusCode, "Validation exception", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -580,8 +575,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -594,7 +588,7 @@ export class PetApiResponseProcessor {
public async updatePetWithForm(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid input");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid input", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -602,8 +596,7 @@ export class PetApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -632,8 +625,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -148,10 +148,10 @@ export class StoreApiResponseProcessor {
public async deleteOrder(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Order not found");
throw new ApiException<undefined>(response.httpStatusCode, "Order not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -159,8 +159,7 @@ export class StoreApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -189,8 +188,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -210,10 +208,10 @@ export class StoreApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Order not found");
throw new ApiException<undefined>(response.httpStatusCode, "Order not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -225,8 +223,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -246,7 +243,7 @@ export class StoreApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid Order");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid Order", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -258,8 +255,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -336,7 +336,7 @@ export class UserApiResponseProcessor {
public async createUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -344,8 +344,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -358,7 +357,7 @@ export class UserApiResponseProcessor {
public async createUsersWithArrayInput(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -366,8 +365,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -380,7 +378,7 @@ export class UserApiResponseProcessor {
public async createUsersWithListInput(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -388,8 +386,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -402,10 +399,10 @@ export class UserApiResponseProcessor {
public async deleteUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -413,8 +410,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -434,10 +430,10 @@ export class UserApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -449,8 +445,7 @@ export class UserApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -470,7 +465,7 @@ export class UserApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username/password supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username/password supplied", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -482,8 +477,7 @@ export class UserApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -496,7 +490,7 @@ export class UserApiResponseProcessor {
public async logoutUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -504,8 +498,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -518,10 +511,10 @@ export class UserApiResponseProcessor {
public async updateUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid user supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid user supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -529,8 +522,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -8,7 +8,7 @@
*
*/
export class ApiException<T> extends Error {
public constructor(public code: number, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + JSON.stringify(body))
public constructor(public code: number, message: string, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + message + "\nBody: " + JSON.stringify(body))
}
}

View File

@ -187,6 +187,22 @@ export class ResponseContext {
const fileName = this.getParsedHeader("content-disposition")["filename"] || "";
return { data, name: fileName };
}
/**
* Use a heuristic to get a body of unknown data structure.
* Return as string if possible, otherwise as binary.
*/
public getBodyAsAny(): Promise<string | Buffer | undefined> {
try {
return this.body.text();
} catch {}
try {
return this.body.binary();
} catch {}
return Promise.resolve(undefined);
}
}
export interface HttpLibrary {

View File

@ -400,7 +400,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid input");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid input", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -412,8 +412,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -426,7 +425,7 @@ export class PetApiResponseProcessor {
public async deletePet(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid pet value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid pet value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -434,8 +433,7 @@ export class PetApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -455,7 +453,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid status value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid status value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -467,8 +465,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -488,7 +485,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid tag value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid tag value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -500,8 +497,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -521,10 +517,10 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Pet not found");
throw new ApiException<undefined>(response.httpStatusCode, "Pet not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -536,8 +532,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -557,13 +552,13 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Pet not found");
throw new ApiException<undefined>(response.httpStatusCode, "Pet not found", undefined);
}
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Validation exception");
throw new ApiException<undefined>(response.httpStatusCode, "Validation exception", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -575,8 +570,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -589,7 +583,7 @@ export class PetApiResponseProcessor {
public async updatePetWithForm(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid input");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid input", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -597,8 +591,7 @@ export class PetApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -627,8 +620,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -143,10 +143,10 @@ export class StoreApiResponseProcessor {
public async deleteOrder(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Order not found");
throw new ApiException<undefined>(response.httpStatusCode, "Order not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -154,8 +154,7 @@ export class StoreApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -184,8 +183,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -205,10 +203,10 @@ export class StoreApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Order not found");
throw new ApiException<undefined>(response.httpStatusCode, "Order not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -220,8 +218,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -241,7 +238,7 @@ export class StoreApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid Order");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid Order", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -253,8 +250,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -331,7 +331,7 @@ export class UserApiResponseProcessor {
public async createUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -339,8 +339,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -353,7 +352,7 @@ export class UserApiResponseProcessor {
public async createUsersWithArrayInput(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -361,8 +360,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -375,7 +373,7 @@ export class UserApiResponseProcessor {
public async createUsersWithListInput(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -383,8 +381,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -397,10 +394,10 @@ export class UserApiResponseProcessor {
public async deleteUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -408,8 +405,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -429,10 +425,10 @@ export class UserApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -444,8 +440,7 @@ export class UserApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -465,7 +460,7 @@ export class UserApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username/password supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username/password supplied", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -477,8 +472,7 @@ export class UserApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -491,7 +485,7 @@ export class UserApiResponseProcessor {
public async logoutUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -499,8 +493,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -513,10 +506,10 @@ export class UserApiResponseProcessor {
public async updateUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid user supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid user supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -524,8 +517,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -8,7 +8,7 @@
*
*/
export class ApiException<T> extends Error {
public constructor(public code: number, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + JSON.stringify(body))
public constructor(public code: number, message: string, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + message + "\nBody: " + JSON.stringify(body))
}
}

View File

@ -201,6 +201,22 @@ export class ResponseContext {
});
}
}
/**
* Use a heuristic to get a body of unknown data structure.
* Return as string if possible, otherwise as binary.
*/
public getBodyAsAny(): Promise<string | Blob | undefined> {
try {
return this.body.text();
} catch {}
try {
return this.body.binary();
} catch {}
return Promise.resolve(undefined);
}
}
export interface HttpLibrary {

View File

@ -402,7 +402,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid input");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid input", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -414,8 +414,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -428,7 +427,7 @@ export class PetApiResponseProcessor {
public async deletePet(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid pet value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid pet value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -436,8 +435,7 @@ export class PetApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -457,7 +455,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid status value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid status value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -469,8 +467,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -490,7 +487,7 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid tag value");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid tag value", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -502,8 +499,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -523,10 +519,10 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Pet not found");
throw new ApiException<undefined>(response.httpStatusCode, "Pet not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -538,8 +534,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -559,13 +554,13 @@ export class PetApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Pet not found");
throw new ApiException<undefined>(response.httpStatusCode, "Pet not found", undefined);
}
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Validation exception");
throw new ApiException<undefined>(response.httpStatusCode, "Validation exception", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -577,8 +572,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -591,7 +585,7 @@ export class PetApiResponseProcessor {
public async updatePetWithForm(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("405", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid input");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid input", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -599,8 +593,7 @@ export class PetApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -629,8 +622,7 @@ export class PetApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -145,10 +145,10 @@ export class StoreApiResponseProcessor {
public async deleteOrder(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Order not found");
throw new ApiException<undefined>(response.httpStatusCode, "Order not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -156,8 +156,7 @@ export class StoreApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -186,8 +185,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -207,10 +205,10 @@ export class StoreApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid ID supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid ID supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Order not found");
throw new ApiException<undefined>(response.httpStatusCode, "Order not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -222,8 +220,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -243,7 +240,7 @@ export class StoreApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid Order");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid Order", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -255,8 +252,7 @@ export class StoreApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -333,7 +333,7 @@ export class UserApiResponseProcessor {
public async createUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -341,8 +341,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -355,7 +354,7 @@ export class UserApiResponseProcessor {
public async createUsersWithArrayInput(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -363,8 +362,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -377,7 +375,7 @@ export class UserApiResponseProcessor {
public async createUsersWithListInput(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -385,8 +383,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -399,10 +396,10 @@ export class UserApiResponseProcessor {
public async deleteUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -410,8 +407,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -431,10 +427,10 @@ export class UserApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -446,8 +442,7 @@ export class UserApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -467,7 +462,7 @@ export class UserApiResponseProcessor {
return body;
}
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid username/password supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid username/password supplied", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -479,8 +474,7 @@ export class UserApiResponseProcessor {
return body;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -493,7 +487,7 @@ export class UserApiResponseProcessor {
public async logoutUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("0", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "successful operation");
throw new ApiException<undefined>(response.httpStatusCode, "successful operation", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -501,8 +495,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
/**
@ -515,10 +508,10 @@ export class UserApiResponseProcessor {
public async updateUser(response: ResponseContext): Promise< void> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("400", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "Invalid user supplied");
throw new ApiException<undefined>(response.httpStatusCode, "Invalid user supplied", undefined);
}
if (isCodeInRange("404", response.httpStatusCode)) {
throw new ApiException<string>(response.httpStatusCode, "User not found");
throw new ApiException<undefined>(response.httpStatusCode, "User not found", undefined);
}
// Work around for missing responses in specification, e.g. for petstore.yaml
@ -526,8 +519,7 @@ export class UserApiResponseProcessor {
return;
}
let body = response.body || "";
throw new ApiException<string>(response.httpStatusCode, "Unknown API Status Code!\nBody: \"" + body + "\"");
throw new ApiException<string | Buffer | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny());
}
}

View File

@ -8,7 +8,7 @@
*
*/
export class ApiException<T> extends Error {
public constructor(public code: number, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + JSON.stringify(body))
public constructor(public code: number, message: string, public body: T) {
super("HTTP-Code: " + code + "\nMessage: " + message + "\nBody: " + JSON.stringify(body))
}
}

View File

@ -187,6 +187,22 @@ export class ResponseContext {
const fileName = this.getParsedHeader("content-disposition")["filename"] || "";
return { data, name: fileName };
}
/**
* Use a heuristic to get a body of unknown data structure.
* Return as string if possible, otherwise as binary.
*/
public getBodyAsAny(): Promise<string | Buffer | undefined> {
try {
return this.body.text();
} catch {}
try {
return this.body.binary();
} catch {}
return Promise.resolve(undefined);
}
}
export interface HttpLibrary {

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
import * as petstore from 'ts-petstore-client'
import { expect, assert } from "chai";
import { expect } from "chai";
import * as fs from 'fs';
const configuration = petstore.createConfiguration()
@ -10,7 +10,11 @@ const tag = new petstore.Tag();
tag.name = "tag1"
tag.id = Math.floor(Math.random() * 100000)
const pet = new petstore.Pet()
let pet: petstore.Pet;
describe("PetApi", () => {
beforeEach(async () => {
pet = new petstore.Pet()
pet.id = Math.floor(Math.random() * 100000)
pet.name = "PetName"
pet.photoUrls = []
@ -18,114 +22,84 @@ pet.status = 'available'
pet.tags = [ tag ]
pet.category = undefined
describe("PetApi", () =>{
it("addPet", (done) => {
petApi.addPet(pet).then(() => {
return petApi.getPetById(pet.id)
}).then((createdPet: petstore.Pet) => {
expect(createdPet).to.deep.equal(pet);
done()
}).catch((err: any) => {
done(err)
})
})
it("deletePet", (done) => {
petApi.addPet(pet).then(() => {
return petApi.deletePet(pet.id)
}).then(() => {
return petApi.getPetById(pet.id)
}).then((pet: petstore.Pet) => {
done("Pet with id " + pet.id + " was not deleted!");
}).catch((err: any) => {
if (err.code && err.code == 404) {
done();
} else {
done(err)
}
})
})
it("findPetsByStatus", (done) => {
petApi.addPet(pet).then(() => {
return petApi.findPetsByStatus(["available"])
}).then((pets: petstore.Pet[]) => {
expect(pets.length).to.be.at.least(1);
done();
}).catch((err: any) => {
done(err)
})
})
// bugged on server side! Code 500
/* it("findPetsByTag", (done) => {
petApi.addPet(pet).then(() => {
return petApi.findPetsByTags([tag.name])
}).then((pets: Pet[]) => {
expect(pets.length).to.be.at.least(1);
done();
}).catch((err: any) => {
done(err);
})
})*/
it("getPetById", (done) => {
petApi.addPet(pet).then(() => {
return petApi.getPetById(pet.id)
}).then((returnedPet: petstore.Pet) => {
expect(returnedPet).to.deep.equal(pet);
done();
}).catch((err: any) => {
done(err);
})
})
it("updatePet", (done) => {
const oldName = pet.name
const updatedName = "updated name";
petApi.addPet(pet).then(() => {
pet.name = updatedName
return petApi.updatePet(pet).then(() => {
pet.name = oldName;
}).catch((err: any) => {
pet.name = oldName
throw err;
await petApi.addPet(pet);
});
}).then(() => {
return petApi.getPetById(pet.id);
}).then((returnedPet: petstore.Pet) => {
expect(returnedPet.id).to.equal(pet.id)
expect(returnedPet.name).to.equal(updatedName);
done();
}).catch((err: any) => {
done(err)
})
it("addPet", async () => {
const createdPet = await petApi.getPetById(pet.id)
expect(createdPet).to.deep.equal(pet);
})
// not supported by online swagger api?
/* it("updatePetWithForm", (done) => {
it("deletePet", async () => {
await petApi.deletePet(pet.id);
let deletedPet;
try {
deletedPet = await petApi.getPetById(pet.id)
} catch (err) {
expect(err.code).to.equal(404);
expect(err.message).to.include("Pet not found");
return;
}
throw new Error("Pet with id " + deletedPet.id + " was not deleted!");
})
it("deleteNonExistantPet", async () => {
// Use an id that is too big for the server to handle.
const nonExistantId = 100000000000000000000000000;
try {
await petApi.deletePet(nonExistantId)
} catch (err) {
// The 404 response for this endpoint is officially documented, but
// that documentation is not used for generating the client code.
// That means we get an error about the response being undefined
// here.
expect(err.code).to.equal(404);
expect(err.message).to.include("Unknown API Status Code");
expect(err.body).to.include("404");
expect(err.body).to.include("message");
return;
}
throw new Error("Deleted non-existant pet with id " + nonExistantId + "!");
})
it("findPetsByStatus", async () => {
const pets = await petApi.findPetsByStatus(["available"]);
expect(pets.length).to.be.at.least(1);
})
it("findPetsByTag", async () => {
const pets = await petApi.findPetsByTags([tag.name])
expect(pets.length).to.be.at.least(1);
})
it("getPetById", async () => {
const returnedPet = await petApi.getPetById(pet.id);
expect(returnedPet).to.deep.equal(pet);
})
it("updatePet", async () => {
pet.name = "updated name";
await petApi.updatePet(pet);
await petApi.updatePet(pet);
const returnedPet = await petApi.getPetById(pet.id);
expect(returnedPet.id).to.equal(pet.id)
expect(returnedPet.name).to.equal(pet.name);
})
it("updatePetWithForm", async () => {
const updatedName = "updated name";
petApi.addPet(pet).then(() => {
return petApi.updatePetWithForm(pet.id, updatedName)
}).then(() => {
return petApi.getPetById(pet.id)
}).then((returnedPet: Pet) => {
await petApi.updatePetWithForm(pet.id, updatedName);
const returnedPet = await petApi.getPetById(pet.id)
expect(returnedPet.id).to.equal(pet.id)
expect(returnedPet.name).to.equal(updatedName);
done()
}).catch((err: any) => {
done(err)
})
})*/
it("uploadFile", (done) => {
it("uploadFile", async () => {
const image = fs.readFileSync(__dirname + "/pet.png")
petApi.uploadFile(pet.id, "Metadata", { name: "pet.png", data: image}).then((response: any) => {
const response = await petApi.uploadFile(pet.id, "Metadata", { name: "pet.png", data: image});
expect(response.code).to.be.gte(200).and.lt(300);
expect(response.message).to.contain("pet.png");
done();
}).catch((err: any) => {
done(err);
})
})
})