Skip to content

Instantly share code, notes, and snippets.

@felinto-dev
Created August 21, 2022 10:20
Show Gist options
  • Select an option

  • Save felinto-dev/a72a44dbe8613e6b9d91697593ac086e to your computer and use it in GitHub Desktop.

Select an option

Save felinto-dev/a72a44dbe8613e6b9d91697593ac086e to your computer and use it in GitHub Desktop.

Revisions

  1. felinto-dev created this gist Aug 21, 2022.
    36 changes: 36 additions & 0 deletions api-nests.service.ts
    Original 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;
    }
    }