1010 * ---------------------------------------------------------------
1111 */
1212
13- import { message as Message } from "@c-x /ui" ;
13+ import { message as Message } from '@ctzhian /ui' ;
1414import type {
1515 AxiosInstance ,
1616 AxiosRequestConfig ,
1717 HeadersDefaults ,
1818 ResponseType ,
19- } from " axios" ;
20- import axios from " axios" ;
19+ } from ' axios' ;
20+ import axios from ' axios' ;
2121
2222export type QueryParamsType = Record < string | number , any > ;
2323
2424export interface FullRequestParams
25- extends Omit < AxiosRequestConfig , " data" | " params" | " url" | " responseType" > {
25+ extends Omit < AxiosRequestConfig , ' data' | ' params' | ' url' | ' responseType' > {
2626 /** set parameter to `true` for call `securityWorker` for this request */
2727 secure ?: boolean ;
2828 /** request path */
@@ -39,34 +39,34 @@ export interface FullRequestParams
3939
4040export type RequestParams = Omit <
4141 FullRequestParams ,
42- " body" | " method" | " query" | " path"
42+ ' body' | ' method' | ' query' | ' path'
4343> ;
4444
4545export interface ApiConfig < SecurityDataType = unknown >
46- extends Omit < AxiosRequestConfig , " data" | " cancelToken" > {
46+ extends Omit < AxiosRequestConfig , ' data' | ' cancelToken' > {
4747 securityWorker ?: (
48- securityData : SecurityDataType | null ,
48+ securityData : SecurityDataType | null
4949 ) => Promise < AxiosRequestConfig | void > | AxiosRequestConfig | void ;
5050 secure ?: boolean ;
5151 format ?: ResponseType ;
5252}
5353
5454export enum ContentType {
55- Json = " application/json" ,
56- FormData = " multipart/form-data" ,
57- UrlEncoded = " application/x-www-form-urlencoded" ,
58- Text = " text/plain" ,
55+ Json = ' application/json' ,
56+ FormData = ' multipart/form-data' ,
57+ UrlEncoded = ' application/x-www-form-urlencoded' ,
58+ Text = ' text/plain' ,
5959}
6060
61- const whitePathnameList = [ " /user/login" , " /login" , " /auth" , " /invite" ] ;
62- const whiteApiList = [ " /api/v1/user/profile" , " /api/v1/admin/profile" ] ;
61+ const whitePathnameList = [ ' /user/login' , ' /login' , ' /auth' , ' /invite' ] ;
62+ const whiteApiList = [ ' /api/v1/user/profile' , ' /api/v1/admin/profile' ] ;
6363
6464const redirectToLogin = ( ) => {
6565 const redirectAfterLogin = encodeURIComponent ( location . href ) ;
6666 const search = `redirect=${ redirectAfterLogin } ` ;
67- const pathname = location . pathname . startsWith ( " /user" )
68- ? " /login"
69- : " /login/admin" ;
67+ const pathname = location . pathname . startsWith ( ' /user' )
68+ ? ' /login'
69+ : ' /login/admin' ;
7070 window . location . href = `${ pathname } ` ;
7171} ;
7272
@@ -75,7 +75,7 @@ type ExtractDataProp<T> = T extends { data?: infer U } ? U : never;
7575export class HttpClient < SecurityDataType = unknown > {
7676 public instance : AxiosInstance ;
7777 private securityData : SecurityDataType | null = null ;
78- private securityWorker ?: ApiConfig < SecurityDataType > [ " securityWorker" ] ;
78+ private securityWorker ?: ApiConfig < SecurityDataType > [ ' securityWorker' ] ;
7979 private secure ?: boolean ;
8080 private format ?: ResponseType ;
8181
@@ -88,7 +88,7 @@ export class HttpClient<SecurityDataType = unknown> {
8888 this . instance = axios . create ( {
8989 withCredentials : true ,
9090 ...axiosConfig ,
91- baseURL : axiosConfig . baseURL || "" ,
91+ baseURL : axiosConfig . baseURL || '' ,
9292 } ) ;
9393 this . secure = secure ;
9494 this . format = format ;
@@ -107,20 +107,20 @@ export class HttpClient<SecurityDataType = unknown> {
107107 if (
108108 whitePathnameList . find ( ( item ) => location . pathname . startsWith ( item ) )
109109 ) {
110- return Promise . reject ( " 尚未登录" ) ;
110+ return Promise . reject ( ' 尚未登录' ) ;
111111 }
112- Message . error ( " 尚未登录" ) ;
112+ Message . error ( ' 尚未登录' ) ;
113113 redirectToLogin ( ) ;
114- return Promise . reject ( " 尚未登录" ) ;
114+ return Promise . reject ( ' 尚未登录' ) ;
115115 }
116116 // 手动取消请求
117- if ( err . code === " ERR_CANCELED" ) {
117+ if ( err . code === ' ERR_CANCELED' ) {
118118 return ;
119119 }
120120 const msg = err ?. response ?. data ?. message || err ?. message ;
121121 Message . error ( msg ) ;
122122 return Promise . reject ( msg ) ;
123- } ,
123+ }
124124 ) ;
125125 }
126126
@@ -130,7 +130,7 @@ export class HttpClient<SecurityDataType = unknown> {
130130
131131 protected mergeRequestParams (
132132 params1 : AxiosRequestConfig ,
133- params2 ?: AxiosRequestConfig ,
133+ params2 ?: AxiosRequestConfig
134134 ) : AxiosRequestConfig {
135135 const method = params1 . method || ( params2 && params2 . method ) ;
136136
@@ -151,7 +151,7 @@ export class HttpClient<SecurityDataType = unknown> {
151151 }
152152
153153 protected stringifyFormItem ( formItem : unknown ) {
154- if ( typeof formItem === " object" && formItem !== null ) {
154+ if ( typeof formItem === ' object' && formItem !== null ) {
155155 return JSON . stringify ( formItem ) ;
156156 } else {
157157 return `${ formItem } ` ;
@@ -168,7 +168,7 @@ export class HttpClient<SecurityDataType = unknown> {
168168 const isFileType = formItem instanceof Blob || formItem instanceof File ;
169169 formData . append (
170170 key ,
171- isFileType ? formItem : this . stringifyFormItem ( formItem ) ,
171+ isFileType ? formItem : this . stringifyFormItem ( formItem )
172172 ) ;
173173 }
174174
@@ -186,7 +186,7 @@ export class HttpClient<SecurityDataType = unknown> {
186186 ...params
187187 } : FullRequestParams ) : Promise < ExtractDataProp < T > > => {
188188 const secureParams =
189- ( ( typeof secure === " boolean" ? secure : this . secure ) &&
189+ ( ( typeof secure === ' boolean' ? secure : this . secure ) &&
190190 this . securityWorker &&
191191 ( await this . securityWorker ( this . securityData ) ) ) ||
192192 { } ;
@@ -197,7 +197,7 @@ export class HttpClient<SecurityDataType = unknown> {
197197 type === ContentType . FormData &&
198198 body &&
199199 body !== null &&
200- typeof body === " object"
200+ typeof body === ' object'
201201 ) {
202202 body = this . createFormData ( body as Record < string , unknown > ) ;
203203 }
@@ -206,7 +206,7 @@ export class HttpClient<SecurityDataType = unknown> {
206206 type === ContentType . Text &&
207207 body &&
208208 body !== null &&
209- typeof body !== " string"
209+ typeof body !== ' string'
210210 ) {
211211 body = JSON . stringify ( body ) ;
212212 }
@@ -216,7 +216,7 @@ export class HttpClient<SecurityDataType = unknown> {
216216 headers : {
217217 ...( requestParams . headers || { } ) ,
218218 ...( type && type !== ContentType . FormData
219- ? { " Content-Type" : type }
219+ ? { ' Content-Type' : type }
220220 : { } ) ,
221221 } ,
222222 params : query ,
@@ -226,4 +226,4 @@ export class HttpClient<SecurityDataType = unknown> {
226226 } ) ;
227227 } ;
228228}
229- export default new HttpClient ( { format : " json" } ) . request ;
229+ export default new HttpClient ( { format : ' json' } ) . request ;
0 commit comments