@b3dotfun/sdk 0.0.47-alpha.0 → 0.0.47-alpha.2

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 (63) hide show
  1. package/README.md +222 -3
  2. package/dist/cjs/anyspend/react/components/AnySpendCustom.js +5 -3
  3. package/dist/cjs/notifications/index.d.ts +3 -0
  4. package/dist/cjs/notifications/index.js +25 -0
  5. package/dist/cjs/notifications/react/hooks/index.d.ts +1 -0
  6. package/dist/cjs/notifications/react/hooks/index.js +17 -0
  7. package/dist/cjs/notifications/react/hooks/useNotifications.d.ts +42 -0
  8. package/dist/cjs/notifications/react/hooks/useNotifications.js +148 -0
  9. package/dist/cjs/notifications/react/index.d.ts +1 -0
  10. package/dist/cjs/notifications/react/index.js +17 -0
  11. package/dist/cjs/notifications/services/api.d.ts +67 -0
  12. package/dist/cjs/notifications/services/api.js +184 -0
  13. package/dist/cjs/notifications/services/index.d.ts +1 -0
  14. package/dist/cjs/notifications/services/index.js +17 -0
  15. package/dist/cjs/notifications/types/index.d.ts +51 -0
  16. package/dist/cjs/notifications/types/index.js +2 -0
  17. package/dist/cjs/shared/utils/auth-token.d.ts +7 -0
  18. package/dist/cjs/shared/utils/auth-token.js +17 -0
  19. package/dist/cjs/shared/utils/index.d.ts +1 -0
  20. package/dist/cjs/shared/utils/index.js +1 -0
  21. package/dist/esm/anyspend/react/components/AnySpendCustom.js +5 -3
  22. package/dist/esm/notifications/index.d.ts +3 -0
  23. package/dist/esm/notifications/index.js +7 -0
  24. package/dist/esm/notifications/react/hooks/index.d.ts +1 -0
  25. package/dist/esm/notifications/react/hooks/index.js +1 -0
  26. package/dist/esm/notifications/react/hooks/useNotifications.d.ts +42 -0
  27. package/dist/esm/notifications/react/hooks/useNotifications.js +145 -0
  28. package/dist/esm/notifications/react/index.d.ts +1 -0
  29. package/dist/esm/notifications/react/index.js +1 -0
  30. package/dist/esm/notifications/services/api.d.ts +67 -0
  31. package/dist/esm/notifications/services/api.js +179 -0
  32. package/dist/esm/notifications/services/index.d.ts +1 -0
  33. package/dist/esm/notifications/services/index.js +1 -0
  34. package/dist/esm/notifications/types/index.d.ts +51 -0
  35. package/dist/esm/shared/utils/auth-token.d.ts +7 -0
  36. package/dist/esm/shared/utils/auth-token.js +11 -0
  37. package/dist/esm/shared/utils/index.d.ts +1 -0
  38. package/dist/esm/shared/utils/index.js +1 -0
  39. package/dist/types/notifications/index.d.ts +3 -0
  40. package/dist/types/notifications/react/hooks/index.d.ts +1 -0
  41. package/dist/types/notifications/react/hooks/useNotifications.d.ts +42 -0
  42. package/dist/types/notifications/react/index.d.ts +1 -0
  43. package/dist/types/notifications/services/api.d.ts +67 -0
  44. package/dist/types/notifications/services/index.d.ts +1 -0
  45. package/dist/types/notifications/types/index.d.ts +51 -0
  46. package/dist/types/shared/utils/auth-token.d.ts +7 -0
  47. package/dist/types/shared/utils/index.d.ts +1 -0
  48. package/package.json +21 -1
  49. package/src/anyspend/react/components/AnySpendCustom.tsx +5 -3
  50. package/src/notifications/index.ts +9 -0
  51. package/src/notifications/react/hooks/index.ts +1 -0
  52. package/src/notifications/react/hooks/useNotifications.ts +153 -0
  53. package/src/notifications/react/index.ts +1 -0
  54. package/src/notifications/services/api.ts +217 -0
  55. package/src/notifications/services/index.ts +1 -0
  56. package/src/notifications/types/index.ts +58 -0
  57. package/src/shared/utils/auth-token.ts +13 -0
  58. package/src/shared/utils/index.ts +1 -0
  59. package/dist/cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -245
  60. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
  61. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -243
  62. package/dist/types/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
  63. /package/dist/{cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts → esm/notifications/types/index.js} +0 -0
@@ -0,0 +1,179 @@
1
+ import { getAuthToken } from "../../shared/utils/auth-token.js";
2
+ const DEFAULT_API_URL = "https://notifications.b3.fun";
3
+ let apiUrl = DEFAULT_API_URL;
4
+ export function setApiUrl(url) {
5
+ apiUrl = url;
6
+ }
7
+ export function getApiUrl() {
8
+ return apiUrl;
9
+ }
10
+ function getHeaders(includeAuth = false) {
11
+ const headers = {
12
+ "Content-Type": "application/json",
13
+ };
14
+ if (includeAuth) {
15
+ const token = getAuthToken();
16
+ if (token) {
17
+ headers["Authorization"] = `Bearer ${token}`;
18
+ }
19
+ }
20
+ return headers;
21
+ }
22
+ export const notificationsAPI = {
23
+ // ===== USER MANAGEMENT =====
24
+ /**
25
+ * Register the current user (userId extracted from JWT)
26
+ */
27
+ async registerUser() {
28
+ const res = await fetch(`${apiUrl}/users`, {
29
+ method: "POST",
30
+ headers: getHeaders(true),
31
+ });
32
+ if (!res.ok) {
33
+ const errorBody = await res.text().catch(() => "Could not read error body");
34
+ throw new Error(`API Error: ${res.status} ${res.statusText} - ${errorBody}`);
35
+ }
36
+ return res.json();
37
+ },
38
+ /**
39
+ * Get current user's profile and preferences
40
+ */
41
+ async getUser() {
42
+ const res = await fetch(`${apiUrl}/users/me`, {
43
+ headers: getHeaders(true),
44
+ });
45
+ return res.json();
46
+ },
47
+ /**
48
+ * Get current user's notification history
49
+ */
50
+ async getHistory(appId, limit = 100) {
51
+ const params = new URLSearchParams();
52
+ if (appId)
53
+ params.append("appId", appId);
54
+ params.append("limit", limit.toString());
55
+ const res = await fetch(`${apiUrl}/users/me/history?${params}`, {
56
+ headers: getHeaders(true),
57
+ });
58
+ return res.json();
59
+ },
60
+ // ===== CHANNELS =====
61
+ /**
62
+ * Add a notification channel for current user
63
+ */
64
+ async addChannel(channelType, channelIdentifier, metadata) {
65
+ const res = await fetch(`${apiUrl}/users/me/channels`, {
66
+ method: "POST",
67
+ headers: getHeaders(true),
68
+ body: JSON.stringify({
69
+ channelType,
70
+ channelIdentifier,
71
+ enabled: true,
72
+ metadata,
73
+ }),
74
+ });
75
+ return res.json();
76
+ },
77
+ /**
78
+ * Connect email for current user
79
+ */
80
+ async connectEmail(email) {
81
+ return this.addChannel("email", email);
82
+ },
83
+ /**
84
+ * Update a notification channel
85
+ */
86
+ async updateChannel(channelId, updates) {
87
+ const res = await fetch(`${apiUrl}/users/me/channels/${channelId}`, {
88
+ method: "PUT",
89
+ headers: getHeaders(true),
90
+ body: JSON.stringify(updates),
91
+ });
92
+ return res.json();
93
+ },
94
+ /**
95
+ * Delete a notification channel
96
+ */
97
+ async deleteChannel(channelId) {
98
+ const res = await fetch(`${apiUrl}/users/me/channels/${channelId}`, {
99
+ method: "DELETE",
100
+ headers: getHeaders(true),
101
+ });
102
+ return res.json();
103
+ },
104
+ // ===== TELEGRAM =====
105
+ /**
106
+ * Get Telegram deep link for current user
107
+ */
108
+ async getTelegramLink() {
109
+ const res = await fetch(`${apiUrl}/telegram/request-link`, {
110
+ method: "POST",
111
+ headers: getHeaders(true),
112
+ });
113
+ return res.json();
114
+ },
115
+ /**
116
+ * Check current user's Telegram connection status
117
+ */
118
+ async checkTelegramStatus() {
119
+ const res = await fetch(`${apiUrl}/telegram/status/me`, {
120
+ headers: getHeaders(true),
121
+ });
122
+ return res.json();
123
+ },
124
+ // ===== APP PREFERENCES =====
125
+ /**
126
+ * Save notification preferences for an app
127
+ * @param appId - The application ID
128
+ * @param settings - Notification preferences including channels, type, and enabled status (defaults to true)
129
+ */
130
+ async savePreferences(appId, settings) {
131
+ const res = await fetch(`${apiUrl}/users/me/apps/${appId}/settings`, {
132
+ method: "POST",
133
+ headers: getHeaders(true),
134
+ body: JSON.stringify({ enabled: true, ...settings }),
135
+ });
136
+ return res.json();
137
+ },
138
+ /**
139
+ * Get notification settings for an app
140
+ */
141
+ async getAppSettings(appId) {
142
+ const res = await fetch(`${apiUrl}/users/me/apps/${appId}/settings`, {
143
+ headers: getHeaders(true),
144
+ });
145
+ return res.json();
146
+ },
147
+ // ===== IN-APP NOTIFICATIONS =====
148
+ /**
149
+ * Get current user's in-app notifications
150
+ */
151
+ async getInAppNotifications() {
152
+ const res = await fetch(`${apiUrl}/users/me/notifications`, {
153
+ headers: getHeaders(true),
154
+ });
155
+ return res.json();
156
+ },
157
+ /**
158
+ * Mark a notification as read
159
+ */
160
+ async markNotificationAsRead(notificationId) {
161
+ const res = await fetch(`${apiUrl}/users/me/notifications/${notificationId}/read`, {
162
+ method: "PUT",
163
+ headers: getHeaders(true),
164
+ });
165
+ return res.json();
166
+ },
167
+ // ===== SENDING NOTIFICATIONS =====
168
+ /**
169
+ * Send a notification (requires auth)
170
+ */
171
+ async sendNotification(data) {
172
+ const res = await fetch(`${apiUrl}/send`, {
173
+ method: "POST",
174
+ headers: getHeaders(true),
175
+ body: JSON.stringify(data),
176
+ });
177
+ return res.json();
178
+ },
179
+ };
@@ -0,0 +1 @@
1
+ export * from "./api";
@@ -0,0 +1 @@
1
+ export * from "./api.js";
@@ -0,0 +1,51 @@
1
+ export type ChannelType = "email" | "telegram" | "discord" | "sms" | "whatsapp" | "in_app";
2
+ export interface NotificationChannel {
3
+ id: number;
4
+ channel_type: ChannelType;
5
+ enabled: number;
6
+ channel_identifier: string;
7
+ }
8
+ export interface UserData {
9
+ user: {
10
+ id: number;
11
+ user_id: string;
12
+ };
13
+ channels: NotificationChannel[];
14
+ appSettings: Array<{
15
+ app_id: string;
16
+ notification_type: string;
17
+ enabled: number;
18
+ channels: string;
19
+ }>;
20
+ }
21
+ export interface NotificationHistory {
22
+ id: string;
23
+ app_id: string;
24
+ notification_type: string;
25
+ title: string;
26
+ message: string;
27
+ created_at: string;
28
+ read: boolean;
29
+ }
30
+ export interface TelegramLinkResponse {
31
+ deepLink: string;
32
+ verificationCode: string;
33
+ botUsername: string;
34
+ }
35
+ export interface TelegramStatusResponse {
36
+ connected: boolean;
37
+ chatId?: string;
38
+ }
39
+ export interface NotificationPreferences {
40
+ notificationType: string;
41
+ channels: string[];
42
+ enabled?: boolean;
43
+ }
44
+ export interface SendNotificationRequest {
45
+ userId: string;
46
+ appId: string;
47
+ notificationType: string;
48
+ message: string;
49
+ title?: string;
50
+ data?: Record<string, any>;
51
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Get the authentication token from the B3 auth cookie
3
+ * This token is managed by the B3 Global Account authentication system
4
+ *
5
+ * @returns The JWT token string or null if not found
6
+ */
7
+ export declare function getAuthToken(): string | null;
@@ -0,0 +1,11 @@
1
+ import Cookies from "js-cookie";
2
+ const B3_AUTH_COOKIE_NAME = "b3-auth";
3
+ /**
4
+ * Get the authentication token from the B3 auth cookie
5
+ * This token is managed by the B3 Global Account authentication system
6
+ *
7
+ * @returns The JWT token string or null if not found
8
+ */
9
+ export function getAuthToken() {
10
+ return Cookies.get(B3_AUTH_COOKIE_NAME) || null;
11
+ }
@@ -1,3 +1,4 @@
1
+ export * from "./auth-token";
1
2
  export * from "./cn";
2
3
  export * from "./formatNumber";
3
4
  export * from "./formatUsername";
@@ -1,4 +1,5 @@
1
1
  // Export utility functions
2
+ export * from "./auth-token.js";
2
3
  export * from "./cn.js";
3
4
  export * from "./formatNumber.js";
4
5
  export * from "./formatUsername.js";
@@ -0,0 +1,3 @@
1
+ export * from "./types";
2
+ export * from "./services";
3
+ export { getAuthToken } from "../shared/utils/auth-token";
@@ -0,0 +1 @@
1
+ export * from "./useNotifications";
@@ -0,0 +1,42 @@
1
+ import type { UserData } from "../../types";
2
+ /**
3
+ * React hook for managing B3 notifications
4
+ * Automatically uses the authenticated user's ID from JWT
5
+ *
6
+ * @example
7
+ * ```tsx
8
+ * import { useNotifications } from '@b3dotfun/sdk/notifications/react';
9
+ *
10
+ * function NotificationSettings() {
11
+ * const { user, loading, connectEmail, connectTelegram, isEmailConnected } = useNotifications();
12
+ *
13
+ * if (loading) return <div>Loading...</div>;
14
+ *
15
+ * return (
16
+ * <div>
17
+ * {!isEmailConnected && (
18
+ * <button onClick={() => connectEmail('user@example.com')}>
19
+ * Connect Email
20
+ * </button>
21
+ * )}
22
+ * <button onClick={connectTelegram}>Connect Telegram</button>
23
+ * </div>
24
+ * );
25
+ * }
26
+ * ```
27
+ */
28
+ export declare function useNotifications(): {
29
+ user: UserData | null;
30
+ loading: boolean;
31
+ error: Error | null;
32
+ refresh: () => Promise<void>;
33
+ connectEmail: (email: string) => Promise<void>;
34
+ connectTelegram: () => Promise<void>;
35
+ updateChannel: (channelId: string, updates: {
36
+ enabled?: boolean;
37
+ }) => Promise<void>;
38
+ deleteChannel: (channelId: string) => Promise<void>;
39
+ isEmailConnected: boolean;
40
+ isTelegramConnected: boolean;
41
+ isDiscordConnected: boolean;
42
+ };
@@ -0,0 +1 @@
1
+ export * from "./hooks";
@@ -0,0 +1,67 @@
1
+ import type { NotificationHistory, NotificationPreferences, SendNotificationRequest, TelegramLinkResponse, TelegramStatusResponse, UserData } from "../types";
2
+ export declare function setApiUrl(url: string): void;
3
+ export declare function getApiUrl(): string;
4
+ export declare const notificationsAPI: {
5
+ /**
6
+ * Register the current user (userId extracted from JWT)
7
+ */
8
+ registerUser(): Promise<any>;
9
+ /**
10
+ * Get current user's profile and preferences
11
+ */
12
+ getUser(): Promise<UserData>;
13
+ /**
14
+ * Get current user's notification history
15
+ */
16
+ getHistory(appId?: string, limit?: number): Promise<NotificationHistory[]>;
17
+ /**
18
+ * Add a notification channel for current user
19
+ */
20
+ addChannel(channelType: string, channelIdentifier: string, metadata?: Record<string, any>): Promise<any>;
21
+ /**
22
+ * Connect email for current user
23
+ */
24
+ connectEmail(email: string): Promise<any>;
25
+ /**
26
+ * Update a notification channel
27
+ */
28
+ updateChannel(channelId: string, updates: {
29
+ enabled?: boolean;
30
+ channelIdentifier?: string;
31
+ metadata?: Record<string, any>;
32
+ }): Promise<any>;
33
+ /**
34
+ * Delete a notification channel
35
+ */
36
+ deleteChannel(channelId: string): Promise<any>;
37
+ /**
38
+ * Get Telegram deep link for current user
39
+ */
40
+ getTelegramLink(): Promise<TelegramLinkResponse>;
41
+ /**
42
+ * Check current user's Telegram connection status
43
+ */
44
+ checkTelegramStatus(): Promise<TelegramStatusResponse>;
45
+ /**
46
+ * Save notification preferences for an app
47
+ * @param appId - The application ID
48
+ * @param settings - Notification preferences including channels, type, and enabled status (defaults to true)
49
+ */
50
+ savePreferences(appId: string, settings: NotificationPreferences): Promise<any>;
51
+ /**
52
+ * Get notification settings for an app
53
+ */
54
+ getAppSettings(appId: string): Promise<any>;
55
+ /**
56
+ * Get current user's in-app notifications
57
+ */
58
+ getInAppNotifications(): Promise<any>;
59
+ /**
60
+ * Mark a notification as read
61
+ */
62
+ markNotificationAsRead(notificationId: string): Promise<any>;
63
+ /**
64
+ * Send a notification (requires auth)
65
+ */
66
+ sendNotification(data: SendNotificationRequest): Promise<any>;
67
+ };
@@ -0,0 +1 @@
1
+ export * from "./api";
@@ -0,0 +1,51 @@
1
+ export type ChannelType = "email" | "telegram" | "discord" | "sms" | "whatsapp" | "in_app";
2
+ export interface NotificationChannel {
3
+ id: number;
4
+ channel_type: ChannelType;
5
+ enabled: number;
6
+ channel_identifier: string;
7
+ }
8
+ export interface UserData {
9
+ user: {
10
+ id: number;
11
+ user_id: string;
12
+ };
13
+ channels: NotificationChannel[];
14
+ appSettings: Array<{
15
+ app_id: string;
16
+ notification_type: string;
17
+ enabled: number;
18
+ channels: string;
19
+ }>;
20
+ }
21
+ export interface NotificationHistory {
22
+ id: string;
23
+ app_id: string;
24
+ notification_type: string;
25
+ title: string;
26
+ message: string;
27
+ created_at: string;
28
+ read: boolean;
29
+ }
30
+ export interface TelegramLinkResponse {
31
+ deepLink: string;
32
+ verificationCode: string;
33
+ botUsername: string;
34
+ }
35
+ export interface TelegramStatusResponse {
36
+ connected: boolean;
37
+ chatId?: string;
38
+ }
39
+ export interface NotificationPreferences {
40
+ notificationType: string;
41
+ channels: string[];
42
+ enabled?: boolean;
43
+ }
44
+ export interface SendNotificationRequest {
45
+ userId: string;
46
+ appId: string;
47
+ notificationType: string;
48
+ message: string;
49
+ title?: string;
50
+ data?: Record<string, any>;
51
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Get the authentication token from the B3 auth cookie
3
+ * This token is managed by the B3 Global Account authentication system
4
+ *
5
+ * @returns The JWT token string or null if not found
6
+ */
7
+ export declare function getAuthToken(): string | null;
@@ -1,3 +1,4 @@
1
+ export * from "./auth-token";
1
2
  export * from "./cn";
2
3
  export * from "./formatNumber";
3
4
  export * from "./formatUsername";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@b3dotfun/sdk",
3
- "version": "0.0.47-alpha.0",
3
+ "version": "0.0.47-alpha.2",
4
4
  "source": "src/index.ts",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "react-native": "./dist/cjs/index.native.js",
@@ -170,6 +170,26 @@
170
170
  "import": "./dist/esm/global-account/server.js",
171
171
  "require": "./dist/cjs/global-account/server.js"
172
172
  },
173
+ "./notifications": {
174
+ "types": "./dist/types/notifications/index.d.ts",
175
+ "import": "./dist/esm/notifications/index.js",
176
+ "require": "./dist/cjs/notifications/index.js"
177
+ },
178
+ "./notifications/react": {
179
+ "types": "./dist/types/notifications/react/index.d.ts",
180
+ "import": "./dist/esm/notifications/react/index.js",
181
+ "require": "./dist/cjs/notifications/react/index.js"
182
+ },
183
+ "./notifications/services": {
184
+ "types": "./dist/types/notifications/services/index.d.ts",
185
+ "import": "./dist/esm/notifications/services/index.js",
186
+ "require": "./dist/cjs/notifications/services/index.js"
187
+ },
188
+ "./notifications/types": {
189
+ "types": "./dist/types/notifications/types/index.d.ts",
190
+ "import": "./dist/esm/notifications/types/index.js",
191
+ "require": "./dist/cjs/notifications/types/index.js"
192
+ },
173
193
  "./shared/utils": {
174
194
  "types": "./dist/types/shared/utils/index.d.ts",
175
195
  "import": "./dist/esm/shared/utils/index.js",
@@ -395,11 +395,13 @@ function AnySpendCustomInner({
395
395
  useEffect(() => {
396
396
  if (oat?.data?.order.status === "executed" && !onSuccessCalled.current) {
397
397
  console.log("Calling onSuccess");
398
- const txHash = oat?.data?.executeTx?.txHash;
398
+ const relayTxs = oat?.data?.relayTxs;
399
+ const lastRelayTxHash = relayTxs?.[relayTxs.length - 1]?.txHash;
400
+ const txHash = oat?.data?.executeTx?.txHash || lastRelayTxHash;
399
401
  onSuccess?.(txHash);
400
402
  onSuccessCalled.current = true;
401
403
  }
402
- }, [oat?.data?.order.status, oat?.data?.executeTx?.txHash, onSuccess]);
404
+ }, [oat?.data?.order.status, oat?.data?.executeTx?.txHash, oat?.data?.relayTxs, onSuccess]);
403
405
 
404
406
  // Reset flag when orderId changes
405
407
  useEffect(() => {
@@ -665,7 +667,7 @@ function AnySpendCustomInner({
665
667
  const orderDetailsView = (
666
668
  <div
667
669
  className={cn(
668
- "mx-auto flex w-full flex-col items-center gap-4",
670
+ "mx-auto flex w-full flex-col items-center gap-4 p-5",
669
671
  mode === "modal" && "bg-b3-react-background rounded-xl",
670
672
  )}
671
673
  >
@@ -0,0 +1,9 @@
1
+ // Export types
2
+ export * from "./types";
3
+
4
+ // Export services
5
+ export * from "./services";
6
+
7
+ // Re-export auth token getter from shared for convenience
8
+ // Note: Auth token is managed by B3 Global Account authentication
9
+ export { getAuthToken } from "../shared/utils/auth-token";
@@ -0,0 +1 @@
1
+ export * from "./useNotifications";