diff --git a/apps/dashboard/src/api/api.client.ts b/apps/dashboard/src/api/api.client.ts index be08e051a0f..af57d28e81c 100644 --- a/apps/dashboard/src/api/api.client.ts +++ b/apps/dashboard/src/api/api.client.ts @@ -16,10 +16,14 @@ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'; const request = async ( endpoint: string, - method: HttpMethod = 'GET', - data?: unknown, - headers?: HeadersInit + options?: { + data?: unknown; + method?: HttpMethod; + headers?: HeadersInit; + version?: 'v1' | 'v2'; + } ): Promise => { + const { data, headers, method = 'GET', version = 'v1' } = options || {}; try { const jwt = await getToken(); const environmentId = getEnvironmentId(); @@ -38,7 +42,7 @@ const request = async ( } const baseUrl = API_HOSTNAME ?? 'https://api.novu.co'; - const response = await fetch(`${baseUrl}/v1${endpoint}`, config); + const response = await fetch(`${baseUrl}/${version}${endpoint}`, config); if (!response.ok) { const errorData = await response.json(); @@ -61,10 +65,14 @@ const request = async ( } }; -export const get = (endpoint: string) => request(endpoint, 'GET'); - -export const post = (endpoint: string, data: unknown) => request(endpoint, 'POST', data); - -export const put = (endpoint: string, data: unknown) => request(endpoint, 'PUT', data); +export const get = (endpoint: string) => request(endpoint, { method: 'GET' }); +export const post = (endpoint: string, data: unknown) => request(endpoint, { method: 'POST', data }); +export const put = (endpoint: string, data: unknown) => request(endpoint, { method: 'PUT', data }); +export const del = (endpoint: string) => request(endpoint, { method: 'DELETE' }); -export const del = (endpoint: string) => request(endpoint, 'DELETE'); +export const getV2 = (endpoint: string) => request(endpoint, { version: 'v2', method: 'GET' }); +export const postV2 = (endpoint: string, data: unknown) => + request(endpoint, { version: 'v2', method: 'POST', data }); +export const putV2 = (endpoint: string, data: unknown) => + request(endpoint, { version: 'v2', method: 'PUT', data }); +export const delV2 = (endpoint: string) => request(endpoint, { version: 'v2', method: 'DELETE' });