Skip to content

Commit fbe7cc3

Browse files
committed
init
0 parents  commit fbe7cc3

16 files changed

+21043
-0
lines changed

.github/workflows/main.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
on: [push]
3+
jobs:
4+
build:
5+
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}
6+
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
node: ['10.x', '12.x', '14.x']
11+
os: [ubuntu-latest, windows-latest, macOS-latest]
12+
13+
steps:
14+
- name: Checkout repo
15+
uses: actions/checkout@v2
16+
17+
- name: Use Node ${{ matrix.node }}
18+
uses: actions/setup-node@v1
19+
with:
20+
node-version: ${{ matrix.node }}
21+
22+
- name: Install deps and build (with cache)
23+
uses: bahmutov/npm-install@v1
24+
25+
- name: Lint
26+
run: yarn lint
27+
28+
- name: Test
29+
run: yarn test --ci --coverage --maxWorkers=2
30+
31+
- name: Build
32+
run: yarn build

.github/workflows/size.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: size
2+
on: [pull_request]
3+
jobs:
4+
size:
5+
runs-on: ubuntu-latest
6+
env:
7+
CI_JOB_NUMBER: 1
8+
steps:
9+
- uses: actions/checkout@v1
10+
- uses: andresz1/size-limit-action@v1
11+
with:
12+
github_token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.log
2+
.DS_Store
3+
node_modules
4+
.cache
5+
dist

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Saiyan Abhishek
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.

README.md

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
**Offline Sync Provider - README**
2+
3+
![Offline Sync Provider](https://static.thenounproject.com/png/27953-200.png)
4+
5+
## Description
6+
7+
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.
8+
9+
## Installation
10+
11+
```bash
12+
$ npm install --save offline-sync-handler
13+
$ yarn add offline-sync-handler
14+
```
15+
16+
## Demo
17+
You can find the working demo [here](https://offline-handler-demo.vercel.app)
18+
19+
## Usage
20+
21+
### Offline Sync Provider
22+
23+
Wrap your application with the `OfflineSyncProvider` component to enable offline sync and manage data synchronization.
24+
25+
```jsx
26+
import { OfflineSyncProvider } from 'offline-sync-handler';
27+
const App = () => {
28+
// Your application components and logic
29+
};
30+
31+
const rootElement = document.getElementById('root');
32+
ReactDOM.render(
33+
<OfflineSyncProvider>
34+
<App />
35+
</OfflineSyncProvider>,
36+
rootElement
37+
);
38+
```
39+
40+
### Sending API Requests
41+
42+
You can use the `sendRequest` function to send API requests. It handles retries in case of failure due to an unstable internet connection.
43+
Refer to the `axios-create` documentation for available config options.
44+
45+
```javascript
46+
import { useOfflineSyncContext } from from 'offline-sync-handler';
47+
const { sendRequest } = useOfflineSyncContext();
48+
const config = {
49+
method: 'POST',
50+
url: 'https://api.example.com/data',
51+
data: { name: 'John Doe', email: '[email protected]' },
52+
};
53+
54+
try {
55+
const response = await sendRequest(config);
56+
console.log('API Response:', response);
57+
} catch (error) {
58+
console.error('API Request failed:', error.message);
59+
}
60+
61+
```
62+
63+
### Passing Custom Component to display during Offline
64+
65+
You can pass the custom component to show during offline using the `render` prop of the `OfflineSyncProvider`.
66+
67+
```jsx
68+
69+
import { OfflineSyncProvider } from './offline-sync-provider';
70+
71+
const App = () => {
72+
// Your application components and logic
73+
};
74+
75+
const rootElement = document.getElementById('root');
76+
ReactDOM.render(
77+
<OfflineSyncProvider
78+
render={({ isOffline, isOnline }) => {
79+
return isOnline ? null : <div>I am offline</div>;
80+
}}
81+
>
82+
<App />
83+
</OfflineSyncProvider>,
84+
rootElement
85+
);
86+
```
87+
88+
89+
### Track online status change to perform certain operation
90+
91+
You can track online status change using the `onStatusChange` prop of the `OfflineSyncProvider`.
92+
93+
```jsx
94+
import { OfflineSyncProvider } from './offline-sync-provider';
95+
96+
const App = () => {
97+
// Your application components and logic
98+
};
99+
100+
101+
const rootElement = document.getElementById('root');
102+
ReactDOM.render(
103+
<OfflineSyncProvider
104+
onStatusChange={(status)=>{
105+
console.log({status})
106+
}}
107+
>
108+
<App />
109+
</OfflineSyncProvider>,
110+
rootElement
111+
);
112+
```
113+
114+
### Customizing Toast Notifications
115+
116+
You can customize toast notifications using the `toastConfig` prop of the `OfflineSyncProvider` component. Refer to the `react-toastify` documentation for available options.
117+
118+
```jsx
119+
import { OfflineSyncProvider } from './offline-sync-provider';
120+
121+
const App = () => {
122+
// Your application components and logic
123+
};
124+
125+
const toastConfig = {
126+
position: 'bottom-left',
127+
autoClose: 3000,
128+
};
129+
130+
const rootElement = document.getElementById('root');
131+
ReactDOM.render(
132+
<OfflineSyncProvider toastConfig={toastConfig}>
133+
<App />
134+
</OfflineSyncProvider>,
135+
rootElement
136+
);
137+
```
138+
139+
## Roadmaps
140+
141+
* Passing callbacks functions to be triggered on request success/failure.
142+
143+
## License
144+
145+
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).
146+
147+
## Issues and Contributions
148+
149+
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.
150+
151+
---
152+
153+
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!

0 commit comments

Comments
 (0)