@codegenapps/frontend-sdk 1.0.2 → 1.0.3
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 +74 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,7 +31,7 @@ async function initialize() {
|
|
|
31
31
|
baseUrl: 'http://localhost:8080/api',
|
|
32
32
|
|
|
33
33
|
// A function that provides your SDK license key
|
|
34
|
-
licenseKeyProvider: () => '
|
|
34
|
+
licenseKeyProvider: () => 'YOUR_API_KEY',
|
|
35
35
|
|
|
36
36
|
// (Optional) A function that provides the JWT for authenticated requests
|
|
37
37
|
tokenProvider: () => localStorage.getItem('jwt_token'),
|
|
@@ -60,7 +60,38 @@ async function fetchDocuments() {
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
initialize().then(
|
|
63
|
+
initialize().then(async () => {
|
|
64
|
+
await fetchDocuments();
|
|
65
|
+
await loginUser(); // 示範登入功能
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// 3. Perform login
|
|
69
|
+
async function loginUser() {
|
|
70
|
+
const API_KEY = 'YOUR_API_KEY';
|
|
71
|
+
const account = 'test@example.com';
|
|
72
|
+
const password = 'testpassword';
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
console.log(`嘗試使用帳號: ${account}, API Key: ${API_KEY} 登入...`);
|
|
76
|
+
const { data, error } = await cga.auth.login(account, password, API_KEY);
|
|
77
|
+
|
|
78
|
+
if (error) {
|
|
79
|
+
console.error('登入失敗:', error);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
console.log('登入成功! 取得的資料:', data);
|
|
84
|
+
// 您可以在這裡處理登入成功後的邏輯,例如儲存 JWT token
|
|
85
|
+
if (data && data.token) {
|
|
86
|
+
localStorage.setItem('jwt_token', data.token);
|
|
87
|
+
console.log('JWT Token 已儲存到 localStorage。');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
} catch (err) {
|
|
91
|
+
console.error('執行登入時發生意外錯誤:', err);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
64
95
|
```
|
|
65
96
|
|
|
66
97
|
## Usage with Frameworks
|
|
@@ -210,6 +241,47 @@ Example: `cga.from('daily_active_user').byPk({ activity_date: '...', user_id: 1,
|
|
|
210
241
|
|
|
211
242
|
Executes the query and returns a `Promise` that resolves to `{ data, error }`. **All query chains must end with `.run()`.**
|
|
212
243
|
|
|
244
|
+
### Customizing Requests
|
|
245
|
+
|
|
246
|
+
While the query builder provides methods for common operations, you can further customize requests by passing an `options` object to the action methods (`select`, `insert`, `update`, `delete`).
|
|
247
|
+
|
|
248
|
+
#### Adding Custom Headers
|
|
249
|
+
|
|
250
|
+
You can add or override headers for a specific request.
|
|
251
|
+
|
|
252
|
+
```javascript
|
|
253
|
+
const { data, error } = await cga
|
|
254
|
+
.from('documents')
|
|
255
|
+
.select('id, title', { // <-- Pass options here
|
|
256
|
+
headers: {
|
|
257
|
+
'X-Request-ID': 'custom-request-12345'
|
|
258
|
+
}
|
|
259
|
+
})
|
|
260
|
+
.run();
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
#### Adding Custom Query Parameters
|
|
264
|
+
|
|
265
|
+
For special query parameters not covered by the built-in filter methods, you can use `axiosConfig.params`. These will be merged with the parameters generated by the builder.
|
|
266
|
+
|
|
267
|
+
```javascript
|
|
268
|
+
const { data, error } = await cga
|
|
269
|
+
.from('documents')
|
|
270
|
+
.select('id, title', { // <-- Pass options here
|
|
271
|
+
axiosConfig: {
|
|
272
|
+
params: {
|
|
273
|
+
'_cache': false,
|
|
274
|
+
'apiVersion': '2.1'
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
})
|
|
278
|
+
.eq('owner_id', 1) // Builder params are still used
|
|
279
|
+
.run();
|
|
280
|
+
|
|
281
|
+
// The above request will be sent to a URL like:
|
|
282
|
+
// /api/documents?_cache=false&apiVersion=2.1&filter=owner_id,eq,1
|
|
283
|
+
```
|
|
284
|
+
|
|
213
285
|
## License
|
|
214
286
|
|
|
215
287
|
MIT
|