@conecli/cone-render 0.8.25-beta.1 → 0.8.25

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 +1 @@
1
- import Taro, { RequestTask } from '@tarojs/taro'
2
1
  jsonHeader = 'application/json;charset=utf-8',
3
2
  formDataHeader = 'application/x-www-form-urlencoded',
4
3
  constructor() {
5
4
  this._init()
6
5
  }
7
6
  _init(): void {
8
7
  httpInterceptors.forEach((item) => {
9
8
  Taro.addInterceptor((chain) => item(chain))
10
9
  })
11
10
  }
12
11
 
13
12
  get(url: string, data: any = null, otherOptions = {}): Taro.RequestTask<any> {
14
13
  return this.request({ url, data, method: 'GET', ...otherOptions })
15
14
  }
16
15
 
17
16
  post(
18
17
  url: string,
19
18
  data: any = null,
20
19
  otherOptions = {},
21
20
  ): Taro.RequestTask<any> {
22
21
  return this.request({ url, data, method: 'POST', ...otherOptions })
23
22
  }
24
23
 
25
24
  request({
26
25
  url,
27
26
  method = 'POST',
28
27
  timeout = 7000,
29
28
  ...otherOptions
30
29
  }): RequestTask<any> {
31
30
  const { header: otherHeader, ...otherOpts } = otherOptions
32
31
  const header = {
33
32
  'content-type':
34
33
  method === 'POST'
35
34
  ? RequestHeaderContentType.formDataHeader
36
35
  : RequestHeaderContentType.jsonHeader,
37
36
  ...otherHeader,
38
37
  }
39
38
  const param: any = {
40
39
  url,
41
40
  method,
42
41
  timeout,
43
42
  header,
44
43
  credentials: 'include',
45
44
  ...otherOpts,
46
45
  }
47
46
  console.warn('request param:' + JSON.stringify(param))
48
47
  const requestTask = Taro.request(param)
49
48
  const requestTimeoutPromise =
50
49
  new Promise<ServiceInterFace.RequestPromiseRes>((resolve) => {
51
50
  setTimeout(() => {
52
51
  resolve({
53
52
  statusCode: 500,
54
53
  resTimeoutState: true,
55
54
  errMsg: 'request timeout',
56
55
  })
57
56
  }, timeout)
58
57
  })
59
58
  return Promise.race([requestTask, requestTimeoutPromise]).then(
60
59
  (res: any) => {
61
60
  if (res && res.statusCode === 500 && res.resTimeoutState) {
62
61
  console.warn('request 请求超时 url:' + url)
63
62
  requestTask.abort && requestTask.abort()
64
63
  }
65
64
  return res
66
65
  },
67
66
  )
68
67
  }
68
+ import Taro, { RequestTask } from '@tarojs/taro'
69
69
  jsonHeader = 'application/json;charset=utf-8',
70
70
  formDataHeader = 'application/x-www-form-urlencoded',
71
71
  constructor() {
72
72
  this._init()
73
73
  }
74
74
  _init(): void {
75
75
  httpInterceptors.forEach((item) => {
76
76
  Taro.addInterceptor((chain) => item(chain))
77
77
  })
78
78
  }
79
79
 
80
80
  get(url: string, data: any = null, otherOptions = {}): Taro.RequestTask<any> {
81
81
  return this.request({ url, data, method: 'GET', ...otherOptions })
82
82
  }
83
83
 
84
84
  post(
85
85
  url: string,
86
86
  data: any = null,
87
87
  otherOptions = {},
88
88
  ): Taro.RequestTask<any> {
89
89
  return this.request({ url, data, method: 'POST', ...otherOptions })
90
90
  }
91
91
 
92
92
  request({
93
93
  url,
94
94
  method = 'POST',
95
95
  timeout = 7000,
96
96
  ...otherOptions
97
97
  }): RequestTask<any> {
98
98
  const { header: otherHeader, ...otherOpts } = otherOptions
99
99
  const header = {
100
100
  'content-type':
101
101
  method === 'POST'
102
102
  ? RequestHeaderContentType.formDataHeader
103
103
  : RequestHeaderContentType.jsonHeader,
104
104
  ...otherHeader,
105
105
  }
106
106
  const param: any = {
107
107
  url,
108
108
  method,
109
109
  timeout,
110
110
  header,
111
111
  credentials: 'include',
112
112
  ...otherOpts,
113
113
  }
114
114
  const requestTask = Taro.request(param)
115
115
  const requestTimeoutPromise =
116
116
  new Promise<ServiceInterFace.RequestPromiseRes>((resolve) => {
117
117
  setTimeout(() => {
118
118
  resolve({
119
119
  statusCode: 500,
120
120
  resTimeoutState: true,
121
121
  errMsg: 'request timeout',
122
122
  })
123
123
  }, timeout)
124
124
  })
125
125
  return Promise.race([requestTask, requestTimeoutPromise]).then(
126
126
  (res: any) => {
127
127
  if (res && res.statusCode === 500 && res.resTimeoutState) {
128
128
  console.warn('request 请求超时 url:' + url)
129
129
  requestTask.abort && requestTask.abort()
130
130
  }
131
131
  return res
132
132
  },
133
133
  )
134
134
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@conecli/cone-render",
3
- "version": "0.8.25-beta.1",
3
+ "version": "0.8.25",
4
4
  "private": false,
5
5
  "files": [
6
6
  "dist/"