From 1ca0f76b090ebdb5a2519155dd347f7e14595e44 Mon Sep 17 00:00:00 2001 From: barbmarcio Date: Mon, 8 Jul 2024 14:45:20 +0100 Subject: [PATCH] feat: adding a function using api client and api client as singleton --- src/functions/report-event-api-class.ts | 31 +++++++++++++++++++++++++ src/lib/api-client.ts | 22 +++++++++++++----- 2 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 src/functions/report-event-api-class.ts diff --git a/src/functions/report-event-api-class.ts b/src/functions/report-event-api-class.ts new file mode 100644 index 0000000..e74c03f --- /dev/null +++ b/src/functions/report-event-api-class.ts @@ -0,0 +1,31 @@ +import { apis, baseURL } from "../constants/apis.constant"; +import type { Config, TopsortEvent } from "../interfaces/events.interface"; +import APIClient from "../lib/api-client"; + +/** + * Reports an event to the Topsort API. + * + * @example + * ```js + * const event = { eventType: "test", eventData: {} }; + * const config = { token: "my-token" }; + * const result = await reportEvent(event, config); + * console.log(result); // { ok: true, retry: false } + * ``` + * + * @param event - The event to report. + * @param config - The configuration object containing URL and token. + * @returns {Promise<{ok: boolean, retry: boolean}>} The result of the report, indicating success and if a retry is needed. + */ +export async function reportEvent( + event: TopsortEvent, + config: Config, +): Promise<{ ok: boolean; retry: boolean }> { + const url = `${config.host || baseURL}/${apis.events}`; + await APIClient.post(url, event); + + return { + ok: true, + retry: false, + }; +} \ No newline at end of file diff --git a/src/lib/api-client.ts b/src/lib/api-client.ts index d3f838b..1c73553 100644 --- a/src/lib/api-client.ts +++ b/src/lib/api-client.ts @@ -1,6 +1,7 @@ +import { baseURL } from "../constants/apis.constant"; import AppError from "./app-error"; -class ApiClient { +class APIClient { private baseUrl: string; constructor(baseUrl: string) { @@ -27,18 +28,27 @@ class ApiClient { endpoint: string, options: RequestInit, ): Promise { - const response = await fetch(`${this.baseUrl}${endpoint}`, options); - return await this.handleResponse(response); + try { + const response = await fetch(`${this.baseUrl}${endpoint}`, options); + return await this.handleResponse(response); + } catch (error) { + if (error instanceof AppError) { + throw error; + } + + const message = error instanceof Error ? error.message : 'Unknown error'; + throw new AppError(500, 'Internal server error', message); + } } public async get(endpoint: string): Promise { - return await this.request(endpoint, { + return this.request(endpoint, { method: "GET", }); } public async post(endpoint: string, body: unknown): Promise { - return await this.request(endpoint, { + return this.request(endpoint, { method: "POST", headers: { "Content-Type": "application/json", @@ -48,4 +58,4 @@ class ApiClient { } } -export { ApiClient }; +export default new APIClient(`${baseURL}`)