npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@swan-io/request

v1.0.6

Published

Wrapper for XMLHttpRequest with better data-structures

Downloads

1,346

Readme

@swan-io/request

mit licence npm version bundlephobia

Wrapper for XMLHttpRequest with better data-structures

Installation

$ yarn add @swan-io/request @swan-io/boxed
# --- or ---
$ npm install --save @swan-io/request @swan-io/boxed

Design principles

  • Has a strong contract with data-structures from Boxed (Future, Result & Option)
  • Makes the request easily cancellable with Future API
  • Gives freedom of interpretation for response status
  • Handles onLoadStart & onProgress events
  • Handles timeouts
  • Types the response using the provided responseType

Getting started

import { Request, badStatusToError, emptyToError } from "@swan-io/request";

// Regular case
Request.make({ url: "/api/health" }).onResolve(console.log);
// Result.Ok({status: 200, ok: true, response: Option.Some("{\"ok\":true}")})

// Timeout
Request.make({ url: "/api/health", timeout: 2000 }).onResolve(console.log);
// Result.Error(TimeoutError)

// Network error
Request.make({ url: "/api/health" }).onResolve(console.log);
// Result.Error(NetworkError)

// Custom response type
Request.make({ url: "/api/health", responseType: "json" }).onResolve(
  console.log,
);
// Result.Ok({status: 200, ok: true, response: Option.Some({ok: true})})

// Handle empty response as an error
Request.make({ url: "/api/health" })
  .mapOkToResult(emptyToError)
  .onResolve(console.log);
// Result.Error(EmptyResponseError)

// Handle bad status as an error
Request.make({ url: "/api/health" })
  .mapOkToResult(badStatusToError)
  .onResolve(console.log);
// Result.Error(BadStatusError)

// Cancel request
useEffect(() => {
  const future = Request.make({ url: "/api/health" });
  return () => future.cancel();
}, []);

API

Request.make(config)

config

  • url: string
  • method: GET (default), POST, OPTIONS, PATCH, PUT or DELETE
  • responseType:
    • text: (default) response will be a string
    • arraybuffer: response will be a ArrayBuffer
    • document: response will be Document
    • blob: response will be Blob
    • json: response will be a JSON value
  • body: request body
  • headers: a record containing the headers
  • withCredentials: boolean
  • onLoadStart: event triggered on load start
  • onProgress: event triggered at different times when the payload is being sent
  • timeout: number

Return value

Returns a Future<Result<Response<T>, NetworkError | TimeoutError>>, where Response<T> has the following properties:

  • status: number
  • ok: boolean
  • response: Option<T>
  • xhr: XMLHttpRequest

T is the type associated with the responseType provided in the config object.

emptyToError

Helper to use with mapOkToResult to consider empty response as an error.

badStatusToError

Helper to use with mapOkToResult to consider a status outside of the 200-299 range as an error.

License