RequestApiResult.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * API 返回结构体定义
  3. *
  4. * 功能介绍:
  5. * 这里定义了API返回数据的基本结构体,分为正常结果和错误结果。
  6. *
  7. * Author: imengyu
  8. * Date: 2020/09/28
  9. *
  10. * Copyright (c) 2021 imengyu.top. Licensed under the MIT License.
  11. * See License.txt in the project root for license information.
  12. */
  13. import { DataModel, type NewDataModel } from "@imengyu/js-request-transform";
  14. import type { KeyValue } from "@imengyu/js-request-transform/dist/DataUtils";
  15. /**
  16. * API 的返回结构体
  17. */
  18. export class RequestApiResult<T extends DataModel> {
  19. public code = 0;
  20. public message = '';
  21. public data: T|KeyValue|null = null;
  22. /**
  23. * 无类型数据
  24. */
  25. public data2: any = null;
  26. public raw: any = null;
  27. public constructor(c: NewDataModel|null, code? : number, message? : string, data?: Record<string, unknown>|null, rawData?: Record<string, unknown>|null) {
  28. if (typeof code !== 'undefined')
  29. this.code = code;
  30. if (typeof message !== 'undefined')
  31. this.message = message;
  32. //转换数据
  33. if (typeof data !== 'undefined' && c)
  34. this.data = new c().fromServerSide(data as KeyValue) as T;//转换data
  35. else if (typeof rawData !== 'undefined' && c)
  36. this.data = new c().fromServerSide(rawData as KeyValue) as T;//如果data为空则转换rawData
  37. else
  38. this.data = data as KeyValue as T; //原始数据
  39. if (typeof rawData !== 'undefined')
  40. this.raw = rawData;
  41. else
  42. this.raw = this.data;
  43. this.data2 = this.data;
  44. }
  45. /**
  46. * 使用另一个数据实例克隆当前结果
  47. * @param model 另一个数据
  48. * @returns
  49. */
  50. public cloneWithOtherModel<U extends DataModel>(model: U) : RequestApiResult<U> {
  51. return new RequestApiResult(
  52. null,
  53. this.code,
  54. this.message,
  55. model.keyValue(),
  56. this.raw
  57. );
  58. }
  59. /**
  60. * 转为纯JSON格式
  61. * @returns
  62. */
  63. public keyValueData() : KeyValue {
  64. return (this.data instanceof DataModel ? this.data?.keyValue() : this.data) || {};
  65. }
  66. /**
  67. * 转为字符串表达形式
  68. * @returns
  69. */
  70. public toString() : string {
  71. return `${this.code} ${this.message} data: ${JSON.stringify(this.data)} raw: ` + JSON.stringify(this.raw);
  72. }
  73. }
  74. /**
  75. * 指示这个错误发生的类型
  76. */
  77. export type RequestApiErrorType = 'networkError'|'statusError'|'serverError'|'businessError'|'scriptError'|'unknow';
  78. /**
  79. * API 的错误信息
  80. */
  81. export class RequestApiError {
  82. /**
  83. * 本次请求错误的 API 名字
  84. */
  85. public apiName = '';
  86. /**
  87. * 本次请求错误的 API URL
  88. */
  89. public apiUrl = '';
  90. /**
  91. * 指示这个错误发生的类型
  92. * * networkError:网络连接错误
  93. * * statusError:状态错误(返回了400-499错误状态码)
  94. * * serverError:服务器错误(返回了500-599错误状态码)
  95. * * businessError:业务错误(状态码200,但是自定义判断条件失败)
  96. * * scriptError:脚本错误(通常是代码异常被catch)
  97. */
  98. public errorType : RequestApiErrorType = 'unknow';
  99. /**
  100. * 错误信息
  101. */
  102. public errorMessage: string;
  103. /**
  104. * code的错误信息
  105. */
  106. public errorCodeMessage: string;
  107. /**
  108. * 错误代号
  109. */
  110. public code = 0;
  111. /**
  112. * 本次请求的返回数据
  113. */
  114. public data: KeyValue|null = null;
  115. /**
  116. * 本次请求的原始返回数据
  117. */
  118. public rawData: KeyValue|null = null;
  119. /**
  120. * 本次请求的原始参数
  121. */
  122. public rawRequest: RequestInit|null = null;
  123. public constructor(
  124. errorType: RequestApiErrorType,
  125. errorMessage = '',
  126. errorCodeMessage = '',
  127. code = 0,
  128. data: KeyValue|null = null,
  129. rawData: unknown|null = null,
  130. rawRequest: RequestInit|null = null,
  131. apiName = '',
  132. apiUrl = ''
  133. ) {
  134. this.errorType = errorType;
  135. this.errorMessage = errorMessage;
  136. this.errorCodeMessage = errorCodeMessage;
  137. this.code = code;
  138. this.data = data;
  139. this.apiName = apiName;
  140. this.apiUrl = apiUrl;
  141. this.rawData = rawData as KeyValue;
  142. this.rawRequest = rawRequest as KeyValue;
  143. }
  144. /**
  145. * 转为详情格式
  146. * @returns
  147. */
  148. public toStringDetail() {
  149. return `请求${this.apiName}错误 ${this.errorMessage} (${this.errorType}) ${this.code}(${this.errorCodeMessage})\n` +
  150. `url: ${this.apiUrl}\n` +
  151. `data: ${JSON.stringify(this.data)}\n` +
  152. `rawData: ${JSON.stringify(this.rawData)}\n` +
  153. `rawRequest: ${JSON.stringify(this.rawRequest)}\n`;
  154. }
  155. /**
  156. * 转为字符串表达形式
  157. * @returns
  158. */
  159. public toString(): string {
  160. return this.errorMessage;
  161. }
  162. }