index.mjs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*!
  2. * TSRPC Miniapp v3.4.16
  3. * -----------------------------------------
  4. * Copyright (c) King Wang.
  5. * MIT License
  6. * https://github.com/k8w/tsrpc-miniapp
  7. */
  8. import 'k8w-extend-native';
  9. import { __extends, __assign } from 'tslib';
  10. import { BaseHttpClient, defaultBaseHttpClientOptions, BaseWsClient, defaultBaseWsClientOptions } from 'tsrpc-base-client';
  11. import { TsrpcError } from 'tsrpc-proto';
  12. export * from 'tsrpc-proto';
  13. var HttpProxy = /** @class */ (function () {
  14. function HttpProxy() {
  15. }
  16. HttpProxy.prototype.fetch = function (options) {
  17. var _a;
  18. if (!this.miniappObj) {
  19. return {
  20. abort: function () { },
  21. promise: Promise.resolve({
  22. isSucc: false,
  23. err: new TsrpcError('miniappObj is not set, please check if this is miniapp environment.', { type: TsrpcError.Type.ClientError })
  24. })
  25. };
  26. }
  27. var rs;
  28. var promise = new Promise(function (_rs) {
  29. rs = _rs;
  30. });
  31. var data;
  32. if (typeof options.data === 'string') {
  33. data = options.data;
  34. }
  35. else {
  36. var buf = options.data;
  37. if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
  38. data = buf.buffer;
  39. }
  40. else {
  41. data = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
  42. }
  43. }
  44. var reqTask = this.miniappObj.request({
  45. url: options.url,
  46. data: data,
  47. method: options.method,
  48. header: (_a = options.headers) !== null && _a !== void 0 ? _a : {
  49. 'content-type': 'application/octet-stream'
  50. },
  51. dataType: '其他',
  52. responseType: options.responseType,
  53. success: function (res) {
  54. if (res.statusCode === 200 || res.statusCode === 500) {
  55. rs({
  56. isSucc: true,
  57. res: typeof res.data === 'string' ? res.data : new Uint8Array(res.data)
  58. });
  59. }
  60. else {
  61. rs({
  62. isSucc: false,
  63. err: new TsrpcError({
  64. message: 'HTTP Error ' + res.statusCode,
  65. type: TsrpcError.Type.ServerError,
  66. httpCode: res.statusCode
  67. })
  68. });
  69. }
  70. },
  71. fail: function (res) {
  72. rs({
  73. isSucc: false,
  74. err: new TsrpcError({
  75. message: 'Network Error',
  76. type: TsrpcError.Type.NetworkError,
  77. innerErr: res
  78. })
  79. });
  80. }
  81. });
  82. var abort = reqTask.abort.bind(reqTask);
  83. return {
  84. promise: promise,
  85. abort: abort
  86. };
  87. };
  88. return HttpProxy;
  89. }());
  90. /**
  91. * Client for TSRPC HTTP Server.
  92. * It uses native http module of NodeJS.
  93. * @typeParam ServiceType - `ServiceType` from generated `proto.ts`
  94. */
  95. var HttpClient = /** @class */ (function (_super) {
  96. __extends(HttpClient, _super);
  97. function HttpClient(proto, options) {
  98. var _this = this;
  99. var httpProxy = new HttpProxy;
  100. _this = _super.call(this, proto, httpProxy, __assign(__assign({}, defaultHttpClientOptions), options)) || this;
  101. httpProxy.miniappObj = _this.options.miniappObj;
  102. return _this;
  103. }
  104. return HttpClient;
  105. }(BaseHttpClient));
  106. var defaultHttpClientOptions = __assign(__assign({}, defaultBaseHttpClientOptions), { miniappObj: typeof wx !== 'undefined' ? wx : undefined, customObjectIdClass: String });
  107. var WebSocketProxy = /** @class */ (function () {
  108. function WebSocketProxy() {
  109. }
  110. WebSocketProxy.prototype.connect = function (server, protocols) {
  111. var _this = this;
  112. var ws = this.miniappObj.connectSocket(__assign(__assign({}, this.client.options.connectSocketOptions), { url: server, protocols: protocols }));
  113. this._ws = ws;
  114. this._ws.onOpen(function (header) {
  115. if (_this._ws !== ws) {
  116. return;
  117. }
  118. _this.options.onOpen();
  119. });
  120. this._ws.onError(function (res) {
  121. if (_this._ws !== ws) {
  122. return;
  123. }
  124. ws.close();
  125. _this._ws = undefined;
  126. _this.options.onError(res);
  127. });
  128. this._ws.onClose(function (e) {
  129. if (_this._ws !== ws) {
  130. return;
  131. }
  132. _this._ws = undefined;
  133. _this.options.onClose(e.code, e.reason);
  134. });
  135. this._ws.onMessage(function (e) {
  136. if (_this._ws !== ws) {
  137. return;
  138. }
  139. if (typeof e.data === 'string') {
  140. _this.options.onMessage(e.data);
  141. }
  142. else {
  143. _this.options.onMessage(new Uint8Array(e.data));
  144. }
  145. });
  146. };
  147. WebSocketProxy.prototype.close = function (code, reason) {
  148. var _this = this;
  149. var _a;
  150. (_a = this._ws) === null || _a === void 0 ? void 0 : _a.close({
  151. code: code,
  152. reason: reason,
  153. fail: function (res) {
  154. var _a;
  155. // 重试一次
  156. console.error('WebSocket closed failed', res);
  157. (_a = _this._ws) === null || _a === void 0 ? void 0 : _a.close();
  158. }
  159. });
  160. this._ws = undefined;
  161. };
  162. WebSocketProxy.prototype.send = function (data) {
  163. var _this = this;
  164. var sendData;
  165. if (typeof data === 'string') {
  166. sendData = data;
  167. }
  168. else {
  169. var buf = data;
  170. if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
  171. sendData = buf.buffer;
  172. }
  173. else {
  174. sendData = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
  175. }
  176. }
  177. return new Promise(function (rs) {
  178. _this._ws.send({
  179. data: sendData,
  180. success: function () { rs({}); },
  181. fail: function (res) {
  182. rs({
  183. err: new TsrpcError({
  184. message: 'Network Error',
  185. type: TsrpcError.Type.NetworkError,
  186. innerErr: res
  187. })
  188. });
  189. }
  190. });
  191. });
  192. };
  193. return WebSocketProxy;
  194. }());
  195. /**
  196. * Client for TSRPC WebSocket Server.
  197. * @typeParam ServiceType - `ServiceType` from generated `proto.ts`
  198. */
  199. var WsClient = /** @class */ (function (_super) {
  200. __extends(WsClient, _super);
  201. function WsClient(proto, options) {
  202. var _this = this;
  203. var wsp = new WebSocketProxy();
  204. _this = _super.call(this, proto, wsp, __assign(__assign({}, defaultWsClientOptions), options)) || this;
  205. if (!_this.options.miniappObj) {
  206. throw new Error('options.miniappObj is not set');
  207. }
  208. wsp.miniappObj = _this.options.miniappObj;
  209. wsp.client = _this;
  210. return _this;
  211. }
  212. return WsClient;
  213. }(BaseWsClient));
  214. var defaultWsClientOptions = __assign(__assign({}, defaultBaseWsClientOptions), { miniappObj: typeof wx !== 'undefined' ? wx : undefined, customObjectIdClass: String });
  215. export { HttpClient, WsClient };