
Security News
CVE Volume Surges Past 48,000 in 2025 as WordPress Plugin Ecosystem Drives Growth
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.
feature-fetch
Advanced tools
Straightforward, typesafe, and feature-based fetch wrapper supporting OpenAPI types
Status: Experimental
feature-fetch is a straightforward, typesafe, and feature-based fetch wrapper supporting OpenAPI types.
fetch, maintaining near-native performancewithRetry(), withOpenApi(), ..openapi-typescript typesfetch, ensuring ease of use in various environmentsCreate a typesafe, straightforward, and lightweight fetch wrapper that seamlessly integrates with OpenAPI schemas using openapi-typescript. It aims to simplify error handling by returning results in a predictable manner with ts-results-es. Additionally, it is designed to be modular & extendable, enabling the creation of straightforward API wrappers, such as for the Google Web Fonts API (see google-webfonts-client). feature-fetch only depends on fetch, making it usable in most sandboxed environments like Figma plugins.
import { createApiFetchClient } from 'feature-fetch';
const fetchClient = createApiFetchClient({
prefixUrl: 'https://api.example.com/v1'
});
// Send request
const response = await fetchClient.get<{ id: string }>('/blogposts/{postId}', {
pathParams: {
postId: '123'
}
});
// Handle response
if (response.isOk()) {
console.log(response.value.data); // Handle successful response
} else {
console.error(response.error.message); // Handle error response or network exception
}
// Or unwrap the response, throwing an exception on error
try {
const data = response.unwrap().data;
console.log(data);
} catch (error) {
console.error(error.message);
}
withApi()Enhance feature-fetch to create a typesafe fetch wrapper. This feature provides common HTTP methods (get, post, put, del) ensuring requests and responses are typed.
Create an API Fetch Client:
Use createApiFetchClient to create a fetch client with a specified base URL.
import { createApiFetchClient } from 'feature-fetch';
const fetchClient = createApiFetchClient({
prefixUrl: 'https://api.example.com/v1'
});
Send Requests: Use the fetch client to send requests, specifying the response type for better type safety.
// Send request
const response = await fetchClient.get<{ id: string }>('/blogposts/{postId}', {
pathParams: {
postId: '123'
}
});
withOpenApi()Enhance feature-fetch with OpenAPI support to create a typesafe fetch wrapper. This feature provides common HTTP methods (get, post, put, del) that are fully typed by leveraging your OpenAPI schema using openapi-typescript.
Generate TypeScript Definitions:
Use openapi-typescript to generate TypeScript definitions from your OpenAPI schema.
npx openapi-typescript ./path/to/my/schema.yaml -o ./path/to/my/schema.d.ts
Create an OpenAPI Fetch Client:
Import the generated paths and use createOpenApiFetchClient() to create a fetch client.
import { createOpenApiFetchClient } from 'feature-fetch';
import { paths } from './openapi-paths';
const fetchClient = createOpenApiFetchClient<paths>({
prefixUrl: 'https://api.example.com/v1'
});
Send Requests: Use the fetch client to send requests, ensuring typesafe parameters and responses.
// Send request
const response = await fetchClient.get('/blogposts/{postId}', {
pathParams: {
postId: '123'
}
});
withGraphQL()Enhance feature-fetch to create a typesafe fetch wrapper specifically for GraphQL requests. This feature allows you to send GraphQL queries and mutations, ensuring requests and responses are typed.
Create a GraphQL Fetch Client:
Use withGraphQL to extend your existing fetch client with GraphQL capabilities.
import { gql, withGraphQL } from 'feature-fetch';
import createFetchClient from './createFetchClient';
const baseFetchClient = createFetchClient({
prefixUrl: 'https://api.example.com/v1/graphql'
});
const graphqlClient = withGraphQL(baseFetchClient);
Define GraphQL Queries:
Use the gql tagged template literal to define your GraphQL queries with syntax highlighting.
const GET_USER = gql`
query GetUser($id: ID!) {
user(id: $id) {
id
name
email
}
}
`;
Send GraphQL Requests: Use the GraphQL-enabled fetch client to send requests, specifying the response type for better type safety.
// Send GraphQL query
const response = await graphqlClient.query<
{ id: number },
{ user: { id: string; name: string; email: string } }
>(GET_USER, {
variables: {
id: '123'
}
});
When handling API error responses (response.isErr()), response can be one of three Error types, each representing a different kind of failure.
NetworkError (extends FetchError)Indicates a failure in network communication, such as loss of connectivity.
if (response.isErr() && response.error instanceof NetworkError) {
console.error('Network error:', response.error.message);
}
RequestError (extends FetchError)Occurs when the server returns a response with a status code indicating an error (e.g., 4xx or 5xx).
if (response.isErr() && response.error instanceof RequestError) {
console.error('Request error:', response.error.message, 'Status:', response.error.status);
}
FetchErrorA general exception type that can encompass other error scenarios not covered by NetworkError or RequestError, for example when the response couldn't be parsed, ..
if (response.isErr() && response.error instanceof FetchError) {
console.error('Service error:', response.error.message);
}
if (response.isErr()) {
const error = response.error;
if (isStatusCode(error, 404)) {
console.error('Not found:', error.data);
}
if (error instanceof NetworkError) {
console.error('Network error:', error.message);
} else if (error instanceof RequestError) {
console.error('Request error:', error.message, 'Status:', error.status);
} else if (error instanceof FetchError) {
console.error('Service error:', error.message);
} else {
console.error('Unexpected error:', error);
}
}
withRetry()Retries each request using an exponential backoff strategy if a network exceptions (NetworkError) or HTTP 429 (Too Many Requests) response occur.
import { createApiFetchClient, withRetry } from 'feature-fetch';
const fetchClient = withRetry(
createApiFetchClient({
prefixUrl: 'https://api.example.com/v1'
}),
{
maxRetries: 3
}
);
maxRetries: Maximum number of retry attemptswithDelay()Delays each request by a specified number of milliseconds before sending it.
import { createApiFetchClient, withDelay } from 'feature-fetch';
const fetchClient = withDelay(
createApiFetchClient({
prefixUrl: 'https://api.example.com/v1'
}),
1000
);
delayInMs: Delay duration in milliseconds@0no-co/graphql.web a dependency if it's not always used?@0no-co/graphql.web is listed as a dependency because it's dynamically imported in the getQueryString() function. If the function isn’t used, Webpack's tree shaking should exclude it from the final bundle. This ensures that only necessary modules are included, keeping your build clean.
FAQs
Straightforward, typesafe, and feature-based fetch wrapper supporting OpenAPI types
The npm package feature-fetch receives a total of 720 weekly downloads. As such, feature-fetch popularity was classified as not popular.
We found that feature-fetch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.