|
| 1 | +type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; |
| 2 | +const PROXY_URL = "https://proxy.attachment-aditya.workers.dev/"; |
| 3 | + |
| 4 | +interface BaseCallerConfig { |
| 5 | + baseApiUrl: string; |
| 6 | + defaultHeaders?: { [key: string]: string }; |
| 7 | + referrer?: string; |
| 8 | + mode?: "cors" | "no-cors" | "same-origin"; |
| 9 | + credentials?: "omit" | "same-origin" | "include"; |
| 10 | + proxied?: boolean; |
| 11 | + lsCacheTTL?: number | null; |
| 12 | + lsCached?: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +export class BaseCaller { |
| 16 | + private baseApiUrl: string; |
| 17 | + private defaultHeaders: { [key: string]: string }; |
| 18 | + private referrer?: string; |
| 19 | + private mode?: "cors" | "no-cors" | "same-origin"; |
| 20 | + private credentials?: "omit" | "same-origin" | "include"; |
| 21 | + private proxied: boolean; |
| 22 | + private lsCacheTTL: number | null; |
| 23 | + private lsCached: boolean; |
| 24 | + |
| 25 | + constructor({ |
| 26 | + baseApiUrl, |
| 27 | + defaultHeaders = {}, |
| 28 | + referrer, |
| 29 | + mode = "cors", |
| 30 | + credentials = "same-origin", |
| 31 | + proxied=false, |
| 32 | + lsCacheTTL = null, |
| 33 | + lsCached = false, |
| 34 | + }: BaseCallerConfig) { |
| 35 | + this.baseApiUrl = baseApiUrl; |
| 36 | + this.defaultHeaders = defaultHeaders; |
| 37 | + this.referrer = referrer; |
| 38 | + this.mode = mode; |
| 39 | + this.credentials = credentials; |
| 40 | + this.proxied = proxied; |
| 41 | + this.lsCacheTTL = lsCacheTTL; |
| 42 | + this.lsCached = lsCached; |
| 43 | + } |
| 44 | + |
| 45 | + async callApi( |
| 46 | + endpoint: string, |
| 47 | + method: Method = "GET", |
| 48 | + body: string | undefined = undefined, |
| 49 | + extraOptions: Partial<{ |
| 50 | + headers: { [key: string]: string }; |
| 51 | + referrer: string; |
| 52 | + mode: "cors" | "no-cors" | "same-origin"; |
| 53 | + credentials: "omit" | "same-origin" | "include"; |
| 54 | + }> = {} |
| 55 | + ) { |
| 56 | + const keyStatic = `${this.baseApiUrl}:${endpoint}` |
| 57 | + const keyDynamic = `${method}:${body}`; |
| 58 | + const lsCacheKey = `apiCache::${keyStatic}:${keyDynamic}`; |
| 59 | + const lsCachedData = localStorage.getItem(lsCacheKey); |
| 60 | + |
| 61 | + if (lsCachedData) { |
| 62 | + const { timestamp, data } = JSON.parse(lsCachedData); |
| 63 | + const alive = this.lsCacheTTL |
| 64 | + ? (Date.now() - timestamp < this.lsCacheTTL) |
| 65 | + : true; |
| 66 | + |
| 67 | + if (!alive) { |
| 68 | + localStorage.removeItem(lsCacheKey); |
| 69 | + } else { |
| 70 | + return data; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + const finalUrl = this.baseApiUrl + endpoint; |
| 75 | + |
| 76 | + const finalHeaders = { |
| 77 | + method, |
| 78 | + body, |
| 79 | + headers: this.defaultHeaders, |
| 80 | + referrer: this.referrer, |
| 81 | + mode: this.mode, |
| 82 | + credentials: this.credentials, |
| 83 | + ...extraOptions, |
| 84 | + } |
| 85 | + |
| 86 | + let response: Response; |
| 87 | + |
| 88 | + if (!this.proxied) { |
| 89 | + response = await fetch(finalUrl, finalHeaders); |
| 90 | + } else { |
| 91 | + response = await fetch(PROXY_URL, { |
| 92 | + method: "POST", |
| 93 | + headers: { |
| 94 | + "Content-Type": "application/json", |
| 95 | + }, |
| 96 | + body: JSON.stringify({ |
| 97 | + url: finalUrl, |
| 98 | + options: finalHeaders, |
| 99 | + }), |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + if (!response.ok) { |
| 104 | + throw new Error(`API call failed with status ${response.status}`); |
| 105 | + } |
| 106 | + |
| 107 | + const responseData = await response.json(); |
| 108 | + |
| 109 | + if (this.lsCached) { |
| 110 | + localStorage.setItem(lsCacheKey, JSON.stringify({ |
| 111 | + timestamp: Date.now(), |
| 112 | + data: responseData, |
| 113 | + })); |
| 114 | + } |
| 115 | + |
| 116 | + return responseData; |
| 117 | + } |
| 118 | +} |
| 119 | + |
0 commit comments