createFetchInstance

Gitlab
A utility function for creating a fetch instance for the Statamic API.
Added since: v1.0.0
Last changed:
Most use cases will be covered by the useStatamicApi composable. If you need more control or want to create a custom fetch instance on your server side, this function is for you.

Usage

The createFetchInstance function is a utility function that creates a configured fetch instance for making requests to the Statamic API.

Basic example

It is recommended to use the $api instance that the module provides via the plugin.

app/pages/index.vue
<script setup lang="ts">
const { 
$api
} =
useNuxtApp
()
</script>

Server side example

If you want to create a custom fetch instance (on your server side for example), you can easily use the createFetchInstance function.

server/api/urls.get.ts
export default 
defineCachedEventHandler
(async (
event
:
H3Event
) => {
const
api
= createFetchInstance()
});

Type Definition

function 
createFetchInstance
(
options
?:
FetchOptions
): $Fetch

API Reference

Params

options
FetchOptions
Optional ofetch options to configure the fetch instance.

Return Value

$Fetch
The configured fetch instance with the following (default) configuration:
  • baseURL
    Set to process.env.NUXT_PUBLIC_STATAMIC_URL
  • Query parameter serialization
  • Optional debug logging for requests
    when process.env.NUXT_DEBUG is set to true or debug mode is enabled in the module configuration
  • Support for custom fetch options and hooks

Changelog

v1.0.0

on

#407d36db

-

feat: released initial version of nuxt-statamic