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

refactor(nuxt): enhance useFetch and useLazyFetch request type #4825

Merged
merged 28 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
645bf0f
feat(nuxt): enchance `useFetch` and `useLazyFetch` request type #4823
didavid61202 May 5, 2022
c5579db
test(nuxt): add type tests for request param of `useFetch` and `useLa…
didavid61202 May 5, 2022
1e75775
docs(nuxt): update `useFetch` and `useLazyFetch` docs for request url…
didavid61202 May 5, 2022
a89ea15
test(nuxt): remove extra type test case for useFetch
didavid61202 May 5, 2022
863a512
docs(nuxt): update data-fetching and `useLazyFetch` docs
didavid61202 May 5, 2022
0cf1647
Merge branch 'main' into usefetch-request-url-type
didavid61202 May 6, 2022
3359b3b
fix(nuxt): configure vite to transpile nuxt (#4915)
danielroe May 10, 2022
61c280b
Merge commit '3359b3bc8e911d20b2e59ea39eace0cb815cb56e' into usefetch…
didavid61202 May 10, 2022
4db5e51
chore(nuxt):
didavid61202 May 10, 2022
a1477d5
refactor(nuxt): import request type `NitroFetchRequest` fron nitro b…
didavid61202 May 10, 2022
5a265e9
docs: update doc
didavid61202 May 10, 2022
b3c6d26
Revert "fix(nuxt): configure vite to transpile nuxt (#4915)"
didavid61202 May 10, 2022
fab4c2b
Update docs/content/3.api/1.composables/use-fetch.md
didavid61202 May 11, 2022
304da8b
Update docs/content/3.api/1.composables/use-fetch.md
didavid61202 May 11, 2022
89d2703
Update docs/content/2.guide/2.features/5.data-fetching.md
didavid61202 May 11, 2022
b7d8ce4
Update docs/content/3.api/1.composables/use-lazy-fetch.md
didavid61202 May 11, 2022
0baecd9
Update docs/content/2.guide/2.features/5.data-fetching.md
didavid61202 May 11, 2022
87e63e3
Merge commit 'cad4edd5a396c447ad99f4016bc856e197c9d85b' into usefetch…
didavid61202 May 12, 2022
3240ecf
chore: bump nitropack to 0.4.4
didavid61202 May 12, 2022
96fc3e3
fix: update `fetch.ts`
didavid61202 May 12, 2022
68fdbbb
test: update types test
didavid61202 May 12, 2022
b49385c
refactor: alias NitroFetchRequest and exclude `/_*` `/api/_*` interal…
didavid61202 May 12, 2022
8ca15f5
test: update types test
didavid61202 May 12, 2022
ccb661b
refactor: revert walkaround changes in types test
didavid61202 May 12, 2022
9a31608
fix last commit on types test
didavid61202 May 12, 2022
3ad6c2c
remove walkarounds
didavid61202 May 12, 2022
8ea23a3
docs: tweaks
danielroe May 12, 2022
25726e1
Merge remote-tracking branch 'origin/main' into usefetch-request-url-…
danielroe Jul 22, 2022
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
2 changes: 1 addition & 1 deletion docs/content/2.guide/2.features/5.data-fetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ watch(count, (newCount) => {

Within your pages, components and plugins you can use `useFetch` to universally fetch from any URL.

This composable provides a convenient wrapper around `useAsyncData` and `$fetch`. It automatically generates a key based on URL and fetch options, as well as infers API response type.
This composable provides a convenient wrapper around `useAsyncData` and `$fetch`. It automatically generates a key based on URL and fetch options, provide type hints for request url base on server routes, as well as infers API response type.

::ReadMore{link="/api/composables/use-fetch"}
::
Expand Down
2 changes: 1 addition & 1 deletion docs/content/3.api/1.composables/use-fetch.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `useFetch`

This composable provides a convenient wrapper around [`useAsyncData`](/api/composables/use-async-data) and [`$fetch`](/api/utils/$fetch). It automatically generates a key based on URL and fetch options, as well as infers API response type.
This composable provides a convenient wrapper around [`useAsyncData`](/api/composables/use-async-data) and [`$fetch`](/api/utils/$fetch). It automatically generates a key based on URL and fetch options, provide type hints for request url base on server routes, as well as infers API response type.

## Type

Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"knitwork": "^0.1.1",
"magic-string": "^0.26.1",
"mlly": "^0.5.2",
"nitropack": "^0.4.3",
"nitropack": "^0.4.4",
"nuxi": "^3.0.0-rc.3",
"ohash": "^0.1.0",
"ohmyfetch": "^0.4.16",
Expand Down
6 changes: 3 additions & 3 deletions packages/nuxt/src/app/composables/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FetchOptions, FetchRequest } from 'ohmyfetch'
import type { FetchOptions } from 'ohmyfetch'
import type { TypedInternalResponse, NitroFetchRequest } from 'nitropack'
import { hash } from 'ohash'
import { computed, isRef, Ref } from 'vue'
Expand Down Expand Up @@ -31,7 +31,7 @@ export function useFetch<
}
const key = '$f_' + (opts.key || hash([request, { ...opts, transform: null }]))
const _request = computed(() => {
let r = request as Ref<FetchRequest> | FetchRequest | (() => FetchRequest)
let r = request as Ref<NitroFetchRequest> | NitroFetchRequest | (() => NitroFetchRequest)
if (typeof r === 'function') {
r = r()
}
Expand All @@ -52,7 +52,7 @@ export function useFetch<
}

const asyncData = useAsyncData<_ResT, ErrorT, Transform, PickKeys>(key, () => {
return $fetch(_request.value, _fetchOptions)
return $fetch(_request.value, _fetchOptions) as Promise<_ResT>
}, _asyncDataOptions)

return asyncData
Expand Down
14 changes: 12 additions & 2 deletions test/fixtures/basic/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,17 @@ describe('composables', () => {
// @ts-expect-error
expectTypeOf(useAsyncData('test', () => Promise.resolve('500'), { default: () => 500 }).data).toMatchTypeOf<Ref<number>>()

expectTypeOf(useFetch('/test', { default: () => ref(500) }).data).toMatchTypeOf<Ref<number>>()
expectTypeOf(useFetch('/test', { default: () => 500 }).data).toMatchTypeOf<Ref<number>>()
expectTypeOf(useFetch('test/default', { default: () => ref(500) }).data).toMatchTypeOf<Ref<number>>()
expectTypeOf(useFetch('test/default', { default: () => 500 }).data).toMatchTypeOf<Ref<number>>()
})

it('infer request url string literal from server/api routes', () => {
// request param should infer string literal type / show auto-complete hint base on server routes, ex: '/api/hello'
const dynamicStringUrl:string = 'https://example.com/api'
expectTypeOf(useFetch(dynamicStringUrl))
expectTypeOf(useFetch('/api/hello').data).toMatchTypeOf<Ref<string>>()
expectTypeOf(useLazyFetch('/api/hello').data).toMatchTypeOf<Ref<string>>()
expectTypeOf(useFetch('https://example.com/api').data).toMatchTypeOf<Ref<Pick<unknown, never>>>()
expectTypeOf(useFetch(new Request('test')).data).toMatchTypeOf<Ref<Pick<unknown, never>>>()
})
})
33 changes: 26 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9495,9 +9495,9 @@ __metadata:
languageName: node
linkType: hard

"nitropack@npm:^0.4.3":
version: 0.4.3
resolution: "nitropack@npm:0.4.3"
"nitropack@npm:^0.4.4":
version: 0.4.4
resolution: "nitropack@npm:0.4.4"
dependencies:
"@cloudflare/kv-asset-handler": ^0.2.0
"@netlify/functions": ^1.0.0
Expand Down Expand Up @@ -9535,7 +9535,7 @@ __metadata:
mime: ^3.0.0
mlly: ^0.5.2
mri: ^1.2.0
node-fetch: ^3.2.4
node-fetch-native: ^0.1.2
ohash: ^0.1.0
ohmyfetch: ^0.4.16
pathe: ^0.3.0
Expand All @@ -9554,13 +9554,13 @@ __metadata:
std-env: ^3.1.1
table: ^6.8.0
ufo: ^0.8.4
unenv: ^0.5.1
unenv: ^0.5.2
unimport: ^0.2.1
unstorage: ^0.4.1
bin:
nitro: dist/cli.mjs
nitropack: dist/cli.mjs
checksum: 41b143ec2c2990df7374f91014c7c13f00acd910039bc902874436655cbbbaec9478c6f374b21f984ebb21b9f6dbfc702dba84cdf0b19b15cd92f8843fad7694
checksum: 5dd9ece9707b775bf87f1f6625ce96bfc29813507157dbd84178220cd0f1216964b474498f51b934ddfcd5f6ea120a4f1071415ac37eed28ea9a406e6c283882
languageName: node
linkType: hard

Expand All @@ -9571,6 +9571,13 @@ __metadata:
languageName: node
linkType: hard

"node-fetch-native@npm:^0.1.2":
version: 0.1.3
resolution: "node-fetch-native@npm:0.1.3"
checksum: b9659a22c41b471e970ef3223682d08c7c439deed9ed647f218bda503dae06d78f1882e1532ff9a70d088eac4a20b12a1711e18a756f609e26d6956955277a40
languageName: node
linkType: hard

"node-fetch@npm:^2.6.1, node-fetch@npm:^2.6.7":
version: 2.6.7
resolution: "node-fetch@npm:2.6.7"
Expand Down Expand Up @@ -10074,7 +10081,7 @@ __metadata:
knitwork: ^0.1.1
magic-string: ^0.26.1
mlly: ^0.5.2
nitropack: ^0.4.3
nitropack: ^0.4.4
nuxi: ^3.0.0-rc.3
ohash: ^0.1.0
ohmyfetch: ^0.4.16
Expand Down Expand Up @@ -13353,6 +13360,18 @@ __metadata:
languageName: node
linkType: hard

"unenv@npm:^0.5.2":
version: 0.5.2
resolution: "unenv@npm:0.5.2"
dependencies:
defu: ^6.0.0
mime: ^3.0.0
node-fetch-native: ^0.1.2
pathe: ^0.3.0
checksum: 07efccc473e419cad22e86aac504bb49f9fc9c22c85fa8b830d6fbdf2aea39f82278bd70306a5c42dc26c6bc5f5034b23ea4f14bfd73ba9db7370a9e9b34fc6e
languageName: node
linkType: hard

"unimport@npm:^0.2.1":
version: 0.2.1
resolution: "unimport@npm:0.2.1"
Expand Down