All Projects → sindresorhus → Ky Universal

sindresorhus / Ky Universal

Licence: mit
Use Ky in both Node.js and browsers

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Ky Universal

Use Http
🐶 React hook for making isomorphic http requests
Stars: ✭ 2,066 (+390.74%)
Mutual labels:  isomorphic, fetch, request, ssr
Ky
🌳 Tiny & elegant JavaScript HTTP client based on the browser Fetch API
Stars: ✭ 7,047 (+1573.87%)
Mutual labels:  http-client, fetch, request, npm-package
Create Request
Apply interceptors to `fetch` and create a custom request function.
Stars: ✭ 34 (-91.92%)
Mutual labels:  http-client, fetch, request
request-extra
⚡️ Extremely stable HTTP request module built on top of libcurl with retries, timeouts and callback API
Stars: ✭ 14 (-96.67%)
Mutual labels:  npm-package, http-client, request
electron-request
Zero-dependency, Lightweight HTTP request client for Electron or Node.js
Stars: ✭ 45 (-89.31%)
Mutual labels:  fetch, http-client, request
Wretch
A tiny wrapper built around fetch with an intuitive syntax. 🍬
Stars: ✭ 2,285 (+442.76%)
Mutual labels:  http-client, fetch, request
Crypto Hash
Tiny hashing module that uses the native crypto API in Node.js and the browser
Stars: ✭ 501 (+19%)
Mutual labels:  isomorphic, npm-package, browser
Cross Fetch
Universal WHATWG Fetch API for Node, Browsers and React Native.
Stars: ✭ 1,063 (+152.49%)
Mutual labels:  isomorphic, http-client, fetch
Use Ssr
☯️ React hook to determine if you are on the server, browser, or react native
Stars: ✭ 230 (-45.37%)
Mutual labels:  isomorphic, browser, ssr
Gretchen
Making fetch happen in TypeScript.
Stars: ✭ 301 (-28.5%)
Mutual labels:  http-client, fetch, request
movies
Real world isomorphic application for movies search, based on Webpack 5 / Express / React 17 + Redux-Saga / Bootstrap 4.6 + CSS Modules / i18next / SSR
Stars: ✭ 20 (-95.25%)
Mutual labels:  isomorphic, ssr
axios-for-observable
A RxJS wrapper for axios, same api as axios absolutely
Stars: ✭ 13 (-96.91%)
Mutual labels:  http-client, request
Trae
📮 Minimalistic Fetch based HTTP client
Stars: ✭ 257 (-38.95%)
Mutual labels:  fetch, request
http4s-dom
http4s, in a browser near you
Stars: ✭ 13 (-96.91%)
Mutual labels:  fetch, http-client
go-axios
HTTP Request package for golang.
Stars: ✭ 29 (-93.11%)
Mutual labels:  http-client, request
Go Starter Kit
[abandoned] Golang isomorphic react/hot reloadable/redux/css-modules/SSR starter kit
Stars: ✭ 2,855 (+578.15%)
Mutual labels:  isomorphic, browser
Angular Ssr
Angular 4+ server-side rendering solution compatible with @angular/material, jQuery, and other libraries that touch the DOM (as well as providing a rich feature set!)
Stars: ✭ 283 (-32.78%)
Mutual labels:  isomorphic, ssr
fetchx
Beautiful way to fetch data in React
Stars: ✭ 71 (-83.14%)
Mutual labels:  fetch, http-client
React Head
⛑ SSR-ready Document Head tag management for React 16+
Stars: ✭ 262 (-37.77%)
Mutual labels:  isomorphic, ssr
React Koa2 Ssr
a Isomorphic framework demo for react server side rendering ( react 同构工程项目骨架,基于create-react-app 和 koa2生成器搭建。)
Stars: ✭ 299 (-28.98%)
Mutual labels:  isomorphic, ssr

ky-universal

Use Ky in both Node.js and browsers

Ky is made for browsers, but this package makes it possible to use it in Node.js too, by polyfilling most of the required browser APIs using node-fetch and abort-controller.

This package can be useful for:

  • Isomorphic code
  • Web apps (React, Vue.js, etc.) that use server-side rendering (SSR)
  • Testing browser libraries using a Node.js test runner

Note: Before opening an issue, make sure it's an issue with Ky and not its polyfills. Generally, if something works in the browser, but not in Node.js, it's an issue with node-fetch or abort-controller.

Keep in mind that Ky targets modern browsers when used in the browser. For older browsers, you will need to transpile and use a fetch polyfill.

If you only target Node.js, I would strongly recommend using Got instead.

Install

$ npm install ky ky-universal

Note that you also need to install ky.

Usage

import ky from 'ky-universal';

const parsed = await ky('https://httpbin.org/json').json();

// …

ReadableStream support

For ReadableStream support, also install web-streams-polyfill:

$ npm install web-streams-polyfill

You can then use it normally:

import ky from 'ky-universal';

const {body} = await ky('https://httpbin.org/bytes/16');
const {value} = await body.getReader().read();
const result = new TextDecoder('utf-8').decode(value);

// …

API

The API is exactly the same as the Ky API.

FAQ

How do I use this with a web app (React, Vue.js, etc.) that uses server-side rendering (SSR)?

Use it like you would use Ky:

import ky from 'ky-universal';

const parsed = await ky('https://httpbin.org/json').json();

// …

Webpack will ensure the polyfills are only included and used when the app is rendered on the server-side.

How do I test a browser library that uses Ky in AVA?

Put the following in package.json:

{
	"ava": {
		"require": [
			"ky-universal"
		]
	}
}

The library that uses Ky will now just work in AVA tests.

clone() hangs with a large response in Node - What should I do?

Streams in Node.js have a smaller internal buffer size (16 kB, aka highWaterMark) than browsers (>1 MB, not consistent across browsers). When using Ky, the default highWaterMark is set to 10 MB, so you shouldn't encounter many issues related to that.

However, you can specify a custom highWaterMark if needed:

import ky from 'ky-universal';

const response = await ky('https://example.com', {
	// 20 MB
	highWaterMark: 1000 * 1000 * 20
});

const data = await response.clone().buffer();

Related

  • ky - Tiny and elegant HTTP client based on the browser Fetch API
  • got - Simplified HTTP requests in Node.js
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].