Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-s19 committed Jun 18, 2024
0 parents commit fbe7cc3
Show file tree
Hide file tree
Showing 16 changed files with 21,043 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI
on: [push]
jobs:
build:
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}

runs-on: ${{ matrix.os }}
strategy:
matrix:
node: ['10.x', '12.x', '14.x']
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- name: Checkout repo
uses: actions/checkout@v2

- name: Use Node ${{ matrix.node }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}

- name: Install deps and build (with cache)
uses: bahmutov/npm-install@v1

- name: Lint
run: yarn lint

- name: Test
run: yarn test --ci --coverage --maxWorkers=2

- name: Build
run: yarn build
12 changes: 12 additions & 0 deletions .github/workflows/size.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: size
on: [pull_request]
jobs:
size:
runs-on: ubuntu-latest
env:
CI_JOB_NUMBER: 1
steps:
- uses: actions/checkout@v1
- uses: andresz1/size-limit-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.log
.DS_Store
node_modules
.cache
dist
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Saiyan Abhishek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
153 changes: 153 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
**Offline Sync Provider - README**

![Offline Sync Provider](https://static.thenounproject.com/png/27953-200.png)

## Description

Offline Sync Provider is a JavaScript module designed to handle API requests in web applications with offline capabilities. It offers a robust solution to synchronize data with the server even when the device is offline and automatically retries failed requests upon reconnection. This module utilizes `axios` for making API requests and `localforage` for offline storage, ensuring data integrity and smooth synchronization.

## Installation

```bash
$ npm install --save offline-sync-handler
$ yarn add offline-sync-handler
```

## Demo
You can find the working demo [here](https://offline-handler-demo.vercel.app)

## Usage

### Offline Sync Provider

Wrap your application with the `OfflineSyncProvider` component to enable offline sync and manage data synchronization.

```jsx
import { OfflineSyncProvider } from 'offline-sync-handler';
const App = () => {
// Your application components and logic
};

const rootElement = document.getElementById('root');
ReactDOM.render(
<OfflineSyncProvider>
<App />
</OfflineSyncProvider>,
rootElement
);
```

### Sending API Requests

You can use the `sendRequest` function to send API requests. It handles retries in case of failure due to an unstable internet connection.
Refer to the `axios-create` documentation for available config options.

```javascript
import { useOfflineSyncContext } from from 'offline-sync-handler';
const { sendRequest } = useOfflineSyncContext();
const config = {
method: 'POST',
url: 'https://api.example.com/data',
data: { name: 'John Doe', email: '[email protected]' },
};

try {
const response = await sendRequest(config);
console.log('API Response:', response);
} catch (error) {
console.error('API Request failed:', error.message);
}

```

### Passing Custom Component to display during Offline

You can pass the custom component to show during offline using the `render` prop of the `OfflineSyncProvider`.

```jsx

import { OfflineSyncProvider } from './offline-sync-provider';

const App = () => {
// Your application components and logic
};

const rootElement = document.getElementById('root');
ReactDOM.render(
<OfflineSyncProvider
render={({ isOffline, isOnline }) => {
return isOnline ? null : <div>I am offline</div>;
}}
>
<App />
</OfflineSyncProvider>,
rootElement
);
```


### Track online status change to perform certain operation

You can track online status change using the `onStatusChange` prop of the `OfflineSyncProvider`.

```jsx
import { OfflineSyncProvider } from './offline-sync-provider';

const App = () => {
// Your application components and logic
};


const rootElement = document.getElementById('root');
ReactDOM.render(
<OfflineSyncProvider
onStatusChange={(status)=>{
console.log({status})
}}
>
<App />
</OfflineSyncProvider>,
rootElement
);
```

### Customizing Toast Notifications

You can customize toast notifications using the `toastConfig` prop of the `OfflineSyncProvider` component. Refer to the `react-toastify` documentation for available options.

```jsx
import { OfflineSyncProvider } from './offline-sync-provider';

const App = () => {
// Your application components and logic
};

const toastConfig = {
position: 'bottom-left',
autoClose: 3000,
};

const rootElement = document.getElementById('root');
ReactDOM.render(
<OfflineSyncProvider toastConfig={toastConfig}>
<App />
</OfflineSyncProvider>,
rootElement
);
```

## Roadmaps

* Passing callbacks functions to be triggered on request success/failure.

## License

This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).

## Issues and Contributions

If you encounter any issues or have suggestions for improvement, please [submit an issue](https://github.com/example/offline-sync-provider/issues). Contributions are welcome! Please fork the repository and create a pull request.

---

Thank you for using the Offline Sync Provider module! We hope it simplifies handling API requests and enhances the offline experience for your web application. If you have any questions or need further assistance, feel free to reach out to us. Happy coding!
Loading

0 comments on commit fbe7cc3

Please sign in to comment.