Skip to content

Commit

Permalink
remove lock on ofetch
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Sep 24, 2024
1 parent fe5bcb9 commit 34c2529
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
42 changes: 16 additions & 26 deletions lib/http/Http.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
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'

type Awaitable<T> = T | PromiseLike<T>

export interface HttpClient {
<T = any>(request: FetchRequest, options?: Omit<FetchOptions, 'method'>): Promise<T>
raw<T = any>(request: FetchRequest, options?: Omit<FetchOptions, 'method'>): Promise<FetchResponse<T>>
(request: FetchRequest, options?: Omit<FetchOptions, 'method'>): Promise<any>
raw(request: FetchRequest, options?: Omit<FetchOptions, 'method'>): Promise<FetchResponse<any>>
}

export interface HttpOptions {
Expand All @@ -36,7 +30,7 @@ export class Http {
private static payloadKey = '__payload__'
private static headers: () => Awaitable<Record<string, string>> = async () => ({})

static config(options: HttpOptions) {
static config(options: HttpOptions): void {
if (options.baseUrl) {
Http.baseUrl = options.baseUrl
}
Expand Down Expand Up @@ -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 [
Expand All @@ -101,12 +89,12 @@ export class Http {
]
}

private async performRequest<T>(url: string, options: FetchOptions = {}) {
return Http.client<T>(...(await this.buildRequest(url, options)))
private async performRequest<T>(url: string, options: FetchOptions = {}): Promise<T> {
return Http.client(...(await this.buildRequest(url, options)))
}

private async performRequestRaw<T>(url: string, options: FetchOptions = {}) {
return Http.client.raw<T>(...(await this.buildRequest(url, options)))
private async performRequestRaw<T>(url: string, options: FetchOptions = {}): Promise<FetchResponse<T>> {
return Http.client.raw(...(await this.buildRequest(url, options)))
}

async get<T = any>(url: string, options?: FetchOptions): Promise<T> {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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) || '/'
)}: `
))
)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 34c2529

Please sign in to comment.