Compare commits
1 Commits
preview-fi
...
preview-fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a6e43901a6 |
@@ -16,6 +16,10 @@ interface ExternalAPIOptions {
|
|||||||
rateLimit?: RateLimitOptions;
|
rateLimit?: RateLimitOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface CustomRequestConfig extends RequestInit {
|
||||||
|
params?: Record<string, unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
class ExternalAPI {
|
class ExternalAPI {
|
||||||
protected fetch: typeof fetch;
|
protected fetch: typeof fetch;
|
||||||
protected params: Record<string, string>;
|
protected params: Record<string, string>;
|
||||||
@@ -67,12 +71,11 @@ class ExternalAPI {
|
|||||||
endpoint: string,
|
endpoint: string,
|
||||||
params?: Record<string, string>,
|
params?: Record<string, string>,
|
||||||
ttl?: number,
|
ttl?: number,
|
||||||
config?: RequestInit
|
config?: CustomRequestConfig
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
const cacheKey = this.serializeCacheKey(endpoint, {
|
const headers = { ...this.defaultHeaders, ...config?.headers };
|
||||||
...this.params,
|
const cacheKey = this.serializeCacheKey(endpoint, config?.params, headers);
|
||||||
...params,
|
|
||||||
});
|
|
||||||
const cachedItem = this.cache?.get<T>(cacheKey);
|
const cachedItem = this.cache?.get<T>(cacheKey);
|
||||||
if (cachedItem) {
|
if (cachedItem) {
|
||||||
return cachedItem;
|
return cachedItem;
|
||||||
@@ -325,13 +328,15 @@ class ExternalAPI {
|
|||||||
|
|
||||||
private serializeCacheKey(
|
private serializeCacheKey(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
params?: Record<string, unknown>
|
params?: Record<string, unknown>,
|
||||||
|
headers?: Record<string, unknown>
|
||||||
) {
|
) {
|
||||||
if (!params) {
|
const key = `${this.baseUrl}${endpoint}`;
|
||||||
return `${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) {
|
private async getDataFromResponse(response: Response) {
|
||||||
|
|||||||
Reference in New Issue
Block a user