parent
5e4d73b44d
commit
4e046647c7
|
|
@ -1,3 +1,3 @@
|
|||
dist
|
||||
node_modules
|
||||
|
||||
target
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ export interface LibreCcmApiClient {
|
|||
*/
|
||||
post(
|
||||
endpoint: string,
|
||||
body: unknown,
|
||||
body: RequestBody,
|
||||
searchParams?: Record<string, string>
|
||||
): Promise<ApiResponse>;
|
||||
/**
|
||||
|
|
@ -39,7 +39,7 @@ export interface LibreCcmApiClient {
|
|||
*/
|
||||
put(
|
||||
endpoint: string,
|
||||
body: Record<string, unknown>,
|
||||
body: unknown,
|
||||
searchParams?: Record<string, string>
|
||||
): Promise<ApiResponse>;
|
||||
/**
|
||||
|
|
@ -72,6 +72,12 @@ export interface LibreCcmApiClient {
|
|||
options(endpoint: string): Promise<ApiResponse>;
|
||||
}
|
||||
|
||||
export type RequestBody =
|
||||
| Record<string, unknown>
|
||||
| ArrayBuffer
|
||||
| string
|
||||
| undefined;
|
||||
|
||||
/**
|
||||
* The response from the API.
|
||||
*/
|
||||
|
|
@ -194,11 +200,22 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
}
|
||||
|
||||
private buildFetchOptions(
|
||||
method: "get" | "post" | "put" | "delete" | "head" | "options"
|
||||
method: "get" | "post" | "put" | "delete" | "head" | "options",
|
||||
body?: RequestBody
|
||||
): RequestInit {
|
||||
const fetchOptions: RequestInit = {};
|
||||
Object.assign(fetchOptions, this.#fetchOptions);
|
||||
const fetchOptions: RequestInit = {
|
||||
...this.#fetchOptions,
|
||||
};
|
||||
fetchOptions.method = method;
|
||||
if (body) {
|
||||
if (body instanceof ArrayBuffer) {
|
||||
fetchOptions.body = body;
|
||||
} else if (typeof body === "string") {
|
||||
fetchOptions.body = body;
|
||||
} else {
|
||||
fetchOptions.body = JSON.stringify(body);
|
||||
}
|
||||
}
|
||||
|
||||
return fetchOptions;
|
||||
}
|
||||
|
|
@ -232,35 +249,151 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
post(
|
||||
async post(
|
||||
endpoint: string,
|
||||
body: unknown,
|
||||
body: RequestBody,
|
||||
searchParams?: Record<string, string>
|
||||
): Promise<ApiResponse> {
|
||||
throw "Not implemented yet.";
|
||||
const url = buildUrl(this.#baseUrl, endpoint, searchParams);
|
||||
try {
|
||||
const response = await fetch(
|
||||
url,
|
||||
this.buildFetchOptions("post", body)
|
||||
);
|
||||
if (response.ok) {
|
||||
return new FetchResponse(response);
|
||||
} else {
|
||||
throw {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
method: "get",
|
||||
message: "API responded with an error.",
|
||||
url,
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
throw {
|
||||
status: -1,
|
||||
statusText: "n/a",
|
||||
method: "get",
|
||||
message: `Failed to execute get: ${err}`,
|
||||
url,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
put(
|
||||
async put(
|
||||
endpoint: string,
|
||||
body: unknown,
|
||||
body: RequestBody,
|
||||
searchParams?: Record<string, string>
|
||||
): Promise<ApiResponse> {
|
||||
throw "Not implemented yet.";
|
||||
const url = buildUrl(this.#baseUrl, endpoint, searchParams);
|
||||
try {
|
||||
const response = await fetch(
|
||||
url,
|
||||
this.buildFetchOptions("put", body)
|
||||
);
|
||||
if (response.ok) {
|
||||
return new FetchResponse(response);
|
||||
} else {
|
||||
throw {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
method: "get",
|
||||
message: "API responded with an error.",
|
||||
url,
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
throw {
|
||||
status: -1,
|
||||
statusText: "n/a",
|
||||
method: "get",
|
||||
message: `Failed to execute get: ${err}`,
|
||||
url,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
delete(
|
||||
async delete(
|
||||
endpoint: string,
|
||||
searchParams?: Record<string, string>
|
||||
): Promise<ApiResponse> {
|
||||
throw "Not implemented yet.";
|
||||
const url = buildUrl(this.#baseUrl, endpoint, searchParams);
|
||||
try {
|
||||
const response = await fetch(url, this.buildFetchOptions("delete"));
|
||||
if (response.ok) {
|
||||
return new FetchResponse(response);
|
||||
} else {
|
||||
throw {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
method: "get",
|
||||
message: "API responded with an error.",
|
||||
url,
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
throw {
|
||||
status: -1,
|
||||
statusText: "n/a",
|
||||
method: "get",
|
||||
message: `Failed to execute get: ${err}`,
|
||||
url,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
head(endpoint: string): Promise<ApiResponse> {
|
||||
throw "Not implemented yet.";
|
||||
async head(endpoint: string): Promise<ApiResponse> {
|
||||
const url = buildUrl(this.#baseUrl, endpoint);
|
||||
try {
|
||||
const response = await fetch(url, this.buildFetchOptions("head"));
|
||||
if (response.ok) {
|
||||
return new FetchResponse(response);
|
||||
} else {
|
||||
throw {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
method: "get",
|
||||
message: "API responded with an error.",
|
||||
url,
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
throw {
|
||||
status: -1,
|
||||
statusText: "n/a",
|
||||
method: "get",
|
||||
message: `Failed to execute get: ${err}`,
|
||||
url,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
options(endpoint: string): Promise<ApiResponse> {
|
||||
throw "Not implemented yet.";
|
||||
async options(endpoint: string): Promise<ApiResponse> {
|
||||
const url = buildUrl(this.#baseUrl, endpoint);
|
||||
try {
|
||||
const response = await fetch(url, this.buildFetchOptions("options"));
|
||||
if (response.ok) {
|
||||
return new FetchResponse(response);
|
||||
} else {
|
||||
throw {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
method: "get",
|
||||
message: "API responded with an error.",
|
||||
url,
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
throw {
|
||||
status: -1,
|
||||
statusText: "n/a",
|
||||
method: "get",
|
||||
message: `Failed to execute get: ${err}`,
|
||||
url,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue