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

ky-universal

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ky-universal

Use Ky in both Node.js and browsers

  • 0.12.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
194K
decreased by-1.44%
Maintainers
1
Weekly downloads
 
Created

What is ky-universal?

The ky-universal package is a tiny HTTP client based on the Fetch API, designed to work in both Node.js and browser environments. It provides a simple and modern API for making HTTP requests, handling JSON, and dealing with common HTTP tasks.

What are ky-universal's main functionalities?

Simple GET Request

This feature allows you to make a simple GET request to fetch data from a URL. The response is automatically parsed as JSON.

const ky = require('ky-universal');

(async () => {
  const json = await ky('https://jsonplaceholder.typicode.com/todos/1').json();
  console.log(json);
})();

POST Request with JSON Body

This feature allows you to make a POST request with a JSON body. The response is also parsed as JSON.

const ky = require('ky-universal');

(async () => {
  const json = await ky.post('https://jsonplaceholder.typicode.com/posts', {
    json: {
      title: 'foo',
      body: 'bar',
      userId: 1
    }
  }).json();
  console.log(json);
})();

Handling Errors

This feature demonstrates how to handle errors when making requests. If the request fails, the error is caught and logged.

const ky = require('ky-universal');

(async () => {
  try {
    const json = await ky('https://jsonplaceholder.typicode.com/invalid-endpoint').json();
    console.log(json);
  } catch (error) {
    console.log('Error:', error);
  }
})();

Retry Mechanism

This feature shows how to use the retry mechanism. If the request fails, it will automatically retry up to the specified number of times.

const ky = require('ky-universal');

(async () => {
  const json = await ky('https://jsonplaceholder.typicode.com/todos/1', {
    retry: 3
  }).json();
  console.log(json);
})();

Other packages similar to ky-universal

Keywords

FAQs

Package last updated on 28 Jul 2023

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