(
url: string,
responseSchema?: ZodType<T>,
options?: RequestOptions & { responseType?: R }
)
| 41 | } |
| 42 | |
| 43 | async request<T, R extends ResponseType = ResponseType.JSON>( |
| 44 | url: string, |
| 45 | responseSchema?: ZodType<T>, |
| 46 | options?: RequestOptions & { responseType?: R } |
| 47 | ): Promise< |
| 48 | R extends ResponseType.JSON |
| 49 | ? T |
| 50 | : R extends ResponseType.TEXT |
| 51 | ? string |
| 52 | : R extends ResponseType.BLOB |
| 53 | ? Blob |
| 54 | : R extends ResponseType.ARRAY_BUFFER |
| 55 | ? ArrayBuffer |
| 56 | : unknown |
| 57 | > { |
| 58 | try { |
| 59 | /** |
| 60 | * ---------- |
| 61 | * REQUEST |
| 62 | * ---------- |
| 63 | */ |
| 64 | const defaultOptions: RequestOptions = { |
| 65 | headers: { |
| 66 | Authorization: `Bearer ${this.bearerToken || ""}`, |
| 67 | }, |
| 68 | method: "GET", |
| 69 | responseType: ResponseType.JSON, |
| 70 | }; |
| 71 | |
| 72 | const mergedOptions: RequestOptions = { |
| 73 | ...defaultOptions, |
| 74 | ...(options ?? {}), |
| 75 | headers: { |
| 76 | ...defaultOptions.headers, |
| 77 | ...(options?.headers ?? {}), |
| 78 | }, |
| 79 | }; |
| 80 | |
| 81 | // Validate the base URL |
| 82 | if (!this.baseUrl) { |
| 83 | throw new AppError("Base URL is not set."); |
| 84 | } |
| 85 | |
| 86 | // Validate the bearer token |
| 87 | if (!this.bearerToken && !this.noAuthEndpoints.includes(url)) { |
| 88 | throw new AuthenticationError("You are not authenticated. Please login again."); |
| 89 | } |
| 90 | |
| 91 | // Construct the full URL |
| 92 | const fullUrl = new URL(url, this.baseUrl).toString(); |
| 93 | |
| 94 | // Prepare fetch options |
| 95 | const fetchOptions: RequestInit = { |
| 96 | method: mergedOptions.method, |
| 97 | headers: mergedOptions.headers, |
| 98 | signal: mergedOptions.signal, |
| 99 | }; |
| 100 |
no test coverage detected