UNPKG

3.12 kBPlain TextView Raw
1/* eslint-disable @typescript-eslint/no-explicit-any */
2import type { CacheAxiosResponse } from 'axios-cache-interceptor';
3import { hash } from 'object-code';
4
5/**
6 * All available options for the `axios-cache-hooks` package.
7 *
8 * @see https://tinylibs.js.org/packages/axios-cache-hooks
9 */
10export type AxiosCacheHooksOptions = {
11 /**
12 * A function that must return parameter index that accepts a `CacheRequestConfig` object.
13 *
14 * **Tip**: You can use `function.length` to find the number of available parameters.
15 *
16 * Default implementation returns the last function parameter.
17 *
18 * @default defaultConfigIndexFinder
19 * @param {Function} fn The provided axios call function
20 * @param {any[]} args All provided arguments to the function. Note, unprovided
21 * arguments won't be present in the array.
22 * @see {@link defaultConfigIndexFinder}
23 */
24 configIndexFinder: ConfigIndexFinder;
25
26 /**
27 * A function that returns a unique id for a given response or error.
28 *
29 * Used to determine if a response is different than the previous one by comparing its
30 * generated hash.
31 *
32 * Default implementation uses `object-code` library.
33 *
34 * **Note**: Returning undefined will ignore the given error or response.
35 *
36 * @default defaultHashGenerator
37 * @param {CacheAxiosResponse} [response] The axios response, if present.
38 * @param {any} [error] A thrown error, if any
39 * @see {@link defaultHashGenerator}
40 */
41 hashGenerator: HashGenerator;
42};
43
44/**
45 * A function that must return parameter index that accepts a `CacheRequestConfig` object.
46 *
47 * **Tip**: You can use `function.length` to find the number of available parameters.
48 *
49 * Default implementation returns the last function parameter.
50 *
51 * @default defaultConfigIndexFinder
52 * @param {Function} fn The provided axios call function
53 * @param {any[]} args All provided arguments to the function. Note, unprovided arguments
54 * won't be present in the array.
55 * @see {@link defaultConfigIndexFinder}
56 */
57// eslint-disable-next-line @typescript-eslint/ban-types
58export type ConfigIndexFinder = (fn: Function, ...args: any[]) => number;
59
60/**
61 * A function that returns a unique id for a given response or error.
62 *
63 * Used to determine if a response is different than the previous one by comparing its
64 * generated hash.
65 *
66 * Default implementation uses `object-code` library.
67 *
68 * **Note**: Returning undefined will ignore the given error or response.
69 *
70 * @default defaultHashGenerator
71 * @param {CacheAxiosResponse} [response] The axios response, if present.
72 * @param {any} [error] A thrown error, if any
73 * @see {@link defaultHashGenerator}
74 */
75export type HashGenerator = (response?: CacheAxiosResponse, error?: any) => number;
76
77export const defaultConfigIndexFinder: ConfigIndexFinder = (fn) => {
78 return fn.length - 1;
79};
80
81export const defaultHashGenerator: HashGenerator = (res, err) => {
82 return res
83 ? hash({ h: res.headers, s: res.status, t: res.statusText })
84 : err
85 ? // eslint-disable-next-line
86 hash({ m: err.message, c: err.code, n: err.name, j: err.toJSON() })
87 : 0;
88};