@goplus123/core-api 1.0.2 → 1.0.3

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/README.md CHANGED
@@ -239,7 +239,65 @@ apiError.use((error, ctx, next) => {
239
239
 
240
240
  ### fetchGuid()
241
241
 
242
- - 作用:生成一个短 id(playground 用于 WS `protocols`)
242
+ - 作用:生成/读取一个稳定的设备唯一标识(unikey),用于跨请求/跨连接的设备识别
243
+ - 行为:
244
+ - 读取存储:优先从当前 `KeyValueStorage` 读取 `uKey`
245
+ - 解析兼容:支持三种历史值格式(UUID / 32 位 hex / base64(bytes))
246
+ - 缓存写入:若不存在或不可解析,会生成新的 UUID 并写回存储
247
+ - 默认输出:返回 UUID(带 `-`),例如 `xxxxxxxx-xxxx-4xxx-8xxx-xxxxxxxxxxxx`
248
+ - 默认存储策略(见 [`utils/uniqueKey.ts`](file:///d:/working/GX/tsup/packages/api/src/utils/uniqueKey.ts)):
249
+ - Web 环境:使用 `localStorage`
250
+ - 非 Web 或 `localStorage` 不可用:使用进程内 memory storage 兜底(保证同进程稳定)
251
+
252
+ ### createFetchGuid(storage, options?)
253
+
254
+ - 作用:基于指定的 `KeyValueStorage` 创建一个 `fetchGuid()` 函数(推荐方式,便于按平台定制)
255
+ - options:
256
+ - `key?: string`:存储 key,默认 `uKey`
257
+ - `format?: 'uuid' | 'hex' | 'base64'`:返回格式,默认 `'uuid'`
258
+ - `prefix?: string`:输出前缀(用于区分来源端,例如 `'pc_' / 'h5_'`),默认 `''`
259
+
260
+ 示例:
261
+
262
+ ```ts
263
+ import { createFetchGuid, webStorage } from '@goplus123/core-api'
264
+
265
+ export const fetchGuidPc = createFetchGuid(webStorage, { format: 'uuid', prefix: 'pc_' })
266
+ export const fetchGuidH5 = createFetchGuid(webStorage, { format: 'hex', prefix: 'h5_' })
267
+ ```
268
+
269
+ ### setUniqueKeyStorage(adapter)
270
+
271
+ - 作用:设置 `fetchGuid()` 的全局存储适配器(用于 App/小程序等非 Web 环境注入 native storage)
272
+ - 建议:尽量在 `initSDK()` 之前调用,避免同一进程内出现“先生成、后换存储”的多份 id
273
+
274
+ 示例(伪代码):
275
+
276
+ ```ts
277
+ import type { KeyValueStorage } from '@goplus123/core-api'
278
+ import { setUniqueKeyStorage } from '@goplus123/core-api'
279
+
280
+ const appStorage: KeyValueStorage = {
281
+ get(key) {
282
+ return NativeStorage.get(key)
283
+ },
284
+ set(key, val) {
285
+ NativeStorage.set(key, val)
286
+ },
287
+ }
288
+
289
+ setUniqueKeyStorage(appStorage)
290
+ ```
291
+
292
+ ### webStorage
293
+
294
+ - 作用:浏览器端 `KeyValueStorage` 适配器,基于 `window.localStorage`
295
+ - 兼容:SSR/Node 下安全返回 `null`;浏览器隐私模式/禁用存储时会吞掉异常(不抛错)
296
+
297
+ ### KeyValueStorage
298
+
299
+ - 作用:unikey 存储抽象(只要求 `get/set` 两个方法)
300
+ - 定义:[`utils/storage.ts`](file:///d:/working/GX/tsup/packages/api/src/utils/storage.ts)
243
301
 
244
302
  ---
245
303
 
package/dist/index.cjs CHANGED
@@ -22,6 +22,7 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  apiError: () => apiError,
24
24
  callApi: () => requestApi,
25
+ createFetchGuid: () => createFetchGuid,
25
26
  destroySDK: () => destroySDK,
26
27
  fetchGuid: () => fetchGuid,
27
28
  getSDK: () => getSDK,
@@ -29,7 +30,9 @@ __export(index_exports, {
29
30
  initSDK: () => initSDK,
30
31
  notify: () => notify,
31
32
  requestApi: () => requestApi,
32
- requestApiSpec: () => requestApi
33
+ requestApiSpec: () => requestApi,
34
+ setUniqueKeyStorage: () => setUniqueKeyStorage,
35
+ webStorage: () => webStorage
33
36
  });
34
37
  module.exports = __toCommonJS(index_exports);
35
38
 
@@ -429,7 +432,7 @@ var WsClient = class {
429
432
  ...options.reconnect
430
433
  },
431
434
  heartbeat: {
432
- interval: 1e4,
435
+ interval: 3e4,
433
436
  timeout: 3e4,
434
437
  pingType: "ping",
435
438
  pongType: "pong",
@@ -715,6 +718,10 @@ var WsClient = class {
715
718
  this.lastPongTime = Date.now();
716
719
  return;
717
720
  }
721
+ if (this.isAliveAck(msg, payload)) {
722
+ this.lastPongTime = Date.now();
723
+ return;
724
+ }
718
725
  const requestId = msg.requestId ?? (payload && typeof payload === "object" ? payload.requestId ?? payload?.data?.requestId : void 0);
719
726
  const resolved = requestId ? this.tryResolvePending(requestId, type, payload) : false;
720
727
  if (!resolved) {
@@ -730,6 +737,10 @@ var WsClient = class {
730
737
  this.lastPongTime = Date.now();
731
738
  return;
732
739
  }
740
+ if (this.isAliveAck(msg, payload)) {
741
+ this.lastPongTime = Date.now();
742
+ return;
743
+ }
733
744
  const requestId = msg.requestId ?? (payload && typeof payload === "object" ? payload.requestId ?? payload?.data?.requestId : void 0);
734
745
  const resolved = requestId ? this.tryResolvePending(requestId, functionName, payload) : false;
735
746
  if (!resolved) {
@@ -779,7 +790,23 @@ var WsClient = class {
779
790
  const ping = () => {
780
791
  if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;
781
792
  this.lastPingTime = Date.now();
782
- this.send(hb.pingType ?? "ping");
793
+ const requestId = this.requestId;
794
+ const deviceType = this.headerConfig.deviceType === "" || this.headerConfig.deviceType === void 0 ? 1 : this.headerConfig.deviceType;
795
+ const message = JSON.stringify({
796
+ service: "connection",
797
+ functionName: "isAlive",
798
+ data: {
799
+ _heartBeat: true,
800
+ deviceType,
801
+ deviceId: this.headerConfig.deviceId ?? "",
802
+ platformId: this.headerConfig.platformId ?? "",
803
+ version: this.headerConfig.version ?? "",
804
+ uuid: this.options.protocols,
805
+ requestId,
806
+ childPlatformId: this.headerConfig.childPlatformId ?? ""
807
+ }
808
+ });
809
+ this.ws.send(message);
783
810
  };
784
811
  ping();
785
812
  this.heartbeatTimer = window.setInterval(() => {
@@ -898,6 +925,12 @@ var WsClient = class {
898
925
  isPong(type) {
899
926
  return type === (this.options.heartbeat?.pongType ?? "pong");
900
927
  }
928
+ isAliveAck(msg, payload) {
929
+ if (!msg || typeof msg !== "object") return false;
930
+ if (msg.service !== "connection") return false;
931
+ if (msg.functionName !== "isAlive" && msg.type !== "isAlive") return false;
932
+ return payload && typeof payload === "object" && payload.status === 200;
933
+ }
901
934
  async buildURL(params) {
902
935
  let urlStr = this.options.url;
903
936
  if (!/^\w+:\/\//.test(urlStr)) {
@@ -4278,11 +4311,286 @@ var cmsEndpoints = {
4278
4311
  })
4279
4312
  };
4280
4313
 
4314
+ // src/spec/auth.ts
4315
+ var authEndpoints = {
4316
+ playerRegisterRequestCode: defineEndpoint({
4317
+ id: "auth.playerRegisterRequestCode",
4318
+ transport: "grpc",
4319
+ grpc: {
4320
+ service: FrontendAuthService,
4321
+ methodName: "playerRegisterRequestCode",
4322
+ requestSchema: PlayerRegisterRequestCodeReqSchema
4323
+ }
4324
+ }),
4325
+ playerRegisterWithCode: defineEndpoint({
4326
+ id: "auth.playerRegisterWithCode",
4327
+ transport: "grpc",
4328
+ grpc: {
4329
+ service: FrontendAuthService,
4330
+ methodName: "playerRegisterWithCode",
4331
+ requestSchema: PlayerRegisterWithCodeReqSchema
4332
+ }
4333
+ }),
4334
+ playerLoginWithSessionToken: defineEndpoint({
4335
+ id: "auth.playerLoginWithSessionToken",
4336
+ transport: "grpc",
4337
+ grpc: {
4338
+ service: FrontendAuthService,
4339
+ methodName: "playerLoginWithSessionToken",
4340
+ requestSchema: PlayerLoginWithSessionTokenReqSchema
4341
+ }
4342
+ }),
4343
+ phoneNumberLoginWithPassword: defineEndpoint({
4344
+ id: "auth.phoneNumberLoginWithPassword",
4345
+ transport: "grpc",
4346
+ grpc: {
4347
+ service: FrontendAuthService,
4348
+ methodName: "phoneNumberLoginWithPassword",
4349
+ requestSchema: PhoneNumberLoginWithPasswordReqSchema
4350
+ }
4351
+ }),
4352
+ playerLoginOrRegisterWithSMS: defineEndpoint({
4353
+ id: "auth.playerLoginOrRegisterWithSMS",
4354
+ transport: "grpc",
4355
+ grpc: {
4356
+ service: FrontendAuthService,
4357
+ methodName: "playerLoginOrRegisterWithSMS",
4358
+ requestSchema: PlayerLoginOrRegisterWithSMSReqSchema
4359
+ }
4360
+ }),
4361
+ playerLogout: defineEndpoint({
4362
+ id: "auth.playerLogout",
4363
+ transport: "grpc",
4364
+ grpc: {
4365
+ service: FrontendAuthService,
4366
+ methodName: "playerLogout",
4367
+ requestSchema: PlayerLogoutReqSchema
4368
+ }
4369
+ }),
4370
+ authenticate: defineEndpoint({
4371
+ id: "auth.authenticate",
4372
+ transport: "grpc",
4373
+ grpc: {
4374
+ service: FrontendAuthService,
4375
+ methodName: "authenticate",
4376
+ requestSchema: AuthenticateReqSchema
4377
+ }
4378
+ }),
4379
+ sendSmsCode: defineEndpoint({
4380
+ id: "auth.sendSmsCode",
4381
+ transport: "grpc",
4382
+ grpc: {
4383
+ service: FrontendAuthService,
4384
+ methodName: "sendSmsCode",
4385
+ requestSchema: SendSmsCodeReqSchema
4386
+ }
4387
+ }),
4388
+ sendSmsCodeViber: defineEndpoint({
4389
+ id: "auth.sendSmsCodeViber",
4390
+ transport: "grpc",
4391
+ grpc: {
4392
+ service: FrontendAuthService,
4393
+ methodName: "sendSmsCodeViber",
4394
+ requestSchema: SendSmsCodeViberReqSchema
4395
+ }
4396
+ }),
4397
+ playerLoginOrRegisterWithFacebook: defineEndpoint({
4398
+ id: "auth.playerLoginOrRegisterWithFacebook",
4399
+ transport: "grpc",
4400
+ grpc: {
4401
+ service: FrontendFacebookService,
4402
+ methodName: "playerLoginOrRegisterWithFacebook",
4403
+ requestSchema: PlayerLoginOrRegisterWithFacebookReqSchema
4404
+ }
4405
+ }),
4406
+ getFacebookConsentPage: defineEndpoint({
4407
+ id: "auth.getFacebookConsentPage",
4408
+ transport: "grpc",
4409
+ grpc: {
4410
+ service: FrontendFacebookService,
4411
+ methodName: "getFacebookConsentPage",
4412
+ requestSchema: GetFacebookConsentPageReqSchema
4413
+ }
4414
+ }),
4415
+ facebookAuthorization: defineEndpoint({
4416
+ id: "auth.facebookAuthorization",
4417
+ transport: "grpc",
4418
+ grpc: {
4419
+ service: FrontendFacebookService,
4420
+ methodName: "facebookAuthorization",
4421
+ requestSchema: FacebookAuthorizationReqSchema
4422
+ }
4423
+ }),
4424
+ getFacebookFriendList: defineEndpoint({
4425
+ id: "auth.getFacebookFriendList",
4426
+ transport: "grpc",
4427
+ grpc: {
4428
+ service: FrontendFacebookService,
4429
+ methodName: "getFacebookFriendList",
4430
+ requestSchema: GetFacebookFriendListReqSchema
4431
+ }
4432
+ }),
4433
+ manualFetchFacebookFriends: defineEndpoint({
4434
+ id: "auth.manualFetchFacebookFriends",
4435
+ transport: "grpc",
4436
+ grpc: {
4437
+ service: FrontendFacebookService,
4438
+ methodName: "manualFetchFacebookFriends",
4439
+ requestSchema: ManualFetchFacebookFriendsReqSchema
4440
+ }
4441
+ }),
4442
+ bindFacebookAccount: defineEndpoint({
4443
+ id: "auth.bindFacebookAccount",
4444
+ transport: "grpc",
4445
+ grpc: {
4446
+ service: FrontendFacebookService,
4447
+ methodName: "bindFacebookAccount",
4448
+ requestSchema: BindFacebookAccountReqSchema
4449
+ }
4450
+ }),
4451
+ unbindFacebookAccount: defineEndpoint({
4452
+ id: "auth.unbindFacebookAccount",
4453
+ transport: "grpc",
4454
+ grpc: {
4455
+ service: FrontendFacebookService,
4456
+ methodName: "unbindFacebookAccount",
4457
+ requestSchema: EmptySchema
4458
+ }
4459
+ }),
4460
+ facebookVerifyPhoneNumber: defineEndpoint({
4461
+ id: "auth.facebookVerifyPhoneNumber",
4462
+ transport: "grpc",
4463
+ grpc: {
4464
+ service: FrontendFacebookService,
4465
+ methodName: "facebookVerifyPhoneNumber",
4466
+ requestSchema: FacebookVerifyPhoneNumberReqSchema
4467
+ }
4468
+ }),
4469
+ enableFaceId: defineEndpoint({
4470
+ id: "auth.enableFaceId",
4471
+ transport: "grpc",
4472
+ grpc: {
4473
+ service: FrontendFaceIdService,
4474
+ methodName: "enableFaceId",
4475
+ requestSchema: EnableFaceIdReqSchema
4476
+ }
4477
+ }),
4478
+ initFaceIdSession: defineEndpoint({
4479
+ id: "auth.initFaceIdSession",
4480
+ transport: "grpc",
4481
+ grpc: {
4482
+ service: FrontendFaceIdService,
4483
+ methodName: "initFaceIdSession",
4484
+ requestSchema: InitFaceIdSessionReqSchema
4485
+ }
4486
+ }),
4487
+ playerLoginWithFaceId: defineEndpoint({
4488
+ id: "auth.playerLoginWithFaceId",
4489
+ transport: "grpc",
4490
+ grpc: {
4491
+ service: FrontendFaceIdService,
4492
+ methodName: "playerLoginWithFaceId",
4493
+ requestSchema: PlayerLoginWithFaceIdReqSchema
4494
+ }
4495
+ }),
4496
+ disableFaceId: defineEndpoint({
4497
+ id: "auth.disableFaceId",
4498
+ transport: "grpc",
4499
+ grpc: {
4500
+ service: FrontendFaceIdService,
4501
+ methodName: "disableFaceId",
4502
+ requestSchema: DisableFaceIdReqSchema
4503
+ }
4504
+ }),
4505
+ initEnrollSession: defineEndpoint({
4506
+ id: "auth.initEnrollSession",
4507
+ transport: "grpc",
4508
+ grpc: {
4509
+ service: FrontendFaceIdService,
4510
+ methodName: "initEnrollSession",
4511
+ requestSchema: InitEnrollSessionReqSchema
4512
+ }
4513
+ }),
4514
+ getFaceIdInfo: defineEndpoint({
4515
+ id: "auth.getFaceIdInfo",
4516
+ transport: "grpc",
4517
+ grpc: {
4518
+ service: FrontendFaceIdService,
4519
+ methodName: "getFaceIdInfo",
4520
+ requestSchema: GetFaceIdInfoReqSchema
4521
+ }
4522
+ }),
4523
+ verifyPassword: defineEndpoint({
4524
+ id: "auth.verifyPassword",
4525
+ transport: "grpc",
4526
+ grpc: {
4527
+ service: FrontendPlayerService,
4528
+ methodName: "verifyPassword",
4529
+ requestSchema: VerifyPasswordReqSchema
4530
+ }
4531
+ }),
4532
+ gLifeAuth: defineEndpoint({
4533
+ id: "auth.gLifeAuth",
4534
+ transport: "grpc",
4535
+ grpc: {
4536
+ service: GLifeService,
4537
+ methodName: "gLifeAuth",
4538
+ requestSchema: GLifeAuthReqSchema
4539
+ }
4540
+ }),
4541
+ playerLoginWithGLife: defineEndpoint({
4542
+ id: "auth.playerLoginWithGLife",
4543
+ transport: "grpc",
4544
+ grpc: {
4545
+ service: GLifeService,
4546
+ methodName: "playerLoginWithGLife",
4547
+ requestSchema: PlayerLoginWithGLifeReqSchema
4548
+ }
4549
+ }),
4550
+ playerLoginOrRegisterWithGLife: defineEndpoint({
4551
+ id: "auth.playerLoginOrRegisterWithGLife",
4552
+ transport: "grpc",
4553
+ grpc: {
4554
+ service: GLifeService,
4555
+ methodName: "playerLoginOrRegisterWithGLife",
4556
+ requestSchema: PlayerLoginOrRegisterWithGLifeReqSchema
4557
+ }
4558
+ }),
4559
+ mayaAuth: defineEndpoint({
4560
+ id: "auth.mayaAuth",
4561
+ transport: "grpc",
4562
+ grpc: {
4563
+ service: MayaService,
4564
+ methodName: "mayaAuth",
4565
+ requestSchema: MayaAuthReqSchema
4566
+ }
4567
+ }),
4568
+ playerLoginWithMaya: defineEndpoint({
4569
+ id: "auth.playerLoginWithMaya",
4570
+ transport: "grpc",
4571
+ grpc: {
4572
+ service: MayaService,
4573
+ methodName: "playerLoginWithMaya",
4574
+ requestSchema: PlayerLoginWithMayaReqSchema
4575
+ }
4576
+ }),
4577
+ playerLoginOrRegisterWithMaya: defineEndpoint({
4578
+ id: "auth.playerLoginOrRegisterWithMaya",
4579
+ transport: "grpc",
4580
+ grpc: {
4581
+ service: MayaService,
4582
+ methodName: "playerLoginOrRegisterWithMaya",
4583
+ requestSchema: PlayerLoginOrRegisterWithMayaReqSchema
4584
+ }
4585
+ })
4586
+ };
4587
+
4281
4588
  // src/spec/index.ts
4282
4589
  var apiMap = {
4283
4590
  platform: platformEndpoints,
4284
4591
  admin: adminEndpoints,
4285
- cms: cmsEndpoints
4592
+ cms: cmsEndpoints,
4593
+ auth: authEndpoints
4286
4594
  };
4287
4595
 
4288
4596
  // src/routing/router.ts
@@ -4455,54 +4763,207 @@ function pickEndpoint(variants, transport, specDefaultTransport, service, functi
4455
4763
  return fallback;
4456
4764
  }
4457
4765
 
4458
- // src/utils/uniqueKey.ts
4459
- var fetchGuid = () => {
4460
- let guid = getLocalItem("uKey");
4461
- console.log("fetchGuid", JSON.stringify(guid));
4462
- if (!guid) {
4463
- guid = generateGuid();
4464
- setLocalItem("uKey", maskingGuid(true, guid));
4465
- } else {
4466
- guid = maskingGuid(false, null);
4766
+ // src/utils/adapters/web.ts
4767
+ var webStorage = {
4768
+ get(key) {
4769
+ if (typeof window === "undefined") return null;
4770
+ try {
4771
+ const storage = window.localStorage;
4772
+ if (!storage) return null;
4773
+ return storage.getItem(key);
4774
+ } catch {
4775
+ return null;
4776
+ }
4777
+ },
4778
+ set(key, value) {
4779
+ if (typeof window === "undefined") return;
4780
+ try {
4781
+ const storage = window.localStorage;
4782
+ if (!storage) return;
4783
+ storage.setItem(key, value);
4784
+ } catch {
4785
+ return;
4786
+ }
4467
4787
  }
4468
- return guid;
4469
- };
4470
- var generateGuid = () => {
4471
- return "10000000-1000-4000-8000-100000000000".replace(
4472
- /[018]/g,
4473
- (c) => (+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
4474
- );
4475
4788
  };
4476
- var maskingGuid = (isMask, oriUKey) => {
4477
- let _uKey2;
4478
- let uKey = oriUKey || getLocalItem("uKey");
4479
- if (isMask) {
4480
- let _uKey;
4481
- uKey = ((_uKey = uKey) == null ? void 0 : _uKey.replace(/-/g, "")) || "";
4482
- const _buffer = new Uint8Array(uKey.length / 2);
4483
- for (let i = 0; i < uKey.length; i += 2) {
4484
- _buffer[i / 2] = parseInt(uKey.substr(i, 2), 16);
4485
- }
4486
- return btoa(String.fromCharCode.apply(null, Array.from(_buffer)));
4789
+
4790
+ // src/utils/fetchGuid.ts
4791
+ var defaultKey = "uKey";
4792
+ var guidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
4793
+ var hex32Pattern = /^[0-9a-f]{32}$/i;
4794
+ function createFetchGuid(storage, options) {
4795
+ return () => fetchGuidWithStorage(storage, options);
4796
+ }
4797
+ function fetchGuidWithStorage(storage, options) {
4798
+ const key = options?.key ?? defaultKey;
4799
+ const current = safeGet(storage, key);
4800
+ if (current) {
4801
+ const unmasked = unmaskGuid(current);
4802
+ if (unmasked) return formatGuid(unmasked, options);
4803
+ }
4804
+ const guid = generateGuid();
4805
+ safeSet(storage, key, maskGuid(guid));
4806
+ return formatGuid(guid, options);
4807
+ }
4808
+ function safeGet(storage, key) {
4809
+ try {
4810
+ return storage.get(key);
4811
+ } catch {
4812
+ return null;
4813
+ }
4814
+ }
4815
+ function safeSet(storage, key, value) {
4816
+ try {
4817
+ storage.set(key, value);
4818
+ } catch {
4819
+ return;
4820
+ }
4821
+ }
4822
+ function generateGuid() {
4823
+ const cryptoObj = typeof globalThis !== "undefined" ? globalThis.crypto : void 0;
4824
+ if (cryptoObj && cryptoObj.getRandomValues) {
4825
+ return "10000000-1000-4000-8000-100000000000".replace(
4826
+ /[018]/g,
4827
+ (c) => (+c ^ cryptoObj.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
4828
+ );
4829
+ }
4830
+ return "xxxxxxxx-xxxx-4xxx-8xxx-xxxxxxxxxxxx".replace(/[xy]/g, (ch) => {
4831
+ const r = Math.random() * 16 | 0;
4832
+ const v = ch === "x" ? r : r & 3 | 8;
4833
+ return v.toString(16);
4834
+ });
4835
+ }
4836
+ function maskGuid(guid) {
4837
+ const hex = guid.replace(/-/g, "");
4838
+ const buffer = new Uint8Array(hex.length / 2);
4839
+ for (let i = 0; i < hex.length; i += 2) {
4840
+ buffer[i / 2] = parseInt(hex.slice(i, i + 2), 16);
4841
+ }
4842
+ return base64Encode(buffer);
4843
+ }
4844
+ function unmaskGuid(value) {
4845
+ const clean = value.replace(/"/g, "").trim();
4846
+ if (guidPattern.test(clean)) return clean;
4847
+ if (hex32Pattern.test(clean)) {
4848
+ return clean.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, "$1-$2-$3-$4-$5");
4487
4849
  }
4488
- uKey = ((_uKey2 = uKey) == null ? void 0 : _uKey2.replace(/"/g, "")) || "";
4489
- const buffer = atob(uKey);
4490
- const hex = Array.prototype.map.call(
4491
- new Uint8Array(buffer.length).map((_, i) => buffer.charCodeAt(i)),
4492
- (byte) => ("0" + (byte & 255).toString(16)).slice(-2)
4493
- ).join("");
4850
+ const bytes = base64Decode(clean);
4851
+ if (!bytes) return null;
4852
+ const hex = Array.prototype.map.call(bytes, (byte) => ("0" + (byte & 255).toString(16)).slice(-2)).join("");
4853
+ if (hex.length !== 32) return null;
4494
4854
  return hex.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, "$1-$2-$3-$4-$5");
4495
- };
4496
- var getLocalItem = (key) => {
4497
- if (typeof window !== "undefined") {
4498
- return window.localStorage.getItem(key);
4855
+ }
4856
+ function formatGuid(guid, options) {
4857
+ const format = options?.format ?? "uuid";
4858
+ const prefix = options?.prefix ?? "";
4859
+ if (format === "uuid") return prefix + guid;
4860
+ const hex = guid.replace(/-/g, "");
4861
+ if (format === "hex") return prefix + hex;
4862
+ const buffer = new Uint8Array(hex.length / 2);
4863
+ for (let i = 0; i < hex.length; i += 2) {
4864
+ buffer[i / 2] = parseInt(hex.slice(i, i + 2), 16);
4865
+ }
4866
+ return prefix + base64Encode(buffer);
4867
+ }
4868
+ function base64Encode(bytes) {
4869
+ const btoaFn = typeof globalThis !== "undefined" ? globalThis.btoa : void 0;
4870
+ if (btoaFn) {
4871
+ let binary = "";
4872
+ for (let i2 = 0; i2 < bytes.length; i2++) binary += String.fromCharCode(bytes[i2]);
4873
+ return btoaFn(binary);
4874
+ }
4875
+ const BufferCtor = typeof globalThis !== "undefined" ? globalThis.Buffer : void 0;
4876
+ if (BufferCtor) return BufferCtor.from(bytes).toString("base64");
4877
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4878
+ let out = "";
4879
+ let i = 0;
4880
+ while (i < bytes.length) {
4881
+ const rem = bytes.length - i;
4882
+ const b1 = bytes[i++];
4883
+ const b2 = rem > 1 ? bytes[i++] : 0;
4884
+ const b3 = rem > 2 ? bytes[i++] : 0;
4885
+ const n = b1 << 16 | b2 << 8 | b3;
4886
+ out += chars[n >> 18 & 63] + chars[n >> 12 & 63] + (rem > 1 ? chars[n >> 6 & 63] : "=") + (rem > 2 ? chars[n & 63] : "=");
4887
+ }
4888
+ return out;
4889
+ }
4890
+ function base64Decode(b64) {
4891
+ const atobFn = typeof globalThis !== "undefined" ? globalThis.atob : void 0;
4892
+ if (atobFn) {
4893
+ try {
4894
+ const bin = atobFn(b64);
4895
+ const out = new Uint8Array(bin.length);
4896
+ for (let i2 = 0; i2 < bin.length; i2++) out[i2] = bin.charCodeAt(i2);
4897
+ return out;
4898
+ } catch {
4899
+ return null;
4900
+ }
4901
+ }
4902
+ const BufferCtor = typeof globalThis !== "undefined" ? globalThis.Buffer : void 0;
4903
+ if (BufferCtor) {
4904
+ try {
4905
+ const buf = BufferCtor.from(b64, "base64");
4906
+ return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
4907
+ } catch {
4908
+ return null;
4909
+ }
4910
+ }
4911
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4912
+ const map = {};
4913
+ for (let i2 = 0; i2 < chars.length; i2++) map[chars[i2]] = i2;
4914
+ const clean = b64.replace(/[^A-Za-z0-9+/=]/g, "");
4915
+ if (!clean) return null;
4916
+ const bytes = [];
4917
+ let i = 0;
4918
+ while (i < clean.length) {
4919
+ const c1 = clean[i++];
4920
+ const c2 = clean[i++];
4921
+ const c3 = clean[i++];
4922
+ const c4 = clean[i++];
4923
+ if (!c1 || !c2) break;
4924
+ const e1 = map[c1];
4925
+ const e2 = map[c2];
4926
+ if (e1 === void 0 || e2 === void 0) return null;
4927
+ const e3 = c3 === "=" || !c3 ? 0 : map[c3];
4928
+ const e4 = c4 === "=" || !c4 ? 0 : map[c4];
4929
+ if (e3 === void 0 || e4 === void 0) return null;
4930
+ const n = e1 << 18 | e2 << 12 | e3 << 6 | e4;
4931
+ const b1 = n >> 16 & 255;
4932
+ const b2 = n >> 8 & 255;
4933
+ const b3 = n & 255;
4934
+ bytes.push(b1);
4935
+ if (c3 !== "=") bytes.push(b2);
4936
+ if (c4 !== "=") bytes.push(b3);
4937
+ }
4938
+ return new Uint8Array(bytes);
4939
+ }
4940
+
4941
+ // src/utils/uniqueKey.ts
4942
+ var memoryStore = /* @__PURE__ */ new Map();
4943
+ var memoryStorage = {
4944
+ get(key) {
4945
+ return memoryStore.get(key) ?? null;
4946
+ },
4947
+ set(key, val) {
4948
+ memoryStore.set(key, val);
4499
4949
  }
4500
4950
  };
4501
- var setLocalItem = (key, val) => {
4502
- if (typeof window !== "undefined") {
4503
- window.localStorage.setItem(key, val);
4951
+ var defaultStorage = {
4952
+ get(key) {
4953
+ return webStorage.get(key) ?? memoryStorage.get(key);
4954
+ },
4955
+ set(key, val) {
4956
+ webStorage.set(key, val);
4957
+ memoryStorage.set(key, val);
4504
4958
  }
4505
4959
  };
4960
+ var storageAdapter = defaultStorage;
4961
+ var fetchGuidImpl = createFetchGuid(storageAdapter);
4962
+ var setUniqueKeyStorage = (adapter) => {
4963
+ storageAdapter = adapter ?? defaultStorage;
4964
+ fetchGuidImpl = createFetchGuid(storageAdapter);
4965
+ };
4966
+ var fetchGuid = () => fetchGuidImpl();
4506
4967
 
4507
4968
  // src/index.ts
4508
4969
  var api;
@@ -4882,6 +5343,7 @@ function createApiServiceClient(service) {
4882
5343
  0 && (module.exports = {
4883
5344
  apiError,
4884
5345
  callApi,
5346
+ createFetchGuid,
4885
5347
  destroySDK,
4886
5348
  fetchGuid,
4887
5349
  getSDK,
@@ -4889,6 +5351,8 @@ function createApiServiceClient(service) {
4889
5351
  initSDK,
4890
5352
  notify,
4891
5353
  requestApi,
4892
- requestApiSpec
5354
+ requestApiSpec,
5355
+ setUniqueKeyStorage,
5356
+ webStorage
4893
5357
  });
4894
5358
  //# sourceMappingURL=index.cjs.map