Skip to content
TypeWire

@tahanabavi/typefetch

v2.0.0

A strongly typed TypeScript HTTP client powered by Zod contracts, middleware, retries, mock data, contract testing, CLI tooling, and report generation.

Middleware System

TypeFetch supports middleware for logging, authentication, caching, retries, encryption, and custom request behavior.

ts
client.use(async (ctx, next) => {
console.log('Request:', ctx.url)

const response = await next()

console.log('Response:', response.status)

return response
})

Middlewares run in registration order before the request, then unwind in reverse order after the response.

ts
client.use(firstMiddleware)
client.use(secondMiddleware)

Execution flow:

txt
firstMiddleware before
secondMiddleware before
fetch
secondMiddleware after
firstMiddleware after

Built-in Middlewares

Depending on how you export your middlewares, they can be registered directly or as factories.

Direct middleware example:

ts
client.use(loggingMiddleware, {
debug: true,
logRequest: true,
logResponse: true,
})

Factory middleware example:

ts
client.use(cacheMiddleware({ ttl: 60_000 }))
client.use(retryMiddleware({ maxRetries: 3, delay: 300 }))

Retry Engine

TypeFetch includes a built-in retry engine on the client.

ts
client.setRetryConfig({
maxRetries: 3,
backoff: 'exponential',
retryCondition: (error, attempt) => {
return error.status !== undefined && error.status >= 500
},
})

Supported backoff strategies:

StrategyBehavior
fixedSame delay each retry
linearDelay increases linearly
exponentialDelay doubles after each retry

Example:

ts
client.setRetryConfig({
maxRetries: 3,
backoff: 'fixed',
})

Timeout and Abort Support

Each request can receive per-call options.

ts
await api.user.getUser(
{
path: { id: '123' },
},
{
timeout: 5000,
}
)

You can also pass an external AbortSignal.

ts
const controller = new AbortController()

await api.user.getUser(
{
path: { id: '123' },
},
{
signal: controller.signal,
}
)

controller.abort()