@seaverse/data-service-sdk 0.3.0 → 0.5.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 +198 -32
- package/dist/browser.js +4266 -0
- package/dist/browser.js.map +1 -0
- package/dist/browser.umd.js +4279 -0
- package/dist/browser.umd.js.map +1 -0
- package/dist/index.cjs +91 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +85 -6
- package/dist/index.js +90 -1
- package/dist/index.js.map +1 -1
- package/package.json +7 -1
package/README.md
CHANGED
|
@@ -41,18 +41,58 @@ These fields are enforced by Firestore Security Rules and ensure proper data iso
|
|
|
41
41
|
npm install @seaverse/data-service-sdk
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
+
## Browser vs Node.js Usage
|
|
45
|
+
|
|
46
|
+
### For Build Tools (Webpack/Vite/Parcel)
|
|
47
|
+
|
|
48
|
+
The SDK automatically uses the correct version:
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
import { DataServiceClient } from '@seaverse/data-service-sdk';
|
|
52
|
+
// Bundler will automatically use the browser version
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### For Direct Browser Use (Without Build Tools)
|
|
56
|
+
|
|
57
|
+
**Option 1: ES Module (Recommended)**
|
|
58
|
+
|
|
59
|
+
```html
|
|
60
|
+
<script type="module">
|
|
61
|
+
import { DataServiceClient } from 'https://unpkg.com/@seaverse/data-service-sdk/dist/browser.js';
|
|
62
|
+
|
|
63
|
+
const client = new DataServiceClient();
|
|
64
|
+
// Use the client...
|
|
65
|
+
</script>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**Option 2: UMD via Script Tag**
|
|
69
|
+
|
|
70
|
+
```html
|
|
71
|
+
<script src="https://unpkg.com/@seaverse/data-service-sdk/dist/browser.umd.js"></script>
|
|
72
|
+
<script>
|
|
73
|
+
const client = new SeaVerseDataService.DataServiceClient();
|
|
74
|
+
// Use the client...
|
|
75
|
+
</script>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### For Node.js
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
const { DataServiceClient } = require('@seaverse/data-service-sdk');
|
|
82
|
+
// or
|
|
83
|
+
import { DataServiceClient } from '@seaverse/data-service-sdk';
|
|
84
|
+
```
|
|
85
|
+
|
|
44
86
|
## Quick Start
|
|
45
87
|
|
|
46
|
-
###
|
|
88
|
+
### 🚀 Easiest Way (Recommended - Auto Firebase Setup)
|
|
47
89
|
|
|
48
90
|
```typescript
|
|
49
|
-
import { DataServiceClient } from '@seaverse/data-service-sdk';
|
|
91
|
+
import { DataServiceClient, initializeWithToken } from '@seaverse/data-service-sdk';
|
|
50
92
|
import { AuthClient } from '@seaverse/auth-sdk';
|
|
51
|
-
import {
|
|
52
|
-
import { getAuth, signInWithCustomToken } from 'firebase/auth';
|
|
53
|
-
import { getFirestore, collection, addDoc, getDocs, serverTimestamp } from 'firebase/firestore';
|
|
93
|
+
import { collection, addDoc, getDocs, serverTimestamp } from 'firebase/firestore';
|
|
54
94
|
|
|
55
|
-
// Step 1: Login user
|
|
95
|
+
// Step 1: Login user
|
|
56
96
|
const authClient = new AuthClient({ appId: 'my-app-123' });
|
|
57
97
|
const loginResponse = await authClient.loginWithEmail({
|
|
58
98
|
email: 'user@example.com',
|
|
@@ -61,20 +101,15 @@ const loginResponse = await authClient.loginWithEmail({
|
|
|
61
101
|
|
|
62
102
|
// Step 2: Get Firestore token
|
|
63
103
|
const dataClient = new DataServiceClient();
|
|
64
|
-
const
|
|
65
|
-
token: loginResponse.token,
|
|
104
|
+
const tokenResponse = await dataClient.generateFirestoreToken({
|
|
105
|
+
token: loginResponse.token,
|
|
66
106
|
app_id: 'my-app-123'
|
|
67
107
|
});
|
|
68
108
|
|
|
69
|
-
// Step 3:
|
|
70
|
-
const
|
|
71
|
-
const auth = getAuth(app);
|
|
72
|
-
await signInWithCustomToken(auth, firestoreToken.custom_token);
|
|
73
|
-
const db = getFirestore(app);
|
|
109
|
+
// Step 3: Auto-initialize Firebase (ONE LINE!)
|
|
110
|
+
const { db, appId, userId } = await initializeWithToken(tokenResponse);
|
|
74
111
|
|
|
75
|
-
// Step 4: Use Firestore
|
|
76
|
-
const appId = firestoreToken.app_id;
|
|
77
|
-
const userId = firestoreToken.user_id;
|
|
112
|
+
// Step 4: Use Firestore directly!
|
|
78
113
|
|
|
79
114
|
// Write to publicData (everyone can write)
|
|
80
115
|
await addDoc(collection(db, `appData/${appId}/publicData/posts`), {
|
|
@@ -101,31 +136,26 @@ await addDoc(collection(db, `appData/${appId}/userData/${userId}/notes`), {
|
|
|
101
136
|
});
|
|
102
137
|
```
|
|
103
138
|
|
|
104
|
-
### For Guest Users
|
|
139
|
+
### 👤 For Guest Users (Even Simpler!)
|
|
105
140
|
|
|
106
141
|
```typescript
|
|
107
|
-
import { DataServiceClient } from '@seaverse/data-service-sdk';
|
|
108
|
-
import {
|
|
109
|
-
import { getAuth, signInWithCustomToken } from 'firebase/auth';
|
|
110
|
-
import { getFirestore, collection, addDoc, serverTimestamp } from 'firebase/firestore';
|
|
142
|
+
import { DataServiceClient, initializeWithToken } from '@seaverse/data-service-sdk';
|
|
143
|
+
import { collection, addDoc, serverTimestamp } from 'firebase/firestore';
|
|
111
144
|
|
|
112
145
|
// Step 1: Get guest token (no authentication needed!)
|
|
113
146
|
const dataClient = new DataServiceClient();
|
|
114
|
-
const
|
|
147
|
+
const tokenResponse = await dataClient.generateGuestFirestoreToken({
|
|
115
148
|
app_id: 'my-app-123'
|
|
116
149
|
});
|
|
117
150
|
|
|
118
|
-
// Step 2:
|
|
119
|
-
const
|
|
120
|
-
const auth = getAuth(app);
|
|
121
|
-
await signInWithCustomToken(auth, guestToken.custom_token);
|
|
122
|
-
const db = getFirestore(app);
|
|
151
|
+
// Step 2: Auto-initialize Firebase (ONE LINE!)
|
|
152
|
+
const { db, appId, userId } = await initializeWithToken(tokenResponse);
|
|
123
153
|
|
|
124
154
|
// Step 3: Guest can write to publicData
|
|
125
|
-
await addDoc(collection(db, `appData/${
|
|
126
|
-
_appId:
|
|
155
|
+
await addDoc(collection(db, `appData/${appId}/publicData/comments`), {
|
|
156
|
+
_appId: appId,
|
|
127
157
|
_createdAt: serverTimestamp(),
|
|
128
|
-
_createdBy:
|
|
158
|
+
_createdBy: userId, // Guest user ID (e.g., 'guest-abc123')
|
|
129
159
|
comment: 'Great app!',
|
|
130
160
|
rating: 5
|
|
131
161
|
});
|
|
@@ -133,6 +163,36 @@ await addDoc(collection(db, `appData/${guestToken.app_id}/publicData/comments`),
|
|
|
133
163
|
// Note: Guests CANNOT access userData
|
|
134
164
|
```
|
|
135
165
|
|
|
166
|
+
### 🔧 Manual Way (If you need more control)
|
|
167
|
+
|
|
168
|
+
If you prefer to initialize Firebase manually:
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { DataServiceClient, getFirebaseConfig } from '@seaverse/data-service-sdk';
|
|
172
|
+
import { initializeApp } from 'firebase/app';
|
|
173
|
+
import { getAuth, signInWithCustomToken } from 'firebase/auth';
|
|
174
|
+
import { getFirestore } from 'firebase/firestore';
|
|
175
|
+
|
|
176
|
+
const dataClient = new DataServiceClient();
|
|
177
|
+
const tokenResponse = await dataClient.generateGuestFirestoreToken({
|
|
178
|
+
app_id: 'my-app-123'
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Option 1: Use getFirebaseConfig helper
|
|
182
|
+
const firebaseConfig = getFirebaseConfig(tokenResponse);
|
|
183
|
+
const app = initializeApp(firebaseConfig);
|
|
184
|
+
|
|
185
|
+
// Option 2: Manual config
|
|
186
|
+
const app = initializeApp({
|
|
187
|
+
apiKey: tokenResponse.web_api_key, // ✅ Provided automatically!
|
|
188
|
+
projectId: tokenResponse.project_id
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const auth = getAuth(app);
|
|
192
|
+
await signInWithCustomToken(auth, tokenResponse.custom_token);
|
|
193
|
+
const db = getFirestore(app);
|
|
194
|
+
```
|
|
195
|
+
|
|
136
196
|
## API Reference
|
|
137
197
|
|
|
138
198
|
### DataServiceClient
|
|
@@ -190,11 +250,12 @@ interface GenerateFirestoreTokenRequest {
|
|
|
190
250
|
```typescript
|
|
191
251
|
interface FirestoreTokenResponse {
|
|
192
252
|
custom_token: string; // Firebase Custom Token - use with signInWithCustomToken()
|
|
253
|
+
web_api_key: string; // Firebase Web API Key - use with initializeApp()
|
|
193
254
|
project_id: string; // Firebase Project ID for initializeApp()
|
|
194
255
|
database_id: string; // Firestore Database ID
|
|
195
256
|
app_id?: string; // Application ID (use in Firestore paths)
|
|
196
257
|
user_id: string; // User ID (use in Firestore paths)
|
|
197
|
-
|
|
258
|
+
user_type: string; // User type ('guest', 'user', 'admin', 'appadmin')
|
|
198
259
|
expires_in: number; // Token expiration in seconds (typically 3600)
|
|
199
260
|
}
|
|
200
261
|
```
|
|
@@ -244,7 +305,63 @@ const guestToken = await client.generateGuestFirestoreToken({
|
|
|
244
305
|
|
|
245
306
|
console.log('Guest Custom Token:', guestToken.custom_token);
|
|
246
307
|
console.log('Guest User ID:', guestToken.user_id);
|
|
247
|
-
console.log('
|
|
308
|
+
console.log('User Type:', guestToken.user_type); // 'guest'
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### Helper Functions
|
|
312
|
+
|
|
313
|
+
#### getFirebaseConfig
|
|
314
|
+
|
|
315
|
+
Extract Firebase configuration from token response.
|
|
316
|
+
|
|
317
|
+
```typescript
|
|
318
|
+
getFirebaseConfig(tokenResponse: FirestoreTokenResponse): FirebaseConfig
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
**Example:**
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
import { getFirebaseConfig } from '@seaverse/data-service-sdk';
|
|
325
|
+
import { initializeApp } from 'firebase/app';
|
|
326
|
+
|
|
327
|
+
const tokenResponse = await client.generateGuestFirestoreToken({ app_id: 'my-app' });
|
|
328
|
+
const firebaseConfig = getFirebaseConfig(tokenResponse);
|
|
329
|
+
// Returns: { apiKey: '...', projectId: '...' }
|
|
330
|
+
|
|
331
|
+
const app = initializeApp(firebaseConfig);
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
#### initializeWithToken
|
|
335
|
+
|
|
336
|
+
Automatically initialize Firebase and sign in with token (one-line setup).
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
initializeWithToken(tokenResponse: FirestoreTokenResponse): Promise<{
|
|
340
|
+
app: FirebaseApp;
|
|
341
|
+
auth: Auth;
|
|
342
|
+
db: Firestore;
|
|
343
|
+
userId: string;
|
|
344
|
+
appId: string;
|
|
345
|
+
}>
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
**Example:**
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
import { initializeWithToken } from '@seaverse/data-service-sdk';
|
|
352
|
+
|
|
353
|
+
const tokenResponse = await client.generateGuestFirestoreToken({ app_id: 'my-app' });
|
|
354
|
+
|
|
355
|
+
// One line to get everything!
|
|
356
|
+
const { db, appId, userId } = await initializeWithToken(tokenResponse);
|
|
357
|
+
|
|
358
|
+
// Ready to use Firestore
|
|
359
|
+
await addDoc(collection(db, `appData/${appId}/publicData/posts`), { ... });
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
**Note:** This function requires Firebase SDK to be installed separately:
|
|
363
|
+
```bash
|
|
364
|
+
npm install firebase
|
|
248
365
|
```
|
|
249
366
|
|
|
250
367
|
## Common Use Cases
|
|
@@ -521,6 +638,55 @@ For issues and questions, please visit:
|
|
|
521
638
|
- GitHub: https://github.com/seaverseai/sv-sdk
|
|
522
639
|
- Email: support@seaverse.com
|
|
523
640
|
|
|
641
|
+
## Browser Example (Vanilla JavaScript)
|
|
642
|
+
|
|
643
|
+
Here's a complete example for using the SDK directly in the browser without any build tools:
|
|
644
|
+
|
|
645
|
+
```html
|
|
646
|
+
<!DOCTYPE html>
|
|
647
|
+
<html>
|
|
648
|
+
<head>
|
|
649
|
+
<title>SeaVerse Data Service SDK - Browser Example</title>
|
|
650
|
+
</head>
|
|
651
|
+
<body>
|
|
652
|
+
<h1>Firestore Token Demo</h1>
|
|
653
|
+
<button id="getGuestToken">Get Guest Token</button>
|
|
654
|
+
<pre id="output"></pre>
|
|
655
|
+
|
|
656
|
+
<script type="module">
|
|
657
|
+
// Import from CDN
|
|
658
|
+
import { DataServiceClient } from 'https://unpkg.com/@seaverse/data-service-sdk/dist/browser.js';
|
|
659
|
+
|
|
660
|
+
const output = document.getElementById('output');
|
|
661
|
+
const client = new DataServiceClient();
|
|
662
|
+
|
|
663
|
+
document.getElementById('getGuestToken').addEventListener('click', async () => {
|
|
664
|
+
try {
|
|
665
|
+
output.textContent = 'Getting guest token...';
|
|
666
|
+
|
|
667
|
+
const guestToken = await client.generateGuestFirestoreToken({
|
|
668
|
+
app_id: 'my-app-123'
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
output.textContent = JSON.stringify({
|
|
672
|
+
custom_token: guestToken.custom_token.substring(0, 50) + '...',
|
|
673
|
+
user_id: guestToken.user_id,
|
|
674
|
+
role: guestToken.role,
|
|
675
|
+
project_id: guestToken.project_id,
|
|
676
|
+
expires_in: guestToken.expires_in
|
|
677
|
+
}, null, 2);
|
|
678
|
+
|
|
679
|
+
// Now you can use this token with Firebase SDK
|
|
680
|
+
console.log('Guest token received:', guestToken);
|
|
681
|
+
} catch (error) {
|
|
682
|
+
output.textContent = 'Error: ' + error.message;
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
</script>
|
|
686
|
+
</body>
|
|
687
|
+
</html>
|
|
688
|
+
```
|
|
689
|
+
|
|
524
690
|
## Related SDKs
|
|
525
691
|
|
|
526
692
|
- [@seaverse/auth-sdk](../auth-sdk) - User authentication and account management
|