1.4.0 • Published 2 years ago

axios-etag-cache v1.4.0

Weekly downloads
864
License
ISC
Repository
github
Last release
2 years ago

axios-etag-cache

Axios etag interceptor to enable If-None-Match request with ETAG support

Basic use:

const axios = require('axios');
const { axiosETAGCache } = require('axios-etag-cache');

// Apply the axios ETAG interceptor
const axiosWithETAGCache = axiosETAGCache(axios);

axiosWithETAGCache
  .get('http://example.com')
  .then(console.log)
  .catch(console.error);

Allow POST requests

const axiosWithETAGCache = axiosETAGCache(axios, {enablePost: true});

External Storage

Example implementation using RXDB

import { axiosETAGCache, BaseCache, CacheValue } from "axios-etag-cache";

const cacheSchema = {
  title: "response cache schema",
  version: 4,
  primaryKey: {
    key: "url",    
    fields: ["url"],     
    separator: "|",
  },
  type: "object",
  properties: {
    url: {
      type: "string", maxLength: 100, 
    }, 
    etag: {type: "string"},
    lastHitAt: {type: "number"}, 
    createdAt: {type: "number"}, 
    value: {type: "object"}
  },
  required: ["url", "etag", "value"],
  indexes: ["url"]
}

class RxDBStorageCache extends BaseCache {
  flushAll(): any {
  }

  async get(key: string): Promise<CacheValue | undefined> {
    const cache = (
      await db["responseCache"].findOne({
        selector: {url: key},
      }).exec())?.toJSON();
    if (cache) {
      const payload: CacheValue = {
        value: cache.value,
        etag: cache.etag,
        createdAt: cache.createdAt,
        lastHitAt: cache.lastHitAt
      } as CacheValue;
      return payload;
    }
    return undefined;
  }

  set(key: string, value: CacheValue): any {
    db["responseCache"].upsert({
      url: key,
      value: value.value,
      etag: value.etag,
      createdAt: value.createdAt,
      lastHitAt: value.lastHitAt,
    });
  }
}

const axiosWithETAGCache = axiosETAGCache(axios, {cacheClass: RxDBStorageCache});
1.4.0

2 years ago

1.3.0

2 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

7 years ago

1.0.1

7 years ago