import { type FetchOptions, type FetchRequest, type FetchResponse, ofetch } from 'ofetch' import { defineStore } from 'pinia' import { type IStringifyOptions } from 'qs' import { computed, ref } from 'vue' import { type Lang } from '../composables/Lang' type Awaitable = T | PromiseLike export interface HttpClient { (request: FetchRequest, options?: Omit): Promise raw?(request: FetchRequest, options?: Omit): Promise> } export interface HttpOptions { baseUrl?: string xsrfUrl?: string | false client?: HttpClient recoverSession?: false | (() => Awaitable) lang?: Lang payloadKey?: string headers?: () => Awaitable> stringifyOptions?: IStringifyOptions } export const useHttpConfig = defineStore('sefirot-http-config', () => { const baseUrl = ref(undefined) const xsrfUrl = ref('/api/csrf-cookie') const client = ref(ofetch) const recoverSession = ref<(() => Awaitable) | undefined>(undefined) const lang = ref(undefined) const payloadKey = ref('__payload__') const headers = ref<() => Awaitable>>(async () => ({})) const stringifyOptions = ref({}) function apply(options: HttpOptions): void { if (options.baseUrl != null) { baseUrl.value = options.baseUrl } if (options.xsrfUrl != null) { xsrfUrl.value = options.xsrfUrl } if (options.client != null) { client.value = options.client } if (options.recoverSession != null) { recoverSession.value = options.recoverSession || undefined } if (options.lang != null) { lang.value = options.lang } if (options.payloadKey != null) { payloadKey.value = options.payloadKey } if (options.headers != null) { headers.value = options.headers } if (options.stringifyOptions != null) { stringifyOptions.value = options.stringifyOptions } } return { baseUrl: computed(() => baseUrl.value), xsrfUrl: computed(() => xsrfUrl.value), client: computed(() => client.value), recoverSession: computed(() => recoverSession.value), lang: computed(() => lang.value), payloadKey: computed(() => payloadKey.value), headers: computed(() => headers.value), stringifyOptions: computed(() => stringifyOptions.value), apply } })