diff --git a/lib/http/Http.ts b/lib/http/Http.ts index 4f1d3f97..9a13df4b 100644 --- a/lib/http/Http.ts +++ b/lib/http/Http.ts @@ -1,13 +1,7 @@ import { parse as parseContentDisposition } from '@tinyhttp/content-disposition' import { parse as parseCookie } from '@tinyhttp/cookie' import FileSaver from 'file-saver' -import { - FetchError, - type FetchOptions, - type FetchRequest, - type FetchResponse, - ofetch -} from 'ofetch' +import { FetchError, type FetchOptions, type FetchRequest, type FetchResponse, ofetch } from 'ofetch' import { stringify } from 'qs' import { type Lang } from '../composables/Lang' import { isBlob, isError, isFormData, isRequest, isResponse, isString } from '../support/Utils' @@ -15,8 +9,8 @@ import { isBlob, isError, isFormData, isRequest, isResponse, isString } from '.. type Awaitable = T | PromiseLike export interface HttpClient { - (request: FetchRequest, options?: Omit): Promise - raw(request: FetchRequest, options?: Omit): Promise> + (request: FetchRequest, options?: Omit): Promise + raw(request: FetchRequest, options?: Omit): Promise> } export interface HttpOptions { @@ -36,7 +30,7 @@ export class Http { private static payloadKey = '__payload__' private static headers: () => Awaitable> = async () => ({}) - static config(options: HttpOptions) { + static config(options: HttpOptions): void { if (options.baseUrl) { Http.baseUrl = options.baseUrl } @@ -72,15 +66,9 @@ export class Http { return xsrfToken } - private async buildRequest( - url: string, - _options: FetchOptions = {} - ): Promise<[string, FetchOptions]> { + private async buildRequest(url: string, _options: FetchOptions = {}): Promise<[string, FetchOptions]> { const { method, params, query, ...options } = _options - - const xsrfToken - = ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method || '') && (await this.ensureXsrfToken()) - + const xsrfToken = ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method || '') && (await this.ensureXsrfToken()) const queryString = stringify({ ...params, ...query }, { encodeValuesOnly: true }) return [ @@ -101,12 +89,12 @@ export class Http { ] } - private async performRequest(url: string, options: FetchOptions = {}) { - return Http.client(...(await this.buildRequest(url, options))) + private async performRequest(url: string, options: FetchOptions = {}): Promise { + return Http.client(...(await this.buildRequest(url, options))) } - private async performRequestRaw(url: string, options: FetchOptions = {}) { - return Http.client.raw(...(await this.buildRequest(url, options))) + private async performRequestRaw(url: string, options: FetchOptions = {}): Promise> { + return Http.client.raw(...(await this.buildRequest(url, options))) } async get(url: string, options?: FetchOptions): Promise { @@ -174,7 +162,7 @@ export class Http { FileSaver.saveAs(blob, filename as string) } - private objectToFormData(obj: any, form?: FormData, namespace?: string, onlyFiles = false) { + private objectToFormData(obj: any, form?: FormData, namespace?: string, onlyFiles = false): FormData { const fd = form || new FormData() let formKey: string @@ -211,9 +199,11 @@ export function isFetchError(e: unknown): e is FetchError { && (isString((e as FetchError).request) || isRequest((e as FetchError).request)) && ((e as FetchError).response === undefined || isResponse((e as FetchError).response)) && e.message.startsWith( - `[${((e as FetchError).request as Request | undefined)?.method || (e as FetchError).options?.method || 'GET'}] ${ - JSON.stringify(((e as FetchError).request as Request | undefined)?.url || String((e as FetchError).request) || '/') - }: ` + `[${ + ((e as FetchError).request as Request | undefined)?.method || (e as FetchError).options?.method || 'GET' + }] ${JSON.stringify( + ((e as FetchError).request as Request | undefined)?.url || String((e as FetchError).request) || '/' + )}: ` )) ) } diff --git a/package.json b/package.json index e28098e4..66220f2f 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "@types/file-saver": "^2.0.7", "@types/qs": "^6.9.16", "file-saver": "^2.0.5", - "ofetch": "~1.3", + "ofetch": "^1.4.0", "qs": "^6.13.0", "unplugin-icons": "^0.19.3" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 51e8afb8..2e269093 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -30,8 +30,8 @@ importers: specifier: ^2.0.5 version: 2.0.5 ofetch: - specifier: ~1.3 - version: 1.3.4 + specifier: ^1.4.0 + version: 1.4.0 qs: specifier: ^6.13.0 version: 6.13.0 @@ -3149,8 +3149,8 @@ packages: resolution: {integrity: sha512-IRZSRuzJiynemAXPYtPe5BoI/RESNYR7TYm50MC5Mqbd3Jmw5y790sErYw3V6SryFJD64b74qQQs9wn5Bg/k3g==} engines: {node: '>= 0.4'} - ofetch@1.3.4: - resolution: {integrity: sha512-KLIET85ik3vhEfS+3fDlc/BAZiAp+43QEC/yCo5zkNoY2YaKvNkOaFr/6wCFgFH1kuYQM5pMNi0Tg8koiIemtw==} + ofetch@1.4.0: + resolution: {integrity: sha512-MuHgsEhU6zGeX+EMh+8mSMrYTnsqJQQrpM00Q6QHMKNqQ0bKy0B43tk8tL1wg+CnsSTy1kg4Ir2T5Ig6rD+dfQ==} on-finished@2.3.0: resolution: {integrity: sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==} @@ -7634,7 +7634,7 @@ snapshots: object-inspect@1.13.2: {} - ofetch@1.3.4: + ofetch@1.4.0: dependencies: destr: 2.0.3 node-fetch-native: 1.6.4