-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6f2a12
commit ab68a9f
Showing
6 changed files
with
131 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters