Skip to content

Commit

Permalink
docs: more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Feb 1, 2022
1 parent e6f2a12 commit ab68a9f
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 4 deletions.
4 changes: 3 additions & 1 deletion docs/pages/usage-examples.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
49 changes: 49 additions & 0 deletions examples/node/index.js
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);
36 changes: 36 additions & 0 deletions examples/react/app.js
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>
);
};
29 changes: 29 additions & 0 deletions examples/react/axios-context.js
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>;
};
13 changes: 13 additions & 0 deletions examples/react/index.js
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')
);
4 changes: 1 addition & 3 deletions examples/runkit.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down

0 comments on commit ab68a9f

Please sign in to comment.