index.js 8.1 KB

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