-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update useCustomFetch from atinux version (#71)
* chore: update useCustomFetch from atinux version * docs: add useCustomFetch read-more article
- Loading branch information
1 parent
a77e403
commit bbe3468
Showing
3 changed files
with
42 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 11 additions & 34 deletions
45
examples/advanced/use-custom-fetch-composable/composables/useCustomFetch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,11 @@ | ||
import type { UseFetchOptions } from '#app' | ||
import { defu } from 'defu' | ||
|
||
export function useCustomFetch<T> (url: string | (() => string), options: UseFetchOptions<T> = {}) { | ||
const userAuth = useCookie('token') | ||
const config = useRuntimeConfig() | ||
|
||
const defaults: UseFetchOptions<T> = { | ||
baseURL: config.baseUrl ?? 'https://api.nuxt.com', | ||
// this overrides the default key generation, which includes a hash of | ||
// url, method, headers, etc. - this should be used with care as the key | ||
// is how Nuxt decides how responses should be deduplicated between | ||
// client and server | ||
key: url, | ||
|
||
// set user token if connected | ||
headers: userAuth.value | ||
? { Authorization: `Bearer ${userAuth.value}` } | ||
: {}, | ||
|
||
onResponse (_ctx) { | ||
// _ctx.response._data = new myBusinessResponse(_ctx.response._data) | ||
}, | ||
|
||
onResponseError (_ctx) { | ||
// throw new myBusinessError() | ||
} | ||
} | ||
|
||
// for nice deep defaults, please use unjs/defu | ||
const params = defu(options, defaults) | ||
|
||
return useFetch(url, params) | ||
} | ||
import type { UseFetchOptions } from 'nuxt/app'; | ||
|
||
export function useCustomFetch<T>( | ||
url: string | (() => string), | ||
options: UseFetchOptions<T> = {} | ||
) { | ||
return useFetch(url, { | ||
...options, | ||
$fetch: useNuxtApp().$customFetch, | ||
}) | ||
} |
29 changes: 29 additions & 0 deletions
29
examples/advanced/use-custom-fetch-composable/plugins/customFetch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export default defineNuxtPlugin(() => { | ||
const userAuth = useCookie('token') | ||
const config = useRuntimeConfig() | ||
|
||
const $customFetch = $fetch.create({ | ||
baseURL: config.baseUrl ?? 'https://api.nuxt.com', | ||
onRequest({ request, options, error }) { | ||
if (userAuth.value) { | ||
// Add Authorization header | ||
options.headers = options.headers || {} | ||
options.headers.Authorization = `Bearer ${userAuth.value}` | ||
} | ||
}, | ||
onResponse ({ response }) { | ||
// response._data = new myBusinessResponse(response._data) | ||
}, | ||
onResponseError({ response }) { | ||
if (response.status === 401) { | ||
return navigateTo('/login') | ||
} | ||
} | ||
}) | ||
// Expose to useNuxtApp().$customFetch | ||
return { | ||
provide: { | ||
customFetch: $customFetch | ||
} | ||
} | ||
}) |