mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
25 lines
675 B
TypeScript
25 lines
675 B
TypeScript
import type { KeyGenerator } from './types';
|
|
|
|
// Remove first and last '/' char, if present
|
|
// https://regex101.com/r/ENqrFy/1
|
|
const SLASHES_REGEX = /^\/|\/+$/g;
|
|
|
|
export const defaultKeyGenerator: KeyGenerator = ({
|
|
baseURL = '',
|
|
url = '',
|
|
method: nullableMethod,
|
|
params,
|
|
id
|
|
}) => {
|
|
if (id) return String(id);
|
|
|
|
// Remove trailing slashes
|
|
baseURL = baseURL.replace(SLASHES_REGEX, '');
|
|
url = url.replace(SLASHES_REGEX, '');
|
|
|
|
const method = nullableMethod?.toLowerCase() || 'get';
|
|
const jsonParams = params ? JSON.stringify(params, Object.keys(params).sort()) : '{}';
|
|
|
|
return `${method}::${baseURL + (url && baseURL ? '/' : '') + url}::${jsonParams}`;
|
|
};
|