|
| 1 | +import { default as axios, AxiosProgressEvent, AxiosRequestConfig, AxiosResponse } from 'axios' |
| 2 | +import { fireExceptionEvent, fireFinishEvent, fireProgressEvent } from './events' |
| 3 | +import { page as currentPage } from './page' |
| 4 | +import { RequestParams } from './requestParams' |
| 5 | +import { Response } from './response' |
| 6 | +import { ActiveVisit, Page } from './types' |
| 7 | +import { urlWithoutHash } from './url' |
| 8 | + |
| 9 | +export class Request { |
| 10 | + protected response!: AxiosResponse |
| 11 | + protected cancelToken!: AbortController |
| 12 | + protected requestParams: RequestParams |
| 13 | + |
| 14 | + constructor( |
| 15 | + params: ActiveVisit, |
| 16 | + protected page: Page, |
| 17 | + ) { |
| 18 | + this.requestParams = RequestParams.create(params) |
| 19 | + this.cancelToken = new AbortController() |
| 20 | + this.requestParams.onCancelToken(() => this.cancel({ cancelled: true })) |
| 21 | + } |
| 22 | + |
| 23 | + public static create(params: ActiveVisit, page: Page): Request { |
| 24 | + return new Request(params, page) |
| 25 | + } |
| 26 | + |
| 27 | + public async send() { |
| 28 | + return axios({ |
| 29 | + method: this.requestParams.params.method, |
| 30 | + url: urlWithoutHash(this.requestParams.params.url).href, |
| 31 | + data: this.requestParams.data(), |
| 32 | + params: this.requestParams.queryParams(), |
| 33 | + signal: this.cancelToken.signal, |
| 34 | + headers: this.getHeaders(), |
| 35 | + onUploadProgress: this.onProgress.bind(this), |
| 36 | + }) |
| 37 | + .then((response) => { |
| 38 | + return Response.create(this.requestParams, response, this.page).handle() |
| 39 | + }) |
| 40 | + .catch((error) => { |
| 41 | + if (error?.response) { |
| 42 | + return Response.create(this.requestParams, error.response, this.page).handle() |
| 43 | + } |
| 44 | + |
| 45 | + return Promise.reject(error) |
| 46 | + }) |
| 47 | + .catch((error) => { |
| 48 | + if (axios.isCancel(error)) { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + if (fireExceptionEvent(error)) { |
| 53 | + return Promise.reject(error) |
| 54 | + } |
| 55 | + }) |
| 56 | + .finally(() => { |
| 57 | + this.finish() |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + protected finish(): void { |
| 62 | + if (this.requestParams.wasCancelledAtAll()) { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + this.requestParams.markAsFinished() |
| 67 | + this.fireFinishEvents() |
| 68 | + } |
| 69 | + |
| 70 | + protected fireFinishEvents(): void { |
| 71 | + fireFinishEvent(this.requestParams.all()) |
| 72 | + this.requestParams.onFinish() |
| 73 | + } |
| 74 | + |
| 75 | + public cancel({ cancelled = false, interrupted = false }: { cancelled?: boolean; interrupted?: boolean }): void { |
| 76 | + this.cancelToken.abort() |
| 77 | + |
| 78 | + this.requestParams.markAsCancelled({ cancelled, interrupted }) |
| 79 | + |
| 80 | + this.fireFinishEvents() |
| 81 | + } |
| 82 | + |
| 83 | + protected onProgress(progress: AxiosProgressEvent): void { |
| 84 | + if (this.requestParams.data() instanceof FormData) { |
| 85 | + progress.percentage = progress.progress ? Math.round(progress.progress * 100) : 0 |
| 86 | + fireProgressEvent(progress) |
| 87 | + this.requestParams.params.onProgress(progress) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + protected getHeaders(): AxiosRequestConfig['headers'] { |
| 92 | + const headers: AxiosRequestConfig['headers'] = { |
| 93 | + ...this.requestParams.headers(), |
| 94 | + Accept: 'text/html, application/xhtml+xml', |
| 95 | + 'X-Requested-With': 'XMLHttpRequest', |
| 96 | + 'X-Inertia': true, |
| 97 | + } |
| 98 | + |
| 99 | + if (currentPage.get().version) { |
| 100 | + headers['X-Inertia-Version'] = currentPage.get().version |
| 101 | + } |
| 102 | + |
| 103 | + return headers |
| 104 | + } |
| 105 | +} |
0 commit comments