@goplus123/core-api 1.0.7 → 1.0.8

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.d.mts CHANGED
@@ -549,6 +549,11 @@ declare class UnifiedApiClient {
549
549
  private dispatch;
550
550
  }
551
551
 
552
+ type KeyValueStorage = {
553
+ get: (key: string) => string | null;
554
+ set: (key: string, val: string) => void;
555
+ };
556
+
552
557
  interface RequestApiConfig {
553
558
  http?: {
554
559
  baseUrl: string;
@@ -574,6 +579,7 @@ interface RequestApiConfig {
574
579
  };
575
580
  logger?: ApiLogger;
576
581
  onApiError?: ApiErrorHandler;
582
+ uniqueKeyStorage?: KeyValueStorage | null;
577
583
  }
578
584
  /**
579
585
  * 统一请求 API 封装
@@ -17085,11 +17091,6 @@ declare function requestApi<TReq = any, TRes = any>(args: {
17085
17091
  meta?: Record<string, any>;
17086
17092
  }): Promise<TRes>;
17087
17093
 
17088
- type KeyValueStorage = {
17089
- get: (key: string) => string | null;
17090
- set: (key: string, val: string) => void;
17091
- };
17092
-
17093
17094
  type FetchGuidOptions = {
17094
17095
  key?: string;
17095
17096
  format?: 'uuid' | 'hex' | 'base64';
package/dist/index.d.ts CHANGED
@@ -549,6 +549,11 @@ declare class UnifiedApiClient {
549
549
  private dispatch;
550
550
  }
551
551
 
552
+ type KeyValueStorage = {
553
+ get: (key: string) => string | null;
554
+ set: (key: string, val: string) => void;
555
+ };
556
+
552
557
  interface RequestApiConfig {
553
558
  http?: {
554
559
  baseUrl: string;
@@ -574,6 +579,7 @@ interface RequestApiConfig {
574
579
  };
575
580
  logger?: ApiLogger;
576
581
  onApiError?: ApiErrorHandler;
582
+ uniqueKeyStorage?: KeyValueStorage | null;
577
583
  }
578
584
  /**
579
585
  * 统一请求 API 封装
@@ -17085,11 +17091,6 @@ declare function requestApi<TReq = any, TRes = any>(args: {
17085
17091
  meta?: Record<string, any>;
17086
17092
  }): Promise<TRes>;
17087
17093
 
17088
- type KeyValueStorage = {
17089
- get: (key: string) => string | null;
17090
- set: (key: string, val: string) => void;
17091
- };
17092
-
17093
17094
  type FetchGuidOptions = {
17094
17095
  key?: string;
17095
17096
  format?: 'uuid' | 'hex' | 'base64';
package/dist/index.js CHANGED
@@ -6144,176 +6144,6 @@ var apiMap = {
6144
6144
  userEngagement: userEngagementEndpoints
6145
6145
  };
6146
6146
 
6147
- // src/routing/router.ts
6148
- var serviceInstances = {};
6149
- var rpcClient;
6150
- var unifiedClient;
6151
- var endpointRegistry = /* @__PURE__ */ new Map();
6152
- var endpointDefaultTransports = /* @__PURE__ */ new Map();
6153
- var defaultTransport = "auto";
6154
- var transportState = {
6155
- hasHttp: false,
6156
- hasWs: false,
6157
- hasGrpc: false
6158
- };
6159
- function resetRoutingState() {
6160
- serviceInstances = {};
6161
- rpcClient = void 0;
6162
- unifiedClient = void 0;
6163
- endpointRegistry.clear();
6164
- endpointDefaultTransports.clear();
6165
- defaultTransport = "auto";
6166
- transportState = { hasHttp: false, hasWs: false, hasGrpc: false };
6167
- }
6168
- function setServiceInstances(instances) {
6169
- serviceInstances = instances;
6170
- }
6171
- function setRpcClient(client) {
6172
- rpcClient = client;
6173
- }
6174
- function setUnifiedClient(client) {
6175
- unifiedClient = client;
6176
- }
6177
- function setTransportState(state) {
6178
- transportState = state;
6179
- }
6180
- function setDefaultTransport(transport) {
6181
- defaultTransport = transport;
6182
- }
6183
- function makeEndpointKey(service, functionName) {
6184
- return `${service}:${functionName}`;
6185
- }
6186
- function isEndpointSpec(value) {
6187
- return !!value && typeof value === "object" && "transport" in value;
6188
- }
6189
- function isEndpointGroup(value) {
6190
- return !!value && typeof value === "object" && "transports" in value;
6191
- }
6192
- function registerEndpointVariant(service, functionName, endpoint) {
6193
- const key = makeEndpointKey(service, functionName);
6194
- let byTransport = endpointRegistry.get(key);
6195
- if (!byTransport) {
6196
- byTransport = /* @__PURE__ */ new Map();
6197
- endpointRegistry.set(key, byTransport);
6198
- }
6199
- byTransport.set(endpoint.transport, endpoint);
6200
- }
6201
- function rebuildEndpointRegistry() {
6202
- endpointRegistry = /* @__PURE__ */ new Map();
6203
- endpointDefaultTransports = /* @__PURE__ */ new Map();
6204
- for (const [serviceName, service] of Object.entries(apiMap)) {
6205
- for (const [functionName, endpoint] of Object.entries(service)) {
6206
- if (isEndpointSpec(endpoint)) {
6207
- registerEndpointVariant(serviceName, functionName, endpoint);
6208
- continue;
6209
- }
6210
- if (isEndpointGroup(endpoint)) {
6211
- if (endpoint.defaultTransport) {
6212
- endpointDefaultTransports.set(
6213
- makeEndpointKey(serviceName, functionName),
6214
- endpoint.defaultTransport
6215
- );
6216
- }
6217
- for (const [transport, config] of Object.entries(endpoint.transports ?? {})) {
6218
- if (!config) continue;
6219
- if (transport === "http") {
6220
- registerEndpointVariant(serviceName, functionName, {
6221
- id: endpoint.id,
6222
- transport: "http",
6223
- http: config,
6224
- request: endpoint.request,
6225
- response: endpoint.response
6226
- });
6227
- } else if (transport === "ws") {
6228
- registerEndpointVariant(serviceName, functionName, {
6229
- id: endpoint.id,
6230
- transport: "ws",
6231
- ws: config,
6232
- request: endpoint.request,
6233
- response: endpoint.response
6234
- });
6235
- } else if (transport === "grpc") {
6236
- registerEndpointVariant(serviceName, functionName, {
6237
- id: endpoint.id,
6238
- transport: "grpc",
6239
- grpc: config,
6240
- request: endpoint.request,
6241
- response: endpoint.response
6242
- });
6243
- }
6244
- }
6245
- continue;
6246
- }
6247
- }
6248
- }
6249
- }
6250
- function requestApi(args) {
6251
- const key = makeEndpointKey(args.service, args.functionName);
6252
- const endpointVariants = endpointRegistry.get(key);
6253
- if (endpointVariants) {
6254
- if (!unifiedClient) {
6255
- throw new Error("SDK not initialized. Please call initSDK first.");
6256
- }
6257
- const specDefaultTransport = endpointDefaultTransports.get(key);
6258
- const endpoint = pickEndpoint(
6259
- endpointVariants,
6260
- args.transport,
6261
- specDefaultTransport,
6262
- args.service,
6263
- args.functionName
6264
- );
6265
- if (endpoint.transport === "grpc" && !transportState.hasGrpc) {
6266
- throw new Error("SDK not initialized. Please call initSDK with grpc config.");
6267
- }
6268
- if (endpoint.transport === "ws" && !transportState.hasWs) {
6269
- throw new Error("SDK not initialized. Please call initSDK with ws config.");
6270
- }
6271
- if (endpoint.transport === "http" && !transportState.hasHttp) {
6272
- throw new Error("SDK not initialized. Please call initSDK with http config.");
6273
- }
6274
- const meta = args.callOptions || args.wsOptions ? {
6275
- ...args.meta ?? {},
6276
- ...args.callOptions ? { callOptions: args.callOptions } : {},
6277
- ...args.wsOptions ? { wsOptions: args.wsOptions } : {}
6278
- } : args.meta;
6279
- return unifiedClient.call(endpoint, args.requestParam, meta);
6280
- }
6281
- const serviceInstance = serviceInstances[args.service];
6282
- const method = serviceInstance?.[args.functionName];
6283
- if (typeof method === "function") {
6284
- if (args.callOptions !== void 0) {
6285
- return method(args.requestParam, args.callOptions);
6286
- }
6287
- return method(args.requestParam);
6288
- }
6289
- if (!rpcClient) {
6290
- throw new Error("SDK not initialized. Please call initSDK first.");
6291
- }
6292
- return rpcClient.call(args.functionName, args.service, args.requestParam, args.wsOptions);
6293
- }
6294
- function pickEndpoint(variants, transport, specDefaultTransport, service, functionName) {
6295
- const resolvedTransport = transport ?? specDefaultTransport ?? defaultTransport;
6296
- if (resolvedTransport !== "auto") {
6297
- const found = variants.get(resolvedTransport);
6298
- if (!found) {
6299
- throw new Error(
6300
- `Endpoint transport not found: ${service}.${functionName} (${resolvedTransport}). Available: ${Array.from(
6301
- variants.keys()
6302
- ).join(", ")}`
6303
- );
6304
- }
6305
- return found;
6306
- }
6307
- if (transportState.hasGrpc && variants.has("grpc")) return variants.get("grpc");
6308
- if (transportState.hasWs && variants.has("ws")) return variants.get("ws");
6309
- if (transportState.hasHttp && variants.has("http")) return variants.get("http");
6310
- const fallback = variants.values().next().value;
6311
- if (!fallback) {
6312
- throw new Error(`Endpoint not found: ${service}.${functionName}`);
6313
- }
6314
- return fallback;
6315
- }
6316
-
6317
6147
  // src/utils/adapters/web.ts
6318
6148
  var webStorage = {
6319
6149
  get(key) {
@@ -6516,6 +6346,176 @@ var setUniqueKeyStorage = (adapter) => {
6516
6346
  };
6517
6347
  var fetchGuid = () => fetchGuidImpl();
6518
6348
 
6349
+ // src/routing/router.ts
6350
+ var serviceInstances = {};
6351
+ var rpcClient;
6352
+ var unifiedClient;
6353
+ var endpointRegistry = /* @__PURE__ */ new Map();
6354
+ var endpointDefaultTransports = /* @__PURE__ */ new Map();
6355
+ var defaultTransport = "auto";
6356
+ var transportState = {
6357
+ hasHttp: false,
6358
+ hasWs: false,
6359
+ hasGrpc: false
6360
+ };
6361
+ function resetRoutingState() {
6362
+ serviceInstances = {};
6363
+ rpcClient = void 0;
6364
+ unifiedClient = void 0;
6365
+ endpointRegistry.clear();
6366
+ endpointDefaultTransports.clear();
6367
+ defaultTransport = "auto";
6368
+ transportState = { hasHttp: false, hasWs: false, hasGrpc: false };
6369
+ }
6370
+ function setServiceInstances(instances) {
6371
+ serviceInstances = instances;
6372
+ }
6373
+ function setRpcClient(client) {
6374
+ rpcClient = client;
6375
+ }
6376
+ function setUnifiedClient(client) {
6377
+ unifiedClient = client;
6378
+ }
6379
+ function setTransportState(state) {
6380
+ transportState = state;
6381
+ }
6382
+ function setDefaultTransport(transport) {
6383
+ defaultTransport = transport;
6384
+ }
6385
+ function makeEndpointKey(service, functionName) {
6386
+ return `${service}:${functionName}`;
6387
+ }
6388
+ function isEndpointSpec(value) {
6389
+ return !!value && typeof value === "object" && "transport" in value;
6390
+ }
6391
+ function isEndpointGroup(value) {
6392
+ return !!value && typeof value === "object" && "transports" in value;
6393
+ }
6394
+ function registerEndpointVariant(service, functionName, endpoint) {
6395
+ const key = makeEndpointKey(service, functionName);
6396
+ let byTransport = endpointRegistry.get(key);
6397
+ if (!byTransport) {
6398
+ byTransport = /* @__PURE__ */ new Map();
6399
+ endpointRegistry.set(key, byTransport);
6400
+ }
6401
+ byTransport.set(endpoint.transport, endpoint);
6402
+ }
6403
+ function rebuildEndpointRegistry() {
6404
+ endpointRegistry = /* @__PURE__ */ new Map();
6405
+ endpointDefaultTransports = /* @__PURE__ */ new Map();
6406
+ for (const [serviceName, service] of Object.entries(apiMap)) {
6407
+ for (const [functionName, endpoint] of Object.entries(service)) {
6408
+ if (isEndpointSpec(endpoint)) {
6409
+ registerEndpointVariant(serviceName, functionName, endpoint);
6410
+ continue;
6411
+ }
6412
+ if (isEndpointGroup(endpoint)) {
6413
+ if (endpoint.defaultTransport) {
6414
+ endpointDefaultTransports.set(
6415
+ makeEndpointKey(serviceName, functionName),
6416
+ endpoint.defaultTransport
6417
+ );
6418
+ }
6419
+ for (const [transport, config] of Object.entries(endpoint.transports ?? {})) {
6420
+ if (!config) continue;
6421
+ if (transport === "http") {
6422
+ registerEndpointVariant(serviceName, functionName, {
6423
+ id: endpoint.id,
6424
+ transport: "http",
6425
+ http: config,
6426
+ request: endpoint.request,
6427
+ response: endpoint.response
6428
+ });
6429
+ } else if (transport === "ws") {
6430
+ registerEndpointVariant(serviceName, functionName, {
6431
+ id: endpoint.id,
6432
+ transport: "ws",
6433
+ ws: config,
6434
+ request: endpoint.request,
6435
+ response: endpoint.response
6436
+ });
6437
+ } else if (transport === "grpc") {
6438
+ registerEndpointVariant(serviceName, functionName, {
6439
+ id: endpoint.id,
6440
+ transport: "grpc",
6441
+ grpc: config,
6442
+ request: endpoint.request,
6443
+ response: endpoint.response
6444
+ });
6445
+ }
6446
+ }
6447
+ continue;
6448
+ }
6449
+ }
6450
+ }
6451
+ }
6452
+ function requestApi(args) {
6453
+ const key = makeEndpointKey(args.service, args.functionName);
6454
+ const endpointVariants = endpointRegistry.get(key);
6455
+ if (endpointVariants) {
6456
+ if (!unifiedClient) {
6457
+ throw new Error("SDK not initialized. Please call initSDK first.");
6458
+ }
6459
+ const specDefaultTransport = endpointDefaultTransports.get(key);
6460
+ const endpoint = pickEndpoint(
6461
+ endpointVariants,
6462
+ args.transport,
6463
+ specDefaultTransport,
6464
+ args.service,
6465
+ args.functionName
6466
+ );
6467
+ if (endpoint.transport === "grpc" && !transportState.hasGrpc) {
6468
+ throw new Error("SDK not initialized. Please call initSDK with grpc config.");
6469
+ }
6470
+ if (endpoint.transport === "ws" && !transportState.hasWs) {
6471
+ throw new Error("SDK not initialized. Please call initSDK with ws config.");
6472
+ }
6473
+ if (endpoint.transport === "http" && !transportState.hasHttp) {
6474
+ throw new Error("SDK not initialized. Please call initSDK with http config.");
6475
+ }
6476
+ const meta = args.callOptions || args.wsOptions ? {
6477
+ ...args.meta ?? {},
6478
+ ...args.callOptions ? { callOptions: args.callOptions } : {},
6479
+ ...args.wsOptions ? { wsOptions: args.wsOptions } : {}
6480
+ } : args.meta;
6481
+ return unifiedClient.call(endpoint, args.requestParam, meta);
6482
+ }
6483
+ const serviceInstance = serviceInstances[args.service];
6484
+ const method = serviceInstance?.[args.functionName];
6485
+ if (typeof method === "function") {
6486
+ if (args.callOptions !== void 0) {
6487
+ return method(args.requestParam, args.callOptions);
6488
+ }
6489
+ return method(args.requestParam);
6490
+ }
6491
+ if (!rpcClient) {
6492
+ throw new Error("SDK not initialized. Please call initSDK first.");
6493
+ }
6494
+ return rpcClient.call(args.functionName, args.service, args.requestParam, args.wsOptions);
6495
+ }
6496
+ function pickEndpoint(variants, transport, specDefaultTransport, service, functionName) {
6497
+ const resolvedTransport = transport ?? specDefaultTransport ?? defaultTransport;
6498
+ if (resolvedTransport !== "auto") {
6499
+ const found = variants.get(resolvedTransport);
6500
+ if (!found) {
6501
+ throw new Error(
6502
+ `Endpoint transport not found: ${service}.${functionName} (${resolvedTransport}). Available: ${Array.from(
6503
+ variants.keys()
6504
+ ).join(", ")}`
6505
+ );
6506
+ }
6507
+ return found;
6508
+ }
6509
+ if (transportState.hasGrpc && variants.has("grpc")) return variants.get("grpc");
6510
+ if (transportState.hasWs && variants.has("ws")) return variants.get("ws");
6511
+ if (transportState.hasHttp && variants.has("http")) return variants.get("http");
6512
+ const fallback = variants.values().next().value;
6513
+ if (!fallback) {
6514
+ throw new Error(`Endpoint not found: ${service}.${functionName}`);
6515
+ }
6516
+ return fallback;
6517
+ }
6518
+
6519
6519
  // src/index.ts
6520
6520
  var api;
6521
6521
  var apiErrorCenter;
@@ -6696,6 +6696,9 @@ function destroySDK() {
6696
6696
  function initSDK(config) {
6697
6697
  clearWsStatusBridge();
6698
6698
  wsStatusSnapshot = void 0;
6699
+ if ("uniqueKeyStorage" in config) {
6700
+ setUniqueKeyStorage(config.uniqueKeyStorage);
6701
+ }
6699
6702
  api = new RequestApi({
6700
6703
  ...config
6701
6704
  });