Untitled
unknown
javascript
9 months ago
1.3 kB
5
Indexable
import { ofetch, type SearchParameters } from 'ofetch' import { useAppConfig, useCookie } from '#app' const options = ({ opts, method }: { opts: SearchParameters; method: 'GET' | 'POST' | 'PUT' | 'DELETE' }): Object => { const token = useCookie('auth:token') const locale = useCookie('locale', { default: () => 'en' }) const { baseURL } = useAppConfig() opts.method = method opts.headers = { Accept: 'application/json', 'Content-Type': 'application/json', 'Accept-Language': locale.value, ...(opts.headers || {}) } if (token.value) { opts.headers = { ...(opts.headers || {}), Authorization: `Bearer ${token.value}` } } return { baseURL, ...opts } } const get = (endpoint: string, opts = {}) => { return ofetch(endpoint, options({ opts, method: 'GET' })) } const post = (endpoint: string, opts = {}) => { return ofetch(endpoint, options({ opts, method: 'POST' })) } const put = (endpoint: string, opts = {}) => { return ofetch(endpoint, options({ opts, method: 'PUT' })) } const remove = (endpoint: string, opts = {}) => { return ofetch(endpoint, options({ opts, method: 'DELETE' })) } export default function () { return { get, post, put, remove } }
Editor is loading...
Leave a Comment