├── .npmignore ├── .gitignore ├── .github └── FUNDING.yml ├── src ├── plugins │ └── localforage.js └── index.js ├── webpack.config.js ├── package.json ├── LICENSE └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | /src 2 | .gitignore 3 | webpack.config.js 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | 3 | yarn\.lock 4 | 5 | dist/ 6 | 7 | package-lock\.json 8 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [jonkofee] 4 | -------------------------------------------------------------------------------- /src/plugins/localforage.js: -------------------------------------------------------------------------------- 1 | import localForage from "localforage" 2 | 3 | export default ({ 4 | storageName = "axios-stack", 5 | storageDriver = localForage.LOCALSTORAGE 6 | } = {}) => { 7 | let instance = localForage.createInstance({ 8 | name: storageName 9 | }) 10 | 11 | instance.setDriver(storageDriver) 12 | 13 | return instance 14 | } 15 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | devtool: 'source-map', 3 | output: { 4 | library: require('./package.json').name, 5 | libraryTarget: 'umd', 6 | umdNamedDefine: true 7 | }, 8 | module: { 9 | rules: [{ 10 | test: /(\.jsx|\.js)$/, 11 | loader: 'babel-loader', 12 | exclude: /(node_modules)/ 13 | } 14 | ] 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "axios-offline", 3 | "version": "1.5.2", 4 | "description": "Store query and repeat", 5 | "main": "./dist/main.js", 6 | "repository": "github:jonkofee/axios-offline.git", 7 | "homepage": "https://github.com/jonkofee/axios-offline", 8 | "author": "jonkofee ", 9 | "keywords": [ 10 | "axios", 11 | "offline", 12 | "extensions", 13 | "adapter", 14 | "store" 15 | ], 16 | "license": "MIT", 17 | "dependencies": { 18 | "axios": "*", 19 | "localforage": "^1.7.1" 20 | }, 21 | "devDependencies": { 22 | "babel-loader": "^7.1.4", 23 | "babel-preset-env": "^1.6.1", 24 | "webpack": "^4.5.0", 25 | "webpack-cli": "^2.0.14" 26 | }, 27 | "scripts": { 28 | "build": "webpack --mode production", 29 | "test": "echo \"Error: no test specified\"" 30 | }, 31 | "babel": { 32 | "presets": [ 33 | "env" 34 | ] 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [2018] [jonkofee] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # axios-offline 2 | 3 | [![npm package](https://img.shields.io/npm/v/axios-offline.svg)](https://www.npmjs.org/package/axios-offline) 4 | [![npm downloads](https://img.shields.io/npm/dt/axios-offline.svg)](https://www.npmjs.org/package/axios-offline) 5 | [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT) 6 | 7 | Remembering failed requests and repeating when an internet connection is available 8 | 9 | ## Install 10 | 11 | ```bash 12 | npm install axios-offline --save 13 | ``` 14 | or 15 | ```bash 16 | yarn add axios-offline 17 | ``` 18 | 19 | ## Usage 20 | 21 | ```javascript 22 | import Axios from 'axios' 23 | import AxiosOffline from 'axios-offline' 24 | import LocalForage from "localforage" 25 | 26 | let AxiosOfflineAdapter = AxiosOffline({ 27 | defaultAdapter: Axios.defaults.adapter, //require, basic adapter 28 | storageName: "axios-offline", //optional, default: "axios-stack" 29 | storageDriver: LocalForage.LOCALSTORAGE //optional, default: LocalForage.LOCALSTORAGE 30 | }) 31 | 32 | let http = Axios.create({ 33 | adapter: AxiosOfflineAdapter 34 | }) 35 | 36 | export default http 37 | ``` 38 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import localforage from "./plugins/localforage" 2 | 3 | export default options => { 4 | let { defaultAdapter } = options 5 | let storage = localforage(options) 6 | 7 | return config => { 8 | config.timeout = config.timeout || 5000 9 | 10 | function storeRequest(data) { 11 | storage.setItem(String(Date.now()), data) 12 | } 13 | 14 | function removeRequest(time) { 15 | storage.removeItem(time) 16 | } 17 | 18 | function sendAllRequest() { 19 | storage.iterate((data, time) => { 20 | defaultAdapter(data) 21 | .then(() => removeRequest(time)) 22 | }) 23 | } 24 | 25 | let response = defaultAdapter(config) 26 | .catch(err => { 27 | let { 28 | code, 29 | message, 30 | response 31 | } = err 32 | 33 | if (response === undefined && (code === 'ECONNABORTED' || message === 'Network Error')) { 34 | storeRequest(config) 35 | } else { 36 | sendAllRequest() 37 | } 38 | 39 | return Promise.reject(err) 40 | }) 41 | .then(resolve => { 42 | sendAllRequest() 43 | 44 | return Promise.resolve(resolve) 45 | }) 46 | 47 | return response 48 | } 49 | } 50 | --------------------------------------------------------------------------------