@appello/axios-offline
TypeScript icon, indicating that this package has built-in type declarations

1.0.14 • Public • Published

@appello/axios-offline

npm package License: MIT

Remembering failed requests and repeating when an internet connection is available

Credentials

This package is based on a work of jonkofee.

Installation

Using npm

npm install axios localforage # install peer dependencies
npm install @appello/axios-offline

Using yarn

yarn add axios localforage # install peer dependencies
yarn add @appello/axios-offline

Usage example

import axios, { AxiosAdapter } from 'axios';
import { AxiosOffline } from '@appello/axios-offline';
import NetInfo from '@react-native-community/netinfo';
import LocalForage from 'localforage';

const offlineUrls = ['/list', '/profile'];

export const axiosOfflineInstance = new AxiosOffline({
  defaultAdapter: axios.defaults.adapter as AxiosAdapter, // require, basic adapter
  storageOptions: {
    name: 'axios-offline', // optional, default: "axios-stack"
    driver: LocalForage.LOCALSTORAGE, // optional, default: LocalForage.LOCALSTORAGE
  },
  shouldStoreRequest: config => {
    return config.method === 'POST' && offlineUrls.includes(config.url as string);
  },
  getResponsePlaceholder: config => ({
    config,
    headers: {},
    data: undefined,
    status: HttpStatusCode.Ok,
    statusText: 'Request successfully stored till back online!',
  }),
});

export const Api = axios.create({
  adapter: axiosOfflineInstance.adapter,
});

window.addEventListener('online', (event) => {
  axiosOfflineInstance.sendRequestsFromStore();
});

Package Sidebar

Install

npm i @appello/axios-offline

Weekly Downloads

10

Version

1.0.14

License

MIT

Unpacked Size

69.2 kB

Total Files

13

Last publish

Collaborators

  • victor-razdorov
  • yaro-appello
  • kuzkokov