MCPcopy
hub / github.com/tsrpc/tsrpc / callApi

Method callApi

src/client/http/HttpClient.ts:35–85  ·  view source on GitHub ↗
(apiName: T, req: ServiceType['req'][T], options: TransportOptions = {})

Source from the content-addressed store, hash-verified

33 }
34
35 callApi<T extends keyof ServiceType['req']>(apiName: T, req: ServiceType['req'][T], options: TransportOptions = {}): SuperPromise<ServiceType['res'][T], TsrpcError> {
36 let sn = this._snCounter.getNext();
37 this.logger.log(`[ApiReq] #${sn}`, apiName, req);
38
39 // GetService
40 let service = this.serviceMap.apiName2Service[apiName as string];
41 if (!service) {
42 throw new Error('Invalid api name: ' + apiName);
43 }
44
45 // Encode
46 let buf = TransportDataUtil.encodeApiReq(this.tsbuffer, service, req);
47
48 // Send
49 return this._sendBuf(buf, options, 'api', sn).then(resBuf => {
50 if (!resBuf) {
51 throw new TsrpcError('Unknown Error', {
52 code: 'EMPTY_RES',
53 isServerOutputError: true
54 })
55 }
56
57 // Parsed res
58 let parsed: ParsedServerOutput;
59 try {
60 parsed = TransportDataUtil.parseServerOutout(this.tsbuffer, this.serviceMap, resBuf);
61 }
62 catch (e) {
63 throw new TsrpcError('Invalid server output', {
64 code: 'INVALID_SERVER_OUTPUT',
65 isServerOutputError: true,
66 innerError: e,
67 buf: resBuf
68 });
69 }
70 if (parsed.type !== 'api') {
71 throw new TsrpcError('Invalid response', {
72 code: 'INVALID_API_ID',
73 isServerOutputError: true
74 });
75 }
76 if (parsed.isSucc) {
77 this.logger.log(`[ApiRes] #${sn}`, parsed.res)
78 return parsed.res;
79 }
80 else {
81 this.logger.log(`[ApiErr] #${sn}`, parsed.error)
82 throw parsed.error;
83 }
84 })
85 }
86
87 sendMsg<T extends keyof ServiceType['msg']>(msgName: T, msg: ServiceType['msg'][T], options: TransportOptions = {}): SuperPromise<void, TsrpcError> {
88 let sn = this._snCounter.getNext();

Callers

nothing calls this directly

Calls 5

_sendBufMethod · 0.95
getNextMethod · 0.80
encodeApiReqMethod · 0.80
parseServerOutoutMethod · 0.80
logMethod · 0.65

Tested by

no test coverage detected