@broadcastingplatforms/sdk 0.0.0-dev.52fbff9

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 (57) hide show
  1. package/dist/auth-CSkJb9Wc.d.mts +1180 -0
  2. package/dist/auth-CSkJb9Wc.d.ts +1180 -0
  3. package/dist/auth.d.mts +1 -0
  4. package/dist/auth.d.ts +1 -0
  5. package/dist/auth.js +456 -0
  6. package/dist/auth.mjs +433 -0
  7. package/dist/base-client-BHrP5nb_.d.ts +70 -0
  8. package/dist/base-client-BbMR6ZrV.d.mts +70 -0
  9. package/dist/browser-client.d.mts +47 -0
  10. package/dist/browser-client.d.ts +47 -0
  11. package/dist/browser-client.js +1983 -0
  12. package/dist/browser-client.mjs +1952 -0
  13. package/dist/channels.d.mts +1 -0
  14. package/dist/channels.d.ts +1 -0
  15. package/dist/channels.js +78 -0
  16. package/dist/channels.mjs +55 -0
  17. package/dist/chat.d.mts +1 -0
  18. package/dist/chat.d.ts +1 -0
  19. package/dist/chat.js +105 -0
  20. package/dist/chat.mjs +80 -0
  21. package/dist/errors.d.mts +68 -0
  22. package/dist/errors.d.ts +68 -0
  23. package/dist/errors.js +151 -0
  24. package/dist/errors.mjs +127 -0
  25. package/dist/http.d.mts +1 -0
  26. package/dist/http.d.ts +1 -0
  27. package/dist/http.js +323 -0
  28. package/dist/http.mjs +298 -0
  29. package/dist/index.d.mts +7 -0
  30. package/dist/index.d.ts +7 -0
  31. package/dist/index.js +2072 -0
  32. package/dist/index.mjs +2025 -0
  33. package/dist/realtime.d.mts +9 -0
  34. package/dist/realtime.d.ts +9 -0
  35. package/dist/realtime.js +1050 -0
  36. package/dist/realtime.mjs +1021 -0
  37. package/dist/resource.d.mts +1 -0
  38. package/dist/resource.d.ts +1 -0
  39. package/dist/resource.js +52 -0
  40. package/dist/resource.mjs +27 -0
  41. package/dist/server-client.d.mts +60 -0
  42. package/dist/server-client.d.ts +60 -0
  43. package/dist/server-client.js +1984 -0
  44. package/dist/server-client.mjs +1954 -0
  45. package/dist/storage.d.mts +149 -0
  46. package/dist/storage.d.ts +149 -0
  47. package/dist/storage.js +243 -0
  48. package/dist/storage.mjs +211 -0
  49. package/dist/streams.d.mts +1 -0
  50. package/dist/streams.d.ts +1 -0
  51. package/dist/streams.js +267 -0
  52. package/dist/streams.mjs +242 -0
  53. package/dist/types.d.mts +1 -0
  54. package/dist/types.d.ts +1 -0
  55. package/dist/types.js +19 -0
  56. package/dist/types.mjs +1 -0
  57. package/package.json +139 -0
@@ -0,0 +1 @@
1
+ export { A as AuthClient, b as AuthClientOptions, c as AuthStateChangeCallback, d as AuthStateChangeEvent, e as SignInPayload, f as SignInWithTokenOptions } from './auth-CSkJb9Wc.mjs';
package/dist/auth.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { A as AuthClient, b as AuthClientOptions, c as AuthStateChangeCallback, d as AuthStateChangeEvent, e as SignInPayload, f as SignInWithTokenOptions } from './auth-CSkJb9Wc.js';
package/dist/auth.js ADDED
@@ -0,0 +1,456 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
+
22
+ // src/auth.ts
23
+ var auth_exports = {};
24
+ __export(auth_exports, {
25
+ AuthClient: () => AuthClient
26
+ });
27
+ module.exports = __toCommonJS(auth_exports);
28
+ var import_jwt_decode = require("jwt-decode");
29
+
30
+ // src/errors.ts
31
+ var SDKError = class extends Error {
32
+ /**
33
+ * Construct an SDKError. Signature is compatible with Error.
34
+ * You can pass in a message or an options object, as with the Error constructor.
35
+ * Additional fields (code, etc) may be added via the second argument.
36
+ */
37
+ constructor(message, details = {}) {
38
+ const resolvedMessage = message ?? (typeof details.message === "string" ? details.message : "Unknown error");
39
+ super(resolvedMessage);
40
+ /**
41
+ * Canonical SDK error code.
42
+ */
43
+ __publicField(this, "code");
44
+ /**
45
+ * HTTP status code if available.
46
+ */
47
+ __publicField(this, "statusCode");
48
+ /**
49
+ * End-user safe message.
50
+ */
51
+ __publicField(this, "userMessage");
52
+ /**
53
+ * Optional request metadata (null when unavailable).
54
+ */
55
+ __publicField(this, "request");
56
+ /**
57
+ * Optional response metadata (null when unavailable).
58
+ */
59
+ __publicField(this, "response");
60
+ Object.setPrototypeOf(this, new.target.prototype);
61
+ if (details && typeof details === "object") {
62
+ for (const [key, value] of Object.entries(details)) {
63
+ if (!["name", "message", "stack"].includes(key)) {
64
+ this[key] = value;
65
+ }
66
+ }
67
+ }
68
+ this.name = "SDKError";
69
+ this.code = details.code ?? "unknown_error";
70
+ this.statusCode = typeof details.statusCode === "number" ? details.statusCode : void 0;
71
+ this.userMessage = typeof details.userMessage === "string" ? details.userMessage : this.message;
72
+ this.request = details.request ?? null;
73
+ this.response = details.response ?? null;
74
+ }
75
+ /**
76
+ * Serialize the error into a plain object, preserving extra fields.
77
+ */
78
+ toJSON() {
79
+ const extras = { ...this };
80
+ delete extras.code;
81
+ delete extras.statusCode;
82
+ delete extras.message;
83
+ delete extras.userMessage;
84
+ delete extras.request;
85
+ delete extras.response;
86
+ delete extras.form;
87
+ delete extras.cause;
88
+ return {
89
+ ...extras,
90
+ code: this.code,
91
+ statusCode: this.statusCode,
92
+ message: this.message,
93
+ userMessage: this.userMessage,
94
+ request: this.request,
95
+ response: this.response
96
+ };
97
+ }
98
+ };
99
+ var STATUS_CODE_DEFAULTS = {
100
+ 401: {
101
+ code: "authentication_required",
102
+ message: "Authentication required",
103
+ userMessage: "Please log in to continue. Your session may have expired."
104
+ },
105
+ 403: {
106
+ code: "permission_denied",
107
+ message: "Permission denied",
108
+ userMessage: "You do not have permission to perform this action."
109
+ },
110
+ 404: {
111
+ code: "not_found",
112
+ message: "Resource not found",
113
+ userMessage: "The requested resource was not found."
114
+ },
115
+ 422: {
116
+ code: "validation_failed",
117
+ message: "Validation failed",
118
+ userMessage: "The provided information is invalid. Please check your input and try again."
119
+ },
120
+ 429: {
121
+ code: "rate_limited",
122
+ message: "Rate limit exceeded",
123
+ userMessage: "Too many requests. Please wait a moment and try again."
124
+ },
125
+ 500: {
126
+ code: "server_error",
127
+ message: "Internal server error",
128
+ userMessage: "Something went wrong on our end. Please try again later."
129
+ }
130
+ };
131
+ var DEFAULT_ERROR = {
132
+ code: "unknown_error",
133
+ userMessage: "An unexpected error occurred. Please try again."
134
+ };
135
+ function createSdkError(message, details = {}) {
136
+ const statusCode = details.response?.status;
137
+ const statusValues = statusCode && STATUS_CODE_DEFAULTS[statusCode] ? STATUS_CODE_DEFAULTS[statusCode] : DEFAULT_ERROR;
138
+ const enrichedDetails = {
139
+ ...details,
140
+ statusCode,
141
+ code: details.code ?? statusValues.code,
142
+ userMessage: details.userMessage ?? statusValues.userMessage,
143
+ request: details.request ?? null,
144
+ response: details.response ?? null
145
+ };
146
+ return new SDKError(message, enrichedDetails);
147
+ }
148
+
149
+ // src/storage.ts
150
+ var DEFAULT_AUTH_COOKIE_NAME = "auth_token";
151
+ var NoopStorageAdapter = class {
152
+ /**
153
+ * Read a stored value (always null).
154
+ */
155
+ get() {
156
+ return null;
157
+ }
158
+ /**
159
+ * Persist a stored value (no-op).
160
+ */
161
+ set() {
162
+ }
163
+ /**
164
+ * Delete a stored value (no-op).
165
+ */
166
+ delete() {
167
+ }
168
+ };
169
+
170
+ // src/auth.ts
171
+ var DEFAULT_REFRESH_THRESHOLD_SECONDS = 60;
172
+ var getTokenExpiry = (token) => {
173
+ try {
174
+ const payload = (0, import_jwt_decode.jwtDecode)(token);
175
+ const exp = payload?.exp;
176
+ return typeof exp === "number" ? exp : null;
177
+ } catch (error) {
178
+ const message = error instanceof Error ? error.message : "Failed to decode token";
179
+ throw createSdkError(message);
180
+ }
181
+ };
182
+ var AuthClient = class {
183
+ /**
184
+ * Create a new auth client instance.
185
+ */
186
+ constructor(http, options = {}) {
187
+ __publicField(this, "http");
188
+ __publicField(this, "storage");
189
+ __publicField(this, "storageKey");
190
+ __publicField(this, "refreshEnabled");
191
+ __publicField(this, "refreshThresholdSeconds");
192
+ __publicField(this, "serviceKey");
193
+ __publicField(this, "currentSession", null);
194
+ __publicField(this, "stateListeners", /* @__PURE__ */ new Set());
195
+ __publicField(this, "restorePromise", null);
196
+ /**
197
+ * Track whether storage restoration has already been attempted.
198
+ */
199
+ __publicField(this, "restoreAttempted", false);
200
+ this.http = http;
201
+ this.storage = options.storage ?? new NoopStorageAdapter();
202
+ this.storageKey = options.storageKey ?? DEFAULT_AUTH_COOKIE_NAME;
203
+ this.refreshEnabled = options.autoRefresh ?? false;
204
+ this.refreshThresholdSeconds = options.refreshThresholdSeconds ?? DEFAULT_REFRESH_THRESHOLD_SECONDS;
205
+ this.serviceKey = options.serviceKey ?? null;
206
+ this.registerAuthHook();
207
+ }
208
+ /**
209
+ * Set the current bearer token, or clear it when null.
210
+ */
211
+ async setToken(token) {
212
+ if (!token) {
213
+ await this.storage.delete(this.storageKey);
214
+ this.currentSession = null;
215
+ this.emitState("SIGNED_OUT", null);
216
+ return null;
217
+ }
218
+ return this.applyToken(token, "SIGNED_IN");
219
+ }
220
+ /**
221
+ * Get the currently configured token (if any).
222
+ */
223
+ getToken() {
224
+ return this.currentSession ?? this.serviceKey ?? null;
225
+ }
226
+ /**
227
+ * Clear the current token and session.
228
+ */
229
+ async clearToken() {
230
+ await this.setToken(null);
231
+ }
232
+ /**
233
+ * Sign in using a JWT token.
234
+ */
235
+ async signInWithToken(token, options = {}) {
236
+ if (options.verifySession) {
237
+ this.assertTokenNotExpired(token);
238
+ }
239
+ return this.applyToken(token, "SIGNED_IN");
240
+ }
241
+ /**
242
+ * Sign in using email/password credentials.
243
+ */
244
+ async signIn(payload) {
245
+ const { data, request, response } = await this.http.requestWithMetadata(
246
+ "/v2/auth/login",
247
+ {
248
+ method: "POST",
249
+ body: JSON.stringify(payload)
250
+ }
251
+ );
252
+ const token = this.resolveTokenFromResponse(data, { request, response });
253
+ return this.applyToken(token, "SIGNED_IN");
254
+ }
255
+ /**
256
+ * Sign out the current user.
257
+ */
258
+ async signOut() {
259
+ try {
260
+ await this.http.request("/v2/auth/logout", { method: "POST" });
261
+ } finally {
262
+ await this.clearToken();
263
+ }
264
+ }
265
+ /**
266
+ * Get the current session, restoring from storage if needed.
267
+ */
268
+ async getSession() {
269
+ if (this.currentSession) {
270
+ return this.currentSession;
271
+ }
272
+ await this.restoreFromStorage();
273
+ return this.currentSession;
274
+ }
275
+ /**
276
+ * Refresh the current session token.
277
+ * Manual refresh is always available; automatic refresh is controlled
278
+ * by the `autoRefresh` option.
279
+ */
280
+ async refreshToken() {
281
+ if (this.isUsingServiceKey()) {
282
+ throw createSdkError("Token refresh is unavailable when using a service key");
283
+ }
284
+ try {
285
+ const { data, request, response } = await this.http.requestWithMetadata(
286
+ "/v2/auth/refresh",
287
+ { method: "POST" }
288
+ );
289
+ const token = this.resolveTokenFromResponse(data, { request, response });
290
+ return await this.applyToken(token, "TOKEN_REFRESHED");
291
+ } catch (error) {
292
+ await this.clearToken();
293
+ throw error;
294
+ }
295
+ }
296
+ /**
297
+ * Subscribe to auth state changes.
298
+ */
299
+ onAuthStateChange(callback) {
300
+ this.stateListeners.add(callback);
301
+ return () => {
302
+ this.stateListeners.delete(callback);
303
+ };
304
+ }
305
+ emitState(event, token) {
306
+ for (const listener of this.stateListeners) {
307
+ listener(event, token);
308
+ }
309
+ }
310
+ /**
311
+ * Register the auth header hook for outgoing HTTP requests.
312
+ */
313
+ registerAuthHook() {
314
+ this.http.onBeforeRequest(async (request) => {
315
+ if (!this.http.isSameOrigin(request.url)) {
316
+ return;
317
+ }
318
+ const token = await this.resolveRequestToken();
319
+ if (!token) {
320
+ return;
321
+ }
322
+ const headers = {
323
+ ...request.init.headers ?? {}
324
+ };
325
+ if (headers.Authorization || headers.authorization) {
326
+ return;
327
+ }
328
+ return {
329
+ ...request,
330
+ init: {
331
+ ...request.init,
332
+ headers: {
333
+ ...headers,
334
+ Authorization: `Bearer ${token}`
335
+ }
336
+ }
337
+ };
338
+ });
339
+ }
340
+ /**
341
+ * Resolve the bearer token to use for outgoing requests.
342
+ */
343
+ async resolveRequestToken() {
344
+ if (this.currentSession) {
345
+ return this.currentSession;
346
+ }
347
+ if (this.serviceKey) {
348
+ return this.serviceKey;
349
+ }
350
+ return await this.getSession();
351
+ }
352
+ /**
353
+ * Persist a token update and notify listeners.
354
+ */
355
+ async applyToken(token, event) {
356
+ await Promise.resolve(this.storage.set(this.storageKey, token));
357
+ this.currentSession = token;
358
+ this.emitState(event, token);
359
+ return token;
360
+ }
361
+ /**
362
+ * Restore a persisted token from storage when needed.
363
+ */
364
+ async restoreFromStorage() {
365
+ if (this.restorePromise) {
366
+ return this.restorePromise;
367
+ }
368
+ if (this.restoreAttempted) {
369
+ return;
370
+ }
371
+ if (this.serviceKey) {
372
+ this.restoreAttempted = true;
373
+ return;
374
+ }
375
+ this.restorePromise = (async () => {
376
+ const storedToken = await Promise.resolve(
377
+ this.storage.get(this.storageKey)
378
+ );
379
+ if (!storedToken) {
380
+ return;
381
+ }
382
+ let exp = null;
383
+ if (this.refreshEnabled) {
384
+ try {
385
+ exp = getTokenExpiry(storedToken);
386
+ } catch {
387
+ this.currentSession = null;
388
+ await Promise.resolve(this.storage.delete(this.storageKey));
389
+ return;
390
+ }
391
+ }
392
+ const hadSession = this.currentSession !== null;
393
+ this.currentSession = storedToken;
394
+ if (!hadSession) {
395
+ this.emitState("SIGNED_IN", storedToken);
396
+ }
397
+ if (this.refreshEnabled && exp !== null) {
398
+ const now = Math.floor(Date.now() / 1e3);
399
+ if (exp - now <= this.refreshThresholdSeconds) {
400
+ try {
401
+ await this.refreshToken();
402
+ } catch {
403
+ }
404
+ }
405
+ }
406
+ })();
407
+ try {
408
+ await this.restorePromise;
409
+ } finally {
410
+ this.restorePromise = null;
411
+ this.restoreAttempted = true;
412
+ }
413
+ }
414
+ /**
415
+ * Determine if the client is currently using the service key.
416
+ */
417
+ isUsingServiceKey() {
418
+ return !!this.serviceKey && !this.currentSession;
419
+ }
420
+ /**
421
+ * Resolve the auth token from a response payload and request metadata.
422
+ */
423
+ resolveTokenFromResponse(response, metadata) {
424
+ if (response.error || response.error_description) {
425
+ const message = response.error_description ?? response.error ?? "Authentication failed";
426
+ throw createSdkError(message, {
427
+ request: metadata.request,
428
+ response: metadata.response,
429
+ userMessage: message
430
+ });
431
+ }
432
+ const token = response.access_token ?? response.token;
433
+ if (!token) {
434
+ throw createSdkError("Missing access token in response", {
435
+ request: metadata.request,
436
+ response: metadata.response
437
+ });
438
+ }
439
+ return token;
440
+ }
441
+ assertTokenNotExpired(token) {
442
+ const exp = getTokenExpiry(token);
443
+ if (exp === null) {
444
+ return;
445
+ }
446
+ const now = Math.floor(Date.now() / 1e3);
447
+ if (exp < now) {
448
+ throw createSdkError("Token has expired");
449
+ }
450
+ }
451
+ };
452
+ // Annotate the CommonJS export names for ESM import in node:
453
+ 0 && (module.exports = {
454
+ AuthClient
455
+ });
456
+ //# sourceMappingURL=auth.js.map