diff --git a/ccm-apiclient-commons/.gitignore b/ccm-apiclient-commons/.gitignore index 1502ec233..27c057bb8 100644 --- a/ccm-apiclient-commons/.gitignore +++ b/ccm-apiclient-commons/.gitignore @@ -1,3 +1,3 @@ dist node_modules - +target diff --git a/ccm-apiclient-commons/src/main/typescript/ApiClient.ts b/ccm-apiclient-commons/src/main/typescript/ApiClient.ts index 3c8c51942..3e041203a 100644 --- a/ccm-apiclient-commons/src/main/typescript/ApiClient.ts +++ b/ccm-apiclient-commons/src/main/typescript/ApiClient.ts @@ -25,7 +25,7 @@ export interface LibreCcmApiClient { */ post( endpoint: string, - body: unknown, + body: RequestBody, searchParams?: Record ): Promise; /** @@ -39,7 +39,7 @@ export interface LibreCcmApiClient { */ put( endpoint: string, - body: Record, + body: unknown, searchParams?: Record ): Promise; /** @@ -72,6 +72,12 @@ export interface LibreCcmApiClient { options(endpoint: string): Promise; } +export type RequestBody = + | Record + | 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 ): Promise { - 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 ): Promise { - 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 ): Promise { - 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 { - throw "Not implemented yet."; + async head(endpoint: string): Promise { + 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 { - throw "Not implemented yet."; + async options(endpoint: string): Promise { + 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, + }; + } } }