Skip to content

Instantly share code, notes, and snippets.

@semlinker
Created October 27, 2022 23:41
Show Gist options
  • Save semlinker/3c656dff2a6ee4e538860a2ffe8528cd to your computer and use it in GitHub Desktop.
Save semlinker/3c656dff2a6ee4e538860a2ffe8528cd to your computer and use it in GitHub Desktop.
Proxy Pattern in TypeScript
class HttpServiceProxy {
private httpService: HttpService;
private dataCache: Map<string, any> = new Map();
constructor() {
this.httpService = new HttpService();
}
async sendRequest(method: string, url: string, body?: BodyInit) {
const cacheKey = method + ":" + url;
if (this.dataCache.has(cacheKey)) {
console.log(`Use key: ${cacheKey} to get cached data`);
return this.dataCache.get(cacheKey);
}
const response = await this.httpService.sendRequest(method, url, body);
this.dataCache.set(cacheKey, response);
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment