Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

axios-cache-interceptor

Package Overview
Dependencies
Maintainers
1
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

axios-cache-interceptor - npm Package Compare versions

Comparing version 0.8.1 to 0.8.2

src/cache/axios.ts

2

cjs/interceptors/request.js

@@ -17,3 +17,3 @@ "use strict";

}
const key = axios.generateKey(config);
const key = (config.id = axios.generateKey(config));
// Assumes that the storage handled staled responses

@@ -20,0 +20,0 @@ let cache = await axios.storage.get(key);

@@ -24,3 +24,4 @@ "use strict";

var _a, _b;
(_a = response.id) !== null && _a !== void 0 ? _a : (response.id = axios.generateKey(response.config));
var _c;
response.id = (_a = (_c = response.config).id) !== null && _a !== void 0 ? _a : (_c.id = axios.generateKey(response.config));
(_b = response.cached) !== null && _b !== void 0 ? _b : (response.cached = false);

@@ -27,0 +28,0 @@ // Response is already cached

@@ -0,3 +1,26 @@

import type { CacheRequestConfig } from '../cache/axios';
import type { KeyGenerator } from './types';
export declare const defaultKeyGenerator: KeyGenerator;
/**
* Builds an generator that received the {@link CacheRequestConfig} and should return a
* string id for it.
*/
export declare function buildKeyGenerator<R = unknown, D = unknown>(hash: false, generator: KeyGenerator): KeyGenerator<R, D>;
/**
* Builds an generator that received the {@link CacheRequestConfig} and has it's return
* value hashed by {@link code}.
*
* ### You can return an object that is hashed into an unique number, example:
*
* ```js
* // This generator will return a hash code.
* // The code will only be the same if url, method and data are the same.
* const generator = buildKeyGenerator(true, ({ url, method, data }) => ({
* url,
* method,
* data
* }));
* ```
*/
export declare function buildKeyGenerator<R = unknown, D = unknown>(hash: true, generator: (options: CacheRequestConfig<R, D>) => unknown): KeyGenerator<R, D>;
export declare const defaultKeyGenerator: KeyGenerator<unknown, unknown>;
//# sourceMappingURL=key-generator.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultKeyGenerator = void 0;
exports.defaultKeyGenerator = exports.buildKeyGenerator = void 0;
const object_code_1 = require("object-code");
// Remove first and last '/' char, if present
const SLASHES_REGEX = /^\/|\/$/g;
const defaultKeyGenerator = ({ baseURL = '', url = '', method = 'get', params, id }) => {
if (id) {
return id;
}
// Remove trailing slashes
baseURL = baseURL.replace(SLASHES_REGEX, '');
url = url.replace(SLASHES_REGEX, '');
return `${
// method
method.toLowerCase()}::${
// complete url
baseURL + (baseURL && url ? '/' : '') + url}::${
// params
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
params ? JSON.stringify(params, Object.keys(params).sort()) : '{}'}`;
};
exports.defaultKeyGenerator = defaultKeyGenerator;
function buildKeyGenerator(hash, generator) {
return (request) => {
if (request.id) {
return request.id;
}
// Remove trailing slashes
request.baseURL && (request.baseURL = request.baseURL.replace(SLASHES_REGEX, ''));
request.url && (request.url = request.url.replace(SLASHES_REGEX, ''));
// lowercase method
request.method && (request.method = request.method.toLowerCase());
const result = generator(request);
return hash ? (0, object_code_1.code)(result).toString() : result;
};
}
exports.buildKeyGenerator = buildKeyGenerator;
exports.defaultKeyGenerator = buildKeyGenerator(true, ({ baseURL = '', url = '', method = 'get', params, data }) => {
return {
url: baseURL + (baseURL && url ? '/' : '') + url,
method,
params: params,
data
};
});

@@ -20,5 +20,5 @@ import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios';

/** A simple function that receives a cache request config and should return a string id for it. */
export declare type KeyGenerator = <R = unknown, D = unknown>(options: CacheRequestConfig<R, D>) => string;
export declare type KeyGenerator<R = unknown, D = unknown> = (options: CacheRequestConfig<R, D>) => string;
export declare type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
export declare type CacheUpdater<R, D> = 'delete' | ((cached: Exclude<StorageValue, LoadingStorageValue>, response: CacheAxiosResponse<R, D>) => MaybePromise<CachedStorageValue | 'delete' | 'ignore'>);
//# sourceMappingURL=types.d.ts.map

@@ -14,3 +14,3 @@ import { deferred } from 'fast-defer';

}
const key = axios.generateKey(config);
const key = (config.id = axios.generateKey(config));
// Assumes that the storage handled staled responses

@@ -17,0 +17,0 @@ let cache = await axios.storage.get(key);

@@ -21,3 +21,4 @@ import { testCachePredicate } from '../util/cache-predicate';

var _a, _b;
(_a = response.id) !== null && _a !== void 0 ? _a : (response.id = axios.generateKey(response.config));
var _c;
response.id = (_a = (_c = response.config).id) !== null && _a !== void 0 ? _a : (_c.id = axios.generateKey(response.config));
(_b = response.cached) !== null && _b !== void 0 ? _b : (response.cached = false);

@@ -24,0 +25,0 @@ // Response is already cached

@@ -0,3 +1,26 @@

import type { CacheRequestConfig } from '../cache/axios';
import type { KeyGenerator } from './types';
export declare const defaultKeyGenerator: KeyGenerator;
/**
* Builds an generator that received the {@link CacheRequestConfig} and should return a
* string id for it.
*/
export declare function buildKeyGenerator<R = unknown, D = unknown>(hash: false, generator: KeyGenerator): KeyGenerator<R, D>;
/**
* Builds an generator that received the {@link CacheRequestConfig} and has it's return
* value hashed by {@link code}.
*
* ### You can return an object that is hashed into an unique number, example:
*
* ```js
* // This generator will return a hash code.
* // The code will only be the same if url, method and data are the same.
* const generator = buildKeyGenerator(true, ({ url, method, data }) => ({
* url,
* method,
* data
* }));
* ```
*/
export declare function buildKeyGenerator<R = unknown, D = unknown>(hash: true, generator: (options: CacheRequestConfig<R, D>) => unknown): KeyGenerator<R, D>;
export declare const defaultKeyGenerator: KeyGenerator<unknown, unknown>;
//# sourceMappingURL=key-generator.d.ts.map

@@ -0,18 +1,25 @@

import { code } from 'object-code';
// Remove first and last '/' char, if present
const SLASHES_REGEX = /^\/|\/$/g;
export const defaultKeyGenerator = ({ baseURL = '', url = '', method = 'get', params, id }) => {
if (id) {
return id;
}
// Remove trailing slashes
baseURL = baseURL.replace(SLASHES_REGEX, '');
url = url.replace(SLASHES_REGEX, '');
return `${
// method
method.toLowerCase()}::${
// complete url
baseURL + (baseURL && url ? '/' : '') + url}::${
// params
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
params ? JSON.stringify(params, Object.keys(params).sort()) : '{}'}`;
};
export function buildKeyGenerator(hash, generator) {
return (request) => {
if (request.id) {
return request.id;
}
// Remove trailing slashes
request.baseURL && (request.baseURL = request.baseURL.replace(SLASHES_REGEX, ''));
request.url && (request.url = request.url.replace(SLASHES_REGEX, ''));
// lowercase method
request.method && (request.method = request.method.toLowerCase());
const result = generator(request);
return hash ? code(result).toString() : result;
};
}
export const defaultKeyGenerator = buildKeyGenerator(true, ({ baseURL = '', url = '', method = 'get', params, data }) => {
return {
url: baseURL + (baseURL && url ? '/' : '') + url,
method,
params: params,
data
};
});

@@ -20,5 +20,5 @@ import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios';

/** A simple function that receives a cache request config and should return a string id for it. */
export declare type KeyGenerator = <R = unknown, D = unknown>(options: CacheRequestConfig<R, D>) => string;
export declare type KeyGenerator<R = unknown, D = unknown> = (options: CacheRequestConfig<R, D>) => string;
export declare type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
export declare type CacheUpdater<R, D> = 'delete' | ((cached: Exclude<StorageValue, LoadingStorageValue>, response: CacheAxiosResponse<R, D>) => MaybePromise<CachedStorageValue | 'delete' | 'ignore'>);
//# sourceMappingURL=types.d.ts.map
{
"name": "axios-cache-interceptor",
"version": "0.8.1",
"version": "0.8.2",
"description": "Cache interceptor for axios",

@@ -11,7 +11,7 @@ "main": "./cjs/index.js",

"require": "./cjs/index.js",
"default": "./umd/es6.min.js"
"default": "./umd/es6.js"
},
"browser": "./umd/es6.min.js",
"jsdelivr": "./umd/es6.min.js",
"unpkg": "./umd/es6.min.js",
"browser": "./umd/es6.js",
"jsdelivr": "./umd/es6.js",
"unpkg": "./umd/es6.js",
"runkitExampleFilename": "./examples/runkit.js",

@@ -51,3 +51,4 @@ "scripts": {

"cache-parser": "^1.1.2",
"fast-defer": "^1.1.3"
"fast-defer": "^1.1.3",
"object-code": "^1.0.1"
},

@@ -54,0 +55,0 @@ "resolutions": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc