@23blocks/react 1.1.1 → 1.1.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.
Files changed (39) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.esm.js +1306 -0
  3. package/dist/src/index.d.ts +2 -0
  4. package/dist/src/index.d.ts.map +1 -0
  5. package/dist/src/lib/context.d.ts +114 -0
  6. package/dist/src/lib/context.d.ts.map +1 -0
  7. package/dist/src/lib/hooks/index.d.ts +5 -0
  8. package/dist/src/lib/hooks/index.d.ts.map +1 -0
  9. package/dist/src/lib/hooks/use-auth.d.ts +52 -0
  10. package/dist/src/lib/hooks/use-auth.d.ts.map +1 -0
  11. package/dist/src/lib/hooks/use-favorites.d.ts +41 -0
  12. package/dist/src/lib/hooks/use-favorites.d.ts.map +1 -0
  13. package/dist/src/lib/hooks/use-search.d.ts +55 -0
  14. package/dist/src/lib/hooks/use-search.d.ts.map +1 -0
  15. package/dist/src/lib/hooks/use-users.d.ts +44 -0
  16. package/dist/src/lib/hooks/use-users.d.ts.map +1 -0
  17. package/dist/src/lib/index.d.ts +4 -0
  18. package/dist/src/lib/index.d.ts.map +1 -0
  19. package/dist/src/lib/simple-provider.d.ts +281 -0
  20. package/dist/src/lib/simple-provider.d.ts.map +1 -0
  21. package/package.json +10 -8
  22. package/dist/index.js +0 -3
  23. package/dist/index.js.map +0 -1
  24. package/dist/lib/context.js +0 -206
  25. package/dist/lib/context.js.map +0 -1
  26. package/dist/lib/hooks/index.js +0 -8
  27. package/dist/lib/hooks/index.js.map +0 -1
  28. package/dist/lib/hooks/use-auth.js +0 -159
  29. package/dist/lib/hooks/use-auth.js.map +0 -1
  30. package/dist/lib/hooks/use-favorites.js +0 -114
  31. package/dist/lib/hooks/use-favorites.js.map +0 -1
  32. package/dist/lib/hooks/use-search.js +0 -120
  33. package/dist/lib/hooks/use-search.js.map +0 -1
  34. package/dist/lib/hooks/use-users.js +0 -115
  35. package/dist/lib/hooks/use-users.js.map +0 -1
  36. package/dist/lib/index.js +0 -17
  37. package/dist/lib/index.js.map +0 -1
  38. package/dist/lib/simple-provider.js +0 -620
  39. package/dist/lib/simple-provider.js.map +0 -1
@@ -1,620 +0,0 @@
1
- import { _ as _extends } from "@swc/helpers/_/_extends";
2
- import { createContext, useContext, useMemo, useCallback } from 'react';
3
- import { createHttpTransport } from '@23blocks/transport-http';
4
- import { createAuthenticationBlock } from '@23blocks/block-authentication';
5
- import { createSearchBlock } from '@23blocks/block-search';
6
- import { createProductsBlock } from '@23blocks/block-products';
7
- import { createCrmBlock } from '@23blocks/block-crm';
8
- import { createContentBlock } from '@23blocks/block-content';
9
- import { createGeolocationBlock } from '@23blocks/block-geolocation';
10
- import { createConversationsBlock } from '@23blocks/block-conversations';
11
- import { createFilesBlock } from '@23blocks/block-files';
12
- import { createFormsBlock } from '@23blocks/block-forms';
13
- import { createAssetsBlock } from '@23blocks/block-assets';
14
- import { createCampaignsBlock } from '@23blocks/block-campaigns';
15
- import { createCompanyBlock } from '@23blocks/block-company';
16
- import { createRewardsBlock } from '@23blocks/block-rewards';
17
- import { createSalesBlock } from '@23blocks/block-sales';
18
- import { createWalletBlock } from '@23blocks/block-wallet';
19
- import { createJarvisBlock } from '@23blocks/block-jarvis';
20
- import { createOnboardingBlock } from '@23blocks/block-onboarding';
21
- import { createUniversityBlock } from '@23blocks/block-university';
22
- // ─────────────────────────────────────────────────────────────────────────────
23
- // Token Manager Implementation
24
- // ─────────────────────────────────────────────────────────────────────────────
25
- /**
26
- * Generate storage key scoped to app and tenant
27
- */ function getStorageKey(type, appId, tenantId) {
28
- const scope = tenantId ? `${appId}_${tenantId}` : appId;
29
- return `23blocks_${scope}_${type}_token`;
30
- }
31
- let MemoryStorage = class MemoryStorage {
32
- getItem(key) {
33
- var _this_data_get;
34
- return (_this_data_get = this.data.get(key)) != null ? _this_data_get : null;
35
- }
36
- setItem(key, value) {
37
- this.data.set(key, value);
38
- }
39
- removeItem(key) {
40
- this.data.delete(key);
41
- }
42
- constructor(){
43
- this.data = new Map();
44
- }
45
- };
46
- function createTokenManager(appId, storageType, tenantId) {
47
- const isBrowser = typeof window !== 'undefined' && typeof window.localStorage !== 'undefined';
48
- const accessTokenKey = getStorageKey('access', appId, tenantId);
49
- const refreshTokenKey = getStorageKey('refresh', appId, tenantId);
50
- let storage;
51
- if (!isBrowser) {
52
- storage = new MemoryStorage();
53
- } else {
54
- switch(storageType){
55
- case 'sessionStorage':
56
- storage = window.sessionStorage;
57
- break;
58
- case 'memory':
59
- storage = new MemoryStorage();
60
- break;
61
- default:
62
- storage = window.localStorage;
63
- }
64
- }
65
- return {
66
- getAccessToken () {
67
- try {
68
- return storage.getItem(accessTokenKey);
69
- } catch (e) {
70
- return null;
71
- }
72
- },
73
- getRefreshToken () {
74
- try {
75
- return storage.getItem(refreshTokenKey);
76
- } catch (e) {
77
- return null;
78
- }
79
- },
80
- setTokens (accessToken, refreshToken) {
81
- try {
82
- storage.setItem(accessTokenKey, accessToken);
83
- if (refreshToken) {
84
- storage.setItem(refreshTokenKey, refreshToken);
85
- }
86
- } catch (e) {
87
- console.warn('[23blocks] Unable to store tokens');
88
- }
89
- },
90
- clearTokens () {
91
- try {
92
- storage.removeItem(accessTokenKey);
93
- storage.removeItem(refreshTokenKey);
94
- } catch (e) {
95
- // Silently fail
96
- }
97
- },
98
- onStorageChange (callback) {
99
- // Only works in browser with localStorage/sessionStorage
100
- if (!isBrowser || storageType === 'memory') {
101
- // Return no-op unsubscribe for SSR/memory storage
102
- return ()=>{};
103
- }
104
- const handler = (event)=>{
105
- // Only trigger if our keys changed
106
- if (event.key === accessTokenKey || event.key === refreshTokenKey) {
107
- callback();
108
- }
109
- };
110
- window.addEventListener('storage', handler);
111
- // Return unsubscribe function
112
- return ()=>{
113
- window.removeEventListener('storage', handler);
114
- };
115
- }
116
- };
117
- }
118
- // ─────────────────────────────────────────────────────────────────────────────
119
- // Context
120
- // ─────────────────────────────────────────────────────────────────────────────
121
- const Blocks23Context = /*#__PURE__*/ createContext(null);
122
- // ─────────────────────────────────────────────────────────────────────────────
123
- // Provider Component
124
- // ─────────────────────────────────────────────────────────────────────────────
125
- /**
126
- * Helper to create a proxy that throws when accessing unconfigured service
127
- */ function createUnconfiguredServiceProxy(serviceName, urlKey) {
128
- return new Proxy({}, {
129
- get (_target, prop) {
130
- throw new Error(`[23blocks] Cannot access '${serviceName}.${String(prop)}': ` + `The ${serviceName} service URL is not configured. ` + `Add 'urls.${urlKey}' to your Provider configuration.`);
131
- }
132
- });
133
- }
134
- /**
135
- * Provider component for 23blocks services.
136
- *
137
- * Wrap your app with this provider to access all 23blocks services
138
- * with automatic token management.
139
- *
140
- * Services are only available if their URL is configured. Accessing
141
- * a service without a configured URL will throw an error.
142
- *
143
- * @example Basic usage with multiple services
144
- * ```tsx
145
- * import { Provider } from '@23blocks/react';
146
- *
147
- * function App() {
148
- * return (
149
- * <Provider
150
- * appId="your-app-id"
151
- * urls={{
152
- * authentication: 'https://gateway.23blocks.com',
153
- * crm: 'https://crm.23blocks.com',
154
- * products: 'https://products.23blocks.com',
155
- * }}
156
- * >
157
- * <MyApp />
158
- * </Provider>
159
- * );
160
- * }
161
- * ```
162
- *
163
- * @example Cookie mode (recommended for security)
164
- * ```tsx
165
- * <Provider
166
- * appId="your-app-id"
167
- * authMode="cookie"
168
- * urls={{
169
- * authentication: 'https://gateway.23blocks.com',
170
- * crm: 'https://crm.23blocks.com',
171
- * }}
172
- * >
173
- * <MyApp />
174
- * </Provider>
175
- * ```
176
- */ export function Provider({ children, urls, appId, tenantId, authMode = 'token', storage = 'localStorage', headers: staticHeaders = {}, timeout }) {
177
- // Create token manager (memoized) with scoped storage keys
178
- const tokenManager = useMemo(()=>authMode === 'token' ? createTokenManager(appId, storage, tenantId) : null, [
179
- authMode,
180
- appId,
181
- storage,
182
- tenantId
183
- ]);
184
- // Factory to create transport for a specific service URL
185
- const createServiceTransport = useCallback((baseUrl)=>{
186
- return createHttpTransport({
187
- baseUrl,
188
- timeout,
189
- credentials: authMode === 'cookie' ? 'include' : undefined,
190
- headers: ()=>{
191
- const headers = _extends({}, staticHeaders, {
192
- appid: appId
193
- });
194
- if (tenantId) {
195
- headers['tenant-id'] = tenantId;
196
- }
197
- if (authMode === 'token' && tokenManager) {
198
- const token = tokenManager.getAccessToken();
199
- if (token) {
200
- headers['Authorization'] = `Bearer ${token}`;
201
- }
202
- }
203
- return headers;
204
- }
205
- });
206
- }, [
207
- appId,
208
- tenantId,
209
- authMode,
210
- staticHeaders,
211
- timeout,
212
- tokenManager
213
- ]);
214
- // Create blocks (memoized) - each with its own transport (no fallback)
215
- const blockConfig = useMemo(()=>({
216
- appId,
217
- tenantId
218
- }), [
219
- appId,
220
- tenantId
221
- ]);
222
- // Create blocks only if URL is configured, otherwise use proxy that throws helpful error
223
- const authentication = useMemo(()=>{
224
- if (!urls.authentication) {
225
- return createUnconfiguredServiceProxy('authentication', 'authentication');
226
- }
227
- return createAuthenticationBlock(createServiceTransport(urls.authentication), blockConfig);
228
- }, [
229
- createServiceTransport,
230
- urls.authentication,
231
- blockConfig
232
- ]);
233
- const search = useMemo(()=>{
234
- if (!urls.search) {
235
- return createUnconfiguredServiceProxy('search', 'search');
236
- }
237
- return createSearchBlock(createServiceTransport(urls.search), blockConfig);
238
- }, [
239
- createServiceTransport,
240
- urls.search,
241
- blockConfig
242
- ]);
243
- const products = useMemo(()=>{
244
- if (!urls.products) {
245
- return createUnconfiguredServiceProxy('products', 'products');
246
- }
247
- return createProductsBlock(createServiceTransport(urls.products), blockConfig);
248
- }, [
249
- createServiceTransport,
250
- urls.products,
251
- blockConfig
252
- ]);
253
- const crm = useMemo(()=>{
254
- if (!urls.crm) {
255
- return createUnconfiguredServiceProxy('crm', 'crm');
256
- }
257
- return createCrmBlock(createServiceTransport(urls.crm), blockConfig);
258
- }, [
259
- createServiceTransport,
260
- urls.crm,
261
- blockConfig
262
- ]);
263
- const content = useMemo(()=>{
264
- if (!urls.content) {
265
- return createUnconfiguredServiceProxy('content', 'content');
266
- }
267
- return createContentBlock(createServiceTransport(urls.content), blockConfig);
268
- }, [
269
- createServiceTransport,
270
- urls.content,
271
- blockConfig
272
- ]);
273
- const geolocation = useMemo(()=>{
274
- if (!urls.geolocation) {
275
- return createUnconfiguredServiceProxy('geolocation', 'geolocation');
276
- }
277
- return createGeolocationBlock(createServiceTransport(urls.geolocation), blockConfig);
278
- }, [
279
- createServiceTransport,
280
- urls.geolocation,
281
- blockConfig
282
- ]);
283
- const conversations = useMemo(()=>{
284
- if (!urls.conversations) {
285
- return createUnconfiguredServiceProxy('conversations', 'conversations');
286
- }
287
- return createConversationsBlock(createServiceTransport(urls.conversations), blockConfig);
288
- }, [
289
- createServiceTransport,
290
- urls.conversations,
291
- blockConfig
292
- ]);
293
- const files = useMemo(()=>{
294
- if (!urls.files) {
295
- return createUnconfiguredServiceProxy('files', 'files');
296
- }
297
- return createFilesBlock(createServiceTransport(urls.files), blockConfig);
298
- }, [
299
- createServiceTransport,
300
- urls.files,
301
- blockConfig
302
- ]);
303
- const forms = useMemo(()=>{
304
- if (!urls.forms) {
305
- return createUnconfiguredServiceProxy('forms', 'forms');
306
- }
307
- return createFormsBlock(createServiceTransport(urls.forms), blockConfig);
308
- }, [
309
- createServiceTransport,
310
- urls.forms,
311
- blockConfig
312
- ]);
313
- const assets = useMemo(()=>{
314
- if (!urls.assets) {
315
- return createUnconfiguredServiceProxy('assets', 'assets');
316
- }
317
- return createAssetsBlock(createServiceTransport(urls.assets), blockConfig);
318
- }, [
319
- createServiceTransport,
320
- urls.assets,
321
- blockConfig
322
- ]);
323
- const campaigns = useMemo(()=>{
324
- if (!urls.campaigns) {
325
- return createUnconfiguredServiceProxy('campaigns', 'campaigns');
326
- }
327
- return createCampaignsBlock(createServiceTransport(urls.campaigns), blockConfig);
328
- }, [
329
- createServiceTransport,
330
- urls.campaigns,
331
- blockConfig
332
- ]);
333
- const company = useMemo(()=>{
334
- if (!urls.company) {
335
- return createUnconfiguredServiceProxy('company', 'company');
336
- }
337
- return createCompanyBlock(createServiceTransport(urls.company), blockConfig);
338
- }, [
339
- createServiceTransport,
340
- urls.company,
341
- blockConfig
342
- ]);
343
- const rewards = useMemo(()=>{
344
- if (!urls.rewards) {
345
- return createUnconfiguredServiceProxy('rewards', 'rewards');
346
- }
347
- return createRewardsBlock(createServiceTransport(urls.rewards), blockConfig);
348
- }, [
349
- createServiceTransport,
350
- urls.rewards,
351
- blockConfig
352
- ]);
353
- const sales = useMemo(()=>{
354
- if (!urls.sales) {
355
- return createUnconfiguredServiceProxy('sales', 'sales');
356
- }
357
- return createSalesBlock(createServiceTransport(urls.sales), blockConfig);
358
- }, [
359
- createServiceTransport,
360
- urls.sales,
361
- blockConfig
362
- ]);
363
- const wallet = useMemo(()=>{
364
- if (!urls.wallet) {
365
- return createUnconfiguredServiceProxy('wallet', 'wallet');
366
- }
367
- return createWalletBlock(createServiceTransport(urls.wallet), blockConfig);
368
- }, [
369
- createServiceTransport,
370
- urls.wallet,
371
- blockConfig
372
- ]);
373
- const jarvis = useMemo(()=>{
374
- if (!urls.jarvis) {
375
- return createUnconfiguredServiceProxy('jarvis', 'jarvis');
376
- }
377
- return createJarvisBlock(createServiceTransport(urls.jarvis), blockConfig);
378
- }, [
379
- createServiceTransport,
380
- urls.jarvis,
381
- blockConfig
382
- ]);
383
- const onboarding = useMemo(()=>{
384
- if (!urls.onboarding) {
385
- return createUnconfiguredServiceProxy('onboarding', 'onboarding');
386
- }
387
- return createOnboardingBlock(createServiceTransport(urls.onboarding), blockConfig);
388
- }, [
389
- createServiceTransport,
390
- urls.onboarding,
391
- blockConfig
392
- ]);
393
- const university = useMemo(()=>{
394
- if (!urls.university) {
395
- return createUnconfiguredServiceProxy('university', 'university');
396
- }
397
- return createUniversityBlock(createServiceTransport(urls.university), blockConfig);
398
- }, [
399
- createServiceTransport,
400
- urls.university,
401
- blockConfig
402
- ]);
403
- // Check if authentication is configured for auth methods
404
- const isAuthConfigured = !!urls.authentication;
405
- // Auth methods with automatic token management
406
- const signIn = useCallback(async (request)=>{
407
- if (!isAuthConfigured) {
408
- throw new Error('[23blocks] Cannot call signIn: The authentication service URL is not configured. ' + "Add 'urls.authentication' to your Provider configuration.");
409
- }
410
- const response = await authentication.auth.signIn(request);
411
- if (authMode === 'token' && tokenManager && response.accessToken) {
412
- tokenManager.setTokens(response.accessToken, response.refreshToken);
413
- }
414
- return response;
415
- }, [
416
- authentication,
417
- authMode,
418
- tokenManager,
419
- isAuthConfigured
420
- ]);
421
- const signUp = useCallback(async (request)=>{
422
- if (!isAuthConfigured) {
423
- throw new Error('[23blocks] Cannot call signUp: The authentication service URL is not configured. ' + "Add 'urls.authentication' to your Provider configuration.");
424
- }
425
- const response = await authentication.auth.signUp(request);
426
- if (authMode === 'token' && tokenManager && response.accessToken) {
427
- tokenManager.setTokens(response.accessToken);
428
- }
429
- return response;
430
- }, [
431
- authentication,
432
- authMode,
433
- tokenManager,
434
- isAuthConfigured
435
- ]);
436
- const signOut = useCallback(async ()=>{
437
- if (!isAuthConfigured) {
438
- throw new Error('[23blocks] Cannot call signOut: The authentication service URL is not configured. ' + "Add 'urls.authentication' to your Provider configuration.");
439
- }
440
- await authentication.auth.signOut();
441
- if (authMode === 'token' && tokenManager) {
442
- tokenManager.clearTokens();
443
- }
444
- }, [
445
- authentication,
446
- authMode,
447
- tokenManager,
448
- isAuthConfigured
449
- ]);
450
- // Token utilities
451
- const getAccessToken = useCallback(()=>{
452
- var _tokenManager_getAccessToken;
453
- return (_tokenManager_getAccessToken = tokenManager == null ? void 0 : tokenManager.getAccessToken()) != null ? _tokenManager_getAccessToken : null;
454
- }, [
455
- tokenManager
456
- ]);
457
- const getRefreshToken = useCallback(()=>{
458
- var _tokenManager_getRefreshToken;
459
- return (_tokenManager_getRefreshToken = tokenManager == null ? void 0 : tokenManager.getRefreshToken()) != null ? _tokenManager_getRefreshToken : null;
460
- }, [
461
- tokenManager
462
- ]);
463
- const setTokens = useCallback((accessToken, refreshToken)=>{
464
- tokenManager == null ? void 0 : tokenManager.setTokens(accessToken, refreshToken);
465
- }, [
466
- tokenManager
467
- ]);
468
- const clearTokens = useCallback(()=>tokenManager == null ? void 0 : tokenManager.clearTokens(), [
469
- tokenManager
470
- ]);
471
- const isAuthenticated = useCallback(()=>{
472
- if (authMode === 'cookie') return null;
473
- return tokenManager ? !!tokenManager.getAccessToken() : false;
474
- }, [
475
- authMode,
476
- tokenManager
477
- ]);
478
- const onStorageChange = useCallback((callback)=>{
479
- var _tokenManager_onStorageChange;
480
- return (_tokenManager_onStorageChange = tokenManager == null ? void 0 : tokenManager.onStorageChange(callback)) != null ? _tokenManager_onStorageChange : ()=>{};
481
- }, [
482
- tokenManager
483
- ]);
484
- const value = useMemo(()=>({
485
- // Blocks
486
- authentication,
487
- search,
488
- products,
489
- crm,
490
- content,
491
- geolocation,
492
- conversations,
493
- files,
494
- forms,
495
- assets,
496
- campaigns,
497
- company,
498
- rewards,
499
- sales,
500
- wallet,
501
- jarvis,
502
- onboarding,
503
- university,
504
- // Auth with token management
505
- signIn,
506
- signUp,
507
- signOut,
508
- // Token utilities
509
- getAccessToken,
510
- getRefreshToken,
511
- setTokens,
512
- clearTokens,
513
- isAuthenticated,
514
- onStorageChange,
515
- // Config
516
- authMode
517
- }), [
518
- authentication,
519
- search,
520
- products,
521
- crm,
522
- content,
523
- geolocation,
524
- conversations,
525
- files,
526
- forms,
527
- assets,
528
- campaigns,
529
- company,
530
- rewards,
531
- sales,
532
- wallet,
533
- jarvis,
534
- onboarding,
535
- university,
536
- signIn,
537
- signUp,
538
- signOut,
539
- getAccessToken,
540
- getRefreshToken,
541
- setTokens,
542
- clearTokens,
543
- isAuthenticated,
544
- onStorageChange,
545
- authMode
546
- ]);
547
- return /*#__PURE__*/ React.createElement(Blocks23Context.Provider, {
548
- value: value
549
- }, children);
550
- }
551
- // ─────────────────────────────────────────────────────────────────────────────
552
- // Hooks
553
- // ─────────────────────────────────────────────────────────────────────────────
554
- /**
555
- * Hook to access all 23blocks services.
556
- *
557
- * @example
558
- * ```tsx
559
- * function Dashboard() {
560
- * const { products, crm, files } = useClient();
561
- *
562
- * // Access any service
563
- * const loadProducts = async () => {
564
- * const { data } = await products.products.list({ limit: 10 });
565
- * };
566
- * }
567
- * ```
568
- */ export function useClient() {
569
- const context = useContext(Blocks23Context);
570
- if (!context) {
571
- throw new Error('useClient must be used within a Provider');
572
- }
573
- return context;
574
- }
575
- /**
576
- * Hook for authentication operations with automatic token management.
577
- *
578
- * @example
579
- * ```tsx
580
- * function LoginPage() {
581
- * const { signIn, signOut, isAuthenticated } = useAuth();
582
- *
583
- * const handleLogin = async () => {
584
- * await signIn({ email: 'user@example.com', password: 'password' });
585
- * };
586
- *
587
- * return (
588
- * <div>
589
- * {isAuthenticated() ? (
590
- * <button onClick={signOut}>Sign Out</button>
591
- * ) : (
592
- * <button onClick={handleLogin}>Sign In</button>
593
- * )}
594
- * </div>
595
- * );
596
- * }
597
- * ```
598
- */ export function useAuth() {
599
- const context = useClient();
600
- return {
601
- signIn: context.signIn,
602
- signUp: context.signUp,
603
- signOut: context.signOut,
604
- getAccessToken: context.getAccessToken,
605
- getRefreshToken: context.getRefreshToken,
606
- setTokens: context.setTokens,
607
- clearTokens: context.clearTokens,
608
- isAuthenticated: context.isAuthenticated,
609
- onStorageChange: context.onStorageChange,
610
- authentication: context.authentication
611
- };
612
- }
613
- // ─────────────────────────────────────────────────────────────────────────────
614
- // Backward Compatibility Aliases (deprecated)
615
- // ─────────────────────────────────────────────────────────────────────────────
616
- /** @deprecated Use `Provider` instead */ export const SimpleBlocks23Provider = Provider;
617
- /** @deprecated Use `useClient` instead */ export const useSimpleBlocks23 = useClient;
618
- /** @deprecated Use `useAuth` instead */ export const useSimpleAuth = useAuth;
619
-
620
- //# sourceMappingURL=simple-provider.js.map