universe-code 0.0.63 โ 0.0.65
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.
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# ๐ universe-code (Angular)
|
|
2
|
+
|
|
3
|
+
A lightweight and powerful **IndexedDB utility for Angular** that helps you cache API responses, manage offline storage, and reduce unnecessary network calls โ with a **clean, service-based API**.
|
|
4
|
+
|
|
5
|
+
Designed for **modern Angular (standalone + DI)** and production-ready applications.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## โจ Features
|
|
10
|
+
|
|
11
|
+
* โก Easy IndexedDB configuration
|
|
12
|
+
* ๐ง Built-in API caching with expiry
|
|
13
|
+
* ๐ฆ Simple CRUD helpers
|
|
14
|
+
* ๐ Auto refresh when cache expires
|
|
15
|
+
* ๐งฉ Angular Dependency Injection support
|
|
16
|
+
* ๐ Clean & professional service-based API
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## ๐ฆ Installation
|
|
21
|
+
|
|
22
|
+
Install the latest version using npm:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install universe-code@latest
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## โ๏ธ IndexedDB Setup (One Time Only)
|
|
31
|
+
|
|
32
|
+
IndexedDB should be initialized **once at application bootstrap level**.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## 1๏ธโฃ Import Angular Compiler (Required)
|
|
37
|
+
|
|
38
|
+
`universe-code` internally uses Angular decorators and DI.
|
|
39
|
+
For JIT environments, Angular Compiler **must be available**.
|
|
40
|
+
|
|
41
|
+
๐ `main.ts`
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import '@angular/compiler';
|
|
45
|
+
import { bootstrapApplication } from '@angular/platform-browser';
|
|
46
|
+
import { appConfig } from './app/app.config';
|
|
47
|
+
import { App } from './app/app';
|
|
48
|
+
|
|
49
|
+
bootstrapApplication(App, appConfig)
|
|
50
|
+
.catch(console.error);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
> โ ๏ธ **Note:**
|
|
54
|
+
> This avoids `JIT compilation failed for injectable class` errors.
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 2๏ธโฃ Import the IndexedDB Service
|
|
59
|
+
|
|
60
|
+
`IdbService` is provided via Angular DI and requires **no manual provider setup**.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { IdbService } from 'universe-code/angular';
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## ๐ Using IndexedDB in Angular Components
|
|
69
|
+
|
|
70
|
+
Inject the service inside any component or service.
|
|
71
|
+
|
|
72
|
+
๐ `app.ts`
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { Component, OnInit } from '@angular/core';
|
|
76
|
+
import { IdbService } from 'universe-code/angular';
|
|
77
|
+
|
|
78
|
+
@Component({
|
|
79
|
+
selector: 'app-root',
|
|
80
|
+
template: `<h1>Angular App</h1>`,
|
|
81
|
+
})
|
|
82
|
+
export class App implements OnInit {
|
|
83
|
+
|
|
84
|
+
constructor(private idb: IdbService) {}
|
|
85
|
+
|
|
86
|
+
async ngOnInit() {
|
|
87
|
+
const data = await this.idb.fetchWithCache(
|
|
88
|
+
'example-key',
|
|
89
|
+
60_000, // cache expiry in ms
|
|
90
|
+
async () => {
|
|
91
|
+
const res = await fetch('https://example.com/api/data');
|
|
92
|
+
return res.json();
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
console.log(data);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## ๐ Fetch API Data with Cache + Expiry
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
const result = await idb.fetchWithCache(
|
|
107
|
+
'users',
|
|
108
|
+
5 * 60 * 1000,
|
|
109
|
+
async () => {
|
|
110
|
+
const res = await fetch('/api/users');
|
|
111
|
+
return res.json();
|
|
112
|
+
}
|
|
113
|
+
);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### ๐ง How it Works
|
|
117
|
+
|
|
118
|
+
* Checks IndexedDB for cached data
|
|
119
|
+
* Validates expiry timestamp
|
|
120
|
+
* Calls API only if cache is missing or expired
|
|
121
|
+
* Stores fresh data automatically
|
|
122
|
+
* Returns cached data instantly on next call
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## ๐ฅ Get Cached Data Only
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
const data = await idb.get('users');
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## โ๏ธ Insert / Update Data Manually
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
await idb.put({
|
|
138
|
+
id: 'profile',
|
|
139
|
+
data: {
|
|
140
|
+
name: 'John',
|
|
141
|
+
role: 'Admin',
|
|
142
|
+
},
|
|
143
|
+
timestamp: Date.now(),
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## ๐๏ธ Remove a Specific Record
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
await idb.remove('profile');
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## ๐งน Clear Entire IndexedDB Store
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
await idb.clear();
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## ๐ API Reference
|
|
166
|
+
|
|
167
|
+
| Method | Description |
|
|
168
|
+
| -------------------------------------- | ---------------------------- |
|
|
169
|
+
| `get(id)` | Retrieve data from IndexedDB |
|
|
170
|
+
| `put(payload)` | Insert or update data |
|
|
171
|
+
| `remove(id)` | Delete a specific record |
|
|
172
|
+
| `clear()` | Clear entire IndexedDB |
|
|
173
|
+
| `fetchWithCache(id, expiry, callback)` | Fetch + cache with expiry |
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## โ
Ideal Use Cases
|
|
178
|
+
|
|
179
|
+
* API response caching
|
|
180
|
+
* Offline-first Angular apps
|
|
181
|
+
* Performance optimization
|
|
182
|
+
* Reducing repeated network calls
|
|
183
|
+
* Storing session-level data
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## ๐ก License
|
|
188
|
+
|
|
189
|
+
MIT License ยฉ universe-code
|
|
190
|
+
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# ๐ universe-code (React)
|
|
2
|
+
|
|
3
|
+
A lightweight and powerful **IndexedDB utility for React** that helps you cache API data, manage offline storage, and reduce unnecessary network calls โ all with a clean and simple API.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## โจ Features
|
|
8
|
+
|
|
9
|
+
- โก Easy IndexedDB setup
|
|
10
|
+
- ๐ง Built-in API caching with expiry
|
|
11
|
+
- ๐ฆ Simple CRUD operations
|
|
12
|
+
- ๐ Auto re-fetch when cache expires
|
|
13
|
+
- ๐ป Works perfectly with React / Next.js
|
|
14
|
+
- ๐ Clean and developer-friendly API
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## ๐ฆ Installation
|
|
19
|
+
|
|
20
|
+
Install the latest version using npm:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install universe-code@latest
|
|
24
|
+
```
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## โ๏ธ IndexedDB Setup (One Time Only)
|
|
28
|
+
|
|
29
|
+
You must configure IndexedDB once at the root level of your app.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## 1๏ธโฃ Create IndexedDB Provider
|
|
34
|
+
|
|
35
|
+
Create the following file:
|
|
36
|
+
|
|
37
|
+
๐ lib/provider/idb-provider.tsx
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
"use client";
|
|
41
|
+
|
|
42
|
+
import { configureIdb } from "universe-code/react";
|
|
43
|
+
|
|
44
|
+
export function IdbProvider() {
|
|
45
|
+
configureIdb({
|
|
46
|
+
dbName: "db",
|
|
47
|
+
dbVersion: 1,
|
|
48
|
+
storeName: "store",
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
### 2๏ธโฃ Register Provider in Root Layout
|
|
58
|
+
|
|
59
|
+
Call the provider once so IndexedDB initializes globally.
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
import { IdbProvider } from "@/lib/provider/idb-provider";
|
|
63
|
+
|
|
64
|
+
export default function RootLayout({
|
|
65
|
+
children,
|
|
66
|
+
}: {
|
|
67
|
+
children: React.ReactNode;
|
|
68
|
+
}) {
|
|
69
|
+
return (
|
|
70
|
+
<html lang="en">
|
|
71
|
+
<body>
|
|
72
|
+
<IdbProvider />
|
|
73
|
+
{children}
|
|
74
|
+
</body>
|
|
75
|
+
</html>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## โ
IndexedDB is now created and ready to use.
|
|
83
|
+
|
|
84
|
+
๐ Using IndexedDB in Components
|
|
85
|
+
Import the Hook
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
import { useIdbStore } from "universe-code/react";
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Initialize Store
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
const idbStore = useIdbStore();
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
All IndexedDB helper functions are now available inside idbStore.
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## ๐ API Fetch + Cache with Expiry
|
|
102
|
+
|
|
103
|
+
Fetch API data and automatically store it in IndexedDB with expiry control.
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
idbStore
|
|
107
|
+
.getWithExpiry("users", minutesToMs(1), async () => {
|
|
108
|
+
const res = await fetch("https://example.com/api/user", {
|
|
109
|
+
method: "GET",
|
|
110
|
+
headers: {
|
|
111
|
+
"Content-Type": "application/json",
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const data = await res.json();
|
|
116
|
+
return data.data;
|
|
117
|
+
})
|
|
118
|
+
.then(console.log);
|
|
119
|
+
```
|
|
120
|
+
### Note:
|
|
121
|
+
|
|
122
|
+
import minutesToMs() from universe-code/core
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
import { minutesToMs } from "universe-code/core";
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### ๐ง How it works
|
|
129
|
+
|
|
130
|
+
- Calls API only if data is missing or expired
|
|
131
|
+
- Saves response in IndexedDB
|
|
132
|
+
- Returns cached data instantly on next call
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## ๐ฅ Get Data from IndexedDB
|
|
137
|
+
```bash
|
|
138
|
+
const data = await idbStore.get("users");
|
|
139
|
+
console.log(data);
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## โ๏ธ Insert / Update Data
|
|
145
|
+
```bash
|
|
146
|
+
idbStore.put({
|
|
147
|
+
id: "users",
|
|
148
|
+
data: {
|
|
149
|
+
name: "Mark",
|
|
150
|
+
country: "USA",
|
|
151
|
+
},
|
|
152
|
+
timestamp: Date.now(),
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
### ๐๏ธ Delete a Specific Record
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
idbStore.remove("users");
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
## ๐งน Clear Entire IndexedDB Store
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
idbStore.clear();
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
### ๐ API Reference
|
|
175
|
+
|
|
176
|
+
| Function | Description |
|
|
177
|
+
|---------|------------|
|
|
178
|
+
| `get(id)` | Retrieve data from IndexedDB |
|
|
179
|
+
| `put(payload)` | Insert or update data |
|
|
180
|
+
| `remove(id)` | Delete a record |
|
|
181
|
+
| `clear()` | Clear all IndexedDB data |
|
|
182
|
+
| `getWithExpiry(id, expiry, callback)` | Fetch and cache data with expiry |
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## โ
Ideal Use Cases
|
|
187
|
+
|
|
188
|
+
- API response caching
|
|
189
|
+
- Offline-first applications
|
|
190
|
+
- Performance optimization
|
|
191
|
+
- Reducing repeated API calls
|
|
192
|
+
- Storing session or user data
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
## ๐ก License
|
|
197
|
+
|
|
198
|
+
MIT License ยฉ universe-code
|