| | |
| | | this.timestamp = /* @__PURE__ */ new Date().toISOString() |
| | | this.url = options?.url |
| | | this.method = options?.method |
| | | this.status = options?.status |
| | | this.contentType = options?.contentType |
| | | } |
| | | toLogData() { |
| | | return { |
| | |
| | | timestamp: this.timestamp, |
| | | url: this.url, |
| | | method: this.method, |
| | | status: this.status, |
| | | contentType: this.contentType, |
| | | stack: this.stack |
| | | } |
| | | } |
| | | } |
| | | function resolveRequestUrl(config) { |
| | | const baseURL = String(config?.baseURL || '').trim() |
| | | const requestUrl = String(config?.url || '').trim() |
| | | if (!baseURL && !requestUrl) { |
| | | return void 0 |
| | | } |
| | | if (!baseURL) { |
| | | return requestUrl || void 0 |
| | | } |
| | | if (!requestUrl) { |
| | | return baseURL |
| | | } |
| | | const isAbsoluteBase = /^https?:\/\//i.test(baseURL) |
| | | if (!isAbsoluteBase) { |
| | | const normalizedBase = baseURL.replace(/\/+$/, '') |
| | | const normalizedUrl = requestUrl.replace(/^\/+/, '') |
| | | if (!normalizedBase) { |
| | | return requestUrl |
| | | } |
| | | if (!normalizedUrl) { |
| | | return normalizedBase |
| | | } |
| | | return `${normalizedBase}/${normalizedUrl}` |
| | | } |
| | | try { |
| | | const origin = globalThis?.location?.origin || 'http://localhost' |
| | | const normalizedBase = new URL(baseURL, origin) |
| | | return new URL(requestUrl, normalizedBase).toString() |
| | | } catch { |
| | | const normalizedBase = baseURL.replace(/\/+$/, '') |
| | | const normalizedUrl = requestUrl.replace(/^\/+/, '') |
| | | if (!normalizedBase) { |
| | | return requestUrl |
| | | } |
| | | if (!normalizedUrl) { |
| | | return normalizedBase |
| | | } |
| | | return `${normalizedBase}/${normalizedUrl}` |
| | | } |
| | | } |
| | | const getErrorMessage = (status) => { |
| | |
| | | const requestConfig = error.config |
| | | if (isRequestCancelled(error)) { |
| | | throw new HttpError($t('httpMsg.requestCancelled'), 'REQUEST_CANCELLED', { |
| | | url: requestConfig?.url, |
| | | url: resolveRequestUrl(requestConfig), |
| | | method: requestConfig?.method?.toUpperCase() |
| | | }) |
| | | } |
| | |
| | | const errorMessage = error.response?.data?.msg || error.message |
| | | if (!error.response) { |
| | | throw new HttpError($t('httpMsg.networkError'), ApiStatus.error, { |
| | | url: requestConfig?.url, |
| | | url: resolveRequestUrl(requestConfig), |
| | | method: requestConfig?.method?.toUpperCase() |
| | | }) |
| | | } |
| | |
| | | : errorMessage || $t('httpMsg.requestFailed') |
| | | throw new HttpError(message, statusCode || ApiStatus.error, { |
| | | data: error.response.data, |
| | | url: requestConfig?.url, |
| | | method: requestConfig?.method?.toUpperCase() |
| | | url: resolveRequestUrl(requestConfig), |
| | | method: requestConfig?.method?.toUpperCase(), |
| | | status: statusCode, |
| | | contentType: error.response?.headers?.['content-type'] |
| | | }) |
| | | } |
| | | function showError(error, showMessage = true) { |
| | |
| | | const isHttpError = (error) => { |
| | | return error instanceof HttpError |
| | | } |
| | | export { HttpError, handleError, isHttpError, showError, showSuccess } |
| | | export { HttpError, handleError, isHttpError, resolveRequestUrl, showError, showSuccess } |