Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

openapi-fetch

Package Overview
Dependencies
Maintainers
1
Versions
67
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-fetch

Fast, type-safe fetch client for your OpenAPI schema. Only 5 kb (min). Works with React, Vue, Svelte, or vanilla JS.

  • 0.10.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
377K
increased by11.07%
Maintainers
1
Weekly downloads
 
Created

What is openapi-fetch?

The openapi-fetch npm package is a tool designed to simplify the process of making HTTP requests to APIs that are defined using the OpenAPI specification. It provides a type-safe way to interact with APIs, ensuring that the requests and responses conform to the defined API schema.

What are openapi-fetch's main functionalities?

Type-safe API requests

This feature allows you to make type-safe API requests based on the OpenAPI definition. The `createClient` function initializes the client with the API definition, and you can then use methods like `get` to make requests. The types are inferred from the OpenAPI schema, ensuring that your requests and responses are type-checked.

const { createClient } = require('openapi-fetch');
const api = createClient({ definition: 'https://api.example.com/openapi.json' });

async function fetchUser() {
  const response = await api.get('/users/{id}', { params: { id: 1 } });
  console.log(response.data);
}

fetchUser();

Automatic request validation

This feature ensures that the requests you make are validated against the OpenAPI schema. If the request does not conform to the schema, an error will be thrown. This helps in catching errors early in the development process.

const { createClient } = require('openapi-fetch');
const api = createClient({ definition: 'https://api.example.com/openapi.json' });

async function createUser() {
  const response = await api.post('/users', { body: { name: 'John Doe', email: 'john.doe@example.com' } });
  console.log(response.data);
}

createUser();

Response validation

This feature validates the responses from the API against the OpenAPI schema. It ensures that the data you receive is in the expected format, reducing the chances of runtime errors due to unexpected data structures.

const { createClient } = require('openapi-fetch');
const api = createClient({ definition: 'https://api.example.com/openapi.json' });

async function fetchUser() {
  const response = await api.get('/users/{id}', { params: { id: 1 } });
  if (response.status === 200) {
    console.log('User data:', response.data);
  } else {
    console.error('Failed to fetch user:', response.error);
  }
}

fetchUser();

Other packages similar to openapi-fetch

Keywords

FAQs

Package last updated on 20 Jun 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc