offline-retry-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +106 -0
- package/dist/index.cjs +690 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +72 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.js +688 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# offline-retry-sdk
|
|
2
|
+
|
|
3
|
+
Browser SDK that queues failed fetch requests offline and auto-retries them when connectivity is restored.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add offline-retry-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createOfflineClient } from 'offline-retry-sdk';
|
|
15
|
+
|
|
16
|
+
const client = createOfflineClient({
|
|
17
|
+
retryLimit: 5, // Max retry attempts per request (default: 5)
|
|
18
|
+
baseDelay: 1000, // Base delay in ms for exponential backoff (default: 1000)
|
|
19
|
+
autoSync: true, // Auto-flush queue when back online (default: true)
|
|
20
|
+
debug: false, // Enable console logging (default: false)
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Make requests — network failures are automatically queued
|
|
24
|
+
try {
|
|
25
|
+
const response = await client.request({
|
|
26
|
+
url: 'https://api.example.com/data',
|
|
27
|
+
method: 'POST',
|
|
28
|
+
headers: { 'Content-Type': 'application/json' },
|
|
29
|
+
body: { key: 'value' },
|
|
30
|
+
});
|
|
31
|
+
console.log('Success:', response.status);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
// Request failed and was queued for retry
|
|
34
|
+
console.log('Offline — request queued');
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## API
|
|
39
|
+
|
|
40
|
+
### `createOfflineClient(config?)`
|
|
41
|
+
|
|
42
|
+
Creates a new client instance.
|
|
43
|
+
|
|
44
|
+
| Option | Type | Default | Description |
|
|
45
|
+
|--------|------|---------|-------------|
|
|
46
|
+
| `retryLimit` | `number` | `5` | Max retries before giving up |
|
|
47
|
+
| `baseDelay` | `number` | `1000` | Base delay (ms) for exponential backoff |
|
|
48
|
+
| `autoSync` | `boolean` | `true` | Auto-retry when `online` event fires |
|
|
49
|
+
| `debug` | `boolean` | `false` | Log internal activity to console |
|
|
50
|
+
|
|
51
|
+
### Client Methods
|
|
52
|
+
|
|
53
|
+
| Method | Description |
|
|
54
|
+
|--------|-------------|
|
|
55
|
+
| `request(config)` | Send a request. Queues on network failure. |
|
|
56
|
+
| `flush()` | Manually process the retry queue. |
|
|
57
|
+
| `pause()` | Pause queue processing. |
|
|
58
|
+
| `resume()` | Resume queue processing. |
|
|
59
|
+
| `getQueueSize()` | Get number of queued requests. |
|
|
60
|
+
| `clearQueue()` | Remove all queued requests. |
|
|
61
|
+
| `on(event, handler)` | Subscribe to events. Returns unsubscribe function. |
|
|
62
|
+
| `destroy()` | Clean up listeners and resources. |
|
|
63
|
+
|
|
64
|
+
### Request Config
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
{
|
|
68
|
+
url: string;
|
|
69
|
+
method?: string; // default: 'GET'
|
|
70
|
+
headers?: Record<string, string>;
|
|
71
|
+
body?: any;
|
|
72
|
+
retry?: boolean; // default: true — set false to skip queueing
|
|
73
|
+
idempotencyKey?: string; // forwarded as Idempotency-Key header on retry
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Events
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
client.on('queued', ({ request }) => { /* request was queued */ });
|
|
81
|
+
client.on('retry', ({ request, attempt }) => { /* retry attempt starting */ });
|
|
82
|
+
client.on('success', ({ request, response }) => { /* retry succeeded */ });
|
|
83
|
+
client.on('failure', ({ request, error }) => { /* max retries exceeded */ });
|
|
84
|
+
client.on('flushStart', ({ queueSize }) => { /* flush beginning */ });
|
|
85
|
+
client.on('flushComplete', ({ processed, failed }) => { /* flush done */ });
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## How It Works
|
|
89
|
+
|
|
90
|
+
1. `client.request()` wraps `fetch`. If fetch succeeds (any HTTP status), the response is returned.
|
|
91
|
+
2. If fetch throws (network error, offline), the request is persisted to **IndexedDB**.
|
|
92
|
+
3. When `window.online` fires, the queue is processed in **FIFO** order with **exponential backoff**.
|
|
93
|
+
4. Successful retries are removed. Failed retries increment the counter until `maxRetries` is reached.
|
|
94
|
+
5. Duplicate requests (same URL + method + body) are deduplicated via content hashing.
|
|
95
|
+
|
|
96
|
+
## Key Behaviors
|
|
97
|
+
|
|
98
|
+
- **Only network errors are queued** — 4xx/5xx responses are returned as-is, never queued.
|
|
99
|
+
- **Requests survive page reloads** — IndexedDB persists across sessions.
|
|
100
|
+
- **Exponential backoff** — `delay = baseDelay * 2^retries`
|
|
101
|
+
- **Idempotency support** — provide `idempotencyKey` and it will be sent as a header on retry.
|
|
102
|
+
- **Graceful fallback** — if IndexedDB is unavailable, an in-memory queue is used (does not survive reload).
|
|
103
|
+
|
|
104
|
+
## License
|
|
105
|
+
|
|
106
|
+
MIT
|