@goplus123/core-api 1.0.6 → 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/README.md +88 -11
- package/dist/index.cjs +353 -175
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +8 -6
- package/dist/index.d.ts +8 -6
- package/dist/index.js +349 -171
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -129,7 +129,7 @@ interface GrpcClientConfig {
|
|
|
129
129
|
headers?: Record<string, string>;
|
|
130
130
|
getHeaders?: () => Record<string, string> | null | undefined | Promise<Record<string, string> | null | undefined>;
|
|
131
131
|
logger?: ApiLogger;
|
|
132
|
-
invoker?: GrpcInvoker;
|
|
132
|
+
invoker?: GrpcInvoker | 'builtin';
|
|
133
133
|
}
|
|
134
134
|
/**
|
|
135
135
|
* 通用 gRPC 客户端封装 (基于 ConnectRPC)
|
|
@@ -149,6 +149,7 @@ declare class GrpcClient {
|
|
|
149
149
|
private getHeaders?;
|
|
150
150
|
private logger?;
|
|
151
151
|
private invoker?;
|
|
152
|
+
private invokerMode;
|
|
152
153
|
private invokerClientByKey;
|
|
153
154
|
constructor(config: GrpcClientConfig);
|
|
154
155
|
private createTransport;
|
|
@@ -548,6 +549,11 @@ declare class UnifiedApiClient {
|
|
|
548
549
|
private dispatch;
|
|
549
550
|
}
|
|
550
551
|
|
|
552
|
+
type KeyValueStorage = {
|
|
553
|
+
get: (key: string) => string | null;
|
|
554
|
+
set: (key: string, val: string) => void;
|
|
555
|
+
};
|
|
556
|
+
|
|
551
557
|
interface RequestApiConfig {
|
|
552
558
|
http?: {
|
|
553
559
|
baseUrl: string;
|
|
@@ -573,6 +579,7 @@ interface RequestApiConfig {
|
|
|
573
579
|
};
|
|
574
580
|
logger?: ApiLogger;
|
|
575
581
|
onApiError?: ApiErrorHandler;
|
|
582
|
+
uniqueKeyStorage?: KeyValueStorage | null;
|
|
576
583
|
}
|
|
577
584
|
/**
|
|
578
585
|
* 统一请求 API 封装
|
|
@@ -17084,11 +17091,6 @@ declare function requestApi<TReq = any, TRes = any>(args: {
|
|
|
17084
17091
|
meta?: Record<string, any>;
|
|
17085
17092
|
}): Promise<TRes>;
|
|
17086
17093
|
|
|
17087
|
-
type KeyValueStorage = {
|
|
17088
|
-
get: (key: string) => string | null;
|
|
17089
|
-
set: (key: string, val: string) => void;
|
|
17090
|
-
};
|
|
17091
|
-
|
|
17092
17094
|
type FetchGuidOptions = {
|
|
17093
17095
|
key?: string;
|
|
17094
17096
|
format?: 'uuid' | 'hex' | 'base64';
|
package/dist/index.d.ts
CHANGED
|
@@ -129,7 +129,7 @@ interface GrpcClientConfig {
|
|
|
129
129
|
headers?: Record<string, string>;
|
|
130
130
|
getHeaders?: () => Record<string, string> | null | undefined | Promise<Record<string, string> | null | undefined>;
|
|
131
131
|
logger?: ApiLogger;
|
|
132
|
-
invoker?: GrpcInvoker;
|
|
132
|
+
invoker?: GrpcInvoker | 'builtin';
|
|
133
133
|
}
|
|
134
134
|
/**
|
|
135
135
|
* 通用 gRPC 客户端封装 (基于 ConnectRPC)
|
|
@@ -149,6 +149,7 @@ declare class GrpcClient {
|
|
|
149
149
|
private getHeaders?;
|
|
150
150
|
private logger?;
|
|
151
151
|
private invoker?;
|
|
152
|
+
private invokerMode;
|
|
152
153
|
private invokerClientByKey;
|
|
153
154
|
constructor(config: GrpcClientConfig);
|
|
154
155
|
private createTransport;
|
|
@@ -548,6 +549,11 @@ declare class UnifiedApiClient {
|
|
|
548
549
|
private dispatch;
|
|
549
550
|
}
|
|
550
551
|
|
|
552
|
+
type KeyValueStorage = {
|
|
553
|
+
get: (key: string) => string | null;
|
|
554
|
+
set: (key: string, val: string) => void;
|
|
555
|
+
};
|
|
556
|
+
|
|
551
557
|
interface RequestApiConfig {
|
|
552
558
|
http?: {
|
|
553
559
|
baseUrl: string;
|
|
@@ -573,6 +579,7 @@ interface RequestApiConfig {
|
|
|
573
579
|
};
|
|
574
580
|
logger?: ApiLogger;
|
|
575
581
|
onApiError?: ApiErrorHandler;
|
|
582
|
+
uniqueKeyStorage?: KeyValueStorage | null;
|
|
576
583
|
}
|
|
577
584
|
/**
|
|
578
585
|
* 统一请求 API 封装
|
|
@@ -17084,11 +17091,6 @@ declare function requestApi<TReq = any, TRes = any>(args: {
|
|
|
17084
17091
|
meta?: Record<string, any>;
|
|
17085
17092
|
}): Promise<TRes>;
|
|
17086
17093
|
|
|
17087
|
-
type KeyValueStorage = {
|
|
17088
|
-
get: (key: string) => string | null;
|
|
17089
|
-
set: (key: string, val: string) => void;
|
|
17090
|
-
};
|
|
17091
|
-
|
|
17092
17094
|
type FetchGuidOptions = {
|
|
17093
17095
|
key?: string;
|
|
17094
17096
|
format?: 'uuid' | 'hex' | 'base64';
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,173 @@
|
|
|
2
2
|
import { createClient } from "@connectrpc/connect";
|
|
3
3
|
import { createConnectTransport, createGrpcWebTransport } from "@connectrpc/connect-web";
|
|
4
4
|
import { create } from "@bufbuild/protobuf";
|
|
5
|
+
|
|
6
|
+
// src/client/invokers/builtinGrpcWebInvoker.ts
|
|
7
|
+
import { fromBinary, toBinary } from "@bufbuild/protobuf";
|
|
8
|
+
function createBuiltinGrpcWebInvoker(options) {
|
|
9
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
10
|
+
const logger = options.logger;
|
|
11
|
+
if (!fetchImpl) {
|
|
12
|
+
return {
|
|
13
|
+
unary: async () => {
|
|
14
|
+
throw new Error("fetch is not available in current environment");
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const grpcWebEncodeUnary = (messageBytes) => {
|
|
19
|
+
const out = new Uint8Array(5 + messageBytes.byteLength);
|
|
20
|
+
out[0] = 0;
|
|
21
|
+
const view = new DataView(out.buffer, out.byteOffset, out.byteLength);
|
|
22
|
+
view.setUint32(1, messageBytes.byteLength, false);
|
|
23
|
+
out.set(messageBytes, 5);
|
|
24
|
+
return out;
|
|
25
|
+
};
|
|
26
|
+
const bytesToAscii = (bytes) => {
|
|
27
|
+
let s = "";
|
|
28
|
+
for (let i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i]);
|
|
29
|
+
return s;
|
|
30
|
+
};
|
|
31
|
+
const parseGrpcWebTrailers = (bytes) => {
|
|
32
|
+
const text = bytesToAscii(bytes);
|
|
33
|
+
const out = {};
|
|
34
|
+
for (const line of text.split("\r\n")) {
|
|
35
|
+
if (!line) continue;
|
|
36
|
+
const idx = line.indexOf(":");
|
|
37
|
+
if (idx < 0) continue;
|
|
38
|
+
const k = line.slice(0, idx).trim().toLowerCase();
|
|
39
|
+
const v = line.slice(idx + 1).trim();
|
|
40
|
+
out[k] = v;
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
};
|
|
44
|
+
const grpcWebDecodeUnary = (body) => {
|
|
45
|
+
let offset = 0;
|
|
46
|
+
let message;
|
|
47
|
+
let trailers = {};
|
|
48
|
+
while (offset + 5 <= body.byteLength) {
|
|
49
|
+
const flags = body[offset];
|
|
50
|
+
const length = new DataView(body.buffer, body.byteOffset + offset + 1, 4).getUint32(0, false);
|
|
51
|
+
offset += 5;
|
|
52
|
+
if (offset + length > body.byteLength) break;
|
|
53
|
+
const frame = body.subarray(offset, offset + length);
|
|
54
|
+
offset += length;
|
|
55
|
+
const isTrailer = (flags & 128) === 128;
|
|
56
|
+
if (isTrailer) {
|
|
57
|
+
trailers = { ...trailers, ...parseGrpcWebTrailers(frame) };
|
|
58
|
+
} else if (message == null) {
|
|
59
|
+
message = frame;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return { message, trailers };
|
|
63
|
+
};
|
|
64
|
+
const joinUrl = (baseUrl, path) => {
|
|
65
|
+
return baseUrl.replace(/\/+$/, "") + "/" + path.replace(/^\/+/, "");
|
|
66
|
+
};
|
|
67
|
+
const capitalizeRpcName = (methodName) => {
|
|
68
|
+
if (!methodName) return methodName;
|
|
69
|
+
return methodName[0].toUpperCase() + methodName.slice(1);
|
|
70
|
+
};
|
|
71
|
+
const getGrpcWebPath = (service, methodName, method) => {
|
|
72
|
+
const typeName = String(service?.typeName ?? "");
|
|
73
|
+
const rpcName = String(method?.name ?? "") || capitalizeRpcName(methodName);
|
|
74
|
+
return `${typeName}/${rpcName}`;
|
|
75
|
+
};
|
|
76
|
+
const safeDecodeGrpcMessage = (value) => {
|
|
77
|
+
if (!value) return value;
|
|
78
|
+
try {
|
|
79
|
+
return decodeURIComponent(value);
|
|
80
|
+
} catch {
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
unary: async (args) => {
|
|
86
|
+
const service = String(args.service?.typeName ?? "");
|
|
87
|
+
const startedAt = Date.now();
|
|
88
|
+
let url;
|
|
89
|
+
const contentType = "application/grpc-web+proto";
|
|
90
|
+
const controller = typeof AbortController !== "undefined" ? new AbortController() : void 0;
|
|
91
|
+
const timeoutMs = args.callOptions?.timeoutMs;
|
|
92
|
+
const timeoutId = controller && typeof timeoutMs === "number" ? setTimeout(() => controller.abort(), timeoutMs) : void 0;
|
|
93
|
+
try {
|
|
94
|
+
const method = args.method;
|
|
95
|
+
if (!method) {
|
|
96
|
+
throw new Error(`gRPC method not found: ${service}.${args.methodName}`);
|
|
97
|
+
}
|
|
98
|
+
url = joinUrl(args.baseUrl, getGrpcWebPath(args.service, args.methodName, method));
|
|
99
|
+
const requestBytes = toBinary(method.input, args.request);
|
|
100
|
+
const body = grpcWebEncodeUnary(requestBytes);
|
|
101
|
+
logger?.debug?.("[Grpc:Invoker:Builtin:Start]", {
|
|
102
|
+
service,
|
|
103
|
+
methodName: args.methodName,
|
|
104
|
+
baseUrl: args.baseUrl,
|
|
105
|
+
url,
|
|
106
|
+
timeoutMs,
|
|
107
|
+
requestBytes: requestBytes.byteLength
|
|
108
|
+
});
|
|
109
|
+
const res = await fetchImpl(url, {
|
|
110
|
+
method: "POST",
|
|
111
|
+
headers: {
|
|
112
|
+
...args.headers,
|
|
113
|
+
"content-type": contentType,
|
|
114
|
+
accept: contentType,
|
|
115
|
+
"x-grpc-web": "1"
|
|
116
|
+
},
|
|
117
|
+
body: body.buffer,
|
|
118
|
+
signal: controller?.signal
|
|
119
|
+
});
|
|
120
|
+
logger?.debug?.("[Grpc:Invoker:Builtin:Http]", {
|
|
121
|
+
service,
|
|
122
|
+
methodName: args.methodName,
|
|
123
|
+
url,
|
|
124
|
+
ok: res.ok,
|
|
125
|
+
status: res.status,
|
|
126
|
+
statusText: res.statusText,
|
|
127
|
+
durationMs: Date.now() - startedAt
|
|
128
|
+
});
|
|
129
|
+
if (!res.ok) {
|
|
130
|
+
throw new Error(`gRPC fetch failed: ${res.status} ${res.statusText}`);
|
|
131
|
+
}
|
|
132
|
+
const raw = new Uint8Array(await res.arrayBuffer());
|
|
133
|
+
const decoded = grpcWebDecodeUnary(raw);
|
|
134
|
+
const statusFromHeaders = typeof res.headers?.get === "function" ? res.headers.get("grpc-status") : void 0;
|
|
135
|
+
const msgFromHeaders = typeof res.headers?.get === "function" ? res.headers.get("grpc-message") : void 0;
|
|
136
|
+
const grpcStatus = decoded.trailers["grpc-status"] ?? statusFromHeaders ?? "0";
|
|
137
|
+
const grpcMessage = safeDecodeGrpcMessage(
|
|
138
|
+
decoded.trailers["grpc-message"] ?? msgFromHeaders
|
|
139
|
+
);
|
|
140
|
+
logger?.debug?.("[Grpc:Invoker:Builtin:End]", {
|
|
141
|
+
service,
|
|
142
|
+
methodName: args.methodName,
|
|
143
|
+
url,
|
|
144
|
+
grpcStatus,
|
|
145
|
+
grpcMessage,
|
|
146
|
+
durationMs: Date.now() - startedAt
|
|
147
|
+
});
|
|
148
|
+
if (grpcStatus !== "0") {
|
|
149
|
+
const msg = grpcMessage ?? "gRPC request failed";
|
|
150
|
+
throw new Error(`gRPC status=${grpcStatus} message=${msg}`);
|
|
151
|
+
}
|
|
152
|
+
if (!decoded.message) throw new Error("gRPC response message missing");
|
|
153
|
+
return fromBinary(method.output, decoded.message);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
logger?.error?.("[Grpc:Invoker:Builtin:Error]", {
|
|
156
|
+
service,
|
|
157
|
+
methodName: args.methodName,
|
|
158
|
+
baseUrl: args.baseUrl,
|
|
159
|
+
url,
|
|
160
|
+
durationMs: Date.now() - startedAt,
|
|
161
|
+
error
|
|
162
|
+
});
|
|
163
|
+
throw error;
|
|
164
|
+
} finally {
|
|
165
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// src/client/grpcClient.ts
|
|
5
172
|
var GrpcApiError = class extends Error {
|
|
6
173
|
name = "GrpcApiError";
|
|
7
174
|
service;
|
|
@@ -32,6 +199,7 @@ var GrpcClient = class {
|
|
|
32
199
|
getHeaders;
|
|
33
200
|
logger;
|
|
34
201
|
invoker;
|
|
202
|
+
invokerMode;
|
|
35
203
|
invokerClientByKey;
|
|
36
204
|
constructor(config) {
|
|
37
205
|
this.baseUrl = config.baseUrl;
|
|
@@ -42,7 +210,6 @@ var GrpcClient = class {
|
|
|
42
210
|
this.getToken = config.getToken;
|
|
43
211
|
this.headers = config.headers;
|
|
44
212
|
this.getHeaders = config.getHeaders;
|
|
45
|
-
this.invoker = config.invoker;
|
|
46
213
|
this.logger = config.logger ?? (config.debug ? { debug: console.log, info: console.log, warn: console.warn, error: console.error } : void 0);
|
|
47
214
|
this.interceptors = [
|
|
48
215
|
this.createRpcFromInterceptor(),
|
|
@@ -53,6 +220,8 @@ var GrpcClient = class {
|
|
|
53
220
|
this.transportByBaseUrl = /* @__PURE__ */ new Map();
|
|
54
221
|
this.transport = this.createTransport(this.baseUrl);
|
|
55
222
|
this.invokerClientByKey = /* @__PURE__ */ new Map();
|
|
223
|
+
this.invokerMode = config.invoker === "builtin" ? "builtin" : config.invoker ? "custom" : "none";
|
|
224
|
+
this.invoker = config.invoker === "builtin" ? createBuiltinGrpcWebInvoker({ fetch: this.fetchImpl, logger: this.logger }) : config.invoker;
|
|
56
225
|
}
|
|
57
226
|
createTransport(baseUrl) {
|
|
58
227
|
const cached = this.transportByBaseUrl.get(baseUrl);
|
|
@@ -220,6 +389,12 @@ var GrpcClient = class {
|
|
|
220
389
|
const baseUrl = this.pickBaseUrl(typeName);
|
|
221
390
|
try {
|
|
222
391
|
if (this.invoker) {
|
|
392
|
+
this.logger?.debug?.("[Grpc:Invoker]", {
|
|
393
|
+
mode: this.invokerMode,
|
|
394
|
+
service: typeName,
|
|
395
|
+
methodName,
|
|
396
|
+
baseUrl
|
|
397
|
+
});
|
|
223
398
|
const method2 = this.getServiceMethodDesc(service, methodName);
|
|
224
399
|
const headers = await this.attachInvokerHeaders(
|
|
225
400
|
this.normalizeHeaders(options?.headers),
|
|
@@ -5969,176 +6144,6 @@ var apiMap = {
|
|
|
5969
6144
|
userEngagement: userEngagementEndpoints
|
|
5970
6145
|
};
|
|
5971
6146
|
|
|
5972
|
-
// src/routing/router.ts
|
|
5973
|
-
var serviceInstances = {};
|
|
5974
|
-
var rpcClient;
|
|
5975
|
-
var unifiedClient;
|
|
5976
|
-
var endpointRegistry = /* @__PURE__ */ new Map();
|
|
5977
|
-
var endpointDefaultTransports = /* @__PURE__ */ new Map();
|
|
5978
|
-
var defaultTransport = "auto";
|
|
5979
|
-
var transportState = {
|
|
5980
|
-
hasHttp: false,
|
|
5981
|
-
hasWs: false,
|
|
5982
|
-
hasGrpc: false
|
|
5983
|
-
};
|
|
5984
|
-
function resetRoutingState() {
|
|
5985
|
-
serviceInstances = {};
|
|
5986
|
-
rpcClient = void 0;
|
|
5987
|
-
unifiedClient = void 0;
|
|
5988
|
-
endpointRegistry.clear();
|
|
5989
|
-
endpointDefaultTransports.clear();
|
|
5990
|
-
defaultTransport = "auto";
|
|
5991
|
-
transportState = { hasHttp: false, hasWs: false, hasGrpc: false };
|
|
5992
|
-
}
|
|
5993
|
-
function setServiceInstances(instances) {
|
|
5994
|
-
serviceInstances = instances;
|
|
5995
|
-
}
|
|
5996
|
-
function setRpcClient(client) {
|
|
5997
|
-
rpcClient = client;
|
|
5998
|
-
}
|
|
5999
|
-
function setUnifiedClient(client) {
|
|
6000
|
-
unifiedClient = client;
|
|
6001
|
-
}
|
|
6002
|
-
function setTransportState(state) {
|
|
6003
|
-
transportState = state;
|
|
6004
|
-
}
|
|
6005
|
-
function setDefaultTransport(transport) {
|
|
6006
|
-
defaultTransport = transport;
|
|
6007
|
-
}
|
|
6008
|
-
function makeEndpointKey(service, functionName) {
|
|
6009
|
-
return `${service}:${functionName}`;
|
|
6010
|
-
}
|
|
6011
|
-
function isEndpointSpec(value) {
|
|
6012
|
-
return !!value && typeof value === "object" && "transport" in value;
|
|
6013
|
-
}
|
|
6014
|
-
function isEndpointGroup(value) {
|
|
6015
|
-
return !!value && typeof value === "object" && "transports" in value;
|
|
6016
|
-
}
|
|
6017
|
-
function registerEndpointVariant(service, functionName, endpoint) {
|
|
6018
|
-
const key = makeEndpointKey(service, functionName);
|
|
6019
|
-
let byTransport = endpointRegistry.get(key);
|
|
6020
|
-
if (!byTransport) {
|
|
6021
|
-
byTransport = /* @__PURE__ */ new Map();
|
|
6022
|
-
endpointRegistry.set(key, byTransport);
|
|
6023
|
-
}
|
|
6024
|
-
byTransport.set(endpoint.transport, endpoint);
|
|
6025
|
-
}
|
|
6026
|
-
function rebuildEndpointRegistry() {
|
|
6027
|
-
endpointRegistry = /* @__PURE__ */ new Map();
|
|
6028
|
-
endpointDefaultTransports = /* @__PURE__ */ new Map();
|
|
6029
|
-
for (const [serviceName, service] of Object.entries(apiMap)) {
|
|
6030
|
-
for (const [functionName, endpoint] of Object.entries(service)) {
|
|
6031
|
-
if (isEndpointSpec(endpoint)) {
|
|
6032
|
-
registerEndpointVariant(serviceName, functionName, endpoint);
|
|
6033
|
-
continue;
|
|
6034
|
-
}
|
|
6035
|
-
if (isEndpointGroup(endpoint)) {
|
|
6036
|
-
if (endpoint.defaultTransport) {
|
|
6037
|
-
endpointDefaultTransports.set(
|
|
6038
|
-
makeEndpointKey(serviceName, functionName),
|
|
6039
|
-
endpoint.defaultTransport
|
|
6040
|
-
);
|
|
6041
|
-
}
|
|
6042
|
-
for (const [transport, config] of Object.entries(endpoint.transports ?? {})) {
|
|
6043
|
-
if (!config) continue;
|
|
6044
|
-
if (transport === "http") {
|
|
6045
|
-
registerEndpointVariant(serviceName, functionName, {
|
|
6046
|
-
id: endpoint.id,
|
|
6047
|
-
transport: "http",
|
|
6048
|
-
http: config,
|
|
6049
|
-
request: endpoint.request,
|
|
6050
|
-
response: endpoint.response
|
|
6051
|
-
});
|
|
6052
|
-
} else if (transport === "ws") {
|
|
6053
|
-
registerEndpointVariant(serviceName, functionName, {
|
|
6054
|
-
id: endpoint.id,
|
|
6055
|
-
transport: "ws",
|
|
6056
|
-
ws: config,
|
|
6057
|
-
request: endpoint.request,
|
|
6058
|
-
response: endpoint.response
|
|
6059
|
-
});
|
|
6060
|
-
} else if (transport === "grpc") {
|
|
6061
|
-
registerEndpointVariant(serviceName, functionName, {
|
|
6062
|
-
id: endpoint.id,
|
|
6063
|
-
transport: "grpc",
|
|
6064
|
-
grpc: config,
|
|
6065
|
-
request: endpoint.request,
|
|
6066
|
-
response: endpoint.response
|
|
6067
|
-
});
|
|
6068
|
-
}
|
|
6069
|
-
}
|
|
6070
|
-
continue;
|
|
6071
|
-
}
|
|
6072
|
-
}
|
|
6073
|
-
}
|
|
6074
|
-
}
|
|
6075
|
-
function requestApi(args) {
|
|
6076
|
-
const key = makeEndpointKey(args.service, args.functionName);
|
|
6077
|
-
const endpointVariants = endpointRegistry.get(key);
|
|
6078
|
-
if (endpointVariants) {
|
|
6079
|
-
if (!unifiedClient) {
|
|
6080
|
-
throw new Error("SDK not initialized. Please call initSDK first.");
|
|
6081
|
-
}
|
|
6082
|
-
const specDefaultTransport = endpointDefaultTransports.get(key);
|
|
6083
|
-
const endpoint = pickEndpoint(
|
|
6084
|
-
endpointVariants,
|
|
6085
|
-
args.transport,
|
|
6086
|
-
specDefaultTransport,
|
|
6087
|
-
args.service,
|
|
6088
|
-
args.functionName
|
|
6089
|
-
);
|
|
6090
|
-
if (endpoint.transport === "grpc" && !transportState.hasGrpc) {
|
|
6091
|
-
throw new Error("SDK not initialized. Please call initSDK with grpc config.");
|
|
6092
|
-
}
|
|
6093
|
-
if (endpoint.transport === "ws" && !transportState.hasWs) {
|
|
6094
|
-
throw new Error("SDK not initialized. Please call initSDK with ws config.");
|
|
6095
|
-
}
|
|
6096
|
-
if (endpoint.transport === "http" && !transportState.hasHttp) {
|
|
6097
|
-
throw new Error("SDK not initialized. Please call initSDK with http config.");
|
|
6098
|
-
}
|
|
6099
|
-
const meta = args.callOptions || args.wsOptions ? {
|
|
6100
|
-
...args.meta ?? {},
|
|
6101
|
-
...args.callOptions ? { callOptions: args.callOptions } : {},
|
|
6102
|
-
...args.wsOptions ? { wsOptions: args.wsOptions } : {}
|
|
6103
|
-
} : args.meta;
|
|
6104
|
-
return unifiedClient.call(endpoint, args.requestParam, meta);
|
|
6105
|
-
}
|
|
6106
|
-
const serviceInstance = serviceInstances[args.service];
|
|
6107
|
-
const method = serviceInstance?.[args.functionName];
|
|
6108
|
-
if (typeof method === "function") {
|
|
6109
|
-
if (args.callOptions !== void 0) {
|
|
6110
|
-
return method(args.requestParam, args.callOptions);
|
|
6111
|
-
}
|
|
6112
|
-
return method(args.requestParam);
|
|
6113
|
-
}
|
|
6114
|
-
if (!rpcClient) {
|
|
6115
|
-
throw new Error("SDK not initialized. Please call initSDK first.");
|
|
6116
|
-
}
|
|
6117
|
-
return rpcClient.call(args.functionName, args.service, args.requestParam, args.wsOptions);
|
|
6118
|
-
}
|
|
6119
|
-
function pickEndpoint(variants, transport, specDefaultTransport, service, functionName) {
|
|
6120
|
-
const resolvedTransport = transport ?? specDefaultTransport ?? defaultTransport;
|
|
6121
|
-
if (resolvedTransport !== "auto") {
|
|
6122
|
-
const found = variants.get(resolvedTransport);
|
|
6123
|
-
if (!found) {
|
|
6124
|
-
throw new Error(
|
|
6125
|
-
`Endpoint transport not found: ${service}.${functionName} (${resolvedTransport}). Available: ${Array.from(
|
|
6126
|
-
variants.keys()
|
|
6127
|
-
).join(", ")}`
|
|
6128
|
-
);
|
|
6129
|
-
}
|
|
6130
|
-
return found;
|
|
6131
|
-
}
|
|
6132
|
-
if (transportState.hasGrpc && variants.has("grpc")) return variants.get("grpc");
|
|
6133
|
-
if (transportState.hasWs && variants.has("ws")) return variants.get("ws");
|
|
6134
|
-
if (transportState.hasHttp && variants.has("http")) return variants.get("http");
|
|
6135
|
-
const fallback = variants.values().next().value;
|
|
6136
|
-
if (!fallback) {
|
|
6137
|
-
throw new Error(`Endpoint not found: ${service}.${functionName}`);
|
|
6138
|
-
}
|
|
6139
|
-
return fallback;
|
|
6140
|
-
}
|
|
6141
|
-
|
|
6142
6147
|
// src/utils/adapters/web.ts
|
|
6143
6148
|
var webStorage = {
|
|
6144
6149
|
get(key) {
|
|
@@ -6341,6 +6346,176 @@ var setUniqueKeyStorage = (adapter) => {
|
|
|
6341
6346
|
};
|
|
6342
6347
|
var fetchGuid = () => fetchGuidImpl();
|
|
6343
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
|
+
|
|
6344
6519
|
// src/index.ts
|
|
6345
6520
|
var api;
|
|
6346
6521
|
var apiErrorCenter;
|
|
@@ -6521,6 +6696,9 @@ function destroySDK() {
|
|
|
6521
6696
|
function initSDK(config) {
|
|
6522
6697
|
clearWsStatusBridge();
|
|
6523
6698
|
wsStatusSnapshot = void 0;
|
|
6699
|
+
if ("uniqueKeyStorage" in config) {
|
|
6700
|
+
setUniqueKeyStorage(config.uniqueKeyStorage);
|
|
6701
|
+
}
|
|
6524
6702
|
api = new RequestApi({
|
|
6525
6703
|
...config
|
|
6526
6704
|
});
|