mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
chore: migrated to biomejs.dev
This commit is contained in:
parent
f8db646132
commit
37e2f1e85a
@ -1,21 +0,0 @@
|
||||
# Javascript
|
||||
coverage
|
||||
node_modules
|
||||
|
||||
# Output
|
||||
/dist
|
||||
/dev
|
||||
/test
|
||||
|
||||
# Random
|
||||
/ignore
|
||||
*.log
|
||||
|
||||
# Vscode
|
||||
.vscode/*
|
||||
!.vscode/launch.json
|
||||
|
||||
# Npm
|
||||
package-lock.json
|
||||
|
||||
benchmark/index.js
|
||||
21
.eslintrc
21
.eslintrc
@ -1,21 +0,0 @@
|
||||
{
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"plugins": ["@typescript-eslint"],
|
||||
"root": true,
|
||||
"env": { "browser": true, "node": true },
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/eslint-recommended",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"plugin:@typescript-eslint/recommended-requiring-type-checking",
|
||||
"plugin:prettier/recommended"
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/await-thenable": "off",
|
||||
"@typescript-eslint/restrict-template-expressions": "off"
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": "latest",
|
||||
"project": "./tsconfig.json"
|
||||
}
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
node_modules
|
||||
|
||||
/ignore
|
||||
/coverage
|
||||
/dist
|
||||
/dev
|
||||
|
||||
.yarn
|
||||
|
||||
changelog.md
|
||||
CHANGELOG.md
|
||||
|
||||
_comparison-benchmark.md
|
||||
pnpm-lock.yaml
|
||||
@ -1,5 +0,0 @@
|
||||
// https://github.com/arthurfiorette/prettier-config
|
||||
|
||||
module.exports = require('@arthurfiorette/prettier-config')({
|
||||
// Any options here will override @arthurfiorette/prettier-config defaults
|
||||
});
|
||||
@ -25,7 +25,11 @@ const runs = {};
|
||||
const app = require('express')();
|
||||
|
||||
app.get('/:name', ({ params }, res) => {
|
||||
data[params.name] ? data[params.name]++ : (data[params.name] = 1);
|
||||
if (data[params.name] === undefined) {
|
||||
data[params.name] = 1;
|
||||
} else {
|
||||
data[params.name]++;
|
||||
}
|
||||
|
||||
return res.json({
|
||||
computation: Math.random(),
|
||||
@ -40,21 +44,36 @@ Benny.suite(
|
||||
|
||||
Benny.add('axios', async () => {
|
||||
const name = 'axios';
|
||||
runs[name] ? runs[name]++ : (runs[name] = 1);
|
||||
|
||||
if (runs[name] === undefined) {
|
||||
runs[name] = 1;
|
||||
} else {
|
||||
runs[name]++;
|
||||
}
|
||||
|
||||
await AxiosInstance.get(`http://${config.host}:${config.port}/${name}`);
|
||||
}),
|
||||
|
||||
Benny.add('cache-interceptor', async () => {
|
||||
const name = 'cache-interceptor';
|
||||
runs[name] ? runs[name]++ : (runs[name] = 1);
|
||||
|
||||
if (runs[name] === undefined) {
|
||||
runs[name] = 1;
|
||||
} else {
|
||||
runs[name]++;
|
||||
}
|
||||
|
||||
await InterceptorInstance.get(`http://${config.host}:${config.port}/${name}`);
|
||||
}),
|
||||
|
||||
Benny.add('cache-adapter', async () => {
|
||||
const name = 'cache-adapter';
|
||||
runs[name] ? runs[name]++ : (runs[name] = 1);
|
||||
|
||||
if (runs[name] === undefined) {
|
||||
runs[name] = 1;
|
||||
} else {
|
||||
runs[name]++;
|
||||
}
|
||||
|
||||
await AdapterInstance.get(`http://${config.host}:${config.port}/${name}`);
|
||||
}),
|
||||
|
||||
40
biome.json
Normal file
40
biome.json
Normal file
@ -0,0 +1,40 @@
|
||||
{
|
||||
"$schema": "https://biomejs.dev/schemas/1.3.3/schema.json",
|
||||
"organizeImports": {
|
||||
"enabled": true
|
||||
},
|
||||
"linter": {
|
||||
"enabled": true,
|
||||
"rules": {
|
||||
"recommended": true,
|
||||
"style": {
|
||||
"noNonNullAssertion": "off",
|
||||
"noParameterAssign": "off"
|
||||
},
|
||||
"suspicious": {
|
||||
"noExplicitAny": "off"
|
||||
}
|
||||
}
|
||||
},
|
||||
"formatter": {
|
||||
"lineWidth": 100,
|
||||
"indentStyle": "space"
|
||||
},
|
||||
"javascript": {
|
||||
"formatter": {
|
||||
"quoteStyle": "single",
|
||||
"trailingComma": "none"
|
||||
}
|
||||
},
|
||||
"files": {
|
||||
"ignore": [
|
||||
"build/**/*",
|
||||
"dist/**/*",
|
||||
"dev/**/*",
|
||||
"coverage/**/*",
|
||||
"node_modules/**/*",
|
||||
"pnpm-lock.json",
|
||||
"docs/.vitepress/cache/**/*"
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -4,7 +4,7 @@
|
||||
const path = require('path');
|
||||
const TerserWebpackPlugin = require('terser-webpack-plugin');
|
||||
const { DefinePlugin } = require('webpack');
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
|
||||
const { version } = require('../package.json');
|
||||
|
||||
const COPYRIGHT = `
|
||||
@ -107,7 +107,7 @@ const config = ({
|
||||
const assets = comp.assets[filename];
|
||||
|
||||
// @ts-expect-error - _value is not a public property
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
||||
|
||||
assets._value = COPYRIGHT + '\n' + assets._value;
|
||||
}
|
||||
}
|
||||
|
||||
@ -185,7 +185,7 @@ const redisStorage = buildStorage({
|
||||
// Or if the cached value cannot enter in stale state.
|
||||
(value.state === 'stale' && value.ttl) ||
|
||||
(value.state === 'cached' && !canStale(value))
|
||||
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
?
|
||||
value.createdAt + value.ttl!
|
||||
: // otherwise, we can't determine when it should expire, so we keep
|
||||
// it indefinitely.
|
||||
|
||||
27
package.json
27
package.json
@ -44,11 +44,13 @@
|
||||
"docs:build": "vitepress build docs",
|
||||
"docs:dev": "vitepress dev docs --port 1227",
|
||||
"docs:serve": "vitepress serve docs",
|
||||
"format": "prettier --write .",
|
||||
"lint": "eslint . --ext .ts",
|
||||
"test": "c8 --reporter lcov --reporter text node --trace-warnings -r tsx/cjs -r ./test/setup --enable-source-maps --trace-warnings --test test/**/*.test.ts",
|
||||
"test:only": "c8 --reporter lcov --reporter text node --trace-warnings -r tsx/cjs -r ./test/setup --enable-source-maps --trace-warnings --test-only",
|
||||
"version": "auto-changelog -p && cp CHANGELOG.md docs/src/others/changelog.md && git add CHANGELOG.md docs/src/others/changelog.md"
|
||||
"test": "c8 --reporter lcov --reporter text node --trace-warnings -r @swc-node/register -r ./test/setup --enable-source-maps --trace-warnings --test test/**/*.test.ts",
|
||||
"test:only": "c8 --reporter lcov --reporter text node --trace-warnings -r @swc-node/register -r ./test/setup --enable-source-maps --trace-warnings --test-only",
|
||||
"version": "auto-changelog -p && cp CHANGELOG.md docs/src/others/changelog.md && git add CHANGELOG.md docs/src/others/changelog.md",
|
||||
"format": "biome format --write .",
|
||||
"lint": "biome check .",
|
||||
"lint:fix": "biome check --apply-unsafe .",
|
||||
"lint:ci": "biome ci ."
|
||||
},
|
||||
"resolutions": {
|
||||
"colors": "1.4.0"
|
||||
@ -59,26 +61,19 @@
|
||||
"object-code": "^1.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@arthurfiorette/prettier-config": "*",
|
||||
"@biomejs/biome": "^1.3.3",
|
||||
"@swc-node/register": "^1.6.8",
|
||||
"@swc/helpers": "^0.5.3",
|
||||
"@types/jsdom": "^21.1.2",
|
||||
"@types/node": "^20.5.0",
|
||||
"@types/webpack": "^5.28.1",
|
||||
"@typescript-eslint/eslint-plugin": "^5.59.9",
|
||||
"@typescript-eslint/parser": "^5.59.9",
|
||||
"auto-changelog": "^2.4.0",
|
||||
"axios": "^1.4.0",
|
||||
"c8": "^8.0.1",
|
||||
"es-check": "^7.1.1",
|
||||
"eslint": "^8.42.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
"eslint-plugin-prettier": "^4.2.1",
|
||||
"jsdom": "^22.1.0",
|
||||
"prettier": "^2.8.8",
|
||||
"prettier-plugin-jsdoc": "^0.4.2",
|
||||
"prettier-plugin-organize-imports": "^3.2.2",
|
||||
"ts-loader": "^9.4.3",
|
||||
"tslib": "^2.5.3",
|
||||
"tsx": "^4.1.2",
|
||||
"typescript": "^5.1.3",
|
||||
"vitepress": "1.0.0-rc.25",
|
||||
"webpack": "^5.86.0",
|
||||
@ -87,7 +82,7 @@
|
||||
"peerDependencies": {
|
||||
"axios": "^1"
|
||||
},
|
||||
"packageManager": "pnpm@8.7.1",
|
||||
"packageManager": "pnpm@8.10.2",
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
|
||||
1497
pnpm-lock.yaml
generated
1497
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
28
src/cache/axios.ts
vendored
28
src/cache/axios.ts
vendored
@ -5,7 +5,7 @@ import type {
|
||||
AxiosRequestConfig,
|
||||
AxiosResponse,
|
||||
AxiosResponseHeaders,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
|
||||
InternalAxiosRequestConfig
|
||||
} from 'axios';
|
||||
import type { CacheInstance, CacheProperties } from './cache';
|
||||
@ -96,26 +96,26 @@ export interface InternalCacheRequestConfig<R = any, D = any>
|
||||
* @see https://axios-cache-interceptor.js.org/guide/getting-started
|
||||
*/
|
||||
export interface AxiosCacheInstance extends CacheInstance, AxiosInstance {
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-new
|
||||
|
||||
new (config?: CacheRequestConfig): AxiosCacheInstance;
|
||||
|
||||
/**
|
||||
* @template T The type returned by this response
|
||||
* @template R The custom response type that the request can return
|
||||
* @template D The type that the request body use
|
||||
* @template T1 The type returned by this response
|
||||
* @template R1 The custom response type that the request can return
|
||||
* @template D1 The type that the request body use
|
||||
*/
|
||||
<T = any, D = any, R = CacheAxiosResponse<T, D>>(
|
||||
config: CacheRequestConfig<T, D>
|
||||
): Promise<R>;
|
||||
<T1 = any, D1 = any, R1 = CacheAxiosResponse<T1, D1>>(
|
||||
config: CacheRequestConfig<T1, D1>
|
||||
): Promise<R1>;
|
||||
/**
|
||||
* @template T The type returned by this response
|
||||
* @template R The custom response type that the request can return
|
||||
* @template D The type that the request body use
|
||||
* @template T2 The type returned by this response
|
||||
* @template R2 The custom response type that the request can return
|
||||
* @template D2 The type that the request body use
|
||||
*/
|
||||
<T = any, D = any, R = CacheAxiosResponse<T, D>>(
|
||||
<T2 = any, D2 = any, R2 = CacheAxiosResponse<T2, D2>>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig<T, D>
|
||||
): Promise<R>;
|
||||
config?: CacheRequestConfig<T2, D2>
|
||||
): Promise<R2>;
|
||||
|
||||
defaults: AxiosInstance['defaults'] & {
|
||||
cache: CacheProperties;
|
||||
|
||||
2
src/cache/create.ts
vendored
2
src/cache/create.ts
vendored
@ -54,7 +54,7 @@ export function setupCache(
|
||||
axiosCache.responseInterceptor =
|
||||
options.responseInterceptor || defaultResponseInterceptor(axiosCache);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
|
||||
axiosCache.debug = options.debug || function noop() {};
|
||||
|
||||
// CacheRequestConfig values
|
||||
|
||||
@ -57,6 +57,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
|
||||
const overrideCache = config.cache.override;
|
||||
|
||||
// Not cached, continue the request, and mark it as fetching
|
||||
// biome-ignore lint/suspicious/noConfusingLabels: required to break condition in simultaneous accesses
|
||||
ignoreAndRequest: if (
|
||||
cache.state === 'empty' ||
|
||||
cache.state === 'stale' ||
|
||||
@ -98,7 +99,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
|
||||
* Adds a default reject handler to catch when the request gets aborted without
|
||||
* others waiting for it.
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
|
||||
axios.waiting[config.id]!.catch(() => undefined);
|
||||
|
||||
await axios.storage.set(
|
||||
@ -115,13 +116,12 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
|
||||
(cache.state as 'stale'),
|
||||
|
||||
// Eslint complains a lot :)
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
|
||||
|
||||
data: cache.data as any,
|
||||
|
||||
// If the cache is empty and asked to override it, use the current timestamp
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
|
||||
createdAt:
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
overrideCache && !cache.createdAt ? Date.now() : (cache.createdAt as any)
|
||||
},
|
||||
config
|
||||
@ -218,7 +218,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
|
||||
status: cachedResponse.status,
|
||||
statusText: cachedResponse.statusText,
|
||||
cached: true,
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
|
||||
id: config.id!
|
||||
});
|
||||
};
|
||||
|
||||
@ -45,7 +45,7 @@ export function defaultResponseInterceptor(
|
||||
throw response;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
|
||||
response.id = response.config.id!;
|
||||
response.cached ??= false;
|
||||
|
||||
|
||||
@ -63,7 +63,7 @@ export function buildMemoryStorage(
|
||||
while (keys.length >= maxEntries) {
|
||||
// There's always at least one key here, otherwise it would not be
|
||||
// in the loop.
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
|
||||
delete storage.data[keys.shift()!];
|
||||
}
|
||||
}
|
||||
@ -115,12 +115,12 @@ export function buildMemoryStorage(
|
||||
// Looping forward, as older entries are more likely to be expired
|
||||
// than newer ones.
|
||||
while (++i < keys.length) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
(key = keys[i]!), (value = storage.data[key]!);
|
||||
key = keys[i]!;
|
||||
value = storage.data[key]!;
|
||||
|
||||
if (value.state === 'empty') {
|
||||
// this storage returns void.
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
|
||||
storage.remove(key);
|
||||
continue;
|
||||
}
|
||||
@ -128,7 +128,7 @@ export function buildMemoryStorage(
|
||||
// If the value is expired and can't be stale, remove it
|
||||
if (value.state === 'cached' && isExpired(value) && !canStale(value)) {
|
||||
// this storage returns void.
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
|
||||
storage.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import type { CacheAxiosResponse } from '../cache/axios';
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
|
||||
import type { CachePredicate, CachePredicateObject } from './types';
|
||||
|
||||
/** Tests an response against a {@link CachePredicateObject}. */
|
||||
|
||||
@ -46,19 +46,34 @@ export function buildKeyGenerator<R = unknown, D = unknown>(
|
||||
}
|
||||
|
||||
export const defaultKeyGenerator = buildKeyGenerator(
|
||||
({ baseURL = '', url = '', method = 'get', params, data }) => {
|
||||
({ baseURL, url, method, params, data }) => {
|
||||
// Remove trailing slashes to avoid generating different keys for the "same" final url.
|
||||
baseURL && (baseURL = baseURL.replace(SLASHES_REGEX, ''));
|
||||
url && (url = url.replace(SLASHES_REGEX, ''));
|
||||
if (baseURL !== undefined) {
|
||||
baseURL = baseURL.replace(SLASHES_REGEX, '');
|
||||
} else {
|
||||
// just to have a consistent hash
|
||||
baseURL = '';
|
||||
}
|
||||
|
||||
// lowercase method
|
||||
method && (method = method.toLowerCase() as Method);
|
||||
if (url !== undefined) {
|
||||
url = url.replace(SLASHES_REGEX, '');
|
||||
} else {
|
||||
// just to have a consistent hash
|
||||
url = '';
|
||||
}
|
||||
|
||||
if (method !== undefined) {
|
||||
method = method.toLowerCase() as Method;
|
||||
} else {
|
||||
// just to have a consistent hash
|
||||
method = 'get';
|
||||
}
|
||||
|
||||
return {
|
||||
url: baseURL + (baseURL && url ? '/' : '') + url,
|
||||
params: params as unknown,
|
||||
method,
|
||||
data
|
||||
params: params,
|
||||
method: method,
|
||||
data: data
|
||||
};
|
||||
}
|
||||
);
|
||||
);
|
||||
Loading…
x
Reference in New Issue
Block a user