|
1 | | -# axios-package |
2 | | - |
3 | | -> axios packaged for general use |
4 | | -
|
5 | | -> [🇨🇳 中文版](./README.zh-Hans.md) |
6 | | -
|
7 | | -## breaking change |
8 | | - |
9 | | -- support ts |
10 | | -- no longer wrapper default config, default export from function change to class |
11 | | - |
12 | | -Incompatible with versions prior to 1.0.0 |
13 | | - |
14 | | -## installation |
15 | | - |
16 | | -```bash |
17 | | -npm i axios query-string axios-package2 |
18 | | -``` |
19 | | - |
20 | | -## usage |
21 | | - |
22 | | -### es6 |
23 | | - |
24 | | -```js |
25 | | -import { stringify } from 'query-string' |
26 | | -import HttpClient from 'axios-package2' |
27 | | - |
28 | | -// 将http的statusText从默认的英文转化为中文,给错误处理函数使用(不传,返回英文) |
29 | | -const statusMap = { |
30 | | - 400: '错误请求', |
31 | | - 401: '未授权,请重新登录', |
32 | | - 403: '拒绝访问', |
33 | | - 404: '请求错误,未找到该资源', |
34 | | - 405: '请求方法未允许', |
35 | | - 408: '请求超时', |
36 | | - 500: '服务器端出错', |
37 | | - 501: '网络未实现', |
38 | | - 502: '网络错误', |
39 | | - 503: '服务不可用', |
40 | | - 504: '网络超时', |
41 | | - 505: 'http版本不支持该请求', |
42 | | - 800: '登陆失效', |
43 | | -} |
44 | | - |
45 | | -// 自定义错误处理函数(不传,则不处理) |
46 | | -const errorHandler = (result) => { |
47 | | - console.log(result) |
48 | | -} |
49 | | - |
50 | | -// 提供默认配置 |
51 | | -const config = { |
52 | | - axiosRequestConfig: { |
53 | | - baseURL: '', |
54 | | - headers: { |
55 | | - 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', |
56 | | - }, |
57 | | - timeout: 60000, |
58 | | - transformRequest: [ |
59 | | - (data, headers) => { |
60 | | - if (headers['Content-Type'].includes('x-www-form-urlencoded')) { |
61 | | - return stringify(data) |
62 | | - } |
63 | | - return data |
64 | | - } |
65 | | - ] |
66 | | - }, |
67 | | - errorHandler, |
68 | | - statusMap |
69 | | -} |
70 | | - |
71 | | -// 实例 |
72 | | -const http = new HttpClient(config) |
73 | | - |
74 | | -const params = { |
75 | | - url: '', // 接口请求地址 |
76 | | - data: {}, // post接口,入参需要, 可选 |
77 | | - config: {}, // axios配置,可选 |
78 | | - isReturnData: true, // 是否直接但会result中的data结果,可选默认为true |
79 | | - isHandleError: true, // 是否执行处理错误的函数,和之前的errorHandler缺一不可,可选默认为true |
80 | | -} |
81 | | - |
82 | | -// http methods: 'get' | 'delete' | 'head' | 'options' | 'post' | 'put' | 'patch' |
83 | | - |
84 | | -http.get(params).then(result => { |
85 | | - console.log(result) |
86 | | -}) |
87 | | - |
88 | | -http.post(params).then(result => { |
89 | | - console.log(result) |
90 | | -}) |
91 | | - |
92 | | -``` |
93 | | - |
94 | | -### typescript |
95 | | - |
96 | | -```ts |
97 | | -import { stringify } from 'query-string' |
98 | | -import HttpClient, {HttpClientConfig,HttpClientResult, HttpRequestParameters } from 'axios-package2' |
99 | | - |
100 | | -// 将http的statusText从默认的英文转化为中文,给错误处理函数使用(不传,返回英文) |
101 | | -const statusMap = { |
102 | | - 400: '错误请求', |
103 | | - 401: '未授权,请重新登录', |
104 | | - 403: '拒绝访问', |
105 | | - 404: '请求错误,未找到该资源', |
106 | | - 405: '请求方法未允许', |
107 | | - 408: '请求超时', |
108 | | - 500: '服务器端出错', |
109 | | - 501: '网络未实现', |
110 | | - 502: '网络错误', |
111 | | - 503: '服务不可用', |
112 | | - 504: '网络超时', |
113 | | - 505: 'http版本不支持该请求', |
114 | | - 800: '登陆失效', |
115 | | -} |
116 | | - |
117 | | -// 自定义错误处理函数(不传,则不处理) |
118 | | -const errorHandler = (result: HttpClientResult) => { |
119 | | - console.log(result) |
120 | | -} |
121 | | - |
122 | | -// 提供默认配置 |
123 | | -const config: HttpClientConfig = { |
124 | | - axiosRequestConfig: { |
125 | | - baseURL: '', |
126 | | - headers: { |
127 | | - 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', |
128 | | - }, |
129 | | - timeout: 60000, |
130 | | - transformRequest: [ |
131 | | - (data, headers) => { |
132 | | - if (headers['Content-Type'].includes('x-www-form-urlencoded')) { |
133 | | - return stringify(data) |
134 | | - } |
135 | | - return data |
136 | | - } |
137 | | - ] |
138 | | - }, |
139 | | - errorHandler, |
140 | | - statusMap |
141 | | -} |
142 | | - |
143 | | -// 实例 |
144 | | -const http = new HttpClient(config) |
145 | | - |
146 | | -const params: HttpRequestParameters = { |
147 | | - url: '', // 接口请求地址 |
148 | | - data: {}, // post接口,入参需要, 可选 |
149 | | - config: {}, // axios配置,可选 |
150 | | - isReturnData: true, // 是否直接但会result中的data结果,可选默认为true |
151 | | - isHandleError: true, // 是否执行处理错误的函数,和之前的errorHandler缺一不可,可选默认为true |
152 | | -} |
153 | | - |
154 | | -// http methods: 'get' | 'delete' | 'head' | 'options' | 'post' | 'put' | 'patch' |
155 | | - |
156 | | -http.get(params).then(result => { |
157 | | - console.log(result) |
158 | | -}) |
159 | | - |
160 | | -http.post(params).then(result => { |
161 | | - console.log(result) |
162 | | -}) |
163 | | - |
164 | | -``` |
165 | | - |
166 | | -## ⚠ warn for importing the source version |
167 | | - |
168 | | -axios-package2 offers untranspiled version for webpack by default.If you are using official Vue CLI to create your project, you may encounter the problem that the default configuration will exclude `node_modules` from files to be transpiled by Babel. You need to modify the configuration as follows: |
169 | | - |
170 | | -For **Vue CLI 3+**, add `axios-package2` (if you use `query-string`, also add) into `transpileDependencies` in `vue.config.js` like this: |
171 | | - |
172 | | -```js |
173 | | -module.exports = { |
174 | | - transpileDependencies: [ |
175 | | - 'axios-package2', |
176 | | - 'query-string' |
177 | | - ] |
178 | | -} |
179 | | -``` |
180 | | - |
181 | | -For **Vue CLI 2** with the `webpack` template, modify `build/webpack.base.conf.js` like this: |
182 | | - |
183 | | -```diff |
184 | | - { |
185 | | - test: /\.js$/, |
186 | | - loader: 'babel-loader', |
187 | | -- include: [resolve('src'), resolve('test')] |
188 | | -+ include: [ |
189 | | -+ resolve('src'), |
190 | | -+ resolve('test'), |
191 | | -+ resolve('node_modules/axios-package2'), |
192 | | -+ resolve('node_modules/query-string') |
193 | | -+ ] |
194 | | - } |
195 | | -``` |
| 1 | +# axios-package |
| 2 | + |
| 3 | +> axios packaged for general use |
| 4 | +
|
| 5 | +> [🇨🇳 中文版](./README.zh-Hans.md) |
| 6 | +
|
| 7 | +## breaking change |
| 8 | + |
| 9 | +- HttpClientConfig remove statusMap configuration |
| 10 | +- you can use getAxiosInstance function get inner axios instance |
| 11 | + |
| 12 | +## installation |
| 13 | + |
| 14 | +```bash |
| 15 | +npm i axios axios-package2 |
| 16 | +``` |
| 17 | + |
| 18 | +## usage |
| 19 | + |
| 20 | +### typescript |
| 21 | + |
| 22 | +```ts |
| 23 | +import { stringify } from 'query-string' |
| 24 | +import { AxiosResponse } from 'axios' |
| 25 | +import HttpClient, { HttpClientConfig } from 'axios-package2' |
| 26 | + |
| 27 | +const statusMap = { |
| 28 | + 400: '错误请求', |
| 29 | + 401: '未授权,请重新登录', |
| 30 | + 403: '拒绝访问', |
| 31 | + 404: '请求错误,未找到该资源', |
| 32 | + 405: '请求方法未允许', |
| 33 | + 408: '请求超时', |
| 34 | + 500: '服务器端出错', |
| 35 | + 501: '网络未实现', |
| 36 | + 502: '网络错误', |
| 37 | + 503: '服务不可用', |
| 38 | + 504: '网络超时', |
| 39 | + 505: 'http版本不支持该请求', |
| 40 | + 800: '登陆失效', |
| 41 | +} |
| 42 | + |
| 43 | +const errorHandler = (result: AxiosResponse) => { |
| 44 | + // request succeed or has http error |
| 45 | + if (resule.status > 0) { |
| 46 | + // do something |
| 47 | + const statusText = statusMap[result.status] |
| 48 | + } |
| 49 | + // requset has other error: like timeout,network,canceled etc |
| 50 | + if (result.status < 0) { // -1: canceled;-2:timeout,network etc |
| 51 | + // do something |
| 52 | + } |
| 53 | + console.log(result) |
| 54 | +} |
| 55 | + |
| 56 | +// httpClient default config |
| 57 | +const config: HttpClientConfig = { |
| 58 | + axiosRequestConfig: { |
| 59 | + baseURL: '', |
| 60 | + headers: { |
| 61 | + 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', |
| 62 | + }, |
| 63 | + timeout: 60000, |
| 64 | + transformRequest: [ |
| 65 | + (data, headers) => { |
| 66 | + if (headers['Content-Type'].includes('x-www-form-urlencoded')) { |
| 67 | + return stringify(data) |
| 68 | + } |
| 69 | + return data |
| 70 | + } |
| 71 | + ] |
| 72 | + }, |
| 73 | + errorHandler, |
| 74 | +} |
| 75 | + |
| 76 | +const http = new HttpClient(config) |
| 77 | + |
| 78 | +// request config, only url is required |
| 79 | +const params: HttpRequestParameters = { |
| 80 | + url: '', |
| 81 | + data: {}, |
| 82 | + config: {}, |
| 83 | + isReturnData: true, // request return response.data, if not passed, default true |
| 84 | + isHandleError: true, // request will call errorHandler or not, if not passed, default value is true |
| 85 | +} |
| 86 | + |
| 87 | +// http methods: 'get' | 'delete' | 'head' | 'options' | 'post' | 'put' | 'patch' |
| 88 | + |
| 89 | +http.get(params).then(result => { |
| 90 | + console.log(result) |
| 91 | +}) |
| 92 | + |
| 93 | +http.post(params).then(result => { |
| 94 | + console.log(result) |
| 95 | +}) |
| 96 | + |
| 97 | +``` |
| 98 | + |
| 99 | +## ⚠ warn for importing the source version |
| 100 | + |
| 101 | +axios-package2 offers untranspiled version for webpack by default.If you are using official Vue CLI to create your project, you may encounter the problem that the default configuration will exclude `node_modules` from files to be transpiled by Babel. You need to modify the configuration as follows: |
| 102 | + |
| 103 | +For **Vue CLI 3+**, add `axios-package2` (if you use `query-string`, also add) into `transpileDependencies` in `vue.config.js` like this: |
| 104 | + |
| 105 | +```js |
| 106 | +module.exports = { |
| 107 | + transpileDependencies: [ |
| 108 | + 'axios-package2', |
| 109 | + 'query-string' |
| 110 | + ] |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +For **Vue CLI 2** with the `webpack` template, modify `build/webpack.base.conf.js` like this: |
| 115 | + |
| 116 | +```diff |
| 117 | + { |
| 118 | + test: /\.js$/, |
| 119 | + loader: 'babel-loader', |
| 120 | +- include: [resolve('src'), resolve('test')] |
| 121 | ++ include: [ |
| 122 | ++ resolve('src'), |
| 123 | ++ resolve('test'), |
| 124 | ++ resolve('node_modules/axios-package2'), |
| 125 | ++ resolve('node_modules/query-string') |
| 126 | ++ ] |
| 127 | + } |
| 128 | +``` |
0 commit comments