@uxda/appkit 1.0.64 → 1.0.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,99 +1,82 @@
1
- /**
2
- *
3
- */
4
-
5
-
1
+ import { HttpInstance, HttpClientConfig,
2
+ HttpMethod, HttpRequestConfig,
3
+ RequestData, ResponseData,
4
+ ResponseRaw } from './types'
6
5
  /**
7
6
  * Useage:
8
- * const $http = useHttp()
7
+ * const $http = createHttp({
8
+ *
9
+ * })
9
10
  * $http.get('/url').then(data => {
10
- *
11
+ *
11
12
  * })
12
13
  */
13
14
 
14
- import Taro from '@tarojs/taro'
15
- import { Http, HttpClientConfig, HttpMethod, HttpRequestConfig, RequestData, ResponseData, ResponseRaw } from './types'
16
-
17
- /**
18
- * 场景配置
19
- * 本地后台团队统一返回值外层
20
- */
21
- const defaultClientConfig: HttpClientConfig = {
22
- baseUrl: 'https://ytech.ddjf.com',
23
- response: {
24
- getCode: (data: ResponseRaw) => data.code,
25
- getMessage: (data: ResponseRaw) => data.msg,
26
- getData: (data: ResponseRaw) => data.result
27
- },
28
- interceptors: {
29
- auth: (data) => data.code == '401',
30
- server: (data) => false,
31
- }
32
- }
33
-
34
- let clientConfig = {
35
- ...defaultClientConfig
36
- }
37
-
38
15
  /**
39
16
  * 统一请求过程
40
- * @param config
41
- * @returns
17
+ * @param config
18
+ * @returns
42
19
  */
43
- const request: Http['request'] = <T>(config: HttpRequestConfig) => {
44
- clientConfig = {
20
+ const request: HttpInstance['request'] = <T>(config: HttpRequestConfig) => {
21
+ const c = {
45
22
  ...clientConfig,
46
23
  ...config,
47
24
  }
48
25
  return new Promise<T>((resolve, reject) => {
49
26
  const data = config.data
50
27
  && clientConfig.translates
51
- && clientConfig.translates[config.url]
52
- ? clientConfig.translates[config.url](config.data)
53
- : config.data
54
- console.log(`[][][][][][][][][][][][] HTTP.${config.method}, ${clientConfig.baseUrl}${config.url}`, data)
55
- Taro.request<ResponseData>({
56
- url: `${clientConfig.baseUrl}${config.url}`,
28
+ && clientConfig.translates[c.url]
29
+ ? clientConfig.translates[c.url]?.(c.data || {})
30
+ : c.data
31
+ console.log(`[][][][][]HTTP.${c.method}, ${c.baseUrl}${c.url}`, data)
32
+ clientConfig.vendor?.request({
33
+ url: `${c.baseUrl}${c.url}`,
57
34
  data,
58
- header: clientConfig.header,
59
- method: config.method,
60
- success (res: Taro.request.SuccessCallbackResult<ResponseRaw>) {
61
- if (!isSuccess(res)) {
62
- // 拦截 statusCode
63
- reject(`==接口报错==${res.statusCode}`)
64
- reject({})
65
- }
66
- const { data: raw } = res
67
- if (clientConfig.interceptors?.auth(raw)) {
68
- //拦截 401
69
- clientConfig.onAuthError?.()
70
- reject('===401===')
71
- }
72
- if (clientConfig.interceptors?.server(raw)) {
73
- // 拦截后端错误
74
- clientConfig.onServerError?.()
75
- reject('server error')
35
+ headers: c.headers,
36
+ method: c.method,
37
+ }).then((raw: ResponseRaw) => {
38
+ // 按顺序执行拦截器
39
+ for (const interc of c.interceptors || []) {
40
+ const r = interc(raw)
41
+ if (r) {
42
+ // 某拦截器命中时
43
+ // 按拦截结果 决定是否继续执行
44
+ reject('===INTERCEPTED===' + raw.status)
45
+ return false
76
46
  }
77
- const response = clientConfig.response?.getData(raw)
78
- if (response) {
79
- // 当用户配置含有 transforms 时, 使用用户提供的 transform
80
- resolve(clientConfig.transforms
81
- && clientConfig.transforms[config.url]
82
- ? clientConfig.transforms[config.url](response) as T
83
- : response as T)
84
- } else {
85
- // 拦截业务异常
86
- reject(`==接口报错==${raw.msg}`)
87
- }
88
- },
89
- fail (res) {
90
- console.log('HTTP failed', res)
91
47
  }
48
+ if (raw.data) {
49
+ // 当用户配置含有 transforms 时, 使用用户提供的 transform
50
+ // 先 endpoints transform
51
+ // 再组装分页数据
52
+ const response = clientConfig.transforms
53
+ && clientConfig.transforms[c.url]
54
+ ? clientConfig.transforms[c.url]?.(raw.data) as T
55
+ : data as T
56
+ // 前端要求分页
57
+ // 在 endpoints transform 之前格式化分页数据
58
+ // 并拼装回原 raw 数据
59
+ const paging = config.data?.page
60
+ ? clientConfig.paging?.transform(raw.data)
61
+ : void 0
62
+ resolve(
63
+ paging
64
+ ? {
65
+ ...paging,
66
+ data: response
67
+ } as T
68
+ : response
69
+ )
70
+ } else {
71
+ reject(`未知错误`)
72
+ }
73
+ }).catch((e: any) => {
74
+ console.log('request.catch===', e)
92
75
  })
93
76
  })
94
77
  }
95
78
 
96
- const get: Http['get'] = <T>(url: string, data?: RequestData) => {
79
+ const get: HttpInstance['get'] = <T = ResponseData>(url: string, data?: RequestData) => {
97
80
  return request<T>({
98
81
  url,
99
82
  data,
@@ -101,7 +84,7 @@ const get: Http['get'] = <T>(url: string, data?: RequestData) => {
101
84
  })
102
85
  }
103
86
 
104
- const post: Http['post'] = <T>(url: string, data: RequestData) => {
87
+ const post: HttpInstance['post'] = <T = ResponseData>(url: string, data: RequestData) => {
105
88
  return request<T>({
106
89
  url,
107
90
  data,
@@ -110,12 +93,26 @@ const post: Http['post'] = <T>(url: string, data: RequestData) => {
110
93
  }
111
94
 
112
95
  /**
113
- * 写入配置并
114
- * @param config
115
- * @returns
96
+ * 场景配置
97
+ * 本地后台团队统一返回值外层
116
98
  */
117
- export function useHttp (config: HttpClientConfig): Http {
99
+ const defaultClientConfig: HttpClientConfig = {
100
+ baseUrl: '/',
101
+ interceptors: [
102
+ (raw) => raw.status == 401,
103
+ ]
104
+ }
118
105
 
106
+ let clientConfig = {
107
+ ...defaultClientConfig
108
+ }
109
+
110
+ /**
111
+ * 写入配置并返回 HTTP instance
112
+ * @param config
113
+ * @returns
114
+ */
115
+ export function createHttp (config: HttpClientConfig): HttpInstance {
119
116
  clientConfig = {
120
117
  ...defaultClientConfig,
121
118
  ...config
@@ -127,9 +124,3 @@ export function useHttp (config: HttpClientConfig): Http {
127
124
  post
128
125
  }
129
126
  }
130
-
131
-
132
-
133
- function isSuccess (res: Taro.request.SuccessCallbackResult<ResponseRaw>) {
134
- return /^2/.test(res.statusCode.toString())
135
- }
@@ -1,53 +1,83 @@
1
+ /**
2
+ * 由使用场景提供的配置
3
+ * createHttp() 使用的配置
4
+ */
5
+ export type HttpClientConfig = {
6
+ vendor?: HttpVendor,
7
+ baseUrl: string,
8
+ /**
9
+ * 向 HTTP header 加入的数据
10
+ * 通常含有 JWT token 以及其他参数
11
+ */
12
+ headers?: HeaderParams,
13
+ paging?: Paging,
14
+ /**
15
+ * 拦截器组
16
+ * 请求返回异常时进行一定的操作
17
+ */
18
+ interceptors?: HttpInterceptor[],
19
+ translates?: HttpTranslates,
20
+ transforms?: HttpTransforms,
21
+ }
22
+
23
+ /**
24
+ * HTTP 请求的底层实现
25
+ * Axios/Taro.request
26
+ */
27
+ export type HttpVendor = {
28
+ request <T = ResponseData>(config: HttpRequestConfig): Promise<ResponseRaw<T>>
29
+ }
30
+
31
+ export type HttpInterceptor = (raw: ResponseRaw) => boolean
32
+
33
+ /**
34
+ * Request Config for request API
35
+ * 沿用 Axios 的部分配置
36
+ */
37
+ export type HttpRequestConfig<D = RequestData> = {
38
+ url: string,
39
+ method?: HttpMethod,
40
+ baseUrl?: string,
41
+ headers?: HeaderData,
42
+ data?: D,
43
+ }
1
44
 
45
+ /**
46
+ * Request data for http get & post method
47
+ */
2
48
  export type RequestData = Record<string, any>
3
49
 
4
50
  /**
5
- * 本地后端团队的接口数据格式
51
+ * 接口请求返回的标准格式
6
52
  */
7
- export type ResponseRaw = Record<string, any>
53
+ export type ResponseRaw<T = ResponseData> = {
54
+ status: number,
55
+ message: string,
56
+ data: T
57
+ }
8
58
 
9
- export type ResponseData = Record<string, any>
59
+ /**
60
+ * 拆箱之后前端拿到的数据
61
+ */
62
+ export type ResponseData = Record<string, any> | any[]
10
63
 
11
- export type HttpTranslates = Record<string, (data: RequestData) => RequestData>
12
- export type HttpTransforms = Record<string, (data: ResponseData) => ResponseData>
64
+ export type HttpInstance = {
65
+ request<T = ResponseData>(config: HttpRequestConfig): Promise<T>
66
+ get<T = ResponseData>(url: string, data?: RequestData): Promise<T>,
67
+ post<T = ResponseData>(url: string, data?: RequestData): Promise<T>,
68
+ }
13
69
 
14
70
  /**
15
- * 向 request header 加入参数
16
- * 多数是 token
71
+ *
17
72
  */
18
- export type HeaderParams = Record<string, string>
73
+ export type HttpTranslates = Record<string, HttpTranslate | undefined>
74
+ export type HttpTransforms = Record<string, HttpTransform | undefined>
19
75
 
20
76
  /**
21
- * 由使用场景提供的配置
77
+ * 向 request header 加入参数
78
+ * 多数是 token
22
79
  */
23
- export type HttpClientConfig = {
24
- baseUrl: string,
25
- /**
26
- * 向 HTTP header 加入的数据
27
- * 通常含有 JWT token 以及其他参数
28
- */
29
- header?: HeaderParams
30
- /**
31
- * 定义返回数据的外层格式
32
- */
33
- response?: {
34
- getCode (data: ResponseData): `${number}` | number,
35
- getMessage (data: RequestData): string,
36
- getData (data: ResponseData): ResponseData
37
- },
38
- onAuthError? (): void
39
- onServerError? (): void
40
- /**
41
- * 错误码配置
42
- * 依据后端给出的 code 定义错误类型及处理方法
43
- */
44
- interceptors?: {
45
- auth (data: ResponseData): boolean
46
- server (data: ResponseData): boolean
47
- },
48
- translates?: HttpTranslates
49
- transforms?: HttpTransforms
50
- }
80
+ export type HeaderParams = Record<string, string>
51
81
 
52
82
  export enum HttpMethod {
53
83
  get = 'GET',
@@ -61,17 +91,7 @@ export enum HttpError {
61
91
  server = 'server'
62
92
  }
63
93
 
64
- export type Http = {
65
- request<T = ResponseData>(config: HttpRequestConfig): Promise<T>
66
- get<T = ResponseData>(url: string, data?: RequestData): Promise<T>,
67
- post<T = ResponseData>(url: string, data?: RequestData): Promise<T>,
68
- }
69
-
70
- export type HttpRequestConfig = {
71
- url: string,
72
- data?: RequestData,
73
- method?: HttpMethod
74
- }
94
+ export type HeaderData = Record<string, string>
75
95
 
76
96
  /**
77
97
  * 接口参数转换器
@@ -97,4 +117,42 @@ export type HttpEndpoint = {
97
117
  */
98
118
  export type HttpEndpoints = {
99
119
  [name: string]: HttpEndpoint
120
+ }
121
+
122
+ /**
123
+ * 分页参数
124
+ */
125
+ export type PagingParams = {
126
+ /**
127
+ * 页数
128
+ */
129
+ page: number,
130
+ /**
131
+ * 页数据条数
132
+ */
133
+ pageSize: number,
134
+ }
135
+
136
+ export type PagingData = {
137
+ /**
138
+ * 页总数
139
+ */
140
+ totalPages: number,
141
+ }
142
+
143
+ /**
144
+ * Wrapped with Paging Data
145
+ */
146
+ export type WithPaging<T = ResponseData> = {
147
+ [K in keyof PagingData]: PagingData[K];
148
+ } & {
149
+ data: T;
150
+ }
151
+
152
+ /**
153
+ * 分页设置
154
+ */
155
+ export type Paging = {
156
+ translate (params: PagingParams): any,
157
+ transform (resonse: any): PagingData,
100
158
  }
@@ -1,3 +1,4 @@
1
1
  export * from './weixin'
2
2
  export * from './components'
3
+ export * from './composables'
3
4
  export * from './http'