|
| 1 | +/** |
| 2 | + * @author Johnny Miller (鍾俊), e-mail: [email protected] |
| 3 | + * @date 1/2/20 9:15 AM |
| 4 | + */ |
| 5 | +// eslint-disable-next-line no-unused-vars |
| 6 | +import Axios, { ResponseType } from 'axios' |
| 7 | +import { validate } from 'class-validator' |
| 8 | +import { ClassValidationUtil } from '@/utils/class-validation-util' |
| 9 | + |
| 10 | +// 1. Create an axios instance. |
| 11 | +export const service = Axios.create({ |
| 12 | + // Base URL of API |
| 13 | + baseURL: process.env.VUE_APP_BASE_URL, |
| 14 | + // Request timeout: 30s |
| 15 | + timeout: 30000, |
| 16 | + /** |
| 17 | + * `validateStatus` defines whether to resolve or reject the promise for a given HTTP response status code. |
| 18 | + * The value of status must be less than 999. |
| 19 | + * @param {number} status HTTP's status code |
| 20 | + * @return {boolean} If `validateStatus` returns `true` (or is set to `null` or `undefined`), |
| 21 | + * the promise will be resolved; otherwise, the promise will be rejected. |
| 22 | + */ |
| 23 | + validateStatus: status => { |
| 24 | + // Only the HTTP status code is equal to 200, axios would resolve the promise |
| 25 | + return status === 200 |
| 26 | + } |
| 27 | +}) |
| 28 | + |
| 29 | +// 2. Request interceptor's configuration. |
| 30 | +service.interceptors.request.use( |
| 31 | + async axiosRequestConfig => { |
| 32 | + if (axiosRequestConfig?.params) { |
| 33 | + const validation = await validate(axiosRequestConfig.params) |
| 34 | + if (validation.length > 0) { |
| 35 | + console.error('Validation failed! Validation:', validation) |
| 36 | + console.error('Validation failed! Error message:', ClassValidationUtil.getAllValidationError(validation)) |
| 37 | + throw new Error(`Validation failed! The 1st error: ${ClassValidationUtil.getFirstValidationError(validation)}`) |
| 38 | + } |
| 39 | + } |
| 40 | + return axiosRequestConfig |
| 41 | + }, |
| 42 | + error => { |
| 43 | + // Do something with request error |
| 44 | + // for debug |
| 45 | + console.error('[Axios] Error occurred when sending request!', error) |
| 46 | + return Promise.reject(error) |
| 47 | + } |
| 48 | +) |
| 49 | + |
| 50 | +// 3. Response interceptor's configuration. |
| 51 | +service.interceptors.response.use( |
| 52 | + response => { |
| 53 | + // Only the HTTP status code is equal to 200, axios would resolve the promise |
| 54 | + const resp = response.data |
| 55 | + return Promise.resolve(resp) |
| 56 | + }, |
| 57 | + error => { |
| 58 | + console.error('[Axios] Error occurred when handling response!', error) |
| 59 | + console.error('[Axios] Error occurred when handling response! Response:', error.response) |
| 60 | + return Promise.reject(error) |
| 61 | + } |
| 62 | +) |
| 63 | + |
| 64 | +// noinspection JSUnusedGlobalSymbols |
| 65 | +/** |
| 66 | + * Send a GET request. |
| 67 | + * |
| 68 | + * The GET method requests a representation of the specified resource. Requests using GET should only retrieve data. |
| 69 | + * |
| 70 | + * @param url URL |
| 71 | + * @param params Params |
| 72 | + * @param responseType Response type. |
| 73 | + * @return {Promise<any>} Response data. |
| 74 | + * @see <a href='https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET'>HTTP request methods — GET | MDN</a> |
| 75 | + */ |
| 76 | +export function get (url: string, params?: any, responseType: ResponseType = 'json'): Promise<any> { |
| 77 | + return new Promise<any>((resolve, reject) => { |
| 78 | + service.get(url, { |
| 79 | + params: params, |
| 80 | + responseType: responseType |
| 81 | + }).then(resp => { |
| 82 | + resolve(resp) |
| 83 | + }).catch(rejectedReason => { |
| 84 | + reject(rejectedReason) |
| 85 | + }) |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +// noinspection JSUnusedGlobalSymbols |
| 90 | +/** |
| 91 | + * The DELETE method deletes the specified resource. |
| 92 | + * |
| 93 | + * @param url URL. |
| 94 | + * @param params Params. |
| 95 | + * @return {Promise<any>} Response data. |
| 96 | + * @see <a href='https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE'>HTTP request methods — DELETE | MDN</a> |
| 97 | + */ |
| 98 | +export function deleteRequest (url: string, params: any): Promise<any> { |
| 99 | + return new Promise<any>((resolve, reject) => { |
| 100 | + service.delete(url, { |
| 101 | + params: params |
| 102 | + }).then(resp => { |
| 103 | + resolve(resp) |
| 104 | + }).catch(rejectedReason => { |
| 105 | + reject(rejectedReason) |
| 106 | + }) |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +// noinspection JSUnusedGlobalSymbols |
| 111 | +/** |
| 112 | + * The HEAD method asks for a response identical to that of a GET request, but without the response body. |
| 113 | + * |
| 114 | + * The HTTP HEAD method requests the headers that are returned if the specified resource would be requested with an HTTP GET method. |
| 115 | + * Such a request can be done before deciding to download a large resource to save bandwidth, for example. |
| 116 | + * |
| 117 | + * @param url URL. |
| 118 | + * @param params Params. |
| 119 | + * @return {Promise<any>} Response data. |
| 120 | + * @see <a href='https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD'>HTTP request methods — HEAD | MDN</a> |
| 121 | + */ |
| 122 | +export function head (url: string, params: any): Promise<any> { |
| 123 | + return new Promise<any>((resolve, reject) => { |
| 124 | + service.head(url, { |
| 125 | + params: params |
| 126 | + }).then(resp => { |
| 127 | + resolve(resp) |
| 128 | + }).catch(rejectedReason => { |
| 129 | + reject(rejectedReason) |
| 130 | + }) |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +// noinspection JSUnusedGlobalSymbols |
| 135 | +/** |
| 136 | + * Send a POST request with payload. |
| 137 | + * |
| 138 | + * The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header. |
| 139 | + * |
| 140 | + * @param url URL |
| 141 | + * @param params Payload. |
| 142 | + * @return {Promise<any>} Response data. |
| 143 | + * @see <a href='https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST'>HTTP request methods — POST | MDN</a> |
| 144 | + */ |
| 145 | +export function post (url: string, params: any): Promise<any> { |
| 146 | + return new Promise<any>((resolve, reject) => { |
| 147 | + service.post(url, params) |
| 148 | + .then(resp => { |
| 149 | + resolve(resp) |
| 150 | + }) |
| 151 | + .catch(rejectedReason => { |
| 152 | + reject(rejectedReason) |
| 153 | + }) |
| 154 | + }) |
| 155 | +} |
| 156 | + |
| 157 | +// noinspection JSUnusedGlobalSymbols |
| 158 | +/** |
| 159 | + * Send a legacy POST request with URL search params. |
| 160 | + * |
| 161 | + * The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header. |
| 162 | + * |
| 163 | + * @see <a href='https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST'>HTTP request methods — POST | MDN</a> |
| 164 | + * @param url URL. |
| 165 | + * @param params URL search params. |
| 166 | + * @return {Promise<any>} Response data. |
| 167 | + */ |
| 168 | +export function legacyPost (url: string, params: any): Promise<any> { |
| 169 | + const urlSearchParams = new URLSearchParams() |
| 170 | + Object.keys(params).forEach(key => { |
| 171 | + urlSearchParams.append(key, params[key]) |
| 172 | + }) |
| 173 | + return new Promise<any>((resolve, reject) => { |
| 174 | + service.post(url, urlSearchParams) |
| 175 | + .then(resp => { |
| 176 | + resolve(resp) |
| 177 | + }) |
| 178 | + .catch(rejectedReason => { |
| 179 | + reject(rejectedReason) |
| 180 | + }) |
| 181 | + }) |
| 182 | +} |
| 183 | + |
| 184 | +// noinspection JSUnusedGlobalSymbols |
| 185 | +/** |
| 186 | + * Send a PUT request. |
| 187 | + * |
| 188 | + * The HTTP PUT request method creates a new resource or replaces a representation of the target resource with the request payload. |
| 189 | + * The difference between PUT and POST is that PUT is idempotent: |
| 190 | + * calling it once or several times successively has the same effect (that is no side effect), where successive identical POST may have additional effects, like passing an order several times. |
| 191 | + * |
| 192 | + * @param url URL. |
| 193 | + * @param params Params. |
| 194 | + * @returns {Promise<any>} Response data. |
| 195 | + * @see <a href='https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT'>HTTP request methods — PUT | MDN</a> |
| 196 | + */ |
| 197 | +export function put (url: string, params: any): Promise<any> { |
| 198 | + return new Promise<any>((resolve, reject) => { |
| 199 | + service.put(url, { |
| 200 | + params: params |
| 201 | + }).then(resp => { |
| 202 | + resolve(resp) |
| 203 | + }).catch(rejectedReason => { |
| 204 | + reject(rejectedReason) |
| 205 | + }) |
| 206 | + }) |
| 207 | +} |
| 208 | + |
| 209 | +// noinspection JSUnusedGlobalSymbols |
| 210 | +/** |
| 211 | + * The PATCH method is used to apply partial modifications to a resource. |
| 212 | + * |
| 213 | + * The HTTP PUT method only allows complete replacement of a document. |
| 214 | + * Unlike PUT, PATCH is not idempotent, meaning successive identical patch requests may have different effects. |
| 215 | + * However, it is possible to issue PATCH requests in such a way as to be idempotent. |
| 216 | + * |
| 217 | + * @param url URL. |
| 218 | + * @param params Params. |
| 219 | + * @return {Promise<any>} Response data. |
| 220 | + * @see <a href='https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH'>HTTP request methods — PATCH | MDN</a> |
| 221 | + */ |
| 222 | +export function patch (url: string, params: any): Promise<any> { |
| 223 | + return new Promise((resolve, reject) => { |
| 224 | + service.patch<any>(url, params) |
| 225 | + .then(resp => { |
| 226 | + resolve(resp) |
| 227 | + }) |
| 228 | + .catch(rejectedReason => { |
| 229 | + reject(rejectedReason) |
| 230 | + }) |
| 231 | + }) |
| 232 | +} |
0 commit comments