From 54c57e3987f87aa2be0d6ad15995302bf907baa7 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Tue, 12 Oct 2021 00:36:50 +0200 Subject: [PATCH] feat(nuxt3): `useFetch` (#721) --- .../content/3.docs/1.usage/1.data-fetching.md | 55 ++++++++++++++++--- examples/use-fetch/nuxt.config.ts | 4 ++ examples/use-fetch/package.json | 12 ++++ examples/use-fetch/pages/index.vue | 9 +++ examples/use-fetch/server/api/hello.ts | 3 + packages/bridge/src/auto-imports.ts | 3 +- packages/nitro/src/build.ts | 3 +- packages/nitro/types/fetch.d.ts | 6 +- .../nuxt3/src/app/composables/asyncData.ts | 53 +++++++++++++++--- packages/nuxt3/src/app/composables/fetch.ts | 44 +++++++++++++++ packages/nuxt3/src/app/composables/index.ts | 1 + .../nuxt3/src/auto-imports/identifiers.ts | 3 +- yarn.lock | 8 +++ 13 files changed, 182 insertions(+), 22 deletions(-) create mode 100644 examples/use-fetch/nuxt.config.ts create mode 100644 examples/use-fetch/package.json create mode 100644 examples/use-fetch/pages/index.vue create mode 100644 examples/use-fetch/server/api/hello.ts create mode 100644 packages/nuxt3/src/app/composables/fetch.ts diff --git a/docs/content/3.docs/1.usage/1.data-fetching.md b/docs/content/3.docs/1.usage/1.data-fetching.md index cdad0a6637..9cf3adfb7b 100644 --- a/docs/content/3.docs/1.usage/1.data-fetching.md +++ b/docs/content/3.docs/1.usage/1.data-fetching.md @@ -1,10 +1,10 @@ # Data Fetching -Nuxt provides `useAsyncData` to handle data fetching within your application. +Nuxt provides `useFetch` and `useAsyncData` to handle data fetching within your application. ## `useAsyncData` -Within your pages and components you can use `useAsyncData` to get access to data that resolves asynchronously. +Within your pages, components and plugins you can use `useAsyncData` to get access to data that resolves asynchronously. ### Usage @@ -17,12 +17,19 @@ useAsyncData(key: string, fn: () => Object, options?: { defer: boolean, server: * **options**: - _defer_: whether to load the route before resolving the async function (defaults to `false`) - _server_: whether the fetch the data on server-side (defaults to `true`) + - _transform_: A function that can be used to alter fn result after resolving + - _pick_: Only pick specified keys in this array from fn result Under the hood, `defer: false` uses `` to block the loading of the route before the data has been fetched. Consider using `defer: true` and implementing a loading state instead for a snappier user experience. ### Example -```vue +```js [server/api/index.ts] +let counter = 0 +export default () => ++counter +``` + +```vue [pages/index.vue] @@ -32,6 +39,42 @@ const { data } = await useAsyncData('time', () => $fetch('/api/count')) ``` +## `useFetch` + +Within your pages, components and plugins you can use `useFetch` to get universally fetch from any URL. + +This composable provides a convenient wrapper around `useAsyncData` and `$fetch` and automatically generates a key based on url and fetch options and infers API response type. + +Usage: + +```ts +useFetch(url: string, options?) +``` + +Available options: +- `key`: Provide a custom key +- Options from [ohmyfetch](https://github.com/unjs/ohmyfetch) + - `method`: Request method + - `params`: Query params + - `baseURL`: Base URL for request +- Options from `useAsyncDaa` + - `defer` + - `server` + - `pick` + - `transform` + +### Example + +```vue [pages/index.vue] + + + +``` + ### Best practices As seen in [Concepts > Data fetching](/concepts/data-fetching), the data returned by `useAsyncData` will be stored inside the page payload. This mean that every key returned that is not used in your component will be added to the payload. @@ -54,13 +97,11 @@ Imagine that `/api/mountains/everest` returns the following object: } ``` -If you plan to only use `title` and `description` in your component, you can select the keys by chaining the result of `$fetch`: +If you plan to only use `title` and `description` in your component, you can select the keys by chaining the result of `$fetch` or `pick` option: ```vue