@goplus123/core-api 1.0.4 → 1.0.6
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 +189 -0
- package/dist/index.cjs +44 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +44 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -94,6 +94,7 @@ type GrpcInvokerUnaryArgs = {
|
|
|
94
94
|
baseUrl: string;
|
|
95
95
|
service: DescService;
|
|
96
96
|
methodName: string;
|
|
97
|
+
method?: unknown;
|
|
97
98
|
request: unknown;
|
|
98
99
|
headers: Record<string, string>;
|
|
99
100
|
callOptions?: CallOptions;
|
|
@@ -156,6 +157,8 @@ declare class GrpcClient {
|
|
|
156
157
|
private hasHeader;
|
|
157
158
|
private attachInvokerHeaders;
|
|
158
159
|
private getInvokerClientKey;
|
|
160
|
+
private getServiceMethodMap;
|
|
161
|
+
private getServiceMethodDesc;
|
|
159
162
|
private getServiceViaInvoker;
|
|
160
163
|
private invokeUnary;
|
|
161
164
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -94,6 +94,7 @@ type GrpcInvokerUnaryArgs = {
|
|
|
94
94
|
baseUrl: string;
|
|
95
95
|
service: DescService;
|
|
96
96
|
methodName: string;
|
|
97
|
+
method?: unknown;
|
|
97
98
|
request: unknown;
|
|
98
99
|
headers: Record<string, string>;
|
|
99
100
|
callOptions?: CallOptions;
|
|
@@ -156,6 +157,8 @@ declare class GrpcClient {
|
|
|
156
157
|
private hasHeader;
|
|
157
158
|
private attachInvokerHeaders;
|
|
158
159
|
private getInvokerClientKey;
|
|
160
|
+
private getServiceMethodMap;
|
|
161
|
+
private getServiceMethodDesc;
|
|
159
162
|
private getServiceViaInvoker;
|
|
160
163
|
private invokeUnary;
|
|
161
164
|
/**
|
package/dist/index.js
CHANGED
|
@@ -148,13 +148,52 @@ var GrpcClient = class {
|
|
|
148
148
|
getInvokerClientKey(service, baseUrl) {
|
|
149
149
|
return `${String(service?.typeName ?? "")}@@${baseUrl}`;
|
|
150
150
|
}
|
|
151
|
+
getServiceMethodMap(service) {
|
|
152
|
+
const out = {};
|
|
153
|
+
const addMethod = (m) => {
|
|
154
|
+
if (!m) return;
|
|
155
|
+
const localName = m.localName != null ? String(m.localName) : "";
|
|
156
|
+
const name = m.name != null ? String(m.name) : "";
|
|
157
|
+
if (localName) out[localName] = m;
|
|
158
|
+
if (name && !(name in out)) out[name] = m;
|
|
159
|
+
};
|
|
160
|
+
const direct = service?.method;
|
|
161
|
+
if (direct && typeof direct === "object") {
|
|
162
|
+
for (const [k, v] of Object.entries(direct)) {
|
|
163
|
+
if (v) out[k] = v;
|
|
164
|
+
addMethod(v);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const methods = service?.methods;
|
|
168
|
+
if (Array.isArray(methods)) {
|
|
169
|
+
for (const m of methods) addMethod(m);
|
|
170
|
+
} else if (methods && typeof methods === "object") {
|
|
171
|
+
for (const [k, v] of Object.entries(methods)) {
|
|
172
|
+
if (v) out[k] = v;
|
|
173
|
+
addMethod(v);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
177
|
+
}
|
|
178
|
+
getServiceMethodDesc(service, methodName) {
|
|
179
|
+
const methodMap = this.getServiceMethodMap(service);
|
|
180
|
+
if (!methodMap) return void 0;
|
|
181
|
+
const direct = methodMap[methodName];
|
|
182
|
+
if (direct) return direct;
|
|
183
|
+
for (const m of Object.values(methodMap)) {
|
|
184
|
+
if (!m) continue;
|
|
185
|
+
const localName = m.localName != null ? String(m.localName) : "";
|
|
186
|
+
const name = m.name != null ? String(m.name) : "";
|
|
187
|
+
if (localName === methodName || name === methodName) return m;
|
|
188
|
+
}
|
|
189
|
+
return void 0;
|
|
190
|
+
}
|
|
151
191
|
getServiceViaInvoker(service) {
|
|
152
192
|
const baseUrl = this.pickBaseUrl(service.typeName ?? "");
|
|
153
193
|
const key = this.getInvokerClientKey(service, baseUrl);
|
|
154
194
|
const cached = this.invokerClientByKey.get(key);
|
|
155
195
|
if (cached) return cached;
|
|
156
|
-
const
|
|
157
|
-
const hasMethodsMap = methods && typeof methods === "object";
|
|
196
|
+
const methodMap = this.getServiceMethodMap(service);
|
|
158
197
|
const typeName = String(service?.typeName ?? "");
|
|
159
198
|
const client = new Proxy(
|
|
160
199
|
{},
|
|
@@ -162,7 +201,7 @@ var GrpcClient = class {
|
|
|
162
201
|
get: (_target, prop) => {
|
|
163
202
|
if (prop === "then") return void 0;
|
|
164
203
|
if (typeof prop !== "string") return void 0;
|
|
165
|
-
if (
|
|
204
|
+
if (methodMap && !(prop in methodMap)) {
|
|
166
205
|
return async () => {
|
|
167
206
|
throw new Error(`gRPC method not found: ${typeName}.${prop}`);
|
|
168
207
|
};
|
|
@@ -181,6 +220,7 @@ var GrpcClient = class {
|
|
|
181
220
|
const baseUrl = this.pickBaseUrl(typeName);
|
|
182
221
|
try {
|
|
183
222
|
if (this.invoker) {
|
|
223
|
+
const method2 = this.getServiceMethodDesc(service, methodName);
|
|
184
224
|
const headers = await this.attachInvokerHeaders(
|
|
185
225
|
this.normalizeHeaders(options?.headers),
|
|
186
226
|
methodName
|
|
@@ -195,6 +235,7 @@ var GrpcClient = class {
|
|
|
195
235
|
baseUrl,
|
|
196
236
|
service,
|
|
197
237
|
methodName,
|
|
238
|
+
method: method2,
|
|
198
239
|
request,
|
|
199
240
|
headers,
|
|
200
241
|
callOptions: options
|