diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index f1a2f6e..0000000 --- a/.prettierrc +++ /dev/null @@ -1,11 +0,0 @@ -{ - "printWidth": 80, - "semi": true, - "singleQuote": true, - "trailingComma": "none", - "arrowParens": "always", - "tsdoc": true, - "jsdocSpaces": 1, - "jsdocPrintWidth": 100, - "jsdocSingleLineComment": false -} diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..fa895e4 --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,8 @@ +// https://github.com/ArthurFiorette/prettier-config + +module.exports = require('@arthurfiorette/prettier-config')({ + tsdoc: true, + jsdocSpaces: 1, + jsdocPrintWidth: 100, + jsdocSingleLineComment: false +}); diff --git a/README.md b/README.md index 6553501..78abc42 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,8 @@ yarn add axios axios-cache-interceptor ### Inspiration -This project is highly inspired by several projects, written entirely in typescript, supporting https headers and much more. +This project is highly inspired by several projects, written entirely in typescript, supporting +https headers and much more. Take a look at some similar projects: @@ -116,6 +117,7 @@ Licensed under the **MIT**. See [`LICENSE`](LICENSE) for more informations. ### Contact -See my contact information on my [github profile](https://github.com/ArthurFiorette) or open a new issue. +See my contact information on my [github profile](https://github.com/ArthurFiorette) or open a new +issue.
diff --git a/package.json b/package.json index 076656f..b4f0be0 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,11 @@ "url": "https://github.com/ArthurFiorette/axios-cache-interceptor/issues" }, "homepage": "https://github.com/ArthurFiorette/axios-cache-interceptor#readme", + "dependencies": { + "@tusbar/cache-control": "^0.6.0" + }, "devDependencies": { + "@arthurfiorette/prettier-config": "^1.0.5", "@types/node": "^16.7.10", "@typescript-eslint/eslint-plugin": "^4.30.0", "@typescript-eslint/parser": "^4.30.0", @@ -44,8 +48,5 @@ "prettier-plugin-jsdoc": "^0.3.23", "prettier-plugin-organize-imports": "^2.3.3", "typescript": "^4.4.2" - }, - "dependencies": { - "@tusbar/cache-control": "^0.6.0" } } diff --git a/src/axios/cache.ts b/src/axios/cache.ts index 2c82a41..fe3050b 100644 --- a/src/axios/cache.ts +++ b/src/axios/cache.ts @@ -7,10 +7,7 @@ import { AxiosCacheInstance, CacheInstance, CacheRequestConfig } from './types'; type Options = CacheRequestConfig['cache'] & Partial; -export function createCache( - axios: AxiosInstance, - options: Options = {} -): AxiosCacheInstance { +export function createCache(axios: AxiosInstance, options: Options = {}): AxiosCacheInstance { const axiosCache = axios as AxiosCacheInstance; axiosCache.storage = options.storage || new MemoryStorage(); diff --git a/src/axios/types.ts b/src/axios/types.ts index 948e104..591fa83 100644 --- a/src/axios/types.ts +++ b/src/axios/types.ts @@ -94,33 +94,17 @@ export interface AxiosCacheInstance extends AxiosInstance, CacheInstance { defaults: CacheRequestConfig; interceptors: { request: AxiosInterceptorManager; - response: AxiosInterceptorManager< - AxiosResponse & { config: CacheRequestConfig } - >; + response: AxiosInterceptorManager; }; getUri(config?: CacheRequestConfig): string; - request>( - config: CacheRequestConfig - ): Promise; + request>(config: CacheRequestConfig): Promise; - get>( - url: string, - config?: CacheRequestConfig - ): Promise; - delete>( - url: string, - config?: CacheRequestConfig - ): Promise; - head>( - url: string, - config?: CacheRequestConfig - ): Promise; - options>( - url: string, - config?: CacheRequestConfig - ): Promise; + get>(url: string, config?: CacheRequestConfig): Promise; + delete>(url: string, config?: CacheRequestConfig): Promise; + head>(url: string, config?: CacheRequestConfig): Promise; + options>(url: string, config?: CacheRequestConfig): Promise; post>( url: string, data?: any, diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index a2b204e..3c4c482 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -1,18 +1,11 @@ import { AxiosCacheInstance } from '../axios/types'; -import { - CACHED_RESPONSE_STATUS, - CACHED_RESPONSE_STATUS_TEXT -} from '../constants'; +import { CACHED_RESPONSE_STATUS, CACHED_RESPONSE_STATUS_TEXT } from '../constants'; import { Deferred } from '../utils/deferred'; export function applyRequestInterceptor(axios: AxiosCacheInstance) { axios.interceptors.request.use(async (config) => { // Only cache specified methods - if ( - config.cache?.methods?.some( - (method) => (config.method || 'get').toLowerCase() == method - ) - ) { + if (config.cache?.methods?.some((method) => (config.method || 'get').toLowerCase() == method)) { return config; } diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index 50a3c4a..7522f22 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -4,9 +4,7 @@ import { AxiosCacheInstance } from '../axios/types'; export function applyResponseInterceptor(axios: AxiosCacheInstance) { axios.interceptors.response.use(async (response) => { // Update other entries before updating himself - for (const [cacheKey, value] of Object.entries( - response.config.cache?.update || {} - )) { + for (const [cacheKey, value] of Object.entries(response.config.cache?.update || {})) { if (value == 'delete') { await axios.storage.remove(cacheKey); continue; @@ -56,9 +54,7 @@ export function applyResponseInterceptor(axios: AxiosCacheInstance) { } else { // If the cache expiration has not been set, use the default expiration. cache.expiration = - cache.expiration || - response.config.cache?.maxAge || - axios.defaults.cache!.maxAge!; + cache.expiration || response.config.cache?.maxAge || axios.defaults.cache!.maxAge!; } const data = { body: response.data, headers: response.headers }; diff --git a/src/storage/web.ts b/src/storage/web.ts index f4c331c..a93efb2 100644 --- a/src/storage/web.ts +++ b/src/storage/web.ts @@ -3,16 +3,11 @@ import { CacheStorage, StorageValue } from './types'; * A storage that uses any {@link Storage} as his storage. */ export abstract class WindowStorageWrapper implements CacheStorage { - constructor( - readonly storage: Storage, - readonly prefix: string = 'axios-cache:' - ) {} + constructor(readonly storage: Storage, readonly prefix: string = 'axios-cache:') {} get = async (key: string): Promise => { const json = this.storage.getItem(this.prefix + key); - return json - ? JSON.parse(json) - : { data: null, expiration: -1, state: 'empty' }; + return json ? JSON.parse(json) : { data: null, expiration: -1, state: 'empty' }; }; set = async (key: string, value: StorageValue): Promise => { diff --git a/src/utils/deferred.ts b/src/utils/deferred.ts index d77f3d8..7040b4d 100644 --- a/src/utils/deferred.ts +++ b/src/utils/deferred.ts @@ -36,14 +36,8 @@ export class Deferred { * @returns A Promise for the completion of which ever callback is executed. */ public readonly then = ( - onfulfilled?: - | ((value: T) => TResult1 | PromiseLike) - | undefined - | null, - onrejected?: - | ((reason: any) => TResult2 | PromiseLike) - | undefined - | null + onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, + onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null ): Promise => { return this.promise.then(onfulfilled, onrejected); }; @@ -54,10 +48,7 @@ export class Deferred { * @returns A Promise for the completion of the callback. */ public readonly catch = ( - onrejected?: - | ((reason: any) => TResult | PromiseLike) - | undefined - | null + onrejected?: ((reason: any) => TResult | PromiseLike) | undefined | null ): Promise => { return this.promise.catch(onrejected); }; @@ -68,9 +59,7 @@ export class Deferred { * @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected). * @returns A Promise for the completion of the callback. */ - public readonly finally = ( - onfinally?: (() => void) | undefined | null - ): Promise => { + public readonly finally = (onfinally?: (() => void) | undefined | null): Promise => { return this.promise.finally(onfinally); }; } diff --git a/src/utils/key-generator.ts b/src/utils/key-generator.ts index fc00623..853f69c 100644 --- a/src/utils/key-generator.ts +++ b/src/utils/key-generator.ts @@ -9,7 +9,5 @@ export function defaultKeyGenerator({ }: CacheRequestConfig): string { return id ? `id::${String(id)}` - : `${method?.toLowerCase() || 'get'}::${baseURL}::${url}::${JSON.stringify( - params || '{}' - )}`; + : `${method?.toLowerCase() || 'get'}::${baseURL}::${url}::${JSON.stringify(params || '{}')}`; } diff --git a/yarn.lock b/yarn.lock index a098168..ef69b5c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,6 +2,11 @@ # yarn lockfile v1 +"@arthurfiorette/prettier-config@^1.0.5": + version "1.0.5" + resolved "https://registry.yarnpkg.com/@arthurfiorette/prettier-config/-/prettier-config-1.0.5.tgz#65447a3830768907f7a3806cbc1eca7c445f98d0" + integrity sha512-ZVGhdM+LVt3T0IfceBlVECb6UsgFJstNBb3BtjXUv8kcVHGmxhj/PGzgBTMeS7cHQJaYQAtoB1qiKsgSc8wv1w== + "@babel/code-frame@7.12.11": version "7.12.11" resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.11.tgz#f4ad435aa263db935b8f10f2c552d23fb716a63f"