@seaverse/data-service-sdk 0.4.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.
@@ -4179,10 +4179,101 @@
4179
4179
  }
4180
4180
  }
4181
4181
 
4182
+ /**
4183
+ * Create Firebase configuration from Firestore token response
4184
+ *
4185
+ * This helper function extracts the Firebase config from the token response,
4186
+ * making it easy to initialize Firebase app.
4187
+ *
4188
+ * @param tokenResponse - The Firestore token response from SDK
4189
+ * @returns Firebase configuration object ready for initializeApp()
4190
+ *
4191
+ * @example
4192
+ * ```typescript
4193
+ * import { initializeApp } from 'firebase/app';
4194
+ * import { getFirebaseConfig } from '@seaverse/data-service-sdk';
4195
+ *
4196
+ * const tokenResponse = await client.generateGuestFirestoreToken({ app_id: 'my-app' });
4197
+ * const firebaseConfig = getFirebaseConfig(tokenResponse);
4198
+ *
4199
+ * const app = initializeApp(firebaseConfig);
4200
+ * ```
4201
+ */
4202
+ function getFirebaseConfig(tokenResponse) {
4203
+ return {
4204
+ apiKey: tokenResponse.web_api_key,
4205
+ projectId: tokenResponse.project_id,
4206
+ };
4207
+ }
4208
+ /**
4209
+ * Initialize Firebase with Firestore token response (browser only)
4210
+ *
4211
+ * This is a convenience function that automatically:
4212
+ * 1. Creates Firebase config from token response
4213
+ * 2. Initializes Firebase app
4214
+ * 3. Signs in with the custom token
4215
+ * 4. Returns authenticated Firebase instances
4216
+ *
4217
+ * IMPORTANT: This function requires Firebase SDK to be installed separately:
4218
+ * npm install firebase
4219
+ *
4220
+ * @param tokenResponse - The Firestore token response from SDK
4221
+ * @returns Object containing initialized Firebase app, auth, and db instances
4222
+ *
4223
+ * @example
4224
+ * ```typescript
4225
+ * import { initializeWithToken } from '@seaverse/data-service-sdk';
4226
+ *
4227
+ * const tokenResponse = await client.generateGuestFirestoreToken({ app_id: 'my-app' });
4228
+ * const { app, auth, db, userId, appId } = await initializeWithToken(tokenResponse);
4229
+ *
4230
+ * // Ready to use Firestore!
4231
+ * const snapshot = await getDocs(collection(db, `appData/${appId}/publicData/posts`));
4232
+ * ```
4233
+ */
4234
+ async function initializeWithToken(tokenResponse) {
4235
+ // Check if Firebase SDK is available
4236
+ let initializeApp;
4237
+ let getAuth;
4238
+ let signInWithCustomToken;
4239
+ let getFirestore;
4240
+ try {
4241
+ // Try to import Firebase modules
4242
+ const firebaseApp = await import('firebase/app');
4243
+ const firebaseAuth = await import('firebase/auth');
4244
+ const firebaseFirestore = await import('firebase/firestore');
4245
+ initializeApp = firebaseApp.initializeApp;
4246
+ getAuth = firebaseAuth.getAuth;
4247
+ signInWithCustomToken = firebaseAuth.signInWithCustomToken;
4248
+ getFirestore = firebaseFirestore.getFirestore;
4249
+ }
4250
+ catch (error) {
4251
+ throw new Error('Firebase SDK not found. Please install it: npm install firebase\n' +
4252
+ 'Or import manually and use getFirebaseConfig() helper instead.');
4253
+ }
4254
+ // Initialize Firebase
4255
+ const config = getFirebaseConfig(tokenResponse);
4256
+ const app = initializeApp(config);
4257
+ // Sign in with custom token
4258
+ const auth = getAuth(app);
4259
+ await signInWithCustomToken(auth, tokenResponse.custom_token);
4260
+ // Get Firestore instance
4261
+ const db = getFirestore(app);
4262
+ return {
4263
+ app,
4264
+ auth,
4265
+ db,
4266
+ userId: tokenResponse.user_id,
4267
+ appId: tokenResponse.app_id || '',
4268
+ };
4269
+ }
4270
+
4182
4271
  exports.DEFAULT_BASE_URL = DEFAULT_BASE_URL;
4183
4272
  exports.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
4184
4273
  exports.DataServiceClient = DataServiceClient;
4185
4274
  exports.ENDPOINTS = ENDPOINTS;
4275
+ exports.getFirebaseConfig = getFirebaseConfig;
4276
+ exports.initializeWithToken = initializeWithToken;
4186
4277
 
4187
4278
  }));
4188
4279
  //# sourceMappingURL=browser.umd.js.map