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