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