diff --git a/server/api/externalapi.ts b/server/api/externalapi.ts index cfdd19131..d17ebf99e 100644 --- a/server/api/externalapi.ts +++ b/server/api/externalapi.ts @@ -70,11 +70,11 @@ class ExternalAPI { config?: RequestInit ): Promise { const headers = { ...this.defaultHeaders, ...config?.headers }; - const cacheKey = this.serializeCacheKey( - endpoint, - { ...this.params, ...params }, - headers - ); + const cacheKey = this.serializeCacheKey(endpoint, { + ...this.params, + ...params, + headers, + }); const cachedItem = this.cache?.get(cacheKey); if (cachedItem) { @@ -84,10 +84,7 @@ class ExternalAPI { const url = this.formatUrl(endpoint, params); const response = await this.fetch(url, { ...config, - headers: { - ...this.defaultHeaders, - ...config?.headers, - }, + headers, }); if (!response.ok) { const text = await response.text(); @@ -115,11 +112,11 @@ class ExternalAPI { config?: RequestInit ): Promise { const headers = { ...this.defaultHeaders, ...config?.headers }; - const cacheKey = this.serializeCacheKey( - endpoint, - { ...this.params, ...params }, - headers - ); + const cacheKey = this.serializeCacheKey(endpoint, { + config: { ...this.params, ...params }, + headers, + data, + }); const cachedItem = this.cache?.get(cacheKey); if (cachedItem) { @@ -130,10 +127,7 @@ class ExternalAPI { const response = await this.fetch(url, { method: 'POST', ...config, - headers: { - ...this.defaultHeaders, - ...config?.headers, - }, + headers, body: data ? JSON.stringify(data) : undefined, }); if (!response.ok) { @@ -162,11 +156,11 @@ class ExternalAPI { config?: RequestInit ): Promise { const headers = { ...this.defaultHeaders, ...config?.headers }; - const cacheKey = this.serializeCacheKey( - endpoint, - { ...this.params, ...params }, - headers - ); + const cacheKey = this.serializeCacheKey(endpoint, { + config: { ...this.params, ...params }, + data, + headers, + }); const cachedItem = this.cache?.get(cacheKey); if (cachedItem) { @@ -177,10 +171,7 @@ class ExternalAPI { const response = await this.fetch(url, { method: 'PUT', ...config, - headers: { - ...this.defaultHeaders, - ...config?.headers, - }, + headers, body: JSON.stringify(data), }); if (!response.ok) { @@ -237,12 +228,11 @@ class ExternalAPI { overwriteBaseUrl?: string ): Promise { const headers = { ...this.defaultHeaders, ...config?.headers }; - const cacheKey = this.serializeCacheKey( - endpoint, - { ...this.params, ...params }, - headers - ); - + const cacheKey = this.serializeCacheKey(endpoint, { + ...this.params, + ...params, + headers, + }); const cachedItem = this.cache?.get(cacheKey); if (cachedItem) { @@ -256,10 +246,7 @@ class ExternalAPI { const url = this.formatUrl(endpoint, params, overwriteBaseUrl); this.fetch(url, { ...config, - headers: { - ...this.defaultHeaders, - ...config?.headers, - }, + headers, }).then(async (response) => { if (!response.ok) { const text = await response.text(); @@ -282,10 +269,7 @@ class ExternalAPI { const url = this.formatUrl(endpoint, params, overwriteBaseUrl); const response = await this.fetch(url, { ...config, - headers: { - ...this.defaultHeaders, - ...config?.headers, - }, + headers, }); if (!response.ok) { const text = await response.text(); @@ -305,10 +289,10 @@ class ExternalAPI { return data; } - protected removeCache(endpoint: string, params?: Record) { + protected removeCache(endpoint: string, options?: Record) { const cacheKey = this.serializeCacheKey(endpoint, { ...this.params, - ...params, + ...options, }); this.cache?.del(cacheKey); } @@ -337,15 +321,13 @@ class ExternalAPI { private serializeCacheKey( endpoint: string, - params?: Record, - headers?: Record + options?: Record ) { - const key = `${this.baseUrl}${endpoint}`; - if (!params && !headers) { - return key; + if (!options) { + return `${this.baseUrl}${endpoint}`; } - return `${key}${JSON.stringify({ params, headers })}`; + return `${this.baseUrl}${endpoint}${JSON.stringify(options)}`; } private async getDataFromResponse(response: Response) {