type.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
  2. export type ContentType =
  3. | 'text/html'
  4. | 'text/plain'
  5. | 'multipart/form-data'
  6. | 'application/json'
  7. | 'application/x-www-form-urlencoded'
  8. | 'application/octet-stream';
  9. export type ResponseTransform<Input = any, Output = any> = (input: Input) => Output | Promise<Output>;
  10. export interface RequestOption<
  11. ResponseData,
  12. ApiData = ResponseData,
  13. State extends Record<string, unknown> = Record<string, unknown>
  14. > {
  15. /**
  16. * The default state
  17. */
  18. defaultState?: State;
  19. /**
  20. * transform the response data to the api data
  21. *
  22. * @param response Axios response
  23. */
  24. transform: ResponseTransform<AxiosResponse<ResponseData>, ApiData>;
  25. /**
  26. * transform the response data to the api data
  27. *
  28. * @deprecated use `transform` instead, will be removed in the next major version v3
  29. * @param response Axios response
  30. */
  31. transformBackendResponse: ResponseTransform<AxiosResponse<ResponseData>, ApiData>;
  32. /**
  33. * The hook before request
  34. *
  35. * For example: You can add header token in this hook
  36. *
  37. * @param config Axios config
  38. */
  39. onRequest: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
  40. /**
  41. * The hook to check backend response is success or not
  42. *
  43. * @param response Axios response
  44. */
  45. isBackendSuccess: (response: AxiosResponse<ResponseData>) => boolean;
  46. /**
  47. * The hook after backend request fail
  48. *
  49. * For example: You can handle the expired token in this hook
  50. *
  51. * @param response Axios response
  52. * @param instance Axios instance
  53. */
  54. onBackendFail: (
  55. response: AxiosResponse<ResponseData>,
  56. instance: AxiosInstance
  57. ) => Promise<AxiosResponse | null> | Promise<void>;
  58. /**
  59. * The hook to handle error
  60. *
  61. * For example: You can show error message in this hook
  62. *
  63. * @param error
  64. */
  65. onError: (error: AxiosError<ResponseData>) => void | Promise<void>;
  66. }
  67. interface ResponseMap {
  68. blob: Blob;
  69. text: string;
  70. arrayBuffer: ArrayBuffer;
  71. stream: ReadableStream<Uint8Array>;
  72. document: Document;
  73. }
  74. export type ResponseType = keyof ResponseMap | 'json';
  75. export type MappedType<R extends ResponseType, JsonType = any> = R extends keyof ResponseMap
  76. ? ResponseMap[R]
  77. : JsonType;
  78. export type CustomAxiosRequestConfig<R extends ResponseType = 'json'> = Omit<AxiosRequestConfig, 'responseType'> & {
  79. responseType?: R;
  80. };
  81. export interface RequestInstanceCommon<State extends Record<string, unknown>> {
  82. /**
  83. * cancel all request
  84. *
  85. * if the request provide abort controller sign from config, it will not collect in the abort controller map
  86. */
  87. cancelAllRequest: () => void;
  88. /** you can set custom state in the request instance */
  89. state: State;
  90. }
  91. /** The request instance */
  92. export interface RequestInstance<ApiData, State extends Record<string, unknown>> extends RequestInstanceCommon<State> {
  93. <T extends ApiData = ApiData, R extends ResponseType = 'json'>(
  94. config: CustomAxiosRequestConfig<R>
  95. ): Promise<MappedType<R, T>>;
  96. }
  97. export type FlatResponseSuccessData<ResponseData, ApiData> = {
  98. data: ApiData;
  99. error: null;
  100. response: AxiosResponse<ResponseData>;
  101. };
  102. export type FlatResponseFailData<ResponseData> = {
  103. data: null;
  104. error: AxiosError<ResponseData>;
  105. response: AxiosResponse<ResponseData>;
  106. };
  107. export type FlatResponseData<ResponseData, ApiData> =
  108. | FlatResponseSuccessData<ResponseData, ApiData>
  109. | FlatResponseFailData<ResponseData>;
  110. export interface FlatRequestInstance<ResponseData, ApiData, State extends Record<string, unknown>>
  111. extends RequestInstanceCommon<State> {
  112. <T extends ApiData = ApiData, R extends ResponseType = 'json'>(
  113. config: CustomAxiosRequestConfig<R>
  114. ): Promise<FlatResponseData<ResponseData, MappedType<R, T>>>;
  115. }