| 58 | * Hub authentication has been removed - requests are made without auth headers. |
| 59 | */ |
| 60 | export async function makeAuthenticatedRequest<T = any>( |
| 61 | endpoint: string, |
| 62 | options: ApiRequestOptions = {}, |
| 63 | ): Promise<ApiResponse<T>> { |
| 64 | const { method = "GET", body, headers = {} } = options; |
| 65 | |
| 66 | const requestOptions: RequestInit = { |
| 67 | method, |
| 68 | headers: { |
| 69 | "Content-Type": "application/json", |
| 70 | ...headers, |
| 71 | }, |
| 72 | }; |
| 73 | |
| 74 | if (body) { |
| 75 | requestOptions.body = |
| 76 | typeof body === "string" ? body : JSON.stringify(body); |
| 77 | } |
| 78 | |
| 79 | try { |
| 80 | const url = new URL(endpoint, env.apiBase); |
| 81 | logger.debug(`Making ${method} request to: ${url.toString()}`); |
| 82 | |
| 83 | const response = await fetch(url, requestOptions); |
| 84 | |
| 85 | if (!response.ok) { |
| 86 | const errorText = await response.text(); |
| 87 | logger.error(`API request failed: ${response.status} ${errorText}`); |
| 88 | throw new ApiRequestError( |
| 89 | response.status, |
| 90 | response.statusText, |
| 91 | errorText, |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | let data: T; |
| 96 | const contentType = response.headers.get("content-type"); |
| 97 | if (contentType && contentType.includes("application/json")) { |
| 98 | data = await response.json(); |
| 99 | } else { |
| 100 | data = (await response.text()) as T; |
| 101 | } |
| 102 | |
| 103 | logger.debug(`API request successful: ${response.status}`); |
| 104 | |
| 105 | return { |
| 106 | data, |
| 107 | status: response.status, |
| 108 | ok: response.ok, |
| 109 | }; |
| 110 | } catch (error) { |
| 111 | if ( |
| 112 | error instanceof AuthenticationRequiredError || |
| 113 | error instanceof ApiRequestError |
| 114 | ) { |
| 115 | throw error; |
| 116 | } |
| 117 | |