@scalemule/sdk 0.0.57 → 0.0.59

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.
package/dist/index.js CHANGED
@@ -319,6 +319,7 @@ __export(index_exports, {
319
319
  FunctionsService: () => FunctionsService,
320
320
  GraphService: () => GraphService,
321
321
  IdentityService: () => IdentityService,
322
+ LEGACY_ANONYMOUS_ID_KEYS: () => LEGACY_ANONYMOUS_ID_KEYS,
322
323
  LeaderboardService: () => LeaderboardService,
323
324
  ListingsService: () => ListingsService,
324
325
  LoggerService: () => LoggerService,
@@ -333,11 +334,13 @@ __export(index_exports, {
333
334
  QueueService: () => QueueService,
334
335
  RealtimeService: () => RealtimeService,
335
336
  ReferralsService: () => ReferralsService,
337
+ STORAGE_KEYS: () => STORAGE_KEYS,
336
338
  ScaleMule: () => ScaleMule,
337
339
  ScaleMuleClient: () => ScaleMuleClient,
338
340
  SchedulerService: () => SchedulerService,
339
341
  SearchService: () => SearchService,
340
342
  ServiceModule: () => ServiceModule,
343
+ SocialPolicyService: () => SocialPolicyService,
341
344
  SocialService: () => SocialService,
342
345
  StorageService: () => StorageService,
343
346
  TeamsService: () => TeamsService,
@@ -358,9 +361,11 @@ __export(index_exports, {
358
361
  default: () => index_default,
359
362
  detectCountryFromE164: () => detectCountryFromE164,
360
363
  detectNetworkClass: () => detectNetworkClass,
364
+ ensureAnonymousId: () => ensureAnonymousId,
361
365
  extractClientContext: () => extractClientContext,
362
366
  findPhoneCountryByCode: () => findPhoneCountryByCode,
363
367
  findPhoneCountryByDialCode: () => findPhoneCountryByDialCode,
368
+ generateAnonymousId: () => generateAnonymousId,
364
369
  generateUploadSessionId: () => generateUploadSessionId,
365
370
  getMeasuredBandwidthMbps: () => getMeasuredBandwidthMbps,
366
371
  getPartRange: () => getPartRange,
@@ -368,6 +373,7 @@ __export(index_exports, {
368
373
  isValidE164Phone: () => isValidE164Phone,
369
374
  normalizeAndValidatePhone: () => normalizeAndValidatePhone,
370
375
  normalizePhoneNumber: () => normalizePhoneNumber,
376
+ readAnonymousId: () => readAnonymousId,
371
377
  resolveStrategy: () => resolveStrategy,
372
378
  uploadMultipartToS3: () => uploadMultipartToS3,
373
379
  uploadSingleToS3: () => uploadSingleToS3,
@@ -451,7 +457,8 @@ function extractClientContext(request) {
451
457
  ip,
452
458
  userAgent: getHeader("user-agent") || void 0,
453
459
  deviceFingerprint: getHeader("x-device-fingerprint") || void 0,
454
- referrer: getHeader("referer") || void 0
460
+ referrer: getHeader("referer") || void 0,
461
+ anonymousId: getHeader("x-anonymous-id") || void 0
455
462
  };
456
463
  }
457
464
  function buildClientContextHeaders(context) {
@@ -464,23 +471,106 @@ function buildClientContextHeaders(context) {
464
471
  if (context.userAgent) headers["X-Client-User-Agent"] = context.userAgent;
465
472
  if (context.deviceFingerprint) headers["X-Client-Device-Fingerprint"] = context.deviceFingerprint;
466
473
  if (context.referrer) headers["X-Client-Referrer"] = context.referrer;
474
+ if (context.anonymousId) headers["x-anonymous-id"] = context.anonymousId;
467
475
  return headers;
468
476
  }
469
477
 
478
+ // src/anonymous-id.ts
479
+ var STORAGE_KEYS = {
480
+ SESSION: "scalemule_session",
481
+ USER_ID: "scalemule_user_id",
482
+ WORKSPACE_ID: "scalemule_workspace_id",
483
+ ANONYMOUS_ID: "scalemule_anonymous_id",
484
+ SESSION_POOL: "scalemule_session_pool",
485
+ ACTIVE_ACCOUNT: "scalemule_active_account",
486
+ KNOWN_ACCOUNTS: "scalemule_known_accounts",
487
+ OFFLINE_QUEUE: "scalemule_offline_queue"
488
+ };
489
+ var LEGACY_ANONYMOUS_ID_KEYS = ["sm_anonymous_id"];
490
+ function generateAnonymousId() {
491
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
492
+ return crypto.randomUUID();
493
+ }
494
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
495
+ const r = Math.random() * 16 | 0;
496
+ const v = c === "x" ? r : r & 3 | 8;
497
+ return v.toString(16);
498
+ });
499
+ }
500
+ async function readAnonymousId(storage) {
501
+ const canonical = await storage.getItem(STORAGE_KEYS.ANONYMOUS_ID);
502
+ if (canonical) return canonical;
503
+ for (const legacyKey of LEGACY_ANONYMOUS_ID_KEYS) {
504
+ const legacy = await storage.getItem(legacyKey);
505
+ if (legacy) return legacy;
506
+ }
507
+ return null;
508
+ }
509
+ var inFlight = /* @__PURE__ */ new WeakMap();
510
+ function ensureAnonymousId(storage) {
511
+ const existing = inFlight.get(storage);
512
+ if (existing) return existing;
513
+ const promise = (async () => {
514
+ try {
515
+ return await resolveAnonymousId(storage);
516
+ } finally {
517
+ inFlight.delete(storage);
518
+ }
519
+ })();
520
+ inFlight.set(storage, promise);
521
+ return promise;
522
+ }
523
+ async function resolveAnonymousId(storage) {
524
+ const canonical = await storage.getItem(STORAGE_KEYS.ANONYMOUS_ID);
525
+ if (canonical) {
526
+ for (const legacyKey of LEGACY_ANONYMOUS_ID_KEYS) {
527
+ const legacy = await storage.getItem(legacyKey);
528
+ if (legacy !== canonical) {
529
+ try {
530
+ await storage.setItem(legacyKey, canonical);
531
+ } catch {
532
+ }
533
+ }
534
+ }
535
+ return canonical;
536
+ }
537
+ for (const legacyKey of LEGACY_ANONYMOUS_ID_KEYS) {
538
+ const legacy = await storage.getItem(legacyKey);
539
+ if (legacy) {
540
+ try {
541
+ await storage.setItem(STORAGE_KEYS.ANONYMOUS_ID, legacy);
542
+ } catch {
543
+ }
544
+ return legacy;
545
+ }
546
+ }
547
+ const fresh = generateAnonymousId();
548
+ try {
549
+ await storage.setItem(STORAGE_KEYS.ANONYMOUS_ID, fresh);
550
+ } catch {
551
+ }
552
+ for (const legacyKey of LEGACY_ANONYMOUS_ID_KEYS) {
553
+ try {
554
+ await storage.setItem(legacyKey, fresh);
555
+ } catch {
556
+ }
557
+ }
558
+ return fresh;
559
+ }
560
+
470
561
  // src/client.ts
471
562
  var SDK_VERSION = "0.0.1";
472
563
  var DEFAULT_TIMEOUT = 3e4;
473
564
  var DEFAULT_MAX_RETRIES = 2;
474
565
  var DEFAULT_BACKOFF_MS = 300;
475
566
  var MAX_BACKOFF_MS = 3e4;
476
- var SESSION_STORAGE_KEY = "scalemule_session";
477
- var USER_ID_STORAGE_KEY = "scalemule_user_id";
478
- var OFFLINE_QUEUE_KEY = "scalemule_offline_queue";
479
- var WORKSPACE_STORAGE_KEY = "scalemule_workspace_id";
480
- var ANONYMOUS_ID_STORAGE_KEY = "scalemule_anonymous_id";
481
- var SESSION_POOL_KEY = "scalemule_session_pool";
482
- var ACTIVE_ACCOUNT_KEY = "scalemule_active_account";
483
- var KNOWN_ACCOUNTS_KEY = "scalemule_known_accounts";
567
+ var SESSION_STORAGE_KEY = STORAGE_KEYS.SESSION;
568
+ var USER_ID_STORAGE_KEY = STORAGE_KEYS.USER_ID;
569
+ var OFFLINE_QUEUE_KEY = STORAGE_KEYS.OFFLINE_QUEUE;
570
+ var WORKSPACE_STORAGE_KEY = STORAGE_KEYS.WORKSPACE_ID;
571
+ var SESSION_POOL_KEY = STORAGE_KEYS.SESSION_POOL;
572
+ var ACTIVE_ACCOUNT_KEY = STORAGE_KEYS.ACTIVE_ACCOUNT;
573
+ var KNOWN_ACCOUNTS_KEY = STORAGE_KEYS.KNOWN_ACCOUNTS;
484
574
  var GATEWAY_URLS = {
485
575
  dev: "https://api-dev.scalemule.com",
486
576
  prod: "https://api.scalemule.com"
@@ -723,6 +813,11 @@ var ScaleMuleClient = class {
723
813
  this.offlineQueue = null;
724
814
  this.workspaceId = null;
725
815
  this.anonymousId = null;
816
+ // Per-client single-flight for the lazy mint. The shared helper has its
817
+ // own WeakMap-keyed guard against concurrent callers; this field caches
818
+ // the resolved value at the instance level so subsequent calls (including
819
+ // those during request bursts) don't even await the helper.
820
+ this.anonymousIdPromise = null;
726
821
  this.sessionPool = /* @__PURE__ */ new Map();
727
822
  this.knownAccounts = /* @__PURE__ */ new Map();
728
823
  this.refreshPromise = null;
@@ -758,12 +853,7 @@ var ScaleMuleClient = class {
758
853
  if (userId) this.userId = userId;
759
854
  const wsId = await this.storage.getItem(WORKSPACE_STORAGE_KEY);
760
855
  if (wsId) this.workspaceId = wsId;
761
- let anonId = await this.storage.getItem(ANONYMOUS_ID_STORAGE_KEY);
762
- if (!anonId) {
763
- anonId = crypto.randomUUID();
764
- await this.storage.setItem(ANONYMOUS_ID_STORAGE_KEY, anonId);
765
- }
766
- this.anonymousId = anonId;
856
+ await this.ensureAnonymousId();
767
857
  if (this.multiSessionEnabled) {
768
858
  const poolJson = await this.storage.getItem(SESSION_POOL_KEY);
769
859
  if (poolJson) {
@@ -815,7 +905,7 @@ var ScaleMuleClient = class {
815
905
  "[ScaleMule] Initialized, session:",
816
906
  !!this.sessionToken,
817
907
  "anonymousId:",
818
- anonId,
908
+ this.anonymousId,
819
909
  "poolSize:",
820
910
  this.sessionPool.size,
821
911
  "knownAccounts:",
@@ -869,9 +959,38 @@ var ScaleMuleClient = class {
869
959
  isAuthenticated() {
870
960
  return this.sessionToken !== null;
871
961
  }
962
+ /**
963
+ * Sync accessor for the cached anonymous ID. Returns `null` until either
964
+ * `initialize()` has run or `ensureAnonymousId()` has been awaited at
965
+ * least once. Use `ensureAnonymousId()` if you need a guaranteed value.
966
+ */
872
967
  getAnonymousId() {
873
968
  return this.anonymousId;
874
969
  }
970
+ /**
971
+ * Lazy-mint or cache-then-return the anonymous ID.
972
+ *
973
+ * Always returns a usable string. Callers that need to attach the
974
+ * `x-anonymous-id` header before `initialize()` has completed (rare —
975
+ * usually only the very first unauthenticated request from a freshly
976
+ * constructed client) should `await` this rather than relying on
977
+ * `getAnonymousId()`.
978
+ *
979
+ * Concurrent callers receive the same in-flight promise — no risk of
980
+ * two simultaneous first requests minting two distinct IDs.
981
+ */
982
+ async ensureAnonymousId() {
983
+ if (this.anonymousId) return this.anonymousId;
984
+ if (!this.anonymousIdPromise) {
985
+ this.anonymousIdPromise = ensureAnonymousId(this.storage).then((id) => {
986
+ this.anonymousId = id;
987
+ return id;
988
+ }).finally(() => {
989
+ this.anonymousIdPromise = null;
990
+ });
991
+ }
992
+ return this.anonymousIdPromise;
993
+ }
875
994
  isMultiSessionEnabled() {
876
995
  return this.multiSessionEnabled;
877
996
  }
@@ -1047,8 +1166,16 @@ var ScaleMuleClient = class {
1047
1166
  if (this.workspaceId) {
1048
1167
  headers["x-sm-workspace-id"] = this.workspaceId;
1049
1168
  }
1050
- if (!this.sessionToken && this.anonymousId) {
1051
- headers["x-anonymous-id"] = this.anonymousId;
1169
+ if (!init.skipAuth && !this.sessionToken) {
1170
+ if (!this.anonymousId) {
1171
+ try {
1172
+ await this.ensureAnonymousId();
1173
+ } catch {
1174
+ }
1175
+ }
1176
+ if (this.anonymousId) {
1177
+ headers["x-anonymous-id"] = this.anonymousId;
1178
+ }
1052
1179
  }
1053
1180
  if (attempt > 0 && (method === "POST" || method === "PUT" || method === "PATCH")) {
1054
1181
  if (!idempotencyKey) {
@@ -5020,6 +5147,118 @@ var SocialService = class extends ServiceModule {
5020
5147
  }
5021
5148
  };
5022
5149
 
5150
+ // src/services/social-policy.ts
5151
+ var SocialPolicyService = class extends ServiceModule {
5152
+ constructor() {
5153
+ super(...arguments);
5154
+ this.basePath = "/v1/social-policy";
5155
+ }
5156
+ decide(request, options) {
5157
+ return this.post("/decision", withDefaultContext(request), options);
5158
+ }
5159
+ batchDecide(request, options) {
5160
+ return this.post("/decisions/batch", request, options);
5161
+ }
5162
+ decideAction(action, actor, target, options) {
5163
+ return this.decide(
5164
+ {
5165
+ app_key: options?.appKey,
5166
+ account_id: options?.accountId,
5167
+ policy_pack: options?.policyPack,
5168
+ actor,
5169
+ target,
5170
+ action,
5171
+ context: options?.context,
5172
+ metadata: options?.metadata
5173
+ },
5174
+ options?.requestOptions
5175
+ );
5176
+ }
5177
+ decideFollow(actor, target, options) {
5178
+ return this.decideAction("follow", actor, target, options);
5179
+ }
5180
+ decideDirectMessage(actor, target, options) {
5181
+ return this.decideAction("message_direct", actor, target, options);
5182
+ }
5183
+ decideContactRequest(actor, target, options) {
5184
+ return this.decideAction("connect_request", actor, target, options);
5185
+ }
5186
+ getSettings(identityId, options) {
5187
+ return this._get(`/settings/${identityId}`, options);
5188
+ }
5189
+ updateSettings(identityId, request, options) {
5190
+ return this.patch(`/settings/${identityId}`, request, options);
5191
+ }
5192
+ createRelationship(request, options) {
5193
+ return this.post("/relationships", request, options);
5194
+ }
5195
+ listRelationships(params, options) {
5196
+ return this._get(this.withQuery("/relationships", toQueryParams(params)), options);
5197
+ }
5198
+ deleteRelationship(relationshipId, options) {
5199
+ return this.del(`/relationships/${relationshipId}`, options);
5200
+ }
5201
+ createContactRequest(request, options) {
5202
+ return this.post("/contact-requests", request, options);
5203
+ }
5204
+ listContactRequestInbox(params, options) {
5205
+ return this._get(
5206
+ this.withQuery("/contact-requests/inbox", toQueryParams(params)),
5207
+ options
5208
+ );
5209
+ }
5210
+ listContactRequestSent(params, options) {
5211
+ return this._get(
5212
+ this.withQuery("/contact-requests/sent", toQueryParams(params)),
5213
+ options
5214
+ );
5215
+ }
5216
+ acceptContactRequest(requestId, options) {
5217
+ return this.post(`/contact-requests/${requestId}/accept`, void 0, options);
5218
+ }
5219
+ ignoreContactRequest(requestId, options) {
5220
+ return this.post(`/contact-requests/${requestId}/ignore`, void 0, options);
5221
+ }
5222
+ declineContactRequest(requestId, options) {
5223
+ return this.post(`/contact-requests/${requestId}/decline`, void 0, options);
5224
+ }
5225
+ reportContactRequest(requestId, options) {
5226
+ return this.post(`/contact-requests/${requestId}/report`, void 0, options);
5227
+ }
5228
+ block(request, options) {
5229
+ return this.post("/block", request, options);
5230
+ }
5231
+ unblock(request, options) {
5232
+ return this.post("/unblock", request, options);
5233
+ }
5234
+ mute(request, options) {
5235
+ return this.post("/mute", request, options);
5236
+ }
5237
+ unmute(request, options) {
5238
+ return this.post("/unmute", request, options);
5239
+ }
5240
+ report(request, options) {
5241
+ return this.post("/report", request, options);
5242
+ }
5243
+ listPolicyPacks(options) {
5244
+ return this._get("/policy-packs", options);
5245
+ }
5246
+ };
5247
+ function withDefaultContext(request) {
5248
+ return {
5249
+ ...request,
5250
+ context: request.context ?? {}
5251
+ };
5252
+ }
5253
+ function toQueryParams(params) {
5254
+ if (!params) return void 0;
5255
+ const query = {};
5256
+ for (const [key, value] of Object.entries(params)) {
5257
+ query[key] = value;
5258
+ }
5259
+ return query;
5260
+ }
5261
+
5023
5262
  // src/services/referrals.ts
5024
5263
  var ReferralsService = class extends ServiceModule {
5025
5264
  constructor() {
@@ -8336,6 +8575,7 @@ var ScaleMule = class {
8336
8575
  this.chat = new ChatService(this._client);
8337
8576
  this.conference = new ConferenceService(this._client);
8338
8577
  this.social = new SocialService(this._client);
8578
+ this.socialPolicy = new SocialPolicyService(this._client);
8339
8579
  this.referrals = new ReferralsService(this._client);
8340
8580
  this.billing = new BillingService(this._client);
8341
8581
  this.analytics = new AnalyticsService(this._client);
@@ -8523,6 +8763,7 @@ var index_default = ScaleMule;
8523
8763
  FunctionsService,
8524
8764
  GraphService,
8525
8765
  IdentityService,
8766
+ LEGACY_ANONYMOUS_ID_KEYS,
8526
8767
  LeaderboardService,
8527
8768
  ListingsService,
8528
8769
  LoggerService,
@@ -8537,11 +8778,13 @@ var index_default = ScaleMule;
8537
8778
  QueueService,
8538
8779
  RealtimeService,
8539
8780
  ReferralsService,
8781
+ STORAGE_KEYS,
8540
8782
  ScaleMule,
8541
8783
  ScaleMuleClient,
8542
8784
  SchedulerService,
8543
8785
  SearchService,
8544
8786
  ServiceModule,
8787
+ SocialPolicyService,
8545
8788
  SocialService,
8546
8789
  StorageService,
8547
8790
  TeamsService,
@@ -8561,9 +8804,11 @@ var index_default = ScaleMule;
8561
8804
  createUploadPlan,
8562
8805
  detectCountryFromE164,
8563
8806
  detectNetworkClass,
8807
+ ensureAnonymousId,
8564
8808
  extractClientContext,
8565
8809
  findPhoneCountryByCode,
8566
8810
  findPhoneCountryByDialCode,
8811
+ generateAnonymousId,
8567
8812
  generateUploadSessionId,
8568
8813
  getMeasuredBandwidthMbps,
8569
8814
  getPartRange,
@@ -8571,6 +8816,7 @@ var index_default = ScaleMule;
8571
8816
  isValidE164Phone,
8572
8817
  normalizeAndValidatePhone,
8573
8818
  normalizePhoneNumber,
8819
+ readAnonymousId,
8574
8820
  resolveStrategy,
8575
8821
  uploadMultipartToS3,
8576
8822
  uploadSingleToS3,