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

shred

v1.1.0-alpha-10

Published

A resource-oriented JavaScript HTTP client.

Downloads

17,514

Readme

Overview

Shred is an HTTP client that wraps HTTP interfaces so you can easily create CoffeeScript or JavaScript clients.

Define your client:

github = resource "https://api.github.com/",
  repo: (resource) ->
    resource "repos/{owner}/{repo}/",
      issues: (resource) ->
        resource "issues",
          create:
            method: "post"
            headers:
              accept: "application/vnd.github.v3.raw+json"
            expect: 201
          list:
            method: "get"
            headers:
              accept: "application/vnd.github.v3.raw+json"
            expect: 200

And then use it:

try
  {data} = yield github
    .repo {owner, repo}
    .issues
    .list()
  console.log number, title for {number, title} in (yield data)
catch error
  console.error error

Installation

npm install shred

Status

Shred 1.0.x is a complete reboot of the project. The goal is the same, but the interface has changed drastically. Shred 1.0.x is not backwards compatible with earlier versions.

Shred is presently alpha quality code, as designated by the version suffix.

Description

HTTP is a rich protocol, but the low-level details of setting headers and checking response codes muck up our code. So we either ignore these nuances or write wrapper functions to hide the details.

Shred makes it easy to declaratively create API wrapper functions. Shred also features support for URL templates, response compression, authorization, and streaming responses. And more features are coming soon!

API

  • resource: Define a resource. Takes a URL, path, or URL template, and an interface declaration.

Interface Definitions

Interface definitions are an object whose properties define subresources or request methods on the resource being defined.

If a property contains an object, it will be defined as a request method described the object.

If it contains a function, the property will define a subresource for which the function is the initializer.

Request Method Descriptions

Request method descriptions can include three properties:

  • method: The HTTP method used to make the request, ex: GET, PUT.

  • headers: An object describing the HTTP headers to send with the request, ex: content-type, accept.

  • expect: The expected HTTP status code, ex: 200. Other status codes will be treated as errors.

Subresource Initializer Functions

The signature for resource initializers is:

(resource) ->

The resource function passed into the initializer functions works just as the resource exported by Shred. You can use it to define the subresource.

Typically, you'll want to use paths or template URLs to define subresources, which will be concatenated with the parent resources' URLs.

Subresources defined using URL templates will be functions, allowing you to pass in an object whose key-value pairs will instantiate the template.

Shred Responses

When you invoke a Shred request functions, you get back a response context. This contains both the HTTP response object and a data object. The data object is a promise that resolves when the response body is finished streaming. If the content-type appears to be JSON-based, Shred will parse the response body as JSON and return an object, otherwise, it will return a string.

Explicit Invocation

You can explicit invoke Shred request functions using the invoke command. This is particularly useful when using helper functions defined on the request function, such as authorize.

Authorization With Shred

You can use the authorization method defined on request functions to pass authorization information into the request. The authorization function takes a object describing the authorization scheme. Currently, basic and bearer are supported.

Example

try
  yield github
    .repo {owner, repo}
    .issues
    .create
    .authorize basic: {username: token, password: ""}
    .invoke {title, body}