lazypock 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/README.md +226 -0
- package/dist/index.cjs +971 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +520 -0
- package/dist/index.d.ts +520 -0
- package/dist/index.global.js +963 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +938 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -0
- package/src/auth.ts +185 -0
- package/src/collection.ts +185 -0
- package/src/files.ts +96 -0
- package/src/http.ts +195 -0
- package/src/index.ts +423 -0
- package/src/realtime.ts +264 -0
- package/src/types.ts +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# Lazypock — TypeScript SDK
|
|
2
|
+
|
|
3
|
+
TypeScript client library for [Lazypock](https://github.com/gnuzd/lazypock), an open-source PocketBase-compatible backend.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install lazypock
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { LazypockClient } from 'lazypock';
|
|
15
|
+
|
|
16
|
+
const client = new LazypockClient({ baseUrl: 'http://localhost:4000/api' });
|
|
17
|
+
|
|
18
|
+
// Superuser login
|
|
19
|
+
await client.login('admin@example.com', 'password');
|
|
20
|
+
|
|
21
|
+
// Or auth collection login
|
|
22
|
+
await client.login('user@example.com', 'password', 'users');
|
|
23
|
+
// Or using the explicit method:
|
|
24
|
+
await client.authWithPassword('users', 'user@example.com', 'password');
|
|
25
|
+
|
|
26
|
+
// List records
|
|
27
|
+
const posts = await client.collection('posts').list({ filter: 'published=true' });
|
|
28
|
+
|
|
29
|
+
// Create a record
|
|
30
|
+
const newPost = await client.collection('posts').create({ title: 'Hello', published: true });
|
|
31
|
+
|
|
32
|
+
// File upload
|
|
33
|
+
const file = await client.files.upload(fileInput.files[0]);
|
|
34
|
+
|
|
35
|
+
// Real-time subscriptions
|
|
36
|
+
client.collection('posts').subscribe('*', (e) => console.log(e.action, e.record));
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## API Reference
|
|
40
|
+
|
|
41
|
+
### LazypockClient
|
|
42
|
+
|
|
43
|
+
The main client class.
|
|
44
|
+
|
|
45
|
+
#### Constructor Options
|
|
46
|
+
|
|
47
|
+
| Option | Type | Default | Description |
|
|
48
|
+
|--------|------|---------|-------------|
|
|
49
|
+
| `baseUrl` | `string` | required | API base URL (e.g. `http://localhost:4000/api`) |
|
|
50
|
+
| `storage` | `StorageAdapter` | `memoryStorage` | Custom storage adapter for token persistence |
|
|
51
|
+
| `authStore` | `AuthStore` | auto-created | Explicit auth store instance |
|
|
52
|
+
| `realtime` | `RealtimeService` | auto-created | Real-time service for WebSocket subscriptions |
|
|
53
|
+
|
|
54
|
+
#### Authentication Methods
|
|
55
|
+
|
|
56
|
+
- `login(email, password, collection?)` — Login as superuser or auth collection user
|
|
57
|
+
- `authWithPassword(collection, identity, password, options?)` — Auth collection login
|
|
58
|
+
- `authRefresh(collection, options?)` — Refresh auth token
|
|
59
|
+
- `checkSuperuser()` — Check if any superuser exists
|
|
60
|
+
- `setup(email, password)` — Create initial superuser
|
|
61
|
+
- `logout()` — Clear auth state
|
|
62
|
+
- `me(options?)` — Get current superuser profile
|
|
63
|
+
|
|
64
|
+
#### Record Methods
|
|
65
|
+
|
|
66
|
+
- `listRecords(collection, params?, options?)` — List records with filter/sort/pagination
|
|
67
|
+
- `getRecord(collection, id, options?)` — Get single record
|
|
68
|
+
- `createRecord(collection, data, options?)` — Create record
|
|
69
|
+
- `updateRecord(collection, id, data, options?)` — Update record
|
|
70
|
+
- `deleteRecord(collection, id, options?)` — Delete record
|
|
71
|
+
|
|
72
|
+
#### Collection Management
|
|
73
|
+
|
|
74
|
+
- `listCollections(query?, options?)` — List all collections
|
|
75
|
+
- `getCollection(id, options?)` — Get collection details
|
|
76
|
+
- `createCollection(data, options?)` — Create new collection
|
|
77
|
+
- `updateCollection(id, data, options?)` — Update collection
|
|
78
|
+
- `deleteCollection(id, options?)` — Delete collection
|
|
79
|
+
|
|
80
|
+
#### File Operations
|
|
81
|
+
|
|
82
|
+
- `files.upload(file, filename?, options?, meta?)` — Upload a file
|
|
83
|
+
- `files.getUrl(fileId)` — Get file metadata
|
|
84
|
+
- `files.delete(fileId, options?)` — Delete a file
|
|
85
|
+
- `getFileUrl(baseUrl, fileId)` — Construct a file URL from base URL and file ID (utility)
|
|
86
|
+
|
|
87
|
+
#### Realtime
|
|
88
|
+
|
|
89
|
+
- `realtime.connect(opts)` — Connect to WebSocket
|
|
90
|
+
- `realtime.disconnect()` — Disconnect
|
|
91
|
+
- `realtime.subscribe(topic, callback)` — Subscribe to collection changes
|
|
92
|
+
- `realtime.unsubscribe(topic, callback?)` — Unsubscribe
|
|
93
|
+
- `collection(name).subscribe(pattern, callback)` — Convenience subscription on collection service
|
|
94
|
+
- `collection(name).unsubscribe(pattern, callback?)` — Convenience unsubscription
|
|
95
|
+
|
|
96
|
+
### CollectionService
|
|
97
|
+
|
|
98
|
+
Returned by `client.collection(name)`.
|
|
99
|
+
|
|
100
|
+
- `list(params?, options?)` — List records
|
|
101
|
+
- `getOne(id, options?)` — Get record by ID
|
|
102
|
+
- `create(data, options?)` — Create record
|
|
103
|
+
- `update(id, data, options?)` — Update record
|
|
104
|
+
- `delete(id, options?)` — Delete record
|
|
105
|
+
- `authWithPassword(identity, password, options?)` — Login to this auth collection
|
|
106
|
+
- `authRefresh(options?)` — Refresh token for this auth collection
|
|
107
|
+
- `authMethods(options?)` — Get available auth methods
|
|
108
|
+
|
|
109
|
+
### AuthStore
|
|
110
|
+
|
|
111
|
+
Handles token persistence and auto-refresh.
|
|
112
|
+
|
|
113
|
+
- `token` — Current JWT token
|
|
114
|
+
- `model` — Current auth model (user record or null)
|
|
115
|
+
- `isValid` — Whether a token exists
|
|
116
|
+
- `isExpired` — Whether the current token has expired (with 30s buffer)
|
|
117
|
+
- `collectionName` — Name of the auth collection used for token refresh
|
|
118
|
+
- `set(token, model)` — Update token and model
|
|
119
|
+
- `setCollectionName(name)` — Set the auth collection name for token refresh
|
|
120
|
+
- `clear()` — Clear all auth state
|
|
121
|
+
- `onChange(callback)` — Listen for auth changes (returns unsubscribe function)
|
|
122
|
+
- `init()` — Restore persisted auth from storage
|
|
123
|
+
|
|
124
|
+
### Types
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
interface ApiRecord {
|
|
128
|
+
id: string;
|
|
129
|
+
collectionId: string;
|
|
130
|
+
collectionName: string;
|
|
131
|
+
created: string;
|
|
132
|
+
updated: string;
|
|
133
|
+
[key: string]: unknown;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
interface ListResult<T> {
|
|
137
|
+
page: number;
|
|
138
|
+
perPage: number;
|
|
139
|
+
totalItems: number;
|
|
140
|
+
totalPages: number;
|
|
141
|
+
items: T[];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
interface AuthModel {
|
|
145
|
+
id: string;
|
|
146
|
+
[key: string]: unknown;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
interface FileRecord {
|
|
150
|
+
id: string;
|
|
151
|
+
filename: string;
|
|
152
|
+
mimeType: string;
|
|
153
|
+
size: number;
|
|
154
|
+
url: string;
|
|
155
|
+
[key: string]: unknown;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
interface RequestOptions {
|
|
159
|
+
signal?: AbortSignal;
|
|
160
|
+
fetch?: typeof fetch;
|
|
161
|
+
headers?: Record<string, string>;
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Error Handling
|
|
166
|
+
|
|
167
|
+
The SDK throws `ApiError` on non-2xx responses:
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { LazypockClient, ApiError } from 'lazypock';
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
await client.collection('posts').create({ title: 'My Post' });
|
|
174
|
+
} catch (err) {
|
|
175
|
+
if (err instanceof ApiError) {
|
|
176
|
+
console.log(err.status); // HTTP status code
|
|
177
|
+
console.log(err.message); // Error message
|
|
178
|
+
console.log(err.data); // Full response data
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Configuration
|
|
184
|
+
|
|
185
|
+
### Storage Adapter
|
|
186
|
+
|
|
187
|
+
By default, the SDK uses `localStorage` for token persistence. You can provide a custom adapter:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
import { LazypockClient, AuthStore } from 'lazypock';
|
|
191
|
+
|
|
192
|
+
const customStorage = {
|
|
193
|
+
get: async (key) => await AsyncStorage.getItem(key),
|
|
194
|
+
set: async (key, value) => await AsyncStorage.setItem(key, value),
|
|
195
|
+
remove: async (key) => await AsyncStorage.removeItem(key),
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const client = new LazypockClient({
|
|
199
|
+
baseUrl: 'http://localhost:4000/api',
|
|
200
|
+
storage: customStorage,
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Auto Token Refresh
|
|
205
|
+
|
|
206
|
+
The SDK automatically refreshes expired auth tokens. When a token expires, the next API call triggers a transparent refresh via the `auth-refresh` endpoint. No manual intervention needed.
|
|
207
|
+
|
|
208
|
+
## Real-time Subscriptions
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
// Subscribe to all changes in a collection
|
|
212
|
+
client.collection('posts').subscribe('*', (event) => {
|
|
213
|
+
console.log(event.action); // 'create' | 'update' | 'delete'
|
|
214
|
+
console.log(event.record);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// Subscribe to a specific record
|
|
218
|
+
client.collection('posts').subscribe('abc123', (event) => { ... });
|
|
219
|
+
|
|
220
|
+
// Unsubscribe
|
|
221
|
+
client.collection('posts').unsubscribe('*');
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## License
|
|
225
|
+
|
|
226
|
+
[MIT](LICENSE) © 2024-2025 Chris Nguyen (gnuzd)
|