@nxcode/sdk 1.0.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/dist/nxcode.js ADDED
@@ -0,0 +1,646 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Nxcode = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ /**
8
+ * Nxcode SDK - Authentication Module
9
+ */
10
+ const STORAGE_KEY = 'nxcode_sdk_auth';
11
+ class NxcodeAuth {
12
+ constructor(apiEndpoint, appId) {
13
+ this.user = null;
14
+ this.token = null;
15
+ this.refreshToken = null;
16
+ this.expiresAt = null;
17
+ this.authCallbacks = [];
18
+ this.popupWindow = null;
19
+ this.messageHandler = null;
20
+ this.apiEndpoint = apiEndpoint;
21
+ this.appId = appId;
22
+ this.loadFromStorage();
23
+ this.setupMessageListener();
24
+ }
25
+ /**
26
+ * Initiate login flow via OAuth popup
27
+ */
28
+ async login(provider = 'google') {
29
+ return new Promise((resolve, reject) => {
30
+ // Open popup
31
+ const width = 500;
32
+ const height = 600;
33
+ const left = window.screenX + (window.outerWidth - width) / 2;
34
+ const top = window.screenY + (window.outerHeight - height) / 2;
35
+ const loginUrl = `${this.apiEndpoint}/api/sdk/auth/login?app_id=${this.appId}&provider=${provider}`;
36
+ this.popupWindow = window.open(loginUrl, 'nxcode_login', `width=${width},height=${height},left=${left},top=${top}`);
37
+ if (!this.popupWindow) {
38
+ reject(new Error('Failed to open login popup. Please allow popups for this site.'));
39
+ return;
40
+ }
41
+ // Set up timeout
42
+ const timeout = setTimeout(() => {
43
+ this.popupWindow?.close();
44
+ reject(new Error('Login timeout'));
45
+ }, 120000); // 2 minute timeout
46
+ // Wait for message from popup
47
+ const handleMessage = (event) => {
48
+ if (event.data?.type !== 'NXCODE_SDK_AUTH')
49
+ return;
50
+ clearTimeout(timeout);
51
+ window.removeEventListener('message', handleMessage);
52
+ const result = event.data.payload;
53
+ if (result.success && result.user && result.token) {
54
+ this.setAuth(result);
55
+ resolve(result.user);
56
+ }
57
+ else {
58
+ reject(new Error(result.error || 'Login failed'));
59
+ }
60
+ };
61
+ window.addEventListener('message', handleMessage);
62
+ });
63
+ }
64
+ /**
65
+ * Logout and clear session
66
+ */
67
+ async logout() {
68
+ try {
69
+ if (this.token) {
70
+ await fetch(`${this.apiEndpoint}/api/sdk/auth/logout`, {
71
+ method: 'POST',
72
+ headers: this.getHeaders(),
73
+ });
74
+ }
75
+ }
76
+ catch (error) {
77
+ console.warn('Logout request failed:', error);
78
+ }
79
+ this.clearAuth();
80
+ }
81
+ /**
82
+ * Get current user
83
+ */
84
+ getUser() {
85
+ return this.user;
86
+ }
87
+ /**
88
+ * Get current auth token
89
+ */
90
+ getToken() {
91
+ // Check if token is expired
92
+ if (this.expiresAt && new Date() > this.expiresAt) {
93
+ // Token is expired, need to refresh
94
+ // Start refresh in background but don't return the expired token
95
+ this.refreshSession().catch(console.error);
96
+ // Return null to force re-auth rather than using expired token
97
+ return null;
98
+ }
99
+ return this.token;
100
+ }
101
+ /**
102
+ * Get token, refreshing if needed (async version)
103
+ */
104
+ async getValidToken() {
105
+ if (this.expiresAt && new Date() > this.expiresAt) {
106
+ await this.refreshSession();
107
+ }
108
+ return this.token;
109
+ }
110
+ /**
111
+ * Register callback for auth state changes
112
+ */
113
+ onAuthStateChange(callback) {
114
+ this.authCallbacks.push(callback);
115
+ // Call immediately with current state
116
+ callback(this.user);
117
+ // Return unsubscribe function
118
+ return () => {
119
+ const index = this.authCallbacks.indexOf(callback);
120
+ if (index > -1) {
121
+ this.authCallbacks.splice(index, 1);
122
+ }
123
+ };
124
+ }
125
+ /**
126
+ * Check if user is logged in
127
+ */
128
+ isLoggedIn() {
129
+ return this.user !== null && this.token !== null;
130
+ }
131
+ /**
132
+ * Refresh the current session
133
+ */
134
+ async refreshSession() {
135
+ if (!this.refreshToken) {
136
+ this.clearAuth();
137
+ return;
138
+ }
139
+ try {
140
+ const response = await fetch(`${this.apiEndpoint}/api/sdk/auth/refresh`, {
141
+ method: 'POST',
142
+ headers: {
143
+ 'Content-Type': 'application/json',
144
+ 'X-App-Id': this.appId,
145
+ },
146
+ body: JSON.stringify({ refreshToken: this.refreshToken }),
147
+ });
148
+ if (!response.ok) {
149
+ throw new Error('Refresh failed');
150
+ }
151
+ const data = await response.json();
152
+ this.setAuth({
153
+ success: true,
154
+ token: data.token,
155
+ refreshToken: data.refreshToken,
156
+ expiresAt: data.expiresAt,
157
+ user: data.user,
158
+ });
159
+ }
160
+ catch (error) {
161
+ console.error('Session refresh failed:', error);
162
+ this.clearAuth();
163
+ }
164
+ }
165
+ /**
166
+ * Fetch current user info from server
167
+ */
168
+ async fetchUser() {
169
+ if (!this.token)
170
+ return null;
171
+ try {
172
+ const response = await fetch(`${this.apiEndpoint}/api/sdk/auth/me`, {
173
+ headers: this.getHeaders(),
174
+ });
175
+ if (!response.ok) {
176
+ if (response.status === 401) {
177
+ await this.refreshSession();
178
+ return this.user;
179
+ }
180
+ throw new Error('Failed to fetch user');
181
+ }
182
+ const user = await response.json();
183
+ this.user = user;
184
+ this.notifyAuthChange();
185
+ this.saveToStorage();
186
+ return user;
187
+ }
188
+ catch (error) {
189
+ console.error('Failed to fetch user:', error);
190
+ return null;
191
+ }
192
+ }
193
+ // ==================== Private Methods ====================
194
+ setAuth(result) {
195
+ if (result.success && result.user && result.token) {
196
+ this.user = result.user;
197
+ this.token = result.token;
198
+ this.refreshToken = result.refreshToken || null;
199
+ this.expiresAt = result.expiresAt ? new Date(result.expiresAt) : null;
200
+ this.saveToStorage();
201
+ this.notifyAuthChange();
202
+ }
203
+ }
204
+ clearAuth() {
205
+ this.user = null;
206
+ this.token = null;
207
+ this.refreshToken = null;
208
+ this.expiresAt = null;
209
+ localStorage.removeItem(`${STORAGE_KEY}_${this.appId}`);
210
+ this.notifyAuthChange();
211
+ }
212
+ saveToStorage() {
213
+ if (this.user && this.token) {
214
+ const data = {
215
+ token: this.token,
216
+ refreshToken: this.refreshToken || '',
217
+ expiresAt: this.expiresAt?.toISOString() || '',
218
+ user: this.user,
219
+ };
220
+ localStorage.setItem(`${STORAGE_KEY}_${this.appId}`, JSON.stringify(data));
221
+ }
222
+ }
223
+ loadFromStorage() {
224
+ try {
225
+ const stored = localStorage.getItem(`${STORAGE_KEY}_${this.appId}`);
226
+ if (stored) {
227
+ const data = JSON.parse(stored);
228
+ this.token = data.token;
229
+ this.refreshToken = data.refreshToken;
230
+ this.expiresAt = data.expiresAt ? new Date(data.expiresAt) : null;
231
+ this.user = data.user;
232
+ // Check if expired
233
+ if (this.expiresAt && new Date() > this.expiresAt) {
234
+ this.refreshSession().catch(() => this.clearAuth());
235
+ }
236
+ }
237
+ }
238
+ catch (error) {
239
+ console.warn('Failed to load auth from storage:', error);
240
+ }
241
+ }
242
+ notifyAuthChange() {
243
+ for (const callback of this.authCallbacks) {
244
+ try {
245
+ callback(this.user);
246
+ }
247
+ catch (error) {
248
+ console.error('Auth callback error:', error);
249
+ }
250
+ }
251
+ }
252
+ setupMessageListener() {
253
+ // Already set up message handling in login()
254
+ }
255
+ getHeaders() {
256
+ const headers = {
257
+ 'Content-Type': 'application/json',
258
+ 'X-App-Id': this.appId,
259
+ };
260
+ if (this.token) {
261
+ headers['Authorization'] = `Bearer ${this.token}`;
262
+ }
263
+ return headers;
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Nxcode SDK - Billing Module
269
+ */
270
+ class NxcodeBilling {
271
+ constructor(apiEndpoint, appId, getToken) {
272
+ this.apiEndpoint = apiEndpoint;
273
+ this.appId = appId;
274
+ this.getToken = getToken;
275
+ }
276
+ /**
277
+ * Get user's current balance
278
+ */
279
+ async getBalance() {
280
+ const token = this.getToken();
281
+ if (!token) {
282
+ throw new Error('Not authenticated. Please login first.');
283
+ }
284
+ const response = await fetch(`${this.apiEndpoint}/api/sdk/billing/balance`, {
285
+ headers: this.getHeaders(token),
286
+ });
287
+ if (!response.ok) {
288
+ if (response.status === 401) {
289
+ throw new Error('Session expired. Please login again.');
290
+ }
291
+ throw new Error('Failed to fetch balance');
292
+ }
293
+ const data = await response.json();
294
+ return data.balance;
295
+ }
296
+ /**
297
+ * Open top-up page in new tab
298
+ */
299
+ topUp() {
300
+ const topUpUrl = `${this.apiEndpoint}/api/sdk/billing/topup?app_id=${this.appId}`;
301
+ window.open(topUpUrl, '_blank');
302
+ }
303
+ // ==================== Private Methods ====================
304
+ getHeaders(token) {
305
+ return {
306
+ 'Content-Type': 'application/json',
307
+ 'X-App-Id': this.appId,
308
+ 'Authorization': `Bearer ${token}`,
309
+ };
310
+ }
311
+ }
312
+
313
+ /**
314
+ * Nxcode SDK - Payment Module
315
+ */
316
+ class NxcodePayment {
317
+ constructor(apiEndpoint, appId, getToken) {
318
+ this.apiEndpoint = apiEndpoint;
319
+ this.appId = appId;
320
+ this.getToken = getToken;
321
+ }
322
+ /**
323
+ * Process an in-app purchase
324
+ *
325
+ * @param options - Charge options
326
+ * @returns Charge result with transaction details
327
+ */
328
+ async charge(options) {
329
+ const token = this.getToken();
330
+ if (!token) {
331
+ return {
332
+ success: false,
333
+ error: 'Not authenticated. Please login first.',
334
+ };
335
+ }
336
+ try {
337
+ const response = await fetch(`${this.apiEndpoint}/api/sdk/payment/charge`, {
338
+ method: 'POST',
339
+ headers: this.getHeaders(token),
340
+ body: JSON.stringify({
341
+ amount: options.amount,
342
+ description: options.description,
343
+ metadata: options.metadata,
344
+ }),
345
+ });
346
+ if (!response.ok) {
347
+ if (response.status === 401) {
348
+ return {
349
+ success: false,
350
+ error: 'Session expired. Please login again.',
351
+ };
352
+ }
353
+ if (response.status === 402) {
354
+ return {
355
+ success: false,
356
+ error: 'Insufficient balance. Please top up.',
357
+ };
358
+ }
359
+ const errorData = await response.json().catch(() => ({}));
360
+ return {
361
+ success: false,
362
+ error: errorData.detail || 'Payment failed',
363
+ };
364
+ }
365
+ const result = await response.json();
366
+ return result;
367
+ }
368
+ catch (error) {
369
+ return {
370
+ success: false,
371
+ error: error instanceof Error ? error.message : 'Payment failed',
372
+ };
373
+ }
374
+ }
375
+ /**
376
+ * Get user's transaction history
377
+ *
378
+ * @param limit - Maximum number of transactions to return
379
+ * @param offset - Pagination offset
380
+ * @returns Array of transactions
381
+ */
382
+ async getTransactions(limit = 50, offset = 0) {
383
+ const token = this.getToken();
384
+ if (!token) {
385
+ throw new Error('Not authenticated. Please login first.');
386
+ }
387
+ const response = await fetch(`${this.apiEndpoint}/api/sdk/payment/transactions?limit=${limit}&offset=${offset}`, {
388
+ headers: this.getHeaders(token),
389
+ });
390
+ if (!response.ok) {
391
+ if (response.status === 401) {
392
+ throw new Error('Session expired. Please login again.');
393
+ }
394
+ throw new Error('Failed to fetch transactions');
395
+ }
396
+ const data = await response.json();
397
+ return data.transactions;
398
+ }
399
+ // ==================== Private Methods ====================
400
+ getHeaders(token) {
401
+ return {
402
+ 'Content-Type': 'application/json',
403
+ 'X-App-Id': this.appId,
404
+ 'Authorization': `Bearer ${token}`,
405
+ };
406
+ }
407
+ }
408
+
409
+ /**
410
+ * Nxcode SDK
411
+ *
412
+ * A lightweight SDK for integrating Nxcode authentication, billing,
413
+ * and payment features into web applications.
414
+ *
415
+ * Usage:
416
+ * <script src="https://sdk.nxcode.io/v1/nxcode.js"></script>
417
+ * <script>
418
+ * const user = await Nxcode.auth.login();
419
+ * console.log('Logged in:', user.email);
420
+ * </script>
421
+ */
422
+ // API endpoint - auto-detect or use default
423
+ const DEFAULT_API_ENDPOINT = 'https://studio-api.nxcode.io';
424
+ class NxcodeSDK {
425
+ constructor() {
426
+ this.config = null;
427
+ this._auth = null;
428
+ this._billing = null;
429
+ this._payment = null;
430
+ this.initPromise = null;
431
+ this.apiEndpoint = this.detectApiEndpoint();
432
+ this.autoInit();
433
+ }
434
+ /**
435
+ * Auto-initialize SDK based on environment
436
+ */
437
+ async autoInit() {
438
+ if (this.initPromise)
439
+ return this.initPromise;
440
+ this.initPromise = this.init();
441
+ return this.initPromise;
442
+ }
443
+ /**
444
+ * Initialize SDK by fetching configuration
445
+ */
446
+ async init() {
447
+ try {
448
+ // Try to get config from server
449
+ const response = await fetch(`${this.apiEndpoint}/api/sdk/config`, {
450
+ credentials: 'include',
451
+ });
452
+ if (response.ok) {
453
+ this.config = await response.json();
454
+ this.config.apiEndpoint = this.apiEndpoint;
455
+ this.setupModules();
456
+ }
457
+ }
458
+ catch (error) {
459
+ console.warn('Nxcode SDK: Could not auto-detect app config. Call Nxcode.configure() manually.');
460
+ }
461
+ }
462
+ /**
463
+ * Manually configure SDK with app ID
464
+ */
465
+ configure(appId) {
466
+ this.config = {
467
+ appId,
468
+ name: '',
469
+ billingMode: 'creator_pays',
470
+ apiEndpoint: this.apiEndpoint,
471
+ };
472
+ this.setupModules();
473
+ }
474
+ /**
475
+ * Set up SDK modules after configuration
476
+ */
477
+ setupModules() {
478
+ if (!this.config)
479
+ return;
480
+ this._auth = new NxcodeAuth(this.config.apiEndpoint, this.config.appId);
481
+ this._billing = new NxcodeBilling(this.config.apiEndpoint, this.config.appId, () => this._auth?.getToken() || null);
482
+ this._payment = new NxcodePayment(this.config.apiEndpoint, this.config.appId, () => this._auth?.getToken() || null);
483
+ }
484
+ /**
485
+ * Detect API endpoint based on environment
486
+ */
487
+ detectApiEndpoint() {
488
+ if (typeof window !== 'undefined') {
489
+ const hostname = window.location.hostname;
490
+ // Development environment
491
+ if (hostname === 'localhost' || hostname === '127.0.0.1') {
492
+ return 'http://localhost:8001';
493
+ }
494
+ // Check for dev environment
495
+ if (hostname.includes('.nxcode.dev') || hostname.includes('.nxcode.app')) {
496
+ return 'https://studio-api.nxcode.io';
497
+ }
498
+ }
499
+ return DEFAULT_API_ENDPOINT;
500
+ }
501
+ /**
502
+ * Ensure SDK is initialized before operations
503
+ */
504
+ async ensureInitialized() {
505
+ if (this.initPromise) {
506
+ await this.initPromise;
507
+ }
508
+ if (!this.config) {
509
+ throw new Error('Nxcode SDK not configured. Add X-App-Id header or call Nxcode.configure(appId).');
510
+ }
511
+ }
512
+ // ==================== Auth Module ====================
513
+ get auth() {
514
+ const self = this;
515
+ return {
516
+ /**
517
+ * Login via OAuth popup
518
+ */
519
+ async login(provider = 'google') {
520
+ await self.ensureInitialized();
521
+ return self._auth.login(provider);
522
+ },
523
+ /**
524
+ * Logout current user
525
+ */
526
+ async logout() {
527
+ await self.ensureInitialized();
528
+ return self._auth.logout();
529
+ },
530
+ /**
531
+ * Get current user
532
+ */
533
+ getUser() {
534
+ return self._auth?.getUser() || null;
535
+ },
536
+ /**
537
+ * Get auth token for API calls
538
+ */
539
+ getToken() {
540
+ return self._auth?.getToken() || null;
541
+ },
542
+ /**
543
+ * Register auth state change callback
544
+ */
545
+ onAuthStateChange(callback) {
546
+ if (!self._auth) {
547
+ // Store the actual unsubscribe function when SDK initializes
548
+ let actualUnsubscribe = null;
549
+ let cancelled = false;
550
+ // Queue callback until initialized
551
+ self.ensureInitialized().then(() => {
552
+ if (!cancelled && self._auth) {
553
+ actualUnsubscribe = self._auth.onAuthStateChange(callback);
554
+ }
555
+ });
556
+ // Return unsubscribe that handles both immediate and deferred cases
557
+ return () => {
558
+ cancelled = true;
559
+ if (actualUnsubscribe) {
560
+ actualUnsubscribe();
561
+ }
562
+ };
563
+ }
564
+ return self._auth.onAuthStateChange(callback);
565
+ },
566
+ /**
567
+ * Check if user is logged in
568
+ */
569
+ isLoggedIn() {
570
+ return self._auth?.isLoggedIn() || false;
571
+ },
572
+ };
573
+ }
574
+ // ==================== Billing Module ====================
575
+ get billing() {
576
+ const self = this;
577
+ return {
578
+ /**
579
+ * Get user's current balance
580
+ */
581
+ async getBalance() {
582
+ await self.ensureInitialized();
583
+ return self._billing.getBalance();
584
+ },
585
+ /**
586
+ * Open top-up page
587
+ */
588
+ topUp() {
589
+ self._billing?.topUp();
590
+ },
591
+ };
592
+ }
593
+ // ==================== Payment Module ====================
594
+ get payment() {
595
+ const self = this;
596
+ return {
597
+ /**
598
+ * Process in-app purchase
599
+ */
600
+ async charge(options) {
601
+ await self.ensureInitialized();
602
+ return self._payment.charge(options);
603
+ },
604
+ /**
605
+ * Get transaction history
606
+ */
607
+ async getTransactions(limit, offset) {
608
+ await self.ensureInitialized();
609
+ return self._payment.getTransactions(limit, offset);
610
+ },
611
+ };
612
+ }
613
+ // ==================== Utility Methods ====================
614
+ /**
615
+ * Get current configuration
616
+ */
617
+ getConfig() {
618
+ return this.config;
619
+ }
620
+ /**
621
+ * Check if SDK is ready
622
+ */
623
+ isReady() {
624
+ return this.config !== null && this._auth !== null;
625
+ }
626
+ /**
627
+ * Wait for SDK to be ready
628
+ */
629
+ async ready() {
630
+ await this.ensureInitialized();
631
+ }
632
+ }
633
+ // Create singleton instance
634
+ const Nxcode = new NxcodeSDK();
635
+ // Export for UMD (browser global)
636
+ if (typeof window !== 'undefined') {
637
+ window.Nxcode = Nxcode;
638
+ }
639
+
640
+ exports.Nxcode = Nxcode;
641
+ exports.default = Nxcode;
642
+
643
+ Object.defineProperty(exports, '__esModule', { value: true });
644
+
645
+ }));
646
+ //# sourceMappingURL=nxcode.js.map