Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt3): useFetch with dynamic reactive request #3731

Merged
merged 4 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(useFetch): reactive request
  • Loading branch information
pi0 committed Mar 17, 2022
commit 1261ab79643979b3fb397be71148723132a10103
14 changes: 10 additions & 4 deletions examples/use-fetch/app.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<script setup>
const { data } = await useFetch('/api/hello', { params: { foo: 'bar' } })
const count = ref(1)
const { data } = await useFetch(() => `/api/hello/${count.value}`, { params: { count: count.value } })
</script>

<template>
<NuxtExampleLayout example="use-fetch" show-tips="true">
Server response
<pre class="text-left"><code>{{ data }}</code></pre>
<NuxtExampleLayout example="use-fetch" :show-tips="true">
<div>
Fetch result:
<pre class="text-left"><code>{{ data }}</code></pre>
<NButton @click="count++">
+
</NButton>
</div>
<template #tips>
<div>
Nuxt will automatically read in files in the
Expand Down
6 changes: 5 additions & 1 deletion examples/use-fetch/server/api/hello.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { useQuery } from 'h3'
import { parseURL } from 'ufo'

export default req => ({ query: useQuery(req) })
export default req => ({
path: '/api/hello' + parseURL(req.url).pathname,
query: useQuery(req)
})
1 change: 1 addition & 0 deletions packages/bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"murmurhash-es": "^0.1.1",
"node-fetch": "^3.2.3",
"nuxi": "3.0.0",
"ohash": "^0.1.0",
"pathe": "^0.2.0",
"perfect-debounce": "^0.1.3",
"postcss": "^8",
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
"knitwork": "^0.1.1",
"magic-string": "^0.26.1",
"mlly": "^0.4.3",
"murmurhash-es": "^0.1.1",
"nitropack": "npm:nitropack-edge@latest",
"nuxi": "3.0.0",
"ohash": "^0.1.0",
"ohmyfetch": "^0.4.15",
"pathe": "^0.2.0",
"perfect-debounce": "^0.1.3",
Expand Down
38 changes: 22 additions & 16 deletions packages/nuxt3/src/app/composables/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { FetchOptions, FetchRequest } from 'ohmyfetch'
import type { TypedInternalResponse } from '@nuxt/nitro'
import { murmurHashV3 } from 'murmurhash-es'
import { hash } from 'ohash'
import { computed, isRef, Ref } from 'vue'
import type { AsyncDataOptions, _Transform, KeyOfRes } from './asyncData'
import { useAsyncData } from './asyncData'

Expand All @@ -14,29 +15,34 @@ export type UseFetchOptions<

export function useFetch<
ResT = void,
ReqT extends string = string,
ReqT extends FetchRequest = FetchRequest,
_ResT = ResT extends void ? FetchResult<ReqT> : ResT,
Transform extends (res: _ResT) => any = (res: _ResT) => _ResT,
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
> (
url: ReqT,
request: Ref<ReqT> | ReqT | (() => ReqT),
opts: UseFetchOptions<_ResT, Transform, PickKeys> = {}
) {
if (!opts.key) {
const keys: any = { u: url }
if (opts.baseURL) {
keys.b = opts.baseURL
}
if (opts.method && opts.method.toLowerCase() !== 'get') {
keys.m = opts.method.toLowerCase()
const key = opts.key || '_f' + hash([request, opts])
const _request = computed<FetchRequest>(() => {
let r = request
if (typeof r === 'function') {
r = r()
}
if (opts.params) {
keys.p = opts.params
}
opts.key = generateKey(keys)
}
return isRef(r) ? r.value : r
})

const asyncData = useAsyncData(key, () => {
return $fetch(_request.value, opts) as Promise<_ResT>
}, {
...opts,
watch: [
_request,
...(opts.watch || [])
]
})

return useAsyncData(opts.key, () => $fetch(url, opts) as Promise<_ResT>, opts)
return asyncData
}

export function useLazyFetch<
Expand Down
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2620,6 +2620,7 @@ __metadata:
node-fetch: ^3.2.3
nuxi: 3.0.0
nuxt: ^2
ohash: ^0.1.0
pathe: ^0.2.0
perfect-debounce: ^0.1.3
postcss: ^8
Expand Down Expand Up @@ -15464,9 +15465,9 @@ __metadata:
knitwork: ^0.1.1
magic-string: ^0.26.1
mlly: ^0.4.3
murmurhash-es: ^0.1.1
nitropack: "npm:nitropack-edge@latest"
nuxi: 3.0.0
ohash: ^0.1.0
ohmyfetch: ^0.4.15
pathe: ^0.2.0
perfect-debounce: ^0.1.3
Expand Down Expand Up @@ -15608,6 +15609,13 @@ __metadata:
languageName: node
linkType: hard

"ohash@npm:^0.1.0":
version: 0.1.0
resolution: "ohash@npm:0.1.0"
checksum: b2bfde97c96bb4e71f3e0b7718239c7e49435394f1f562e0d21a069b84302bea0a235c65e2615009f6a4e2008963914917bfcd576d65ff56016f174d0b1e206b
languageName: node
linkType: hard

"ohmyfetch@npm:^0.4.15, ohmyfetch@npm:^0.4.5":
version: 0.4.15
resolution: "ohmyfetch@npm:0.4.15"
Expand Down