|
1 | 1 | import Vue from 'vue' |
2 | | -import VueApiQueries, { Validator } from 'vue-api-queries' |
| 2 | +import { CancelToken } from 'axios' |
| 3 | +import VueApiQueries, { isPlainObject, Validator } from 'vue-api-queries' |
3 | 4 |
|
4 | | -const options = <%= serialize(options) %> || {} |
5 | | -const { errorProperty, parsedQs } = options |
| 5 | +const errorProperty = '<%= options.errorProperty %>' |
| 6 | +const parsedQs = '<%= options.parsedQs %>' |
| 7 | +const blockDuplicate = '<%= options.blockDuplicate %>' |
6 | 8 |
|
7 | | -export default function (ctx) { |
8 | | - Vue.use(VueApiQueries, { errorProperty, axios: ctx.$axios, parsedQs }) |
9 | | - ctx.$errors = Validator |
| 9 | +function createRequestKey(url, params) { |
| 10 | + let requestKey = url |
| 11 | + if (params) { |
| 12 | + requestKey += `:${createStringFromParameters(params)}` |
| 13 | + } |
| 14 | + return requestKey |
| 15 | +} |
| 16 | + |
| 17 | +function createStringFromParameters(obj) { |
| 18 | + const keysArr = [] |
| 19 | + for (const key in obj) { |
| 20 | + keysArr.push(key) |
| 21 | + if (isPlainObject(obj[key])) { |
| 22 | + keysArr.push(createStringFromParameters(obj[key])) |
| 23 | + } |
| 24 | + } |
| 25 | + return keysArr.join('|') |
| 26 | +} |
| 27 | + |
| 28 | +function createCancelMessage(requestKey, paramsStr) { |
| 29 | + return { |
| 30 | + statusCode: 100, |
| 31 | + requestKey: requestKey, |
| 32 | + message: `Request canceled: ${requestKey}`, |
| 33 | + paramsStr: paramsStr, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +export default function ({ $axios, app }, inject) { |
| 38 | + if (blockDuplicate === 'true') { |
| 39 | + $axios.activeRequests = {} |
| 40 | + $axios.onRequest((config) => { |
| 41 | + let blockerConfigContainer = config |
| 42 | + const headerBlockerKey = '<%= options.headerBlockerKey %>' |
| 43 | + if (headerBlockerKey) { |
| 44 | + if (config.headers.hasOwnProperty(headerBlockerKey)) { |
| 45 | + blockerConfigContainer = config.headers[headerBlockerKey] |
| 46 | + delete config.headers[headerBlockerKey] |
| 47 | + } |
| 48 | + } |
| 49 | + let requestBlockingAllowed = blockerConfigContainer.blockAllowed |
| 50 | + if (requestBlockingAllowed === undefined) { |
| 51 | + requestBlockingAllowed = '<%= options.blockByDefault %>' === 'true' |
| 52 | + } |
| 53 | + if (!requestBlockingAllowed) { |
| 54 | + return config |
| 55 | + } |
| 56 | + |
| 57 | + //Check if user has set a custom requestKey |
| 58 | + let { requestKey } = blockerConfigContainer |
| 59 | + if (!requestKey) { |
| 60 | + //If there is no custom requestKey, create a default requestKey based on url and parameters |
| 61 | + requestKey = createRequestKey( |
| 62 | + config.baseURL + config.url, |
| 63 | + config.params, |
| 64 | + ) |
| 65 | + } |
| 66 | + const paramsStr = JSON.stringify(config.params) |
| 67 | + //If another request with the same requestKey already exists, cancel it |
| 68 | + if ( |
| 69 | + $axios.activeRequests.hasOwnProperty(requestKey) && |
| 70 | + $axios.activeRequests[requestKey].cancelToken |
| 71 | + ) { |
| 72 | + $axios.activeRequests[requestKey].cancelToken.cancel( |
| 73 | + createCancelMessage(requestKey, paramsStr), |
| 74 | + ) |
| 75 | + } |
| 76 | + if (!$axios.hasOwnProperty(requestKey)) { |
| 77 | + //If request has not been sent before, create a custom promise |
| 78 | + let reqResolve, reqReject |
| 79 | + const promise = new Promise((resolve, reject) => { |
| 80 | + reqResolve = resolve |
| 81 | + reqReject = reject |
| 82 | + }) |
| 83 | + //Insert current request to activeRequests |
| 84 | + $axios.activeRequests[requestKey] = { |
| 85 | + promise: promise, |
| 86 | + resolve: reqResolve, |
| 87 | + reject: reqReject, |
| 88 | + } |
| 89 | + } |
| 90 | + //Update the active request's params |
| 91 | + $axios.activeRequests[requestKey].paramsStr = paramsStr |
| 92 | + //Create a cancel token for current request |
| 93 | + const cancelToken = CancelToken.source() |
| 94 | + $axios.activeRequests[requestKey].cancelToken = cancelToken |
| 95 | + //Add the cancel token to the request |
| 96 | + return { |
| 97 | + ...config, |
| 98 | + cancelToken: cancelToken && cancelToken.token, |
| 99 | + } |
| 100 | + }) |
| 101 | + $axios.onError((err) => { |
| 102 | + //Check if error message has a requestKey set in active requests |
| 103 | + if ( |
| 104 | + err.hasOwnProperty('message') && |
| 105 | + err.message.hasOwnProperty('requestKey') && |
| 106 | + $axios.activeRequests.hasOwnProperty(err.message.requestKey) |
| 107 | + ) { |
| 108 | + const currentRequest = $axios.activeRequests[err.message.requestKey] |
| 109 | + //Check if error concerns a cancellation |
| 110 | + if ( |
| 111 | + err.message && |
| 112 | + err.message.statusCode === 100 && |
| 113 | + currentRequest && |
| 114 | + currentRequest.paramsStr === err.message.paramsStr |
| 115 | + ) { |
| 116 | + //Handle the cancellation error |
| 117 | + const debug = '<%= options.debug %>' |
| 118 | + if (debug === 'true') { |
| 119 | + console.warn(err.message.message) |
| 120 | + } |
| 121 | + //Return a promise to the active request that overrides the current one |
| 122 | + return $axios.activeRequests[err.message.requestKey].promise |
| 123 | + } |
| 124 | + } |
| 125 | + return Promise.reject(err) |
| 126 | + }) |
| 127 | + $axios.onResponse((response) => { |
| 128 | + //Check if user has set a custom requestKey |
| 129 | + let { requestKey } = response.config |
| 130 | + if (!requestKey) { |
| 131 | + //If there is no custom requestKey, create a default requestKey based on url and parameters |
| 132 | + requestKey = createRequestKey( |
| 133 | + response.config.url, |
| 134 | + response.config.params, |
| 135 | + ) |
| 136 | + } |
| 137 | + if ($axios.activeRequests.hasOwnProperty(requestKey)) { |
| 138 | + //Inform all previously cancelled requests with the current response & remove requestKey from activeRequests |
| 139 | + $axios.activeRequests[requestKey].resolve(response) |
| 140 | + delete $axios.activeRequests[requestKey] |
| 141 | + } |
| 142 | + }) |
| 143 | + const onPageChange = '<%= options.onPageChange %>' |
| 144 | + if (onPageChange === 'true') { |
| 145 | + app.router.beforeEach((to, from, next) => { |
| 146 | + for (const requestKey in $axios.activeRequests) { |
| 147 | + $axios.activeRequests[requestKey].cancelToken.cancel( |
| 148 | + createCancelMessage(requestKey), |
| 149 | + ) |
| 150 | + delete $axios.activeRequests[requestKey] |
| 151 | + } |
| 152 | + next() |
| 153 | + }) |
| 154 | + } |
| 155 | + } |
| 156 | + Vue.use(VueApiQueries, { errorProperty, $axios, parsedQs }) |
| 157 | + inject('errors', Validator) |
10 | 158 | } |
0 commit comments