Added a ok property to ApiResponse

Former-commit-id: 58fb0937d2
restapi
Jens Pelzetter 2020-07-23 19:10:17 +02:00
parent 901375c4cc
commit 1b916fa7e9
1 changed files with 20 additions and 1 deletions

View File

@ -84,6 +84,10 @@ export type RequestBody =
* The response from the API. * The response from the API.
*/ */
export interface ApiResponse { export interface ApiResponse {
/**
* Indicates that the request was processed successfully.
*/
ok: boolean;
/** /**
* The HTTP status code returned by the RESTful API. * The HTTP status code returned by the RESTful API.
*/ */
@ -168,6 +172,7 @@ export function isFetchAvailable(): boolean {
* by browsers. * by browsers.
*/ */
class FetchResponse implements ApiResponse { class FetchResponse implements ApiResponse {
readonly ok: boolean;
readonly status: number; readonly status: number;
readonly statusText: string; readonly statusText: string;
#response: Response; #response: Response;
@ -175,6 +180,7 @@ class FetchResponse implements ApiResponse {
constructor(response: Response) { constructor(response: Response) {
this.status = response.status; this.status = response.status;
this.statusText = response.statusText; this.statusText = response.statusText;
this.ok = response.ok;
this.#response = response; this.#response = response;
} }
@ -415,12 +421,19 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
* by browsers. * by browsers.
*/ */
class NodeResponse implements ApiResponse { class NodeResponse implements ApiResponse {
readonly ok: boolean;
readonly status: number; readonly status: number;
readonly statusText: string; readonly statusText: string;
#data: Buffer | null; #data: Buffer | null;
constructor(status: number, statusText: string, data: Buffer | null) { constructor(
ok: boolean,
status: number,
statusText: string,
data: Buffer | null
) {
this.ok = ok;
this.status = status; this.status = status;
this.statusText = statusText; this.statusText = statusText;
this.#data = data; this.#data = data;
@ -515,6 +528,7 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
response.on("end", () => { response.on("end", () => {
const buffer: Buffer = Buffer.concat(data); const buffer: Buffer = Buffer.concat(data);
const apiResponse: ApiResponse = new NodeResponse( const apiResponse: ApiResponse = new NodeResponse(
status === 200,
status, status,
statusText, statusText,
buffer buffer
@ -552,6 +566,7 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
response.on("end", () => { response.on("end", () => {
const apiResponse: ApiResponse = new NodeResponse( const apiResponse: ApiResponse = new NodeResponse(
status === 200 || status === 201,
status, status,
statusText, statusText,
null null
@ -592,6 +607,7 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
response.on("end", () => { response.on("end", () => {
const apiResponse: ApiResponse = new NodeResponse( const apiResponse: ApiResponse = new NodeResponse(
status === 200 || status === 201,
status, status,
statusText, statusText,
null null
@ -631,6 +647,7 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
response.on("end", () => { response.on("end", () => {
const apiResponse: ApiResponse = new NodeResponse( const apiResponse: ApiResponse = new NodeResponse(
status === 200 || status === 204,
status, status,
statusText, statusText,
null null
@ -671,6 +688,7 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
response.on("end", () => { response.on("end", () => {
const buffer: Buffer = Buffer.concat(data); const buffer: Buffer = Buffer.concat(data);
const apiResponse: ApiResponse = new NodeResponse( const apiResponse: ApiResponse = new NodeResponse(
status === 200,
status, status,
statusText, statusText,
buffer buffer
@ -710,6 +728,7 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
response.on("end", () => { response.on("end", () => {
const buffer: Buffer = Buffer.concat(data); const buffer: Buffer = Buffer.concat(data);
const apiResponse: ApiResponse = new NodeResponse( const apiResponse: ApiResponse = new NodeResponse(
status === 200,
status, status,
statusText, statusText,
buffer buffer