@hostlink/light 3.2.8 → 3.3.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 +422 -10
- package/dist/drive.d.ts +16 -0
- package/dist/file.d.ts +10 -0
- package/dist/filesystem.d.ts +3 -0
- package/dist/folder.d.ts +6 -0
- package/dist/light.js +235 -202
- package/dist/light.umd.cjs +1 -1
- package/package.json +1 -1
- package/dist/drive.basic.test.d.ts +0 -1
- package/dist/drive.robust.test.d.ts +0 -1
- package/dist/drive.test.d.ts +0 -1
package/README.md
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hostlink/light
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A lightweight JavaScript/TypeScript client library for the **Light Framework**, providing an easy-to-use API for GraphQL queries/mutations, authentication, file management, user management, and more.
|
|
4
4
|
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔐 **Authentication** - Login/logout, password management, OAuth (Google, Facebook, Microsoft), WebAuthn support
|
|
8
|
+
- 📊 **GraphQL Client** - Simplified query and mutation APIs with automatic file upload handling
|
|
9
|
+
- 📁 **File System** - Complete file and folder management operations
|
|
10
|
+
- 👥 **User Management** - CRUD operations for users and roles
|
|
11
|
+
- 📧 **Email** - Send emails via the backend
|
|
12
|
+
- 🔄 **Token Refresh** - Automatic access token refresh with request queuing
|
|
13
|
+
- 🌐 **Universal** - Works in both browser and Node.js environments
|
|
5
14
|
|
|
6
15
|
## Installation
|
|
7
16
|
|
|
@@ -9,27 +18,430 @@ A javascript library for Light Framework.
|
|
|
9
18
|
npm install @hostlink/light
|
|
10
19
|
```
|
|
11
20
|
|
|
12
|
-
##
|
|
21
|
+
## Quick Start
|
|
13
22
|
|
|
14
|
-
```
|
|
23
|
+
```typescript
|
|
15
24
|
import { createClient } from '@hostlink/light';
|
|
16
25
|
|
|
17
|
-
|
|
26
|
+
// Create a client instance
|
|
27
|
+
const api = createClient('https://your-api.com/graphql');
|
|
28
|
+
|
|
29
|
+
// Login
|
|
30
|
+
await api.auth.login('username', 'password');
|
|
18
31
|
|
|
32
|
+
// Query data
|
|
33
|
+
const users = await api.query({
|
|
34
|
+
app: {
|
|
35
|
+
users: {
|
|
36
|
+
user_id: true,
|
|
37
|
+
username: true,
|
|
38
|
+
first_name: true
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Logout
|
|
44
|
+
await api.auth.logout();
|
|
19
45
|
```
|
|
20
46
|
|
|
21
|
-
##
|
|
47
|
+
## API Reference
|
|
22
48
|
|
|
23
|
-
|
|
24
|
-
|
|
49
|
+
### Creating a Client
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { createClient } from '@hostlink/light';
|
|
53
|
+
|
|
54
|
+
const api = createClient('https://your-api.com/graphql');
|
|
25
55
|
```
|
|
26
56
|
|
|
27
|
-
|
|
57
|
+
The client provides:
|
|
58
|
+
- `api.auth` - Authentication methods
|
|
59
|
+
- `api.query` - GraphQL queries
|
|
60
|
+
- `api.mutation` - GraphQL mutations
|
|
61
|
+
- `api.users` - User management
|
|
62
|
+
- `api.roles` - Role management
|
|
63
|
+
- `api.mail` - Email sending
|
|
64
|
+
- `api.config` - Configuration access
|
|
65
|
+
- `api.collect` - Collection builder
|
|
66
|
+
- `api.list` - List builder
|
|
67
|
+
|
|
68
|
+
---
|
|
28
69
|
|
|
29
|
-
|
|
70
|
+
### Authentication
|
|
71
|
+
|
|
72
|
+
#### Basic Login/Logout
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
// Login with username and password
|
|
76
|
+
await api.auth.login('username', 'password');
|
|
77
|
+
|
|
78
|
+
// Login with 2FA code
|
|
79
|
+
await api.auth.login('username', 'password', '123456');
|
|
80
|
+
|
|
81
|
+
// Logout
|
|
30
82
|
await api.auth.logout();
|
|
31
83
|
```
|
|
32
84
|
|
|
85
|
+
#### Get Current User
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
const user = await api.auth.getCurrentUser();
|
|
89
|
+
// Returns: { user_id, username, first_name, last_name, status }
|
|
90
|
+
|
|
91
|
+
// With custom fields
|
|
92
|
+
const user = await api.auth.getCurrentUser({
|
|
93
|
+
user_id: true,
|
|
94
|
+
username: true,
|
|
95
|
+
email: true
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### Password Management
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
// Update password
|
|
103
|
+
await api.auth.updatePassword('oldPassword', 'newPassword');
|
|
104
|
+
|
|
105
|
+
// Change expired password
|
|
106
|
+
await api.auth.changeExpiredPassword('username', 'oldPassword', 'newPassword');
|
|
107
|
+
|
|
108
|
+
// Forgot password flow
|
|
109
|
+
const jwt = await api.auth.forgetPassword('username', 'email@example.com');
|
|
110
|
+
await api.auth.verifyCode(jwt, '123456');
|
|
111
|
+
await api.auth.resetPassword(jwt, 'newPassword', '123456');
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### OAuth Providers
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
// Google
|
|
118
|
+
await api.auth.google.login(credential);
|
|
119
|
+
await api.auth.google.register(credential);
|
|
120
|
+
await api.auth.google.unlink();
|
|
121
|
+
|
|
122
|
+
// Facebook
|
|
123
|
+
await api.auth.facebook.login(accessToken);
|
|
124
|
+
await api.auth.facebook.register(accessToken);
|
|
125
|
+
await api.auth.facebook.unlink();
|
|
126
|
+
|
|
127
|
+
// Microsoft
|
|
128
|
+
await api.auth.microsoft.login(accessToken);
|
|
129
|
+
await api.auth.microsoft.register(accountId);
|
|
130
|
+
await api.auth.microsoft.unlink();
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### WebAuthn (Passkeys)
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
import { webAuthn } from '@hostlink/light';
|
|
137
|
+
|
|
138
|
+
// Register a new passkey
|
|
139
|
+
await webAuthn.register();
|
|
140
|
+
|
|
141
|
+
// Login with passkey
|
|
142
|
+
await webAuthn.login();
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### Permission Checking
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
// Check single permission
|
|
149
|
+
const canEdit = await api.auth.isGranted('edit_users');
|
|
150
|
+
|
|
151
|
+
// Check multiple permissions
|
|
152
|
+
const rights = await api.auth.grantedRights(['edit_users', 'delete_users']);
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
### GraphQL Queries & Mutations
|
|
158
|
+
|
|
159
|
+
#### Query
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
const result = await api.query({
|
|
163
|
+
app: {
|
|
164
|
+
users: {
|
|
165
|
+
user_id: true,
|
|
166
|
+
username: true,
|
|
167
|
+
profile: {
|
|
168
|
+
avatar: true,
|
|
169
|
+
bio: true
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
#### Mutation
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
const result = await api.mutation({
|
|
180
|
+
createPost: {
|
|
181
|
+
__args: {
|
|
182
|
+
title: 'Hello World',
|
|
183
|
+
content: 'My first post'
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### File Upload
|
|
190
|
+
|
|
191
|
+
File uploads are automatically handled in mutations:
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
// Single file upload
|
|
195
|
+
await api.mutation({
|
|
196
|
+
uploadAvatar: {
|
|
197
|
+
__args: {
|
|
198
|
+
file: fileInput.files[0]
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Multiple file upload
|
|
204
|
+
await api.mutation({
|
|
205
|
+
uploadImages: {
|
|
206
|
+
__args: {
|
|
207
|
+
files: Array.from(fileInput.files)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
### Collection & List Builders
|
|
216
|
+
|
|
217
|
+
#### createList - Simple List Queries
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { createList } from '@hostlink/light';
|
|
221
|
+
|
|
222
|
+
// Create a list query
|
|
223
|
+
const users = await createList('Users', {
|
|
224
|
+
user_id: true,
|
|
225
|
+
username: true,
|
|
226
|
+
first_name: true
|
|
227
|
+
})
|
|
228
|
+
.where('status', 1)
|
|
229
|
+
.where('role', 'admin')
|
|
230
|
+
.sort('-created_at')
|
|
231
|
+
.limit(10)
|
|
232
|
+
.fetch();
|
|
233
|
+
|
|
234
|
+
// Get first item
|
|
235
|
+
const user = await createList('Users', { user_id: true, username: true })
|
|
236
|
+
.where('username', 'john')
|
|
237
|
+
.first();
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
#### Where Clauses
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
list
|
|
244
|
+
.where('status', 1) // Exact match
|
|
245
|
+
.where('age', '>', 18) // Greater than
|
|
246
|
+
.where('age', '>=', 18) // Greater than or equal
|
|
247
|
+
.where('age', '<', 65) // Less than
|
|
248
|
+
.where('age', '<=', 65) // Less than or equal
|
|
249
|
+
.where('status', '!=', 0) // Not equal
|
|
250
|
+
.whereIn('role', ['admin', 'moderator']) // In array
|
|
251
|
+
.whereContains('name', 'john') // Contains string
|
|
252
|
+
.whereBetween('age', 18, 65) // Between range
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
#### createCollection - Advanced Collection Operations
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
import { createCollection } from '@hostlink/light';
|
|
259
|
+
|
|
260
|
+
const collection = createCollection('Products', {
|
|
261
|
+
id: true,
|
|
262
|
+
name: true,
|
|
263
|
+
price: true,
|
|
264
|
+
category: true
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
// Filter and transform data
|
|
268
|
+
const result = await collection
|
|
269
|
+
.where('category', '==', 'electronics')
|
|
270
|
+
.where('price', '<', 1000)
|
|
271
|
+
.sortBy('price')
|
|
272
|
+
.map(item => ({ ...item, discounted: item.price * 0.9 }))
|
|
273
|
+
.all();
|
|
274
|
+
|
|
275
|
+
// Aggregations
|
|
276
|
+
const avgPrice = await collection.avg('price');
|
|
277
|
+
const total = await collection.count();
|
|
278
|
+
const maxPrice = await collection.max('price');
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
### User Management
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
// List users
|
|
287
|
+
const users = await api.users.list();
|
|
288
|
+
|
|
289
|
+
// Create user
|
|
290
|
+
await api.users.create({
|
|
291
|
+
username: 'newuser',
|
|
292
|
+
first_name: 'John',
|
|
293
|
+
last_name: 'Doe',
|
|
294
|
+
password: 'securepassword',
|
|
295
|
+
join_date: '2024-01-01'
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// Update user
|
|
299
|
+
await api.users.update(userId, {
|
|
300
|
+
first_name: 'Jane'
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// Delete user
|
|
304
|
+
await api.users.delete(userId);
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
### Role Management
|
|
310
|
+
|
|
311
|
+
```typescript
|
|
312
|
+
// List roles
|
|
313
|
+
const roles = await api.roles.list();
|
|
314
|
+
|
|
315
|
+
// Create role with child roles
|
|
316
|
+
await api.roles.create('admin', ['editor', 'viewer']);
|
|
317
|
+
|
|
318
|
+
// Delete role
|
|
319
|
+
await api.roles.delete('admin');
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
---
|
|
323
|
+
|
|
324
|
+
### File System API
|
|
325
|
+
|
|
326
|
+
For direct filesystem access:
|
|
327
|
+
|
|
328
|
+
```typescript
|
|
329
|
+
import { fs } from '@hostlink/light';
|
|
330
|
+
|
|
331
|
+
// Create folder
|
|
332
|
+
await fs.createFolder('local://path/to/folder');
|
|
333
|
+
|
|
334
|
+
// Delete folder
|
|
335
|
+
await fs.deleteFolder('local://path/to/folder');
|
|
336
|
+
|
|
337
|
+
// Rename folder
|
|
338
|
+
await fs.renameFolder('local://path/to/folder', 'newName');
|
|
339
|
+
|
|
340
|
+
// Write file
|
|
341
|
+
await fs.writeFile('local://path/to/file.txt', 'content');
|
|
342
|
+
|
|
343
|
+
// Upload file
|
|
344
|
+
await fs.uploadFile('local://path/to/destination', file);
|
|
345
|
+
|
|
346
|
+
// Delete file
|
|
347
|
+
await fs.deleteFile('local://path/to/file.txt');
|
|
348
|
+
|
|
349
|
+
// Check if exists
|
|
350
|
+
const exists = await fs.exists('local://path/to/file.txt');
|
|
351
|
+
|
|
352
|
+
// Move file/folder
|
|
353
|
+
await fs.move('local://from/path', 'local://to/path');
|
|
354
|
+
|
|
355
|
+
// Search files
|
|
356
|
+
const results = await fs.find('searchterm', 'document'); // labels: document, image, audio, video, archive
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
---
|
|
360
|
+
|
|
361
|
+
### Email
|
|
362
|
+
|
|
363
|
+
```typescript
|
|
364
|
+
await api.mail.send(
|
|
365
|
+
'recipient@example.com',
|
|
366
|
+
'Subject Line',
|
|
367
|
+
'Email message body'
|
|
368
|
+
);
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
---
|
|
372
|
+
|
|
373
|
+
### Model Helper
|
|
374
|
+
|
|
375
|
+
Create reusable model definitions:
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
import model from '@hostlink/light';
|
|
379
|
+
|
|
380
|
+
const Product = model('Product', {
|
|
381
|
+
id: { name: 'product_id' },
|
|
382
|
+
name: { gql: { name: true } },
|
|
383
|
+
price: { gql: { price: true, currency: true } }
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
// CRUD operations
|
|
387
|
+
await Product.add({ name: 'New Product', price: 99.99 });
|
|
388
|
+
await Product.update(1, { price: 89.99 });
|
|
389
|
+
await Product.delete(1);
|
|
390
|
+
const product = await Product.get({ id: 1 }, { name: true, price: true });
|
|
391
|
+
const products = await Product.list({ name: true, price: true }).fetch();
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
---
|
|
395
|
+
|
|
396
|
+
## Advanced Configuration
|
|
397
|
+
|
|
398
|
+
### Direct API Client Access
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
import { setApiClient, getApiClient } from '@hostlink/light';
|
|
402
|
+
|
|
403
|
+
// Set a custom API client
|
|
404
|
+
setApiClient(customClient);
|
|
405
|
+
|
|
406
|
+
// Get the current API client
|
|
407
|
+
const client = getApiClient();
|
|
408
|
+
|
|
409
|
+
// Access the underlying axios instance
|
|
410
|
+
const { axios } = getApiClient();
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### Token Refresh
|
|
414
|
+
|
|
415
|
+
The client automatically handles token refresh when receiving `TOKEN_EXPIRED` errors. Failed requests are queued and retried after the token is refreshed.
|
|
416
|
+
|
|
417
|
+
---
|
|
418
|
+
|
|
419
|
+
## TypeScript Support
|
|
420
|
+
|
|
421
|
+
This library is written in TypeScript and includes full type definitions.
|
|
422
|
+
|
|
423
|
+
```typescript
|
|
424
|
+
import type {
|
|
425
|
+
UserFields,
|
|
426
|
+
CreateUserFields,
|
|
427
|
+
QueryUserFieldsUserFields,
|
|
428
|
+
RoleFields,
|
|
429
|
+
FileFields,
|
|
430
|
+
FolderFields,
|
|
431
|
+
GraphQLQuery
|
|
432
|
+
} from '@hostlink/light';
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
---
|
|
436
|
+
|
|
437
|
+
## License
|
|
438
|
+
|
|
439
|
+
MIT
|
|
440
|
+
|
|
441
|
+
## Repository
|
|
442
|
+
|
|
443
|
+
[https://github.com/HostLink/light](https://github.com/HostLink/light)
|
|
444
|
+
|
|
33
445
|
|
|
34
446
|
|
|
35
447
|
|
package/dist/drive.d.ts
CHANGED
|
@@ -1,4 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated Use `fs.listFolders` / `fs.listFiles` from `filesystem` instead.
|
|
3
|
+
*/
|
|
1
4
|
export declare const listDrives: () => Promise<any>;
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated Use functions from `filesystem` (fs.*) instead.
|
|
7
|
+
* - listFiles → fs.listFiles(location)
|
|
8
|
+
* - listFolders → fs.listFolders(location)
|
|
9
|
+
* - writeFile → fs.writeFile(location, content)
|
|
10
|
+
* - deleteFile → fs.deleteFile(location)
|
|
11
|
+
* - renameFile → fs.renameFile(location, newName)
|
|
12
|
+
* - moveFile → fs.move(from, to)
|
|
13
|
+
* - createFolder → fs.createFolder(location)
|
|
14
|
+
* - deleteFolder → fs.deleteFolder(location)
|
|
15
|
+
* - renameFolder → fs.renameFolder(location, newName)
|
|
16
|
+
* - uploadTempFile → fs.uploadTempFile(location, file)
|
|
17
|
+
*/
|
|
2
18
|
export declare const getDrive: (index: number) => {
|
|
3
19
|
folders: {
|
|
4
20
|
list: (path: string, fields?: import('./folder').QueryFolderFields) => Promise<Record<keyof import('./folder').FolderFields, any>[]>;
|
package/dist/file.d.ts
CHANGED
|
@@ -6,8 +6,18 @@ export type FileFields = {
|
|
|
6
6
|
url?: string;
|
|
7
7
|
};
|
|
8
8
|
export type QueryFileFields = Partial<Record<keyof FileFields, boolean>>;
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated Use `fs.listFiles` / `fs.readFile` from `filesystem` instead.
|
|
11
|
+
*/
|
|
9
12
|
export declare const listFiles: (index: number, path: string, fields?: QueryFileFields) => Promise<any>;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Use `fs.readFile(location, 'base64')` from `filesystem` instead.
|
|
15
|
+
*/
|
|
10
16
|
export declare const readFileAsBase64: (index: number, path: string) => Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated Use `fs.listFiles` from `filesystem` instead.
|
|
19
|
+
* The default export factory function is also deprecated — use `fs.*` directly.
|
|
20
|
+
*/
|
|
11
21
|
declare const _default: (index: number) => {
|
|
12
22
|
list: (path: string, fields?: QueryFileFields) => Promise<any>;
|
|
13
23
|
get: (path: string, fields?: QueryFileFields) => Promise<any>;
|
package/dist/filesystem.d.ts
CHANGED
|
@@ -11,3 +11,6 @@ export declare const find: (search?: string, label?: "document" | "image" | "aud
|
|
|
11
11
|
export declare const list: (location: string) => Promise<any>;
|
|
12
12
|
export declare const readFile: (location: string, type?: "text" | "base64") => Promise<any>;
|
|
13
13
|
export declare const uploadBase64: (location: string, base64: string) => Promise<any>;
|
|
14
|
+
export declare const duplicateFile: (location: string) => Promise<string>;
|
|
15
|
+
export declare const uploadTempFile: (location: string, file: File) => Promise<any>;
|
|
16
|
+
export declare const listFiles: (location: string, type?: "image" | "video" | "audio" | "document", search?: string) => Promise<any>;
|
package/dist/folder.d.ts
CHANGED
|
@@ -3,7 +3,13 @@ export type FolderFields = {
|
|
|
3
3
|
path?: string;
|
|
4
4
|
};
|
|
5
5
|
export type QueryFolderFields = Record<keyof FolderFields, boolean>;
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated Use `fs.listFolders` from `filesystem` instead.
|
|
8
|
+
*/
|
|
6
9
|
export declare const listFolders: (index: number, path: string, fields?: QueryFolderFields) => Promise<Array<Record<keyof FolderFields, any>>>;
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated Use `fs.createFolder` / `fs.deleteFolder` / `fs.renameFolder` from `filesystem` instead.
|
|
12
|
+
*/
|
|
7
13
|
declare const _default: (index: number) => {
|
|
8
14
|
list: (path: string, fields?: QueryFolderFields) => Promise<Record<keyof FolderFields, any>[]>;
|
|
9
15
|
create: (path: string) => Promise<any>;
|