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

@stead/request

v1.2.2

Published

[![pipeline status](https://gitlab.com/mecolela/request/badges/master/pipeline.svg)](https://gitlab.com/mecolela/request) > Request Client [Fetch]

Downloads

67

Readme

Request

pipeline status

Request Client [Fetch]

A lightweight request client with an in-built rest query builder

Installation

npm i @stead/request
yarn add @stead/request

Usage

Given the servers responses are encapsulated as follows:

{
  data: "Response goes here..."
}

Setup

import { SteadClient } from '@stead/request'

const client = new SteadClient({
  // [Required]
  host: 'https://api.example.com',

  // [Optional] - for versioned cases
  version : 'v1',

  // [Optional] - String or Function (can be asynchronous) that returns a string 
  token: () => 'your_bearer_token',

  // [Optional] - Global headers carried with every request
  headers: {
    'X-STEAD-SRC': 'REQUEST'
  },

  // [Optional] - Object with the following signature; Functions can also be asynchronous.
  storage: {
    get: localStorage.getItem,
    set: localStorage.setItem,
    delete: localStorage.removeItem,
  }
});

Sample Requests

Get

Gets the id, name and email of 10 active users.

client.$get('users', {
  query: {
    select: ['id', 'name', 'email'],
    limit: 10,
    filter: {
      eq: {
        active: true
      }
    }
  }
});

// https://api.example.com/v1/users?select=id,name,email&page[limit]=10&filter=eq(active,true)

Users list of 10 should look something like this:

[
  {
    "id": "091238749082",
    "name": "Sample Name",
    "email": "[email protected]"
  },

  // ...the other 9...
]

Post

Create a new product item and get back it's id, name and add in a collection join. We could also skip versioning for this request.

client.$post('products', {
  name: 'Denim Short',
  slug: 'denim_short',
  price: 1000,
  collection: 'shorts',
  tags: ['short', 'denim']
},
{
  skipVersion: true,
  query: {
    select: ['id', 'name'],
    include: ['collection']
  }
});

// https://api.example.com/products?select=id,name&include=collection

Response should look something like

{
  "id": "092370498237",
  "name": "Denim Short",
  "collection": {
    "slug": "shorts"
    "name": "Shorts",
    "description": "Cool Shorts"
  }
}

Put, Patch, Delete

Pretty much the same pattern as the above

Creating a file with PUT

client.$put('files', yourFile, {isFile: true, query: {select: ['name', 'fileType']}})

// https://api.example.com/v1/files?select=name,fileType

Running a PATCH

client.$patch('item/092370498234', yourUpdate, {query: {select: ['name']}})

// https://api.example.com/v1/item/092370498234?select=name

Running a DELETE

client.$delete('files/092370498234', null, {query: {select: ['id', 'name']}})

// https://api.example.com/v1/files/092370498234?select=id,name