diff --git a/server/api/externalapi.ts b/server/api/externalapi.ts index 9d6ef8d4..741ef58e 100644 --- a/server/api/externalapi.ts +++ b/server/api/externalapi.ts @@ -16,6 +16,10 @@ interface ExternalAPIOptions { rateLimit?: RateLimitOptions; } +interface CustomRequestConfig extends RequestInit { + params?: Record; +} + class ExternalAPI { protected fetch: typeof fetch; protected params: Record; @@ -67,12 +71,11 @@ class ExternalAPI { endpoint: string, params?: Record, ttl?: number, - config?: RequestInit + config?: CustomRequestConfig ): Promise { - const cacheKey = this.serializeCacheKey(endpoint, { - ...this.params, - ...params, - }); + const headers = { ...this.defaultHeaders, ...config?.headers }; + const cacheKey = this.serializeCacheKey(endpoint, config?.params, headers); + const cachedItem = this.cache?.get(cacheKey); if (cachedItem) { return cachedItem; @@ -325,13 +328,15 @@ class ExternalAPI { private serializeCacheKey( endpoint: string, - params?: Record + params?: Record, + headers?: Record ) { - if (!params) { - return `${this.baseUrl}${endpoint}`; + const key = `${this.baseUrl}${endpoint}`; + if (!params && !headers) { + return key; } - return `${this.baseUrl}${endpoint}${JSON.stringify(params)}`; + return `${key}${JSON.stringify({ params, headers })}`; } private async getDataFromResponse(response: Response) {