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

@subiz/ajax

v3.0.18

Published

[![npm][npm]][npm-url] [![deps][deps]][deps-url] [![size][size]][size-url]

Downloads

209

Readme

npm deps size

@subiz/[email protected]

  • exception-free
  • simple
  • tiny (2K Gzipped)
  • zero dependencies
  • immutable

fetch api wrapper

Work in progress

  1. docs
  2. add timeout options

Usage

Simple

const ajax = require('@subiz/ajax')

// try to making GET https://httpstat.us/200

let res = await ajax.get("https://httpstat.us/200")
console.log(req.body, res.code, res.error) // "200, OK" 200 undefined

Derived from old object

var req = ajax.setBaseUrl("https://httpstat.us")
res = await req.post("200")
// POST https://httpstat.us/200

References

Request object

Request objects represences HTTP request.

Request object are immutable, which mean, you cannot change its current state or data. Immutiblity makes object behavious more predictable, reducing bug.

The object's state is initialize once when creating the object. You can create request object by either using Ajax object or making a derived object from old one.

Available methods:

addQuery(key, value)

create a new derived object by appending new (key, value) pair into old request query

removeQuery(key)

create a new derived object by removing a (key, value) pair from old request query

setQuery(query)

create a new derived object by replacing old query with new one.

examples:

req = req.setQuery({a:"xin chao", b: 6})

beforeHook(promise)

create a new derived object by registering (appending) a before hook

clearHooks

create a new derived object by removing all before hooks and after hooks

afterHook(promise)

create a new derived object by registering (appending) a hook

setBaseUrl(url)

create a new derived object by changing old request base url. New derived request with relative url will be append to this base url.

setHeader(header)

create a new derived object by merging old header with new header

setMeta(key, value)

attach hidden metadata to request, those key-value will not be sent to the server, designed to keep state in hooks

examples:

req = req.setHeader({"x-real-ip": "193.155.45.3"})

get(url, data, cb)

post(url, data, cb)

put(url, data, cb)

del(url, data, cb)

head(url, data, cb)

patch(url, data, cb)

setBaseUrl

setMethod

contentTypeJson

contentTypeForm

setContentType

setParser

send

setMeta

Ajax object (singleton)

Ajax object is lets you create request object. Available requests:

  • get(url): // create new GET request to address
  • post(url): // create new POST request to address
  • put(url): // create new PUT request to address
  • del(url): // create new DELETE request to address
  • head(url): // create new HEAD request to address
  • patch(url): // create new PATCH request to address

Before hook

before hooks let you modify the request right before sending it. You register a before hook by calling beforeHook. beforeHook function return a promise and take in a reference to the request object as parameter.

for examples: to add query ?a=5 and ?b=6 right before sending the request

let req = ajax.setBaseUrl("https://google.com")

req = req.beforeHook(async param => {
  param.request = param.request.addQuery('a', 5)
}).beforeHook(async param => {
  param.request = param.request.addQuery('b', 6)
})

await req.get() // https://google.com?a=5&b=6

before hook is called one by one in registered order, which hook registered first will be called first.

With after hook

const apireq = ajax
  .setBaseUrl("https://appv4.subiz.com/4.0/")
  .afterHook(param => {
		if (param.code !== 500) return
		var retry = param.request.meta.retry || 0
		if (retry === 3) return
		var req = param.request.setMeta('retry', retry + 1) // increase number of attempt
		// continue retry
		return req.get().then(out => {
			var [code, body, err] = out
			param.code = code
			param.body = body
			param.err = err
		})
	})
})

let [code, body, err] = await apireq.get("me")