📘️ openapi-typescript-fetch
A typed fetch client for openapi-typescript
Install
npm install openapi-typescript-fetch
Or
yarn add openapi-typescript-fetch
Features
Supports JSON request and responses
Usage
Generate typescript definition from schema
npx openapi-typescript https://petstore.swagger.io/v2/swagger.json --output petstore.ts
Typed fetch client
import 'whatwg-fetch'
import { Fetcher } from 'openapi-typescript-fetch'
import { paths } from './petstore'
const fetcher = Fetcher.for<paths>()
fetcher.configure({
baseUrl: 'https://petstore.swagger.io/v2',
init: {
headers: {
...
},
},
use: [...]
})
const findPetsByStatus = fetcher.path('/pet/findByStatus').method('get').create()
const addPet = fetcher.path('/pet').method('post').create()
const { status, data: pets } = await findPetsByStatus({
status: ['available', 'pending'],
})
console.log(pets[0])
Typed Error Handling
A non-ok fetch response throws a generic ApiError
But an Openapi document can declare a different response type for each status code, or a default error response type
These can be accessed via a discriminated union
on status, as in code snippet below
const findPetsByStatus = fetcher.path('/pet/findByStatus').method('get').create()
const addPet = fetcher.path('/pet').method('post').create()
try {
await findPetsByStatus({ ... })
await addPet({ ... })
} catch(e) {
if (e instanceof addPet.Error) {
const error = e.getActualType()
if (error.status === 400) {
error.data.validationErrors
} else if (error.status === 500) {
error.data.errorMessage
} else {
...
}
}
}
Middleware
Middlewares can be used to pre and post process fetch operations (log api calls, add auth headers etc)
import { Middleware } from 'openapi-typescript-fetch'
const logger: Middleware = async (url, init, next) => {
console.log(`fetching ${url}`)
const response = await next(url, init)
console.log(`fetched ${url}`)
return response
}
fetcher.configure({
baseUrl: 'https://petstore.swagger.io/v2',
init: { ... },
use: [logger],
})
fetcher.use(logger)
Server Side Usage
This library can be used server side with node-fetch
Node CommonJS setup
npm install node-fetch@2
npm install @types/node-fetch@2
import fetch, { Headers, Request, Response } from 'node-fetch'
if (!globalThis.fetch) {
globalThis.fetch = fetch as any
globalThis.Headers = Headers as any
globalThis.Request = Request as any
globalThis.Response = Response as any
}
import './fetch-polyfill'
Utility Types
OpArgType
- Infer argument type of an operationOpReturnType
- Infer return type of an operationOpErrorType
- Infer error type of an operationFetchArgType
- Argument type of a typed fetch operationFetchReturnType
- Return type of a typed fetch operationFetchErrorType
- Error type of a typed fetch operationTypedFetch
- Fetch operation type
import { paths, operations } from './petstore'
type Arg = OpArgType<operations['findPetsByStatus']>
type Ret = OpReturnType<operations['findPetsByStatus']>
type Err = OpErrorType<operations['findPetsByStatus']>
type Arg = OpArgType<paths['/pet/findByStatus']['get']>
type Ret = OpReturnType<paths['/pet/findByStatus']['get']>
type Err = OpErrorType<paths['/pet/findByStatus']['get']>
type FindPetsByStatus = TypedFetch<operations['findPetsByStatus']>
const findPetsByStatus = fetcher.path('/pet/findByStatus').method('get').create()
type Arg = FetchArgType<typeof findPetsByStatus>
type Ret = FetchReturnType<typeof findPetsByStatus>
type Err = FetchErrorType<typeof findPetsByStatus>
Utility Methods
arrayRequestBody
- Helper to merge params when request body is an array
const body = arrayRequestBody([{ item: 1}], { param: 2})
Happy fetching! 👍