Registry / devops / nuxt-strict-fetch

nuxt-strict-fetch

JSON →
library1.4.0jsnpmunverified

A Nuxt 3 module that provides a type-safe and schema-validated wrapper around the built-in $fetch. Current version 1.4.0, actively maintained. Key differentiators: supports Yup and Zod for runtime schema validation of request/response bodies, query parameters, and URL params. Requires either yup (>= 1.5.0) or zod (>= 3.0.0) as peer dependencies. Simplifies API method definitions with StrictFetch.prepare and integrates with Nuxt's composables (useRequest, useAutoFetch). Provides global configuration via module options and a plugin init method for custom fetch overrides.

npm install nuxt-strict-fetch
INSTALL
IMPORT
SIG · NUXT-STRICT-FETCH
N
nuxt-strict-fetch
devopsjavascriptv1.4.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

StrictFetch
import { StrictFetch } from '#imports'
import StrictFetch from 'nuxt-strict-fetch'
Must be auto-imported from Nuxt modules. Do not try to import from the npm package directly.
useRequest
const { data, error } = useRequest('endpoint')
import { useRequest } from 'nuxt-strict-fetch'
Auto-imported composable; use directly in components/pages.
useAutoFetch
useAutoFetch(CommonAPI.details, { params: { id: 1 } })
useAutoFetch('endpoint')
Only works with prepared API methods, not raw URLs.
HTTPMethod
import { HTTPMethod } from '#imports'
import { HTTPMethod } from 'nuxt-strict-fetch'
Enum with values like GET, POST, etc. Auto-imported in Nuxt context.

Shows module setup, API method definition with Zod validation, and usage of useRequest composable.

// nuxt.config.ts export default defineNuxtConfig({ modules: ['nuxt-strict-fetch'], strictFetch: { baseURL: '/api/', validator: 'zod', }, }) // app/utils/api.ts import { z } from 'zod' const itemSchema = z.object({ id: z.number(), name: z.string() }) const listSchema = z.array(itemSchema) const CommonAPI = { list: StrictFetch.prepare({ url: 'items', schemas: { response: listSchema }, }), } // In a component const { data, error } = await useRequest(CommonAPI.list) console.log(data.value) // typed as Item[]
Debug
Known issues
breakingRequires at least one of yup (>=1.5.0) or zod (>=3.0.0) as a peer dependency if validator module option is set.
fix
Install the appropriate validator: npm install zod or npm install yup
affects: >=1.0.0
gotchaStrictFetch.prepare returns a prepared object, not a function. Use it with the composables (useRequest, useAutoFetch) or call .$fetch() method.
fix
Do not call StrictFetch.prepare() directly as a function. Assign to variable and use with useRequest or invoke .$fetch(requestOptions).
affects: >=1.0.0
gotchaIf using TypeScript, ensure the compilerOptions.paths in tsconfig.json include '#imports' or the module may not auto-import correctly.
fix
Add '~/#imports' to paths if errors appear: "Cannot find module '#imports'".
affects: >=1.0.0
deprecatedThe 'validator' module option is required when using schemas; if omitted, validation schemas are ignored silently.
fix
Explicitly set validator: 'yup' or 'zod' in nuxt.config even if using only one.
affects: >=1.0.0 <2.0.0
gotchaWhen using schemas, the response type is inferred from the schema, not from the generic type parameter in prepare(). Generic types are overridden by schemas if both provided.
fix
Either use schemas for full type safety, or rely on generic types without schemas. Mixing may cause type mismatches.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module '#imports' or its corresponding type declarations.
Nuxt auto-imports not set up properly. tsconfig.json missing path alias.
fix
Ensure tsconfig.json includes: "compilerOptions": { "paths": { "#imports": ["./node_modules/nuxt-strict-fetch/dist/runtime/composables/index.mjs"] } } or use nuxt prepare.
TypeError: StrictFetch.prepare is not a function
Importing incorrectly from the package.
fix
Do not import from 'nuxt-strict-fetch'. Use auto-import: remove the import statement entirely.
No overload matches this call. Overload 1 of 2, 'prepare<R, B, P, Q>(...)': Argument of type '{ url: string; schemas: { response: ZodArray<...>; }; }' is not assignable to parameter of type 'MethodOptions<...>'. Property 'schemas' does not exist on type 'MethodOptions<...>'.
Using schemas without setting validator in nuxt.config.
fix
Add 'validator: 'zod'' (or 'yup') to strictFetch options in nuxt.config.ts.
Upgrade
Version history
1.4.0latest on npm
Audit
Dependencies
yupoptionalRequired for schema validation when validator is set to 'yup'
zodoptionalRequired for schema validation when validator is set to 'zod'
Agent activity
4 hits · last 30 days
node
4
Resources