| 38 | } |
| 39 | |
| 40 | const createMethod = (method: HttpMethod) => { |
| 41 | return async <T>( |
| 42 | endpoint: string, |
| 43 | data?: unknown, |
| 44 | options: RequestInit = {} |
| 45 | ) => { |
| 46 | const headers: HeadersInit = { |
| 47 | "Content-Type": "application/json", |
| 48 | ...(TWENTY_FIRST_API_KEY ? { "x-api-key": TWENTY_FIRST_API_KEY } : {}), |
| 49 | ...options.headers, |
| 50 | }; |
| 51 | |
| 52 | console.log("BASE_URL", BASE_URL); |
| 53 | |
| 54 | const response = await fetch(`${BASE_URL}${endpoint}`, { |
| 55 | ...options, |
| 56 | method, |
| 57 | headers, |
| 58 | ...(data ? { body: JSON.stringify(data) } : {}), |
| 59 | }); |
| 60 | |
| 61 | console.log("response", response); |
| 62 | return { status: response.status, data: (await response.json()) as T }; |
| 63 | }; |
| 64 | }; |
| 65 | |
| 66 | export const twentyFirstClient: HttpClient = { |
| 67 | get: createMethod("GET"), |