Registry / testing / ts-fetch

ts-fetch

JSON →
library1.1.4jsnpmunverified

A lightweight, type-safe wrapper around the native fetch API that uses TypeScript generics to enforce request and response types. Version 1.1.4 is the latest stable release. It provides opinionated defaults (JSON encoding/decoding, status code validation, timeouts) while allowing full customization. Compared to alternatives like axios or ky, ts-fetch focuses on simplicity and type safety without additional dependencies, returning a discriminated union for success/error/network-error states. The package ships with TypeScript definitions and has a small footprint, suitable for projects wanting typed HTTP requests without heavy abstraction.

npm install ts-fetch
INSTALL
IMPORT
SIG · TS-FETCH
T
ts-fetch
testingjavascriptv1.1.4
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.

request
import { request } from 'ts-fetch'
import request from 'ts-fetch'
Named import from the package's main export; no default export exists.
RequestParams
import type { RequestParams } from 'ts-fetch'
import { RequestParams } from 'ts-fetch'
RequestParams is a TypeScript type; use `import type` for type-only imports.
ResponseData
const { request } = require('ts-fetch')
const tsFetch = require('ts-fetch')
CJS require returns an object with the `request` property; dot-notation is required.

Shows a GET request with typed success and error responses, including custom headers and handling all possible response statuses.

import { request } from 'ts-fetch'; async function getUser(): Promise<void> { const response = await request<{ id: number; name: string }, { error: string }>({ url: 'https://api.example.com/user', method: 'GET', extraHeaders: [{ key: 'Authorization', value: 'Bearer ' + (process.env.API_KEY ?? '') }], }); if (response.status === 'OK') { const user = response.data; console.log(user.name); } else if (response.status === 'NETWORK_ERROR') { console.error('Network error occurred'); } else { console.error(response.data.error); } } getUser();
Debug
Known issues
gotchaThe 'request' function will attempt to parse the response as JSON by default (jsonResponse: true). If the endpoint returns a non-JSON response, it will result in an error.
fix
Set `jsonResponse: false` in the request params if the response is not JSON.
affects: >=1.0.0
gotchaThe 'timeout' default is 12 seconds. Requests exceeding this timeout will result in a NETWORK_ERROR status.
fix
Adjust `timeout` in the request params to a higher value if needed.
affects: >=1.0.0
gotchaStatus codes outside the range 200-299 (or specified validStatusCodes) will be treated as errors, not success.
fix
Use `validStatusCodes` or adjust `validStatusCodeStart`/`validStatusCodeEnd` to include desired codes.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'ts-fetch'
Package not installed or incorrect import path
fix
Run `npm install ts-fetch` and use `import { request } from 'ts-fetch'`
Type 'ResponseData<unknown, unknown>' has no call signatures
Trying to use the default import instead of named import
fix
Use `import { request } from 'ts-fetch'` instead of `import request from 'ts-fetch'`
Property 'data' does not exist on type 'ResponseData<...>'
Accessing response.data without checking status first
fix
Check `if (response.status === 'OK')` before accessing response.data
Upgrade
Version history
1.1.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
14
Resources
ts-fetch — npm install ts-fetch · libregistry