Created
August 21, 2022 10:20
-
-
Save felinto-dev/a72a44dbe8613e6b9d91697593ac086e to your computer and use it in GitHub Desktop.
Revisions
-
felinto-dev created this gist
Aug 21, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ import { HttpService } from '@nestjs/axios'; import { Injectable } from '@nestjs/common'; import { setupCache, buildMemoryStorage, defaultKeyGenerator, } from 'axios-cache-interceptor'; @Injectable() export class ApiService { constructor(private readonly httpService: HttpService) {} private readonly axiosCacheWrapper = setupCache(this.httpService.axiosRef, { storage: buildMemoryStorage(), generateKey: defaultKeyGenerator, }); async getAxiosRequest() { const response = await this.axiosCacheWrapper.get( 'https://example.com/wp/v2/keywords', { params: {}, cache: { ttl: 1000 * 60 * 60, // 1 hour interpretHeader: false, methods: ['get'], cachePredicate: { statusCheck: (status) => status >= 200 && status < 400, }, }, }, ); return response.data; } }