@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.mjs CHANGED
@@ -78,7 +78,8 @@ function extractClientContext(request) {
78
78
  ip,
79
79
  userAgent: getHeader("user-agent") || void 0,
80
80
  deviceFingerprint: getHeader("x-device-fingerprint") || void 0,
81
- referrer: getHeader("referer") || void 0
81
+ referrer: getHeader("referer") || void 0,
82
+ anonymousId: getHeader("x-anonymous-id") || void 0
82
83
  };
83
84
  }
84
85
  function buildClientContextHeaders(context) {
@@ -91,23 +92,106 @@ function buildClientContextHeaders(context) {
91
92
  if (context.userAgent) headers["X-Client-User-Agent"] = context.userAgent;
92
93
  if (context.deviceFingerprint) headers["X-Client-Device-Fingerprint"] = context.deviceFingerprint;
93
94
  if (context.referrer) headers["X-Client-Referrer"] = context.referrer;
95
+ if (context.anonymousId) headers["x-anonymous-id"] = context.anonymousId;
94
96
  return headers;
95
97
  }
96
98
 
99
+ // src/anonymous-id.ts
100
+ var STORAGE_KEYS = {
101
+ SESSION: "scalemule_session",
102
+ USER_ID: "scalemule_user_id",
103
+ WORKSPACE_ID: "scalemule_workspace_id",
104
+ ANONYMOUS_ID: "scalemule_anonymous_id",
105
+ SESSION_POOL: "scalemule_session_pool",
106
+ ACTIVE_ACCOUNT: "scalemule_active_account",
107
+ KNOWN_ACCOUNTS: "scalemule_known_accounts",
108
+ OFFLINE_QUEUE: "scalemule_offline_queue"
109
+ };
110
+ var LEGACY_ANONYMOUS_ID_KEYS = ["sm_anonymous_id"];
111
+ function generateAnonymousId() {
112
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
113
+ return crypto.randomUUID();
114
+ }
115
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
116
+ const r = Math.random() * 16 | 0;
117
+ const v = c === "x" ? r : r & 3 | 8;
118
+ return v.toString(16);
119
+ });
120
+ }
121
+ async function readAnonymousId(storage) {
122
+ const canonical = await storage.getItem(STORAGE_KEYS.ANONYMOUS_ID);
123
+ if (canonical) return canonical;
124
+ for (const legacyKey of LEGACY_ANONYMOUS_ID_KEYS) {
125
+ const legacy = await storage.getItem(legacyKey);
126
+ if (legacy) return legacy;
127
+ }
128
+ return null;
129
+ }
130
+ var inFlight = /* @__PURE__ */ new WeakMap();
131
+ function ensureAnonymousId(storage) {
132
+ const existing = inFlight.get(storage);
133
+ if (existing) return existing;
134
+ const promise = (async () => {
135
+ try {
136
+ return await resolveAnonymousId(storage);
137
+ } finally {
138
+ inFlight.delete(storage);
139
+ }
140
+ })();
141
+ inFlight.set(storage, promise);
142
+ return promise;
143
+ }
144
+ async function resolveAnonymousId(storage) {
145
+ const canonical = await storage.getItem(STORAGE_KEYS.ANONYMOUS_ID);
146
+ if (canonical) {
147
+ for (const legacyKey of LEGACY_ANONYMOUS_ID_KEYS) {
148
+ const legacy = await storage.getItem(legacyKey);
149
+ if (legacy !== canonical) {
150
+ try {
151
+ await storage.setItem(legacyKey, canonical);
152
+ } catch {
153
+ }
154
+ }
155
+ }
156
+ return canonical;
157
+ }
158
+ for (const legacyKey of LEGACY_ANONYMOUS_ID_KEYS) {
159
+ const legacy = await storage.getItem(legacyKey);
160
+ if (legacy) {
161
+ try {
162
+ await storage.setItem(STORAGE_KEYS.ANONYMOUS_ID, legacy);
163
+ } catch {
164
+ }
165
+ return legacy;
166
+ }
167
+ }
168
+ const fresh = generateAnonymousId();
169
+ try {
170
+ await storage.setItem(STORAGE_KEYS.ANONYMOUS_ID, fresh);
171
+ } catch {
172
+ }
173
+ for (const legacyKey of LEGACY_ANONYMOUS_ID_KEYS) {
174
+ try {
175
+ await storage.setItem(legacyKey, fresh);
176
+ } catch {
177
+ }
178
+ }
179
+ return fresh;
180
+ }
181
+
97
182
  // src/client.ts
98
183
  var SDK_VERSION = "0.0.1";
99
184
  var DEFAULT_TIMEOUT = 3e4;
100
185
  var DEFAULT_MAX_RETRIES = 2;
101
186
  var DEFAULT_BACKOFF_MS = 300;
102
187
  var MAX_BACKOFF_MS = 3e4;
103
- var SESSION_STORAGE_KEY = "scalemule_session";
104
- var USER_ID_STORAGE_KEY = "scalemule_user_id";
105
- var OFFLINE_QUEUE_KEY = "scalemule_offline_queue";
106
- var WORKSPACE_STORAGE_KEY = "scalemule_workspace_id";
107
- var ANONYMOUS_ID_STORAGE_KEY = "scalemule_anonymous_id";
108
- var SESSION_POOL_KEY = "scalemule_session_pool";
109
- var ACTIVE_ACCOUNT_KEY = "scalemule_active_account";
110
- var KNOWN_ACCOUNTS_KEY = "scalemule_known_accounts";
188
+ var SESSION_STORAGE_KEY = STORAGE_KEYS.SESSION;
189
+ var USER_ID_STORAGE_KEY = STORAGE_KEYS.USER_ID;
190
+ var OFFLINE_QUEUE_KEY = STORAGE_KEYS.OFFLINE_QUEUE;
191
+ var WORKSPACE_STORAGE_KEY = STORAGE_KEYS.WORKSPACE_ID;
192
+ var SESSION_POOL_KEY = STORAGE_KEYS.SESSION_POOL;
193
+ var ACTIVE_ACCOUNT_KEY = STORAGE_KEYS.ACTIVE_ACCOUNT;
194
+ var KNOWN_ACCOUNTS_KEY = STORAGE_KEYS.KNOWN_ACCOUNTS;
111
195
  var GATEWAY_URLS = {
112
196
  dev: "https://api-dev.scalemule.com",
113
197
  prod: "https://api.scalemule.com"
@@ -350,6 +434,11 @@ var ScaleMuleClient = class {
350
434
  this.offlineQueue = null;
351
435
  this.workspaceId = null;
352
436
  this.anonymousId = null;
437
+ // Per-client single-flight for the lazy mint. The shared helper has its
438
+ // own WeakMap-keyed guard against concurrent callers; this field caches
439
+ // the resolved value at the instance level so subsequent calls (including
440
+ // those during request bursts) don't even await the helper.
441
+ this.anonymousIdPromise = null;
353
442
  this.sessionPool = /* @__PURE__ */ new Map();
354
443
  this.knownAccounts = /* @__PURE__ */ new Map();
355
444
  this.refreshPromise = null;
@@ -385,12 +474,7 @@ var ScaleMuleClient = class {
385
474
  if (userId) this.userId = userId;
386
475
  const wsId = await this.storage.getItem(WORKSPACE_STORAGE_KEY);
387
476
  if (wsId) this.workspaceId = wsId;
388
- let anonId = await this.storage.getItem(ANONYMOUS_ID_STORAGE_KEY);
389
- if (!anonId) {
390
- anonId = crypto.randomUUID();
391
- await this.storage.setItem(ANONYMOUS_ID_STORAGE_KEY, anonId);
392
- }
393
- this.anonymousId = anonId;
477
+ await this.ensureAnonymousId();
394
478
  if (this.multiSessionEnabled) {
395
479
  const poolJson = await this.storage.getItem(SESSION_POOL_KEY);
396
480
  if (poolJson) {
@@ -442,7 +526,7 @@ var ScaleMuleClient = class {
442
526
  "[ScaleMule] Initialized, session:",
443
527
  !!this.sessionToken,
444
528
  "anonymousId:",
445
- anonId,
529
+ this.anonymousId,
446
530
  "poolSize:",
447
531
  this.sessionPool.size,
448
532
  "knownAccounts:",
@@ -496,9 +580,38 @@ var ScaleMuleClient = class {
496
580
  isAuthenticated() {
497
581
  return this.sessionToken !== null;
498
582
  }
583
+ /**
584
+ * Sync accessor for the cached anonymous ID. Returns `null` until either
585
+ * `initialize()` has run or `ensureAnonymousId()` has been awaited at
586
+ * least once. Use `ensureAnonymousId()` if you need a guaranteed value.
587
+ */
499
588
  getAnonymousId() {
500
589
  return this.anonymousId;
501
590
  }
591
+ /**
592
+ * Lazy-mint or cache-then-return the anonymous ID.
593
+ *
594
+ * Always returns a usable string. Callers that need to attach the
595
+ * `x-anonymous-id` header before `initialize()` has completed (rare —
596
+ * usually only the very first unauthenticated request from a freshly
597
+ * constructed client) should `await` this rather than relying on
598
+ * `getAnonymousId()`.
599
+ *
600
+ * Concurrent callers receive the same in-flight promise — no risk of
601
+ * two simultaneous first requests minting two distinct IDs.
602
+ */
603
+ async ensureAnonymousId() {
604
+ if (this.anonymousId) return this.anonymousId;
605
+ if (!this.anonymousIdPromise) {
606
+ this.anonymousIdPromise = ensureAnonymousId(this.storage).then((id) => {
607
+ this.anonymousId = id;
608
+ return id;
609
+ }).finally(() => {
610
+ this.anonymousIdPromise = null;
611
+ });
612
+ }
613
+ return this.anonymousIdPromise;
614
+ }
502
615
  isMultiSessionEnabled() {
503
616
  return this.multiSessionEnabled;
504
617
  }
@@ -674,8 +787,16 @@ var ScaleMuleClient = class {
674
787
  if (this.workspaceId) {
675
788
  headers["x-sm-workspace-id"] = this.workspaceId;
676
789
  }
677
- if (!this.sessionToken && this.anonymousId) {
678
- headers["x-anonymous-id"] = this.anonymousId;
790
+ if (!init.skipAuth && !this.sessionToken) {
791
+ if (!this.anonymousId) {
792
+ try {
793
+ await this.ensureAnonymousId();
794
+ } catch {
795
+ }
796
+ }
797
+ if (this.anonymousId) {
798
+ headers["x-anonymous-id"] = this.anonymousId;
799
+ }
679
800
  }
680
801
  if (attempt > 0 && (method === "POST" || method === "PUT" || method === "PATCH")) {
681
802
  if (!idempotencyKey) {
@@ -4644,6 +4765,118 @@ var SocialService = class extends ServiceModule {
4644
4765
  }
4645
4766
  };
4646
4767
 
4768
+ // src/services/social-policy.ts
4769
+ var SocialPolicyService = class extends ServiceModule {
4770
+ constructor() {
4771
+ super(...arguments);
4772
+ this.basePath = "/v1/social-policy";
4773
+ }
4774
+ decide(request, options) {
4775
+ return this.post("/decision", withDefaultContext(request), options);
4776
+ }
4777
+ batchDecide(request, options) {
4778
+ return this.post("/decisions/batch", request, options);
4779
+ }
4780
+ decideAction(action, actor, target, options) {
4781
+ return this.decide(
4782
+ {
4783
+ app_key: options?.appKey,
4784
+ account_id: options?.accountId,
4785
+ policy_pack: options?.policyPack,
4786
+ actor,
4787
+ target,
4788
+ action,
4789
+ context: options?.context,
4790
+ metadata: options?.metadata
4791
+ },
4792
+ options?.requestOptions
4793
+ );
4794
+ }
4795
+ decideFollow(actor, target, options) {
4796
+ return this.decideAction("follow", actor, target, options);
4797
+ }
4798
+ decideDirectMessage(actor, target, options) {
4799
+ return this.decideAction("message_direct", actor, target, options);
4800
+ }
4801
+ decideContactRequest(actor, target, options) {
4802
+ return this.decideAction("connect_request", actor, target, options);
4803
+ }
4804
+ getSettings(identityId, options) {
4805
+ return this._get(`/settings/${identityId}`, options);
4806
+ }
4807
+ updateSettings(identityId, request, options) {
4808
+ return this.patch(`/settings/${identityId}`, request, options);
4809
+ }
4810
+ createRelationship(request, options) {
4811
+ return this.post("/relationships", request, options);
4812
+ }
4813
+ listRelationships(params, options) {
4814
+ return this._get(this.withQuery("/relationships", toQueryParams(params)), options);
4815
+ }
4816
+ deleteRelationship(relationshipId, options) {
4817
+ return this.del(`/relationships/${relationshipId}`, options);
4818
+ }
4819
+ createContactRequest(request, options) {
4820
+ return this.post("/contact-requests", request, options);
4821
+ }
4822
+ listContactRequestInbox(params, options) {
4823
+ return this._get(
4824
+ this.withQuery("/contact-requests/inbox", toQueryParams(params)),
4825
+ options
4826
+ );
4827
+ }
4828
+ listContactRequestSent(params, options) {
4829
+ return this._get(
4830
+ this.withQuery("/contact-requests/sent", toQueryParams(params)),
4831
+ options
4832
+ );
4833
+ }
4834
+ acceptContactRequest(requestId, options) {
4835
+ return this.post(`/contact-requests/${requestId}/accept`, void 0, options);
4836
+ }
4837
+ ignoreContactRequest(requestId, options) {
4838
+ return this.post(`/contact-requests/${requestId}/ignore`, void 0, options);
4839
+ }
4840
+ declineContactRequest(requestId, options) {
4841
+ return this.post(`/contact-requests/${requestId}/decline`, void 0, options);
4842
+ }
4843
+ reportContactRequest(requestId, options) {
4844
+ return this.post(`/contact-requests/${requestId}/report`, void 0, options);
4845
+ }
4846
+ block(request, options) {
4847
+ return this.post("/block", request, options);
4848
+ }
4849
+ unblock(request, options) {
4850
+ return this.post("/unblock", request, options);
4851
+ }
4852
+ mute(request, options) {
4853
+ return this.post("/mute", request, options);
4854
+ }
4855
+ unmute(request, options) {
4856
+ return this.post("/unmute", request, options);
4857
+ }
4858
+ report(request, options) {
4859
+ return this.post("/report", request, options);
4860
+ }
4861
+ listPolicyPacks(options) {
4862
+ return this._get("/policy-packs", options);
4863
+ }
4864
+ };
4865
+ function withDefaultContext(request) {
4866
+ return {
4867
+ ...request,
4868
+ context: request.context ?? {}
4869
+ };
4870
+ }
4871
+ function toQueryParams(params) {
4872
+ if (!params) return void 0;
4873
+ const query = {};
4874
+ for (const [key, value] of Object.entries(params)) {
4875
+ query[key] = value;
4876
+ }
4877
+ return query;
4878
+ }
4879
+
4647
4880
  // src/services/referrals.ts
4648
4881
  var ReferralsService = class extends ServiceModule {
4649
4882
  constructor() {
@@ -7960,6 +8193,7 @@ var ScaleMule = class {
7960
8193
  this.chat = new ChatService(this._client);
7961
8194
  this.conference = new ConferenceService(this._client);
7962
8195
  this.social = new SocialService(this._client);
8196
+ this.socialPolicy = new SocialPolicyService(this._client);
7963
8197
  this.referrals = new ReferralsService(this._client);
7964
8198
  this.billing = new BillingService(this._client);
7965
8199
  this.analytics = new AnalyticsService(this._client);
@@ -8146,6 +8380,7 @@ export {
8146
8380
  FunctionsService,
8147
8381
  GraphService,
8148
8382
  IdentityService,
8383
+ LEGACY_ANONYMOUS_ID_KEYS,
8149
8384
  LeaderboardService,
8150
8385
  ListingsService,
8151
8386
  LoggerService,
@@ -8160,11 +8395,13 @@ export {
8160
8395
  QueueService,
8161
8396
  RealtimeService,
8162
8397
  ReferralsService,
8398
+ STORAGE_KEYS,
8163
8399
  ScaleMule,
8164
8400
  ScaleMuleClient,
8165
8401
  SchedulerService,
8166
8402
  SearchService,
8167
8403
  ServiceModule,
8404
+ SocialPolicyService,
8168
8405
  SocialService,
8169
8406
  StorageService,
8170
8407
  TeamsService,
@@ -8185,9 +8422,11 @@ export {
8185
8422
  index_default as default,
8186
8423
  detectCountryFromE164,
8187
8424
  detectNetworkClass,
8425
+ ensureAnonymousId,
8188
8426
  extractClientContext,
8189
8427
  findPhoneCountryByCode,
8190
8428
  findPhoneCountryByDialCode,
8429
+ generateAnonymousId,
8191
8430
  generateUploadSessionId,
8192
8431
  getMeasuredBandwidthMbps,
8193
8432
  getPartRange,
@@ -8195,6 +8434,7 @@ export {
8195
8434
  isValidE164Phone,
8196
8435
  normalizeAndValidatePhone,
8197
8436
  normalizePhoneNumber,
8437
+ readAnonymousId,
8198
8438
  resolveStrategy,
8199
8439
  uploadMultipartToS3,
8200
8440
  uploadSingleToS3,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scalemule/sdk",
3
- "version": "0.0.57",
3
+ "version": "0.0.59",
4
4
  "description": "Official TypeScript/JavaScript SDK for ScaleMule Backend-as-a-Service",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -76,6 +76,7 @@
76
76
  "devDependencies": {
77
77
  "@scalemule/ui": ">=0.0.1",
78
78
  "@types/node": "^20.10.0",
79
+ "@types/semver": "7.7.1",
79
80
  "@typescript-eslint/eslint-plugin": "^6.13.0",
80
81
  "@typescript-eslint/parser": "^6.13.0",
81
82
  "@vitest/coverage-v8": "^1.0.0",