All Projects → serviejs → Popsicle

serviejs / Popsicle

Licence: mit
Simple HTTP requests for node and the browser

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Popsicle

miniprogram-network
Redefine the Network API of Wechat MiniProgram (小程序网络库)
Stars: ✭ 93 (-60.92%)
Mutual labels:  promise, request
Create Request
Apply interceptors to `fetch` and create a custom request function.
Stars: ✭ 34 (-85.71%)
Mutual labels:  promise, request
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-93.7%)
Mutual labels:  promise, request
Wx Promise Request
解决微信小程序 wx.request 请求的并发数限制、不支持异步问题
Stars: ✭ 226 (-5.04%)
Mutual labels:  promise, request
Simple Fs
Handles files on indexeddb like you would do in node.js (promise)
Stars: ✭ 111 (-53.36%)
Mutual labels:  promise, browser
Await Of
await wrapper for easier errors handling without try-catch
Stars: ✭ 240 (+0.84%)
Mutual labels:  promise, browser
Ky Universal
Use Ky in both Node.js and browsers
Stars: ✭ 421 (+76.89%)
Mutual labels:  request, browser
Evt
💧EventEmitter's typesafe replacement
Stars: ✭ 305 (+28.15%)
Mutual labels:  promise, browser
Mfetch
mfetch will provide you with a strong ability to request resource management
Stars: ✭ 90 (-62.18%)
Mutual labels:  promise, request
Pquic
The PQUIC implementation, a framework that enables QUIC clients and servers to dynamically exchange protocol plugins that extend the protocol on a per-connection basis
Stars: ✭ 63 (-73.53%)
Mutual labels:  plugins, transport
Ws Wrapper
Lightweight WebSocket lib with socket.io-like event handling, requests, and channels
Stars: ✭ 58 (-75.63%)
Mutual labels:  promise, browser
Browser Base
Modern and feature-rich web browser base based on Electron
Stars: ✭ 2,417 (+915.55%)
Mutual labels:  plugins, browser
Wretch
A tiny wrapper built around fetch with an intuitive syntax. 🍬
Stars: ✭ 2,285 (+860.08%)
Mutual labels:  promise, request
Luch Request
luch-request 是一个基于Promise 开发的uni-app跨平台、项目级别的请求库,它有更小的体积,易用的api,方便简单的自定义能力。
Stars: ✭ 219 (-7.98%)
Mutual labels:  promise, request
Massive Js
A data mapper for Node.js and PostgreSQL.
Stars: ✭ 2,521 (+959.24%)
Mutual labels:  promise
Kuriimu
A general purpose game translation toolkit for authors of fan translations.
Stars: ✭ 226 (-5.04%)
Mutual labels:  plugins
Griddle
Simple Grid Component written in React
Stars: ✭ 2,494 (+947.9%)
Mutual labels:  plugins
Layui Excel
简单快捷的导出插件,导出仅需一句话
Stars: ✭ 239 (+0.42%)
Mutual labels:  plugins
Use Ssr
☯️ React hook to determine if you are on the server, browser, or react native
Stars: ✭ 230 (-3.36%)
Mutual labels:  browser
Android Upload Service
Easily upload files (Multipart/Binary/FTP out of the box) in the background with progress notification. Support for persistent upload requests, customizations and custom plugins.
Stars: ✭ 2,593 (+989.5%)
Mutual labels:  plugins

Popsicle

NPM version NPM downloads Build status Test coverage Bundle size

Advanced HTTP requests in node.js and browsers, using Servie.

Installation

npm install popsicle --save

Usage

import { fetch } from "popsicle";

const res = await fetch("http://example.com");
const data = await res.text();

Popsicle is a universal package, meaning node.js and browsers are supported without any configuration. This means the primary endpoint requires some dom types in TypeScript. When in a node.js or browser only environments prefer importing popsicle/dist/{node,browser} instead.

Popsicle re-exports Request, Response, Headers and AbortController from servie. The fetch function accepts the same arguments as Request and returns a promise that resolves to Response. You can use the Signal event emitter (from AbortController#signal) to listen to request life cycle events.

Browser

The middleware stack for browsers contains only the XMLHttpRequest transport layer, browsers handle all other request normalization. This means a smaller and faster package for browsers.

Node.js

The middleware stack for node.js includes normalization to act similar to browsers:

Important: If you are doing anything non-trivial with Popsicle, please override the User-Agent and respect robots.txt.

Recipes

Aborting a Request

import { fetch, AbortController } from "popsicle";

const controller = new AbortController();

setTimeout(() => controller.abort(), 500);

const res = fetch("http://example.com", {
  signal: controller.signal,
});

Errors

Transports can return an error. The built-in codes are documented below:

  • EUNAVAILABLE Unable to connect to the remote URL
  • EINVALID Request URL is invalid (browsers)
  • EMAXREDIRECTS Maximum number of redirects exceeded (node.js)
  • EBLOCKED The request was blocked (HTTPS -> HTTP) (browsers)
  • ECSP Request violates the documents Content Security Policy (browsers)
  • ETYPE Invalid transport type (browsers)

Customization

Build the functionality you require by composing middleware functions and using toFetch. See src/node.ts for an example.

Plugins

Creating Plugins

See Throwback for more information:

type Plugin = (
  req: Request,
  next: () => Promise<Response>
) => Promise<Response>;

TypeScript

This project is written using TypeScript and publishes the types to NPM alongside the package.

Related Projects

  • Superagent - HTTP requests for node and browsers
  • Fetch - Browser polyfill for promise-based HTTP requests
  • Axios - HTTP request API based on Angular's $http service

License

MIT

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].