Skip to content

Commit f12ef8f

Browse files
authored
feat(dashboard): Api methods for v2 (#6642)
1 parent 7830b4a commit f12ef8f

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

apps/dashboard/src/api/api.client.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
1616

1717
const request = async <T>(
1818
endpoint: string,
19-
method: HttpMethod = 'GET',
20-
data?: unknown,
21-
headers?: HeadersInit
19+
options?: {
20+
data?: unknown;
21+
method?: HttpMethod;
22+
headers?: HeadersInit;
23+
version?: 'v1' | 'v2';
24+
}
2225
): Promise<T> => {
26+
const { data, headers, method = 'GET', version = 'v1' } = options || {};
2327
try {
2428
const jwt = await getToken();
2529
const environmentId = getEnvironmentId();
@@ -38,7 +42,7 @@ const request = async <T>(
3842
}
3943

4044
const baseUrl = API_HOSTNAME ?? 'https://api.novu.co';
41-
const response = await fetch(`${baseUrl}/v1${endpoint}`, config);
45+
const response = await fetch(`${baseUrl}/${version}${endpoint}`, config);
4246

4347
if (!response.ok) {
4448
const errorData = await response.json();
@@ -61,10 +65,14 @@ const request = async <T>(
6165
}
6266
};
6367

64-
export const get = <T>(endpoint: string) => request<T>(endpoint, 'GET');
65-
66-
export const post = <T>(endpoint: string, data: unknown) => request<T>(endpoint, 'POST', data);
67-
68-
export const put = <T>(endpoint: string, data: unknown) => request<T>(endpoint, 'PUT', data);
68+
export const get = <T>(endpoint: string) => request<T>(endpoint, { method: 'GET' });
69+
export const post = <T>(endpoint: string, data: unknown) => request<T>(endpoint, { method: 'POST', data });
70+
export const put = <T>(endpoint: string, data: unknown) => request<T>(endpoint, { method: 'PUT', data });
71+
export const del = <T>(endpoint: string) => request<T>(endpoint, { method: 'DELETE' });
6972

70-
export const del = <T>(endpoint: string) => request<T>(endpoint, 'DELETE');
73+
export const getV2 = <T>(endpoint: string) => request<T>(endpoint, { version: 'v2', method: 'GET' });
74+
export const postV2 = <T>(endpoint: string, data: unknown) =>
75+
request<T>(endpoint, { version: 'v2', method: 'POST', data });
76+
export const putV2 = <T>(endpoint: string, data: unknown) =>
77+
request<T>(endpoint, { version: 'v2', method: 'PUT', data });
78+
export const delV2 = <T>(endpoint: string) => request<T>(endpoint, { version: 'v2', method: 'DELETE' });

0 commit comments

Comments
 (0)