@bandeira-tech/b3nd-web 0.2.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.
@@ -0,0 +1,219 @@
1
+ /**
2
+ * @b3nd/sdk Types
3
+ * Core types for the universal B3nd protocol interface
4
+ */
5
+ /**
6
+ * Persistence record with timestamp
7
+ */
8
+ interface PersistenceRecord<T = unknown> {
9
+ ts: number;
10
+ data: T;
11
+ }
12
+ /**
13
+ * Result of a write operation
14
+ */
15
+ interface WriteResult<T = unknown> {
16
+ success: boolean;
17
+ record?: PersistenceRecord<T>;
18
+ error?: string;
19
+ }
20
+ /**
21
+ * Result of a read operation
22
+ */
23
+ interface ReadResult<T> {
24
+ success: boolean;
25
+ record?: PersistenceRecord<T>;
26
+ error?: string;
27
+ }
28
+ /**
29
+ * Result of a delete operation
30
+ */
31
+ interface DeleteResult {
32
+ success: boolean;
33
+ error?: string;
34
+ }
35
+ /**
36
+ * Item returned from list operations
37
+ */
38
+ interface ListItem {
39
+ uri: string;
40
+ type: "file" | "directory";
41
+ }
42
+ /**
43
+ * Options for list operations
44
+ */
45
+ interface ListOptions {
46
+ page?: number;
47
+ limit?: number;
48
+ pattern?: string;
49
+ sortBy?: "name" | "timestamp";
50
+ sortOrder?: "asc" | "desc";
51
+ }
52
+ /**
53
+ * Result of a list operation
54
+ */
55
+ type ListResult = {
56
+ success: true;
57
+ data: ListItem[];
58
+ pagination: {
59
+ page: number;
60
+ limit: number;
61
+ total?: number;
62
+ };
63
+ } | {
64
+ success: false;
65
+ error: string;
66
+ };
67
+ /**
68
+ * Health status response
69
+ */
70
+ interface HealthStatus {
71
+ status: "healthy" | "degraded" | "unhealthy";
72
+ message?: string;
73
+ details?: Record<string, unknown>;
74
+ }
75
+ /**
76
+ * Validation function for write operations
77
+ * Returns an object with valid boolean and optional error message
78
+ */
79
+ type ValidationFn = (write: {
80
+ uri: string;
81
+ value: unknown;
82
+ read: <T = unknown>(uri: string) => Promise<ReadResult<T>>;
83
+ }) => Promise<{
84
+ valid: boolean;
85
+ error?: string;
86
+ }>;
87
+ /**
88
+ * Schema mapping program keys to validation functions
89
+ */
90
+ type Schema = Record<string, ValidationFn>;
91
+ /**
92
+ * NodeProtocolInterface - The universal interface implemented by all clients
93
+ *
94
+ * All B3nd clients (Memory, HTTP, WebSocket, Postgres, IndexedDB, etc.)
95
+ * implement this interface, enabling recursive composition and uniform usage.
96
+ */
97
+ interface NodeProtocolWriteInterface {
98
+ /** Write data to a URI */
99
+ write<T = unknown>(uri: string, value: T): Promise<WriteResult<T>>;
100
+ /** Delete data at a URI */
101
+ delete(uri: string): Promise<DeleteResult>;
102
+ /** Health status */
103
+ health(): Promise<HealthStatus>;
104
+ /** Supported program keys */
105
+ getSchema(): Promise<string[]>;
106
+ /** Cleanup resources */
107
+ cleanup(): Promise<void>;
108
+ }
109
+ interface NodeProtocolReadInterface {
110
+ /** Read data from a URI */
111
+ read<T = unknown>(uri: string): Promise<ReadResult<T>>;
112
+ /** List items at a URI path */
113
+ list(uri: string, options?: ListOptions): Promise<ListResult>;
114
+ /** Health status */
115
+ health(): Promise<HealthStatus>;
116
+ /** Supported program keys */
117
+ getSchema(): Promise<string[]>;
118
+ /** Cleanup resources */
119
+ cleanup(): Promise<void>;
120
+ }
121
+ type NodeProtocolInterface = NodeProtocolWriteInterface & NodeProtocolReadInterface;
122
+ /**
123
+ * Configuration for HttpClient
124
+ */
125
+ interface HttpClientConfig {
126
+ /**
127
+ * Base URL of the HTTP API
128
+ */
129
+ url: string;
130
+ /**
131
+ * Optional custom headers
132
+ */
133
+ headers?: Record<string, string>;
134
+ /**
135
+ * Request timeout in milliseconds (default: 30000)
136
+ */
137
+ timeout?: number;
138
+ }
139
+ /**
140
+ * Configuration for WebSocketClient
141
+ */
142
+ interface WebSocketClientConfig {
143
+ /**
144
+ * WebSocket server URL
145
+ */
146
+ url: string;
147
+ /**
148
+ * Optional authentication configuration
149
+ */
150
+ auth?: {
151
+ type: "bearer" | "basic" | "custom";
152
+ token?: string;
153
+ username?: string;
154
+ password?: string;
155
+ custom?: Record<string, unknown>;
156
+ };
157
+ /**
158
+ * Optional reconnection configuration
159
+ */
160
+ reconnect?: {
161
+ enabled: boolean;
162
+ maxAttempts?: number;
163
+ interval?: number;
164
+ backoff?: "linear" | "exponential";
165
+ };
166
+ /**
167
+ * Request timeout in milliseconds (default: 30000)
168
+ */
169
+ timeout?: number;
170
+ }
171
+ /**
172
+ * Configuration for LocalStorageClient
173
+ */
174
+ interface LocalStorageClientConfig {
175
+ /**
176
+ * Optional prefix for localStorage keys to avoid collisions
177
+ */
178
+ keyPrefix?: string;
179
+ /**
180
+ * Optional schema for validation (like MemoryClient)
181
+ */
182
+ schema?: Schema;
183
+ /**
184
+ * Optional serialization functions
185
+ */
186
+ serializer?: {
187
+ serialize?: (data: unknown) => string;
188
+ deserialize?: (data: string) => unknown;
189
+ };
190
+ /**
191
+ * Optional injectable storage dependency (defaults to global localStorage)
192
+ */
193
+ storage?: Storage;
194
+ }
195
+ /**
196
+ * Error class for client operations
197
+ * Preserves error context without hiding details
198
+ */
199
+ declare class ClientError extends Error {
200
+ readonly code: string;
201
+ readonly details?: unknown;
202
+ constructor(message: string, code: string, details?: unknown);
203
+ }
204
+ /**
205
+ * WebSocket protocol types for request/response communication
206
+ */
207
+ interface WebSocketRequest {
208
+ id: string;
209
+ type: "write" | "read" | "list" | "delete" | "health" | "getSchema";
210
+ payload: unknown;
211
+ }
212
+ interface WebSocketResponse {
213
+ id: string;
214
+ success: boolean;
215
+ data?: unknown;
216
+ error?: string;
217
+ }
218
+
219
+ export { ClientError as C, type DeleteResult as D, type HealthStatus as H, type ListItem as L, type NodeProtocolInterface as N, type PersistenceRecord as P, type ReadResult as R, type Schema as S, type ValidationFn as V, type WebSocketClientConfig as W, type HttpClientConfig as a, type ListOptions as b, type ListResult as c, type LocalStorageClientConfig as d, type WebSocketRequest as e, type WebSocketResponse as f, type WriteResult as g };
@@ -0,0 +1,278 @@
1
+ /**
2
+ * Wallet Client Types
3
+ *
4
+ * Type definitions for the B3nd Wallet Client that interacts with wallet servers.
5
+ */
6
+ /**
7
+ * Configuration for wallet client
8
+ */
9
+ interface WalletClientConfig {
10
+ /**
11
+ * Wallet server URL (e.g., "http://localhost:3001")
12
+ */
13
+ walletServerUrl: string;
14
+ /**
15
+ * API base path prefix (e.g., "/api/v1"). Must be provided explicitly.
16
+ */
17
+ apiBasePath: string;
18
+ /**
19
+ * Optional fetch implementation (for custom HTTP handling)
20
+ */
21
+ fetch?: typeof fetch;
22
+ }
23
+ /**
24
+ * User credentials for authentication
25
+ */
26
+ interface UserCredentials {
27
+ username: string;
28
+ password: string;
29
+ }
30
+ /**
31
+ * Authenticated session with JWT token
32
+ */
33
+ interface AuthSession {
34
+ username: string;
35
+ token: string;
36
+ expiresIn: number;
37
+ }
38
+ /**
39
+ * User's public keys
40
+ */
41
+ interface UserPublicKeys {
42
+ accountPublicKeyHex: string;
43
+ encryptionPublicKeyHex: string;
44
+ }
45
+ /**
46
+ * Password reset token
47
+ */
48
+ interface PasswordResetToken {
49
+ resetToken: string;
50
+ expiresIn: number;
51
+ }
52
+ /**
53
+ * Write proxy request
54
+ */
55
+ interface ProxyWriteRequest {
56
+ uri: string;
57
+ data: unknown;
58
+ encrypt?: boolean;
59
+ }
60
+ /**
61
+ * Write proxy response
62
+ */
63
+ interface ProxyWriteResponse {
64
+ success: boolean;
65
+ uri: string;
66
+ resolvedUri?: string;
67
+ data: unknown;
68
+ record?: {
69
+ data: unknown;
70
+ ts: number;
71
+ };
72
+ }
73
+ /**
74
+ * API response wrapper
75
+ */
76
+ interface ApiResponse<T = unknown> {
77
+ success: boolean;
78
+ error?: string;
79
+ [key: string]: unknown;
80
+ }
81
+ /**
82
+ * Signup response
83
+ */
84
+ interface SignupResponse extends ApiResponse {
85
+ username: string;
86
+ token: string;
87
+ expiresIn: number;
88
+ }
89
+ /**
90
+ * Login response
91
+ */
92
+ interface LoginResponse extends ApiResponse {
93
+ username: string;
94
+ token: string;
95
+ expiresIn: number;
96
+ }
97
+ /**
98
+ * Public keys response
99
+ */
100
+ interface PublicKeysResponse extends ApiResponse {
101
+ accountPublicKeyHex: string;
102
+ encryptionPublicKeyHex: string;
103
+ }
104
+ /**
105
+ * Change password response
106
+ */
107
+ interface ChangePasswordResponse extends ApiResponse {
108
+ message: string;
109
+ }
110
+ /**
111
+ * Request password reset response
112
+ */
113
+ interface RequestPasswordResetResponse extends ApiResponse {
114
+ message: string;
115
+ resetToken: string;
116
+ expiresIn: number;
117
+ }
118
+ /**
119
+ * Reset password response
120
+ */
121
+ interface ResetPasswordResponse extends ApiResponse {
122
+ message: string;
123
+ username: string;
124
+ token: string;
125
+ expiresIn: number;
126
+ }
127
+ /**
128
+ * Health check response
129
+ */
130
+ interface HealthResponse extends ApiResponse {
131
+ status: string;
132
+ server: string;
133
+ timestamp: string;
134
+ }
135
+
136
+ /**
137
+ * B3nd Wallet Client
138
+ *
139
+ * Client library for interacting with B3nd wallet servers.
140
+ * Provides authentication, key management, and write proxying functionality.
141
+ *
142
+ * Works in both Deno and browser environments.
143
+ */
144
+
145
+ /**
146
+ * B3nd Wallet Client
147
+ *
148
+ * Manages authentication with a wallet server and provides methods
149
+ * for user management, key retrieval, and proxied writes.
150
+ *
151
+ * @example
152
+ * ```typescript
153
+ * const wallet = new WalletClient({
154
+ * walletServerUrl: "http://localhost:3001",
155
+ * apiBasePath: "/api/v1",
156
+ * });
157
+ *
158
+ * // Sign up a new user
159
+ * const session = await wallet.signup({
160
+ * username: "alice",
161
+ * password: "secure-password-123"
162
+ * });
163
+ *
164
+ * // Activate the session
165
+ * wallet.setSession(session);
166
+ *
167
+ * // Write data through proxy
168
+ * await wallet.proxyWrite({
169
+ * uri: "mutable://data/my-app/profile",
170
+ * data: { name: "Alice" },
171
+ * encrypt: true
172
+ * });
173
+ * ```
174
+ */
175
+ declare class WalletClient {
176
+ private walletServerUrl;
177
+ private apiBasePath;
178
+ private fetchImpl;
179
+ private currentSession;
180
+ constructor(config: WalletClientConfig);
181
+ /**
182
+ * Get the current authenticated session
183
+ */
184
+ getSession(): AuthSession | null;
185
+ /**
186
+ * Set the current session (useful for restoring from storage)
187
+ */
188
+ setSession(session: AuthSession | null): void;
189
+ /**
190
+ * Check if user is currently authenticated
191
+ */
192
+ isAuthenticated(): boolean;
193
+ /**
194
+ * Get current username (if authenticated)
195
+ */
196
+ getUsername(): string | null;
197
+ /**
198
+ * Get current JWT token (if authenticated)
199
+ */
200
+ getToken(): string | null;
201
+ /**
202
+ * Clear current session (logout)
203
+ */
204
+ logout(): void;
205
+ /**
206
+ * Check wallet server health
207
+ */
208
+ health(): Promise<HealthResponse>;
209
+ /**
210
+ * Sign up a new user
211
+ * Returns session data - call setSession() to activate it
212
+ */
213
+ signup(_credentials: UserCredentials): Promise<AuthSession>;
214
+ /**
215
+ * Login existing user
216
+ * Returns session data - call setSession() to activate it
217
+ */
218
+ login(_credentials: UserCredentials): Promise<AuthSession>;
219
+ /**
220
+ * Change password for current user
221
+ * Requires active authentication session
222
+ */
223
+ changePassword(oldPassword: string, newPassword: string): Promise<void>;
224
+ /**
225
+ * Request a password reset token
226
+ * Does not require authentication
227
+ */
228
+ requestPasswordReset(_username: string): Promise<PasswordResetToken>;
229
+ /**
230
+ * Reset password using a reset token
231
+ * Returns session data - call setSession() to activate it
232
+ */
233
+ resetPassword(_username: string, _resetToken: string, _newPassword: string): Promise<AuthSession>;
234
+ /**
235
+ * Sign up with app token (scoped to an app)
236
+ */
237
+ signupWithToken(token: string, credentials: UserCredentials): Promise<AuthSession>;
238
+ /**
239
+ * Login with app token and session (scoped to an app)
240
+ */
241
+ loginWithTokenSession(token: string, session: string, credentials: UserCredentials): Promise<AuthSession>;
242
+ /**
243
+ * Request password reset scoped to app token
244
+ */
245
+ requestPasswordResetWithToken(token: string, username: string): Promise<PasswordResetToken>;
246
+ /**
247
+ * Reset password scoped to app token
248
+ */
249
+ resetPasswordWithToken(token: string, username: string, resetToken: string, newPassword: string): Promise<AuthSession>;
250
+ /**
251
+ * Get public keys for the current authenticated user.
252
+ * Requires an active authentication session.
253
+ */
254
+ getPublicKeys(): Promise<UserPublicKeys>;
255
+ /**
256
+ * Proxy a write request through the wallet server
257
+ * The server signs the write with its identity key
258
+ * Requires active authentication session
259
+ */
260
+ proxyWrite(request: ProxyWriteRequest): Promise<ProxyWriteResponse>;
261
+ /**
262
+ * Convenience method: Get current user's public keys
263
+ * Requires active authentication session
264
+ */
265
+ getMyPublicKeys(): Promise<UserPublicKeys>;
266
+ /**
267
+ * Get server's public keys
268
+ *
269
+ * @returns Server's identity and encryption public keys
270
+ * @throws Error if request fails
271
+ */
272
+ getServerKeys(): Promise<{
273
+ identityPublicKeyHex: string;
274
+ encryptionPublicKeyHex: string;
275
+ }>;
276
+ }
277
+
278
+ export { type ApiResponse, type AuthSession, type ChangePasswordResponse, type HealthResponse, type LoginResponse, type PasswordResetToken, type ProxyWriteRequest, type ProxyWriteResponse, type PublicKeysResponse, type RequestPasswordResetResponse, type ResetPasswordResponse, type SignupResponse, type UserCredentials, type UserPublicKeys, WalletClient, type WalletClientConfig };
@@ -0,0 +1,7 @@
1
+ import {
2
+ WalletClient
3
+ } from "../chunk-2D2RT2DW.js";
4
+ import "../chunk-MLKGABMK.js";
5
+ export {
6
+ WalletClient
7
+ };
package/package.json ADDED
@@ -0,0 +1,84 @@
1
+ {
2
+ "name": "@bandeira-tech/b3nd-web",
3
+ "version": "0.2.0",
4
+ "description": "Browser-focused B3nd SDK bundle",
5
+ "type": "module",
6
+ "main": "./dist/src/mod.web.js",
7
+ "module": "./dist/src/mod.web.js",
8
+ "types": "./dist/src/mod.web.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/src/mod.web.js",
12
+ "types": "./dist/src/mod.web.d.ts"
13
+ },
14
+ "./encrypt": {
15
+ "import": "./dist/encrypt/mod.js",
16
+ "types": "./dist/encrypt/mod.d.ts"
17
+ },
18
+ "./clients/http": {
19
+ "import": "./dist/clients/http/mod.js",
20
+ "types": "./dist/clients/http/mod.d.ts"
21
+ },
22
+ "./clients/local-storage": {
23
+ "import": "./dist/clients/local-storage/mod.js",
24
+ "types": "./dist/clients/local-storage/mod.d.ts"
25
+ },
26
+ "./clients/websocket": {
27
+ "import": "./dist/clients/websocket/mod.js",
28
+ "types": "./dist/clients/websocket/mod.d.ts"
29
+ },
30
+ "./wallet": {
31
+ "import": "./dist/wallet/mod.js",
32
+ "types": "./dist/wallet/mod.d.ts"
33
+ },
34
+ "./apps": {
35
+ "import": "./dist/apps/mod.js",
36
+ "types": "./dist/apps/mod.d.ts"
37
+ }
38
+ },
39
+ "files": [
40
+ "dist"
41
+ ],
42
+ "scripts": {
43
+ "build": "tsup src/mod.web.ts wallet/mod.ts apps/mod.ts encrypt/mod.ts clients/http/mod.ts clients/local-storage/mod.ts clients/websocket/mod.ts --dts --format esm --out-dir dist --clean",
44
+ "clean": "rm -rf dist",
45
+ "lint": "deno lint src/",
46
+ "format": "deno fmt src/"
47
+ },
48
+ "keywords": [
49
+ "persistence",
50
+ "storage",
51
+ "universal",
52
+ "cross-platform",
53
+ "indexeddb",
54
+ "localstorage",
55
+ "memory",
56
+ "http",
57
+ "websocket",
58
+ "wallet",
59
+ "authentication",
60
+ "typescript"
61
+ ],
62
+ "author": "B3nd",
63
+ "license": "MIT",
64
+ "repository": {
65
+ "type": "git",
66
+ "url": "https://github.com/bandeira-tech/b3nd-sdk"
67
+ },
68
+ "bugs": {
69
+ "url": "https://github.com/bandeira-tech/b3nd-sdk/issues"
70
+ },
71
+ "homepage": "https://github.com/bandeira-tech/b3nd-sdk#readme",
72
+ "dependencies": {},
73
+ "devDependencies": {
74
+ "tsup": "^8.3.5",
75
+ "typescript": "^5.6.3",
76
+ "@types/node": "^22.10.1"
77
+ },
78
+ "engines": {
79
+ "node": ">=24.11.1"
80
+ },
81
+ "publishConfig": {
82
+ "access": "public"
83
+ }
84
+ }