Skip to content

Commit

Permalink
refactor: apply review commits and rename params to options in cachekey
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenbagel committed Jan 11, 2025
1 parent 4308d13 commit 1d183c5
Showing 1 changed file with 31 additions and 49 deletions.
80 changes: 31 additions & 49 deletions server/api/externalapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ class ExternalAPI {
config?: RequestInit
): Promise<T> {
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<T>(cacheKey);
if (cachedItem) {
Expand All @@ -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();
Expand Down Expand Up @@ -115,11 +112,11 @@ class ExternalAPI {
config?: RequestInit
): Promise<T> {
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<T>(cacheKey);
if (cachedItem) {
Expand All @@ -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) {
Expand Down Expand Up @@ -162,11 +156,11 @@ class ExternalAPI {
config?: RequestInit
): Promise<T> {
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<T>(cacheKey);
if (cachedItem) {
Expand All @@ -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) {
Expand Down Expand Up @@ -237,12 +228,11 @@ class ExternalAPI {
overwriteBaseUrl?: string
): Promise<T> {
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<T>(cacheKey);

if (cachedItem) {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -305,10 +289,10 @@ class ExternalAPI {
return data;
}

protected removeCache(endpoint: string, params?: Record<string, string>) {
protected removeCache(endpoint: string, options?: Record<string, string>) {
const cacheKey = this.serializeCacheKey(endpoint, {
...this.params,
...params,
...options,
});
this.cache?.del(cacheKey);
}
Expand Down Expand Up @@ -337,15 +321,13 @@ class ExternalAPI {

private serializeCacheKey(
endpoint: string,
params?: Record<string, unknown>,
headers?: Record<string, unknown>
options?: Record<string, unknown>
) {
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) {
Expand Down

0 comments on commit 1d183c5

Please sign in to comment.