mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
docs: more examples
This commit is contained in:
parent
e6f2a12898
commit
ab68a9f756
@ -1,4 +1,6 @@
|
||||
# Interceptor
|
||||
# Usage and Examples
|
||||
|
||||
There are some other examples in the [examples]([https://](https://github.com/arthurfiorette/axios-cache-interceptor/tree/main/examples), check them out! You can also make a PR to add some more.
|
||||
|
||||
## Applying
|
||||
|
||||
|
||||
49
examples/node/index.js
Normal file
49
examples/node/index.js
Normal file
@ -0,0 +1,49 @@
|
||||
/* eslint-disable */
|
||||
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
|
||||
const Axios = require('axios');
|
||||
const { setupCache } = require('axios-cache-interceptor');
|
||||
|
||||
const api = setupCache(
|
||||
Axios.create({ baseURL: 'https://jsonplaceholder.typicode.com/' }),
|
||||
// 5 seconds
|
||||
{ ttl: 5 * 1000 }
|
||||
);
|
||||
|
||||
// Every time an api call reaches here, it will
|
||||
// make another internal request and forward the response.
|
||||
app.get('/', (req, res) => {
|
||||
api.get('/users').then(
|
||||
({ data, cached, id }) => {
|
||||
res.json({
|
||||
cached,
|
||||
id: {
|
||||
value: id,
|
||||
deleteUrl: `/cache/${id}/delete`,
|
||||
getUrl: `/cache/${id}/get`
|
||||
},
|
||||
data
|
||||
});
|
||||
},
|
||||
(error) => {
|
||||
res.json({ error });
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
app.get('/cache/:id/delete', async (req, res) => {
|
||||
await api.storage.remove(req.params.id);
|
||||
res.send({
|
||||
status: 'Deleted!',
|
||||
current: await api.storage.get(req.params.id)
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/cache/:id/get', async (req, res) => {
|
||||
const cache = await api.storage.get(req.params.id);
|
||||
res.json(cache);
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
36
examples/react/app.js
Normal file
36
examples/react/app.js
Normal file
@ -0,0 +1,36 @@
|
||||
/* eslint-disable */
|
||||
|
||||
import React, { useState } from 'react';
|
||||
import { useAxios } from './axios-context';
|
||||
|
||||
export const App = () => {
|
||||
// You can use react context, as exemplified here
|
||||
// but you can also just export the AxiosCacheInstance
|
||||
// from a file and use it directly. Happy coding :)
|
||||
const axios = useAxios();
|
||||
|
||||
const [{ data, error, loading }, setResponse] = useState({
|
||||
data: [],
|
||||
loading: true,
|
||||
error: null
|
||||
});
|
||||
|
||||
axios.get('https://jsonplaceholder.typicode.com/users').then(
|
||||
({ data }) => setResponse({ data, loading: false, error: null }),
|
||||
(error) => setResponse({ data: [], loading: false, error })
|
||||
);
|
||||
|
||||
if (loading) {
|
||||
return <div>Loading...</div>;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return <div>Error: {error.message}</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div>{data}</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
29
examples/react/axios-context.js
Normal file
29
examples/react/axios-context.js
Normal file
@ -0,0 +1,29 @@
|
||||
/* eslint-disable */
|
||||
|
||||
/*
|
||||
* Replace ../../src with axios-cache-interceptor
|
||||
*/
|
||||
|
||||
import Axios from 'axios';
|
||||
import { createContext, useContext, useState } from 'react';
|
||||
import { setupCache } from '../../src'; // axios-cache-interceptor
|
||||
|
||||
/**
|
||||
* @type {import('react').Context<import('../../src').AxiosCacheInstance>}
|
||||
*/
|
||||
const AxiosContext = createContext(null);
|
||||
|
||||
export const useAxios = () => useContext(AxiosContext);
|
||||
|
||||
export const AxiosProvider = ({ children }) => {
|
||||
const [axios] = useState(
|
||||
setupCache(
|
||||
// Custom instance to prevent conflict with other pieces of code
|
||||
Axios.create(),
|
||||
// cache config
|
||||
{}
|
||||
)
|
||||
);
|
||||
|
||||
return <AxiosContext.Provider value={axios}>{children}</AxiosContext.Provider>;
|
||||
};
|
||||
13
examples/react/index.js
Normal file
13
examples/react/index.js
Normal file
@ -0,0 +1,13 @@
|
||||
/* eslint-disable */
|
||||
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import { App } from './app';
|
||||
import { AxiosProvider } from './axios-context';
|
||||
|
||||
ReactDOM.render(
|
||||
<AxiosProvider>
|
||||
<App />
|
||||
</AxiosProvider>,
|
||||
document.getElementById('root')
|
||||
);
|
||||
@ -1,6 +1,4 @@
|
||||
//@ts-check
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
/* eslint-disable */
|
||||
|
||||
const { create: createAxios } = require('axios').default;
|
||||
const { setupCache } = require('axios-cache-interceptor');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user