Skip to content

$fetch-Like Composable ​

Sends a plain fetch and returns the response data. Intended for actions inside methods, e.g. when sending form data on submit.

Placeholder

$myApi is a placeholder. The composable is generated based on the endpoint ID. For example, endpoint jsonPlaceholder generates $jsonPlaceholder.

Type Declarations ​

ts
export type ApiClientFetchOptions = Omit<FetchOptions, 'method'> & {
  method?: RequestMethod
  path?: Record<string, string>
}

export type ApiClient = <T = unknown>(path: string, opts?: ApiClientFetchOptions) => Promise<T>

Every ofetch option but baseURL, which the endpoint decides, passes through – cache for the browser's HTTP cache included.

Examples ​

Demo Setup

These examples assume that you have set up an endpoint called jsonPlaceholder:

ts
export default defineNuxtConfig({
  modules: ['nuxt-api-party'],

  apiParty: {
    endpoints: {
      jsonPlaceholder: {
        url: 'https://jsonplaceholder.typicode.com',
      },
    },
  },
})
ts
async function createPost() {
  const post = await $jsonPlaceholder('posts', {
    method: 'POST',
    body: {
      foo: 'bar',
    },
    async onRequest({ request }) {
      console.log(request)
    },
    async onResponse({ response }) {
      console.log(response)
    },
    async onRequestError({ error }) {
      console.log(error)
    },
    async onResponseError({ response }) {
      console.log(response.status, response._data)
    },
  })
}

In Component Setup ​

$myApi sends a request every time it runs and stores nothing in the Nuxt payload. Called in a component's setup, it runs once during SSR and once more in the browser while the page hydrates – two requests for one result. In development, the module warns in the console when that happens.

Fetch data for a page with useMyApiData, or wrap the call in useAsyncData, which hands the server's response to the browser:

ts
const { data } = await useAsyncData('posts', () => $jsonPlaceholder('posts'))

Several Calls in useAsyncData ​

Nuxt Context Issue

$myApi reads the Nuxt app when it is called. In a useAsyncData handler on the server, a second call after an await runs without it and throws.

Start the calls together, before the first await:

ts
const { data } = await useAsyncData('post-with-comments', async () => {
  const [post, comments] = await Promise.all([
    $jsonPlaceholder('posts/1'),
    $jsonPlaceholder('posts/1/comments'),
  ])

  return { post, comments }
})

Where one call needs the result of the other, run the second inside nuxtApp.runWithContext:

ts
const { data } = await useAsyncData('author-of-post', async (nuxtApp) => {
  const post = await $jsonPlaceholder<{ userId: number }>('posts/1')

  return await nuxtApp.runWithContext(() => $jsonPlaceholder(`users/${post.userId}`))
})

Client Endpoints ​

A call to a client endpoint goes straight to the API – from the browser, and from the server during SSR – instead of through the server proxy. The endpoint sets this for all of its calls; no call option overrides it.

Released under the MIT License.