Socket
Book a DemoInstallSign in
Socket

axios-cache-adapter-util

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

axios-cache-adapter-util

axios-cache-adapter export/import json for save/load file

latest
Source
npmnpm
Version
1.0.57
Version published
Maintainers
1
Created
Source

README

axios-cache-adapter export/import json for save/load file
yarn add axios axios-cache-adapter fs-extra axios-cache-adapter-util

import { setupCache, ISetupCache } from 'axios-cache-adapter';
import Axios, { AxiosAdapter } from 'axios';
import { exportCache, importCache, processExitHook } from '../lib/index';
import fs from 'fs-extra';
import path from 'path';

(async () => {

	const saveCacheFileBySelf = true;

	let cache = setupCache({
		maxAge: 15 * 60 * 1000,
	});

	const cacheFile = path.join(__dirname, 'temp', 'axios.cache.json');

	await fs.readJSON(cacheFile)
		.catch(e => {
			return {}
		})
		.then(async (json) => {

			let len = await cache.store.length();

			await importCache(cache.store, json);

			let len2 = await cache.store.length();

			console.log(`before: ${len}`, `after: ${len2}`);
		})
	;

	function saveCache()
	{
		return exportCache(cache.store, (json) => {
			fs.outputJSONSync(cacheFile, json, {
				spaces: 2,
			});

			console.debug(`[Cache]`, Object.keys(json).length, `saved`, cacheFile);

		})
	}

	if (!saveCacheFileBySelf)
	{
		await processExitHook(() => {
			return saveCache();
		});
	}

	let axios = Axios.create({
		adapter: cache.adapter as AxiosAdapter
	});

	await axios.get('https://github.com/RasCarlito/axios-cache-adapter')
		.then((ret) => {
			console.log(ret.status, ret.statusText);
			//console.dir(ret.headers);
		})
	;

	if (saveCacheFileBySelf)
	{
		await saveCache();
	}

})();

Keywords

axios

FAQs

Package last updated on 12 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts