parent
4e046647c7
commit
3cb28232a6
|
|
@ -1,3 +1,5 @@
|
|||
import * as http from "http";
|
||||
|
||||
/**
|
||||
* The interface of a client for the LibreCcm RESTful API.
|
||||
*/
|
||||
|
|
@ -373,7 +375,10 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
async options(endpoint: string): Promise<ApiResponse> {
|
||||
const url = buildUrl(this.#baseUrl, endpoint);
|
||||
try {
|
||||
const response = await fetch(url, this.buildFetchOptions("options"));
|
||||
const response = await fetch(
|
||||
url,
|
||||
this.buildFetchOptions("options")
|
||||
);
|
||||
if (response.ok) {
|
||||
return new FetchResponse(response);
|
||||
} else {
|
||||
|
|
@ -397,16 +402,108 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An implementation of the {@link ApiResponse} for the Node.js HTTP API supported
|
||||
* by browsers.
|
||||
*/
|
||||
class NodeResponse implements ApiResponse {
|
||||
readonly status: number;
|
||||
readonly statusText: string;
|
||||
|
||||
#data: Buffer;
|
||||
|
||||
constructor(status: number, statusText: string, data: Buffer) {
|
||||
this.status = status;
|
||||
this.statusText = statusText;
|
||||
this.#data = data;
|
||||
}
|
||||
|
||||
json(): Promise<Record<string, unknown>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
resolve(this.#data.toJSON());
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
arrayBuffer(): Promise<ArrayBuffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const arrayBuf: ArrayBuffer = this.#data.buffer.slice(
|
||||
this.#data.byteOffset,
|
||||
this.#data.byteLength + this.#data.byteLength
|
||||
);
|
||||
resolve(arrayBuf);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
text(): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
return this.#data.toString();
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An implementation of the {@link LibreCcmApiClient} interface using the HTTP API
|
||||
* provided by node.js.
|
||||
*/
|
||||
export class ApiClientNodeImpl implements LibreCcmApiClient {
|
||||
readonly #baseUrl: string;
|
||||
|
||||
constructor(baseUrl: string) {
|
||||
this.#baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
get(
|
||||
endpoint: string,
|
||||
searchParams?: Record<string, string>
|
||||
): Promise<ApiResponse> {
|
||||
throw "Not implemented yet.";
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = buildUrl(this.#baseUrl, endpoint, searchParams);
|
||||
const request: http.ClientRequest = http.request(url, {
|
||||
headers: {
|
||||
Authorization: "",
|
||||
},
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
request.on("response", (response) => {
|
||||
const status: number = response.statusCode
|
||||
? response.statusCode
|
||||
: -1;
|
||||
const statusText: string = response.statusMessage
|
||||
? response.statusMessage
|
||||
: "";
|
||||
|
||||
const data: Array<Buffer> = [];
|
||||
|
||||
response.on("data", (chunk: Buffer) => {
|
||||
data.push(chunk);
|
||||
});
|
||||
response.on("end", () => {
|
||||
const buffer: Buffer = Buffer.concat(data);
|
||||
const apiResponse: ApiResponse = new NodeResponse(
|
||||
status,
|
||||
statusText,
|
||||
buffer
|
||||
);
|
||||
resolve(apiResponse);
|
||||
});
|
||||
});
|
||||
request.on("error", (error) => reject(error));
|
||||
|
||||
request.end();
|
||||
});
|
||||
}
|
||||
|
||||
post(
|
||||
|
|
|
|||
Loading…
Reference in New Issue