Skip to content

Commit

Permalink
fix: remove lock on ofetch
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Oct 8, 2024
1 parent 4effab5 commit 3ad5a7d
Show file tree
Hide file tree
Showing 4 changed files with 1,118 additions and 1,053 deletions.
8 changes: 6 additions & 2 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ export default defineConfig({
},

themeConfig: {
outline: [2, 3],

editLink: {
pattern: 'https://github.com/globalbrain/sefirot/edit/main/docs/:path'
},

nav: [
{ text: 'Playground', link: `https://${getStoryHost()}` },
],

outline: [2, 3],

search: {
provider: 'local'
},
Expand Down
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
54 changes: 27 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@globalbrain/sefirot",
"type": "module",
"version": "4.4.2",
"packageManager": "pnpm@9.9.0",
"packageManager": "pnpm@9.12.1",
"description": "Vue Components for Global Brain Design System.",
"author": "Kia Ishii <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -48,53 +48,53 @@
"@types/body-scroll-lock": "^3.1.2",
"@types/lodash-es": "^4.17.12",
"@types/markdown-it": "^14.1.2",
"@vue/reactivity": "^3.5.1",
"@vue/reactivity": "^3.5.11",
"@vuelidate/core": "^2.0.3",
"@vuelidate/validators": "^2.0.4",
"@vueuse/core": "^11.0.3",
"@vueuse/core": "^11.1.0",
"body-scroll-lock": "4.0.0-beta.0",
"dayjs": "^1.11.13",
"fuse.js": "^7.0.0",
"lodash-es": "^4.17.21",
"markdown-it": "^14.1.0",
"normalize.css": "^8.0.1",
"pinia": "^2.2.2",
"postcss": "^8.4.45",
"pinia": "^2.2.4",
"postcss": "^8.4.47",
"postcss-nested": "^6.2.0",
"v-calendar": "3.0.1",
"vue": "^3.5.1",
"vue-router": "^4.4.3"
"vue": "^3.5.11",
"vue-router": "^4.4.5"
},
"dependencies": {
"@sentry/browser": "^8.28.0",
"@sentry/browser": "^8.33.1",
"@tanstack/vue-virtual": "3.0.0-beta.62",
"@tinyhttp/content-disposition": "^2.2.1",
"@tinyhttp/content-disposition": "^2.2.2",
"@tinyhttp/cookie": "^2.1.1",
"@types/file-saver": "^2.0.7",
"@types/qs": "^6.9.15",
"@types/qs": "^6.9.16",
"file-saver": "^2.0.5",
"magic-string": "^0.30.11",
"ofetch": "^1.3.4",
"ofetch": "^1.4.0",
"qs": "^6.13.0",
"unplugin-icons": "^0.19.2"
"unplugin-icons": "^0.19.3"
},
"devDependencies": {
"@globalbrain/eslint-config": "^1.7.1",
"@histoire/plugin-vue": "0.16.5",
"@iconify-json/ph": "^1.2.0",
"@iconify-json/ri": "^1.2.0",
"@release-it/conventional-changelog": "^8.0.1",
"@release-it/conventional-changelog": "^8.0.2",
"@types/body-scroll-lock": "^3.1.2",
"@types/lodash-es": "^4.17.12",
"@types/markdown-it": "^14.1.2",
"@types/node": "^22.5.3",
"@vitejs/plugin-vue": "^5.1.3",
"@vitest/coverage-v8": "^2.0.5",
"@vue/reactivity": "^3.5.1",
"@types/node": "^22.7.5",
"@vitejs/plugin-vue": "^5.1.4",
"@vitest/coverage-v8": "^2.1.2",
"@vue/reactivity": "^3.5.11",
"@vue/test-utils": "^2.4.6",
"@vuelidate/core": "^2.0.3",
"@vuelidate/validators": "^2.0.4",
"@vueuse/core": "^11.0.3",
"@vueuse/core": "^11.1.0",
"body-scroll-lock": "4.0.0-beta.0",
"dayjs": "^1.11.13",
"eslint": "8.57.0",
Expand All @@ -104,18 +104,18 @@
"lodash-es": "^4.17.21",
"markdown-it": "^14.1.0",
"normalize.css": "^8.0.1",
"pinia": "^2.2.2",
"postcss": "^8.4.45",
"pinia": "^2.2.4",
"postcss": "^8.4.47",
"postcss-nested": "^6.2.0",
"punycode": "^2.3.1",
"release-it": "^17.6.0",
"typescript": "~5.5.4",
"release-it": "^17.7.0",
"typescript": "~5.6.2",
"v-calendar": "3.0.1",
"vite": "^5.4.3",
"vitepress": "^1.3.4",
"vitest": "^2.0.5",
"vue": "^3.5.1",
"vue-router": "^4.4.3",
"vite": "^5.4.8",
"vitepress": "^1.4.0",
"vitest": "^2.1.2",
"vue": "^3.5.11",
"vue-router": "^4.4.5",
"vue-tsc": "^2.1.6"
}
}
Loading

0 comments on commit 3ad5a7d

Please sign in to comment.