@seaverse/data-service-sdk 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 ADDED
@@ -0,0 +1,296 @@
1
+ # @seaverse/data-service-sdk
2
+
3
+ SeaVerse Data Service SDK for managing Firestore tokens in frontend applications.
4
+
5
+ ## Features
6
+
7
+ - Generate Firestore tokens for authenticated users
8
+ - Generate Firestore tokens for guest users
9
+ - TypeScript support with full type definitions
10
+ - Promise-based API using axios
11
+ - Automatic response unwrapping
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install @seaverse/data-service-sdk
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ ```typescript
22
+ import { DataServiceClient } from '@seaverse/data-service-sdk';
23
+
24
+ // Create client instance
25
+ const client = new DataServiceClient();
26
+
27
+ // For authenticated users
28
+ const token = await client.generateFirestoreToken({
29
+ token: 'user-jwt-token',
30
+ app_id: 'your-app-id',
31
+ });
32
+
33
+ // For guest users
34
+ const guestToken = await client.generateGuestFirestoreToken({
35
+ app_id: 'your-app-id',
36
+ });
37
+ ```
38
+
39
+ ## API Reference
40
+
41
+ ### DataServiceClient
42
+
43
+ The main client for interacting with the Data Service API.
44
+
45
+ #### Constructor
46
+
47
+ ```typescript
48
+ new DataServiceClient(options?: DataServiceClientOptions)
49
+ ```
50
+
51
+ **Options:**
52
+
53
+ - `baseURL?: string` - Base URL for the API (default: `https://auth.seaverse.ai`)
54
+ - `timeout?: number` - Request timeout in milliseconds (default: `10000`)
55
+ - `headers?: Record<string, string>` - Custom request headers
56
+
57
+ **Example:**
58
+
59
+ ```typescript
60
+ const client = new DataServiceClient({
61
+ baseURL: 'https://auth.seaverse.ai',
62
+ timeout: 15000,
63
+ headers: {
64
+ 'X-Custom-Header': 'value',
65
+ },
66
+ });
67
+ ```
68
+
69
+ ### Methods
70
+
71
+ #### generateFirestoreToken
72
+
73
+ Generate a Firestore token for an authenticated user.
74
+
75
+ ```typescript
76
+ generateFirestoreToken(
77
+ request: GenerateFirestoreTokenRequest,
78
+ options?: AxiosRequestConfig
79
+ ): Promise<FirestoreTokenResponse>
80
+ ```
81
+
82
+ **Request:**
83
+
84
+ ```typescript
85
+ interface GenerateFirestoreTokenRequest {
86
+ token: string; // User's JWT token
87
+ app_id: string; // Application ID
88
+ }
89
+ ```
90
+
91
+ **Response:**
92
+
93
+ ```typescript
94
+ interface FirestoreTokenResponse {
95
+ id_token: string; // Firebase ID Token
96
+ refresh_token?: string; // Firebase Refresh Token (optional)
97
+ project_id: string; // Firebase Project ID
98
+ database_id: string; // Firestore Database ID
99
+ app_id?: string; // Application ID
100
+ user_id: string; // User ID
101
+ role?: string; // User role
102
+ expires_in: number; // Expiration time in seconds (typically 3600)
103
+ }
104
+ ```
105
+
106
+ **Example:**
107
+
108
+ ```typescript
109
+ try {
110
+ const response = await client.generateFirestoreToken({
111
+ token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
112
+ app_id: 'my-app',
113
+ });
114
+
115
+ console.log('Firestore ID Token:', response.id_token);
116
+ console.log('User ID:', response.user_id);
117
+ console.log('Expires in:', response.expires_in, 'seconds');
118
+ } catch (error) {
119
+ console.error('Failed to generate Firestore token:', error);
120
+ }
121
+ ```
122
+
123
+ #### generateGuestFirestoreToken
124
+
125
+ Generate a Firestore token for a guest (unauthenticated) user.
126
+
127
+ ```typescript
128
+ generateGuestFirestoreToken(
129
+ request: GenerateGuestFirestoreTokenRequest,
130
+ options?: AxiosRequestConfig
131
+ ): Promise<FirestoreTokenResponse>
132
+ ```
133
+
134
+ **Request:**
135
+
136
+ ```typescript
137
+ interface GenerateGuestFirestoreTokenRequest {
138
+ app_id: string; // Application ID
139
+ }
140
+ ```
141
+
142
+ **Response:**
143
+
144
+ Same as `FirestoreTokenResponse` above, with `role` typically set to `'guest'`.
145
+
146
+ **Example:**
147
+
148
+ ```typescript
149
+ try {
150
+ const response = await client.generateGuestFirestoreToken({
151
+ app_id: 'my-app',
152
+ });
153
+
154
+ console.log('Guest Firestore ID Token:', response.id_token);
155
+ console.log('Guest User ID:', response.user_id);
156
+ console.log('Role:', response.role); // 'guest'
157
+ } catch (error) {
158
+ console.error('Failed to generate guest Firestore token:', error);
159
+ }
160
+ ```
161
+
162
+ ## Usage Examples
163
+
164
+ ### Browser Environment
165
+
166
+ ```typescript
167
+ import { DataServiceClient } from '@seaverse/data-service-sdk';
168
+
169
+ const client = new DataServiceClient();
170
+
171
+ // Get user token from your auth system
172
+ const userToken = localStorage.getItem('authToken');
173
+
174
+ // Generate Firestore token
175
+ const firestoreToken = await client.generateFirestoreToken({
176
+ token: userToken,
177
+ app_id: 'my-app',
178
+ });
179
+
180
+ // Use the Firestore token to initialize Firebase
181
+ import { initializeApp } from 'firebase/app';
182
+ import { getFirestore } from 'firebase/firestore';
183
+ import { signInWithCustomToken, getAuth } from 'firebase/auth';
184
+
185
+ const firebaseApp = initializeApp({
186
+ projectId: firestoreToken.project_id,
187
+ // ... other Firebase config
188
+ });
189
+
190
+ const auth = getAuth(firebaseApp);
191
+ await signInWithCustomToken(auth, firestoreToken.id_token);
192
+
193
+ const db = getFirestore(firebaseApp);
194
+ // Now you can use Firestore with the authenticated user
195
+ ```
196
+
197
+ ### Node.js Environment
198
+
199
+ ```typescript
200
+ import { DataServiceClient } from '@seaverse/data-service-sdk';
201
+
202
+ const client = new DataServiceClient();
203
+
204
+ async function getFirestoreAccess(userToken: string, appId: string) {
205
+ try {
206
+ const response = await client.generateFirestoreToken({
207
+ token: userToken,
208
+ app_id: appId,
209
+ });
210
+
211
+ return {
212
+ token: response.id_token,
213
+ projectId: response.project_id,
214
+ databaseId: response.database_id,
215
+ userId: response.user_id,
216
+ expiresIn: response.expires_in,
217
+ };
218
+ } catch (error) {
219
+ console.error('Error getting Firestore access:', error);
220
+ throw error;
221
+ }
222
+ }
223
+ ```
224
+
225
+ ### Guest User Example
226
+
227
+ ```typescript
228
+ import { DataServiceClient } from '@seaverse/data-service-sdk';
229
+
230
+ const client = new DataServiceClient();
231
+
232
+ // For anonymous/guest users who don't have an account
233
+ async function initializeGuestSession(appId: string) {
234
+ const guestToken = await client.generateGuestFirestoreToken({
235
+ app_id: appId,
236
+ });
237
+
238
+ // Initialize Firebase with guest token
239
+ const auth = getAuth(firebaseApp);
240
+ await signInWithCustomToken(auth, guestToken.id_token);
241
+
242
+ console.log('Guest session initialized:', guestToken.user_id);
243
+ }
244
+ ```
245
+
246
+ ## Error Handling
247
+
248
+ The SDK automatically handles API response errors and throws meaningful error messages:
249
+
250
+ ```typescript
251
+ try {
252
+ const token = await client.generateFirestoreToken({
253
+ token: 'invalid-token',
254
+ app_id: 'my-app',
255
+ });
256
+ } catch (error) {
257
+ if (error instanceof Error) {
258
+ console.error('Error:', error.message);
259
+ // Error messages come from the API response
260
+ }
261
+ }
262
+ ```
263
+
264
+ ## TypeScript Support
265
+
266
+ This SDK is written in TypeScript and provides full type definitions:
267
+
268
+ ```typescript
269
+ import type {
270
+ DataServiceClient,
271
+ DataServiceClientOptions,
272
+ GenerateFirestoreTokenRequest,
273
+ GenerateGuestFirestoreTokenRequest,
274
+ FirestoreTokenResponse,
275
+ ApiResponse,
276
+ ApiError,
277
+ } from '@seaverse/data-service-sdk';
278
+ ```
279
+
280
+ ## API Endpoints
281
+
282
+ The SDK connects to the following endpoints by default:
283
+
284
+ - **Base URL:** `https://auth.seaverse.ai`
285
+ - **Firestore Token:** `POST /api/v1/firestore/token`
286
+ - **Guest Token:** `POST /api/v1/firestore/guest-token`
287
+
288
+ ## License
289
+
290
+ MIT
291
+
292
+ ## Support
293
+
294
+ For issues and questions, please visit:
295
+ - GitHub: https://github.com/seaverseai/sv-sdk
296
+ - Email: support@seaverse.com
package/dist/index.cjs ADDED
@@ -0,0 +1,157 @@
1
+ 'use strict';
2
+
3
+ var axios = require('axios');
4
+
5
+ /**
6
+ * Data Service SDK configuration
7
+ */
8
+ /**
9
+ * Default base URL for Data Service
10
+ */
11
+ const DEFAULT_BASE_URL = 'https://auth.seaverse.ai';
12
+ /**
13
+ * Default timeout in milliseconds
14
+ */
15
+ const DEFAULT_TIMEOUT = 10000;
16
+ /**
17
+ * API endpoints
18
+ */
19
+ const ENDPOINTS = {
20
+ FIRESTORE_TOKEN: '/api/v1/firestore/token',
21
+ FIRESTORE_GUEST_TOKEN: '/api/v1/firestore/guest-token',
22
+ };
23
+
24
+ /**
25
+ * SeaVerse Data Service SDK Client
26
+ *
27
+ * Provides methods to manage Firestore tokens for authenticated users and guests.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * // Create client instance
32
+ * const client = new DataServiceClient();
33
+ *
34
+ * // Generate Firestore token for authenticated user
35
+ * const token = await client.generateFirestoreToken({
36
+ * token: 'user-jwt-token',
37
+ * app_id: 'your-app-id',
38
+ * });
39
+ *
40
+ * // Generate Firestore token for guest
41
+ * const guestToken = await client.generateGuestFirestoreToken({
42
+ * app_id: 'your-app-id',
43
+ * });
44
+ * ```
45
+ */
46
+ class DataServiceClient {
47
+ constructor(options = {}) {
48
+ const { baseURL = DEFAULT_BASE_URL, timeout = DEFAULT_TIMEOUT, headers = {}, } = options;
49
+ this.axiosInstance = axios.create({
50
+ baseURL,
51
+ timeout,
52
+ headers: {
53
+ 'Content-Type': 'application/json',
54
+ ...headers,
55
+ },
56
+ });
57
+ // Add response interceptor to handle API response format
58
+ this.axiosInstance.interceptors.response.use((response) => {
59
+ // Extract data from ApiResponse wrapper
60
+ if (response.data && typeof response.data === 'object') {
61
+ if ('code' in response.data && 'data' in response.data) {
62
+ // Standard API response format
63
+ const apiResponse = response.data;
64
+ if (apiResponse.code === 0 && apiResponse.data) {
65
+ response.data = apiResponse.data;
66
+ }
67
+ else if (apiResponse.code !== 0) {
68
+ // Handle API-level errors
69
+ const error = {
70
+ code: apiResponse.code,
71
+ message: apiResponse.message,
72
+ request_id: apiResponse.request_id,
73
+ };
74
+ throw error;
75
+ }
76
+ }
77
+ }
78
+ return response;
79
+ }, (error) => {
80
+ // Handle HTTP errors
81
+ if (error.response?.data) {
82
+ const apiError = error.response.data;
83
+ throw new Error(apiError.message || error.message);
84
+ }
85
+ throw error;
86
+ });
87
+ }
88
+ /**
89
+ * Generate Firestore token for authenticated user
90
+ *
91
+ * This method generates a Firestore custom token for an authenticated user.
92
+ * The user must provide a valid JWT token.
93
+ *
94
+ * @param request - The request containing user token and app ID
95
+ * @param options - Additional axios request configuration
96
+ * @returns Firestore token response with ID token and metadata
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * const response = await client.generateFirestoreToken({
101
+ * token: 'user-jwt-token',
102
+ * app_id: 'my-app',
103
+ * });
104
+ *
105
+ * console.log('Firestore ID Token:', response.id_token);
106
+ * console.log('User ID:', response.user_id);
107
+ * console.log('Expires in:', response.expires_in, 'seconds');
108
+ * ```
109
+ */
110
+ async generateFirestoreToken(request, options) {
111
+ const config = {
112
+ method: 'POST',
113
+ url: ENDPOINTS.FIRESTORE_TOKEN,
114
+ data: request,
115
+ ...options,
116
+ };
117
+ const response = await this.axiosInstance.request(config);
118
+ return response.data;
119
+ }
120
+ /**
121
+ * Generate Firestore token for guest user
122
+ *
123
+ * This method generates a Firestore custom token for a guest (unauthenticated) user.
124
+ * No user authentication token is required.
125
+ *
126
+ * @param request - The request containing app ID
127
+ * @param options - Additional axios request configuration
128
+ * @returns Firestore token response with ID token and metadata
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * const response = await client.generateGuestFirestoreToken({
133
+ * app_id: 'my-app',
134
+ * });
135
+ *
136
+ * console.log('Guest Firestore ID Token:', response.id_token);
137
+ * console.log('Guest User ID:', response.user_id);
138
+ * console.log('Role:', response.role); // 'guest'
139
+ * ```
140
+ */
141
+ async generateGuestFirestoreToken(request, options) {
142
+ const config = {
143
+ method: 'POST',
144
+ url: ENDPOINTS.FIRESTORE_GUEST_TOKEN,
145
+ data: request,
146
+ ...options,
147
+ };
148
+ const response = await this.axiosInstance.request(config);
149
+ return response.data;
150
+ }
151
+ }
152
+
153
+ exports.DEFAULT_BASE_URL = DEFAULT_BASE_URL;
154
+ exports.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
155
+ exports.DataServiceClient = DataServiceClient;
156
+ exports.ENDPOINTS = ENDPOINTS;
157
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/config.ts","../src/client.ts"],"sourcesContent":[null,null],"names":[],"mappings":";;;;AAAA;;AAEG;AAEH;;AAEG;AACI,MAAM,gBAAgB,GAAG;AAEhC;;AAEG;AACI,MAAM,eAAe,GAAG;AAE/B;;AAEG;AACI,MAAM,SAAS,GAAG;AACvB,IAAA,eAAe,EAAE,yBAAyB;AAC1C,IAAA,qBAAqB,EAAE,+BAA+B;;;ACaxD;;;;;;;;;;;;;;;;;;;;;AAqBG;MACU,iBAAiB,CAAA;AAG5B,IAAA,WAAA,CAAY,UAAoC,EAAE,EAAA;AAChD,QAAA,MAAM,EACJ,OAAO,GAAG,gBAAgB,EAC1B,OAAO,GAAG,eAAe,EACzB,OAAO,GAAG,EAAE,GACb,GAAG,OAAO;AAEX,QAAA,IAAI,CAAC,aAAa,GAAG,KAAK,CAAC,MAAM,CAAC;YAChC,OAAO;YACP,OAAO;AACP,YAAA,OAAO,EAAE;AACP,gBAAA,cAAc,EAAE,kBAAkB;AAClC,gBAAA,GAAG,OAAO;AACX,aAAA;AACF,SAAA,CAAC;;AAGF,QAAA,IAAI,CAAC,aAAa,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAC1C,CAAC,QAAQ,KAAI;;YAEX,IAAI,QAAQ,CAAC,IAAI,IAAI,OAAO,QAAQ,CAAC,IAAI,KAAK,QAAQ,EAAE;AACtD,gBAAA,IAAI,MAAM,IAAI,QAAQ,CAAC,IAAI,IAAI,MAAM,IAAI,QAAQ,CAAC,IAAI,EAAE;;AAEtD,oBAAA,MAAM,WAAW,GAAG,QAAQ,CAAC,IAAwB;oBACrD,IAAI,WAAW,CAAC,IAAI,KAAK,CAAC,IAAI,W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C,YAAA,IAAI,EAAE,OAAO;AACb,YAAA,GAAG,OAAO;SACX;QAED,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,OAAO,CAAyB,MAAM,CAAC;QACjF,OAAO,QAAQ,CAAC,IAAI;IACtB;AACD;;;;;;;"}
@@ -0,0 +1,207 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+
3
+ /**
4
+ * Type definitions for SeaVerse Data Service API
5
+ * Firestore token management
6
+ */
7
+ /**
8
+ * Request to generate Firestore token for authenticated user
9
+ */
10
+ interface GenerateFirestoreTokenRequest {
11
+ /**
12
+ * User account token (JWT)
13
+ */
14
+ token: string;
15
+ /**
16
+ * Application ID
17
+ */
18
+ app_id: string;
19
+ }
20
+ /**
21
+ * Request to generate Firestore token for guest user
22
+ */
23
+ interface GenerateGuestFirestoreTokenRequest {
24
+ /**
25
+ * Application ID
26
+ */
27
+ app_id: string;
28
+ }
29
+ /**
30
+ * Firestore token response
31
+ */
32
+ interface FirestoreTokenResponse {
33
+ /**
34
+ * Firebase ID Token (exchanged from Custom Token)
35
+ */
36
+ id_token: string;
37
+ /**
38
+ * Firebase Refresh Token (optional)
39
+ */
40
+ refresh_token?: string;
41
+ /**
42
+ * Firebase Project ID
43
+ */
44
+ project_id: string;
45
+ /**
46
+ * Firestore Database ID
47
+ */
48
+ database_id: string;
49
+ /**
50
+ * Application ID
51
+ */
52
+ app_id?: string;
53
+ /**
54
+ * User ID
55
+ */
56
+ user_id: string;
57
+ /**
58
+ * User role (guest, appadmin, superadmin)
59
+ */
60
+ role?: string;
61
+ /**
62
+ * Token expiration time in seconds (typically 3600 = 1 hour)
63
+ */
64
+ expires_in: number;
65
+ }
66
+ /**
67
+ * Standard API response wrapper
68
+ */
69
+ interface ApiResponse<T> {
70
+ /**
71
+ * Response code (0 for success)
72
+ */
73
+ code: number;
74
+ /**
75
+ * Response message
76
+ */
77
+ message: string;
78
+ /**
79
+ * Request ID for tracking
80
+ */
81
+ request_id?: string;
82
+ /**
83
+ * Response data
84
+ */
85
+ data?: T;
86
+ }
87
+ /**
88
+ * API error response
89
+ */
90
+ interface ApiError {
91
+ code: number;
92
+ message: string;
93
+ request_id?: string;
94
+ }
95
+
96
+ /**
97
+ * Data Service SDK Client configuration options
98
+ */
99
+ interface DataServiceClientOptions {
100
+ /**
101
+ * Base URL for the Data Service API
102
+ * @default 'https://auth.seaverse.ai'
103
+ */
104
+ baseURL?: string;
105
+ /**
106
+ * Request timeout in milliseconds
107
+ * @default 10000
108
+ */
109
+ timeout?: number;
110
+ /**
111
+ * Custom request headers
112
+ */
113
+ headers?: Record<string, string>;
114
+ }
115
+ /**
116
+ * SeaVerse Data Service SDK Client
117
+ *
118
+ * Provides methods to manage Firestore tokens for authenticated users and guests.
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * // Create client instance
123
+ * const client = new DataServiceClient();
124
+ *
125
+ * // Generate Firestore token for authenticated user
126
+ * const token = await client.generateFirestoreToken({
127
+ * token: 'user-jwt-token',
128
+ * app_id: 'your-app-id',
129
+ * });
130
+ *
131
+ * // Generate Firestore token for guest
132
+ * const guestToken = await client.generateGuestFirestoreToken({
133
+ * app_id: 'your-app-id',
134
+ * });
135
+ * ```
136
+ */
137
+ declare class DataServiceClient {
138
+ private axiosInstance;
139
+ constructor(options?: DataServiceClientOptions);
140
+ /**
141
+ * Generate Firestore token for authenticated user
142
+ *
143
+ * This method generates a Firestore custom token for an authenticated user.
144
+ * The user must provide a valid JWT token.
145
+ *
146
+ * @param request - The request containing user token and app ID
147
+ * @param options - Additional axios request configuration
148
+ * @returns Firestore token response with ID token and metadata
149
+ *
150
+ * @example
151
+ * ```typescript
152
+ * const response = await client.generateFirestoreToken({
153
+ * token: 'user-jwt-token',
154
+ * app_id: 'my-app',
155
+ * });
156
+ *
157
+ * console.log('Firestore ID Token:', response.id_token);
158
+ * console.log('User ID:', response.user_id);
159
+ * console.log('Expires in:', response.expires_in, 'seconds');
160
+ * ```
161
+ */
162
+ generateFirestoreToken(request: GenerateFirestoreTokenRequest, options?: AxiosRequestConfig): Promise<FirestoreTokenResponse>;
163
+ /**
164
+ * Generate Firestore token for guest user
165
+ *
166
+ * This method generates a Firestore custom token for a guest (unauthenticated) user.
167
+ * No user authentication token is required.
168
+ *
169
+ * @param request - The request containing app ID
170
+ * @param options - Additional axios request configuration
171
+ * @returns Firestore token response with ID token and metadata
172
+ *
173
+ * @example
174
+ * ```typescript
175
+ * const response = await client.generateGuestFirestoreToken({
176
+ * app_id: 'my-app',
177
+ * });
178
+ *
179
+ * console.log('Guest Firestore ID Token:', response.id_token);
180
+ * console.log('Guest User ID:', response.user_id);
181
+ * console.log('Role:', response.role); // 'guest'
182
+ * ```
183
+ */
184
+ generateGuestFirestoreToken(request: GenerateGuestFirestoreTokenRequest, options?: AxiosRequestConfig): Promise<FirestoreTokenResponse>;
185
+ }
186
+
187
+ /**
188
+ * Data Service SDK configuration
189
+ */
190
+ /**
191
+ * Default base URL for Data Service
192
+ */
193
+ declare const DEFAULT_BASE_URL = "https://auth.seaverse.ai";
194
+ /**
195
+ * Default timeout in milliseconds
196
+ */
197
+ declare const DEFAULT_TIMEOUT = 10000;
198
+ /**
199
+ * API endpoints
200
+ */
201
+ declare const ENDPOINTS: {
202
+ readonly FIRESTORE_TOKEN: "/api/v1/firestore/token";
203
+ readonly FIRESTORE_GUEST_TOKEN: "/api/v1/firestore/guest-token";
204
+ };
205
+
206
+ export { DEFAULT_BASE_URL, DEFAULT_TIMEOUT, DataServiceClient, ENDPOINTS };
207
+ export type { ApiError, ApiResponse, DataServiceClientOptions, FirestoreTokenResponse, GenerateFirestoreTokenRequest, GenerateGuestFirestoreTokenRequest };
package/dist/index.js ADDED
@@ -0,0 +1,152 @@
1
+ import axios from 'axios';
2
+
3
+ /**
4
+ * Data Service SDK configuration
5
+ */
6
+ /**
7
+ * Default base URL for Data Service
8
+ */
9
+ const DEFAULT_BASE_URL = 'https://auth.seaverse.ai';
10
+ /**
11
+ * Default timeout in milliseconds
12
+ */
13
+ const DEFAULT_TIMEOUT = 10000;
14
+ /**
15
+ * API endpoints
16
+ */
17
+ const ENDPOINTS = {
18
+ FIRESTORE_TOKEN: '/api/v1/firestore/token',
19
+ FIRESTORE_GUEST_TOKEN: '/api/v1/firestore/guest-token',
20
+ };
21
+
22
+ /**
23
+ * SeaVerse Data Service SDK Client
24
+ *
25
+ * Provides methods to manage Firestore tokens for authenticated users and guests.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * // Create client instance
30
+ * const client = new DataServiceClient();
31
+ *
32
+ * // Generate Firestore token for authenticated user
33
+ * const token = await client.generateFirestoreToken({
34
+ * token: 'user-jwt-token',
35
+ * app_id: 'your-app-id',
36
+ * });
37
+ *
38
+ * // Generate Firestore token for guest
39
+ * const guestToken = await client.generateGuestFirestoreToken({
40
+ * app_id: 'your-app-id',
41
+ * });
42
+ * ```
43
+ */
44
+ class DataServiceClient {
45
+ constructor(options = {}) {
46
+ const { baseURL = DEFAULT_BASE_URL, timeout = DEFAULT_TIMEOUT, headers = {}, } = options;
47
+ this.axiosInstance = axios.create({
48
+ baseURL,
49
+ timeout,
50
+ headers: {
51
+ 'Content-Type': 'application/json',
52
+ ...headers,
53
+ },
54
+ });
55
+ // Add response interceptor to handle API response format
56
+ this.axiosInstance.interceptors.response.use((response) => {
57
+ // Extract data from ApiResponse wrapper
58
+ if (response.data && typeof response.data === 'object') {
59
+ if ('code' in response.data && 'data' in response.data) {
60
+ // Standard API response format
61
+ const apiResponse = response.data;
62
+ if (apiResponse.code === 0 && apiResponse.data) {
63
+ response.data = apiResponse.data;
64
+ }
65
+ else if (apiResponse.code !== 0) {
66
+ // Handle API-level errors
67
+ const error = {
68
+ code: apiResponse.code,
69
+ message: apiResponse.message,
70
+ request_id: apiResponse.request_id,
71
+ };
72
+ throw error;
73
+ }
74
+ }
75
+ }
76
+ return response;
77
+ }, (error) => {
78
+ // Handle HTTP errors
79
+ if (error.response?.data) {
80
+ const apiError = error.response.data;
81
+ throw new Error(apiError.message || error.message);
82
+ }
83
+ throw error;
84
+ });
85
+ }
86
+ /**
87
+ * Generate Firestore token for authenticated user
88
+ *
89
+ * This method generates a Firestore custom token for an authenticated user.
90
+ * The user must provide a valid JWT token.
91
+ *
92
+ * @param request - The request containing user token and app ID
93
+ * @param options - Additional axios request configuration
94
+ * @returns Firestore token response with ID token and metadata
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * const response = await client.generateFirestoreToken({
99
+ * token: 'user-jwt-token',
100
+ * app_id: 'my-app',
101
+ * });
102
+ *
103
+ * console.log('Firestore ID Token:', response.id_token);
104
+ * console.log('User ID:', response.user_id);
105
+ * console.log('Expires in:', response.expires_in, 'seconds');
106
+ * ```
107
+ */
108
+ async generateFirestoreToken(request, options) {
109
+ const config = {
110
+ method: 'POST',
111
+ url: ENDPOINTS.FIRESTORE_TOKEN,
112
+ data: request,
113
+ ...options,
114
+ };
115
+ const response = await this.axiosInstance.request(config);
116
+ return response.data;
117
+ }
118
+ /**
119
+ * Generate Firestore token for guest user
120
+ *
121
+ * This method generates a Firestore custom token for a guest (unauthenticated) user.
122
+ * No user authentication token is required.
123
+ *
124
+ * @param request - The request containing app ID
125
+ * @param options - Additional axios request configuration
126
+ * @returns Firestore token response with ID token and metadata
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * const response = await client.generateGuestFirestoreToken({
131
+ * app_id: 'my-app',
132
+ * });
133
+ *
134
+ * console.log('Guest Firestore ID Token:', response.id_token);
135
+ * console.log('Guest User ID:', response.user_id);
136
+ * console.log('Role:', response.role); // 'guest'
137
+ * ```
138
+ */
139
+ async generateGuestFirestoreToken(request, options) {
140
+ const config = {
141
+ method: 'POST',
142
+ url: ENDPOINTS.FIRESTORE_GUEST_TOKEN,
143
+ data: request,
144
+ ...options,
145
+ };
146
+ const response = await this.axiosInstance.request(config);
147
+ return response.data;
148
+ }
149
+ }
150
+
151
+ export { DEFAULT_BASE_URL, DEFAULT_TIMEOUT, DataServiceClient, ENDPOINTS };
152
+ //#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AO,QAAQ,CAAC,IAAI;IACtB;AACD;;;;"}
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@seaverse/data-service-sdk",
3
+ "version": "0.1.0",
4
+ "description": "SDK for SeaVerse Data Service - Firestore token management",
5
+ "type": "module",
6
+ "main": "dist/index.cjs",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
20
+ "scripts": {
21
+ "build": "rollup -c",
22
+ "build:tsc": "tsc",
23
+ "dev": "ROLLUP_WATCH=true rollup -c -w",
24
+ "clean": "rm -rf dist",
25
+ "test": "vitest",
26
+ "prepublishOnly": "npm run clean && npm run build"
27
+ },
28
+ "keywords": [
29
+ "seaverse",
30
+ "sdk",
31
+ "firestore",
32
+ "data-service",
33
+ "token"
34
+ ],
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/seaverseai/sv-sdk.git",
38
+ "directory": "packages/data-service-sdk"
39
+ },
40
+ "author": "SeaVerse Team <support@seaverse.com>",
41
+ "license": "MIT",
42
+ "dependencies": {
43
+ "axios": "^1.6.2"
44
+ },
45
+ "devDependencies": {
46
+ "@rollup/plugin-commonjs": "^29.0.0",
47
+ "@rollup/plugin-node-resolve": "^16.0.3",
48
+ "@rollup/plugin-typescript": "^12.3.0",
49
+ "@types/node": "^20.10.0",
50
+ "rollup": "^4.54.0",
51
+ "rollup-plugin-dts": "^6.3.0",
52
+ "tslib": "^2.8.1",
53
+ "typescript": "^5.3.3",
54
+ "vitest": "^1.0.4"
55
+ },
56
+ "publishConfig": {
57
+ "access": "public"
58
+ }
59
+ }