Skip to content

OpenAPI Type Helpers ​

For each endpoint with a schema, Nuxt API Party generates a type carrying everything one operation declares. It is named after the endpoint ID and takes a path and a method:

ts
import type { PetStore } from '#nuxt-api-party'

type GetUser = PetStore<'/user/{username}', 'get'>

// Extract any part of the operation
type PathParams = GetUser['path'] // { username: string }
type QueryParams = GetUser['query'] // Query parameters
type RequestBody = GetUser['request'] // Request body type
type Response = GetUser['response'] // Success response
type ErrorResponse = GetUser['responses'][404] // Specific status code

Both parameters are required, and both are checked against the schema. A path the schema does not declare is a compile error, and so is a method the path does not allow – PetStore<'/pet', 'get'> does not compile, because the Petstore declares only post and put there.

Core Type Properties ​

Each property is read off the operation in your schema:

PropertyDescriptionExample
pathPath parameters{ petId: number }
queryQuery parameters{ status?: 'available' | 'pending' | 'sold' }
requestRequest body, for whichever media type the operation declares{ name: string, photoUrls: string[] }
responseBody of the successful response, for whichever 2xx status it declares{ id?: number, name: string }
responsesEvery status code the operation declares, mapped to the body it returns{ 200: Pet, 400: undefined, 404: undefined }
fullPathComplete path string'/pet/{petId}'
methodHTTP method'get'
operationFull OpenAPI operationComplete operation object

Where an operation declares nothing at all for a property, the type says so rather than inventing an empty object: path and query are never, request is undefined, and response is undefined for an operation whose success carries no body.

request and response match the composables: a value annotated with PetStore<Path, Method>['response'] is exactly what $petStore hands back for that call.

Examples ​

A request body the schema marks optional comes back widened with undefined, matching what the composables accept:

ts
type PlaceOrderBody = PetStore<'/store/order', 'post'>['request']
//   ^? Order | URLSearchParams | undefined

Error Handling Types ​

Extract the body a particular status code carries:

ts
// All responses the operation declares
type AllPetResponses = PetStore<'/pet/{petId}', 'get'>['responses']
//   ^? { 200: Pet; 400: undefined; 404: undefined }

// A single status code
type PetNotFound = PetStore<'/pet/{petId}', 'get'>['responses'][404]
//   ^? undefined

Only the codes the operation itself declares are available, so PetStore<'/pet', 'post'>['responses'] offers 200 and 405 and nothing else. A status declared without a response body – which is every error in the Petstore schema – resolves to undefined.

This is the body a status maps to. To type the error a failed request actually throws, see Error Handling.

Schema Discovery ​

Two further types enumerate what the schema declares, for code that walks the API rather than calling one path:

ts
import type { PetStoreApiMethods, PetStoreApiPaths } from '#nuxt-api-party'

// Get all available paths
type AllPaths = PetStoreApiPaths
//   ^? '/pet' | '/pet/{petId}' | '/pet/findByStatus' | /* ... */

// Get all available methods for a specific path
type PetMethods = PetStoreApiMethods<'/pet'>
//   ^? 'post' | 'put'

Schema Model Types ​

Models are also reachable on their own, without going through an operation:

ts
import type { PetStoreModel } from '#nuxt-api-party'

// Extract schema models directly
type Pet = PetStoreModel<'Pet'>
//   ^? { id?: number; name: string; category?: Category; photoUrls: string[]; tags?: Tag[]; status?: 'available' | 'pending' | 'sold' }

type Category = PetStoreModel<'Category'>
//   ^? { id?: number; name?: string }

type User = PetStoreModel<'User'>
//   ^? { id?: number; username?: string; firstName?: string; lastName?: string; email?: string; password?: string; phone?: string; userStatus?: number }

Reach for this where a type belongs to your domain rather than to one operation – a shared component, a form, a helper that calls several.

Released under the MIT License.