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

@s524797336/restful-fetch

v1.1.1

Published

Restful library

Downloads

3

Readme

restful-fetch

NPM License Downloads

A RESTful library based on isomorphic-fetch.

Installation

$ npm i restful-fetch

Quick start

import Restful from 'restful-fetch';

const restful = new Restful();

const Foo = restful.model('foo');
Foo.get().then(data => console.log(data));    // GET /foo

Foo.Single = Foo.model(':id');
Foo.Single.fill({id: 1}).get().then(data => console.log(data));   // GET /foo/1

Foo.Bar = Foo.Single.model('bar');
Foo.Bar.fill({id: 1}).get().then(data => console.log(data));      // GET /foo/1/bar

Documents

Overview

Everything is a model, derives from models and derives models.

restful-fetch

Restful

A Restful instance is the root model, which has a few properties to be shared by all its derivatives. The properties are:

  • prehandlers
  • posthandlers
  • errhandlers

The handlers will be discussed more as interceptors.

Parameters

  • options: (Optional) Object

    • root: (Optional) String

      Base address for all requests, default as ''.

    • headers: (Optional) Object

      Default request headers.

    • config: (Optional) Object

      Extra config to be passed to fetch.

    • methods: (Optional) Object

      Custom methods to be bound to models. The object are composed of methodName: methodInfo pairs. methodInfo may have following properties:

      • method

        The HTTP method to be used for requests, default as GET.

      • args

        The argument name list. Names may be url, params, body and headers.

    const restful = new Restful({
      root: '/api',
      headers: {
        'X-From': 'restful-fetch',
      },
      methods: {
        foo: {
          method: 'GET',
          args: ['url', 'params']
        }
      }
    });
    
    // restful.foo(url, params)

Methods

An Restful instance has all methods that a derived model has, except for fill, which should only work in abstract models.

const restful = new Restful(options);
// or
const restful = Restful(options);

Notes

A Restful instance should be a singleton and used all over the project. The options will be handled and can be reached and modified by restful.options.

Model

A Model instance is an object derived from a Restful instance or another model.

A model instance has following properties:

  • prehandlers
  • posthandlers
  • overrides

The handlers will be discussed more as interceptors.

Methods

  • model(...args: []String)

    Derive a submodel.

    A model is always generated by instance.model(...args). Each argument is a part of URL and will be joined with / as the submodel's relative path.

    If a part of URL starts with :, it will be marked as a placeholder. Models with placeholders are abstract models, and can not make requests directly. Abstract models have to be filled with data to get non-abstract models before making requests. This is designed to share properties between different model instances.

  • fill(data: Object)

    Derive a new Model instance with placeholders filled with data. data is an object with keys as the placeholder names and values to be filled with.

  • request(options: Object)

    A low-level request method called by get, post, put, etc. options may have following properties:

    • url: String, default as ''
    • method: String, default as GET
    • headers: Object, default as null
    • params: Object, default as null
    • body: Any, default as null
  • post(url: String, data: Any, params: (Optional) Object)

    POST request.

  • get(url: String, params: (Optional) Object)

    GET request.

  • put(url: String, data: Any, params: (Optional) Object)

    PUT request.

  • patch(url: String, data: Any, params: (Optional) Object)

    PATCH request.

  • delete(url: String, params: (Optional) Object)

    DELETE request.

const Cars = restful.model('cars');

const AbstractCar = restful.model('cars', ':id');
// or
const AbstractCar = Cars.model(':id');

AbstractCar.prehandlers.push(function () {
  console.log('Get a car!');
});

const car1 = AbstractCar.fill({id: 1});
const car2 = AbstractCar.fill({id: 2});

const seats = car1.model('seats');
seats.get().then(data => console.log(data));

Interceptors

Restful and Model instances have a several handler properties, which contains lists of interceptors. Those on Restful instances are global interceptors and those on Model instances are model specific interceptors.

  • prehandlers (present on Restful and Model)

    Process order: Model::prehandlers -> Restful::prehandlers.

    Each prehandler is called with one parameter: the request options. Returned value will be merged into it, so only the changed attributes need to be returned.

  • posthandlers (present on Restful and Model)

    Process order: Restful::posthandlers -> Model::posthandlers.

    Each posthandler is called with two parameters: data, options. data is the current data object and can be modified by returning a new one. options contains all information of the request, including method, url (full url), relative (url relative to current model).

    There are two default posthandlers on a Restful object so that you may either resolve the response data or reject an object with the response object and its data:

    • response => ({response, data})
    • ({response, data}) => response.ok ? data : reject({response, data})
  • errhandlers (present on Restful)

    Each errhandler is called with the error captured. The last errhandler should throw the error to make a rejection to the promise.

  • overrides (present on Model)

    Overrides global handlers on Restful object for current model.

const restful = new Restful();
const model = restful.model('/cars/1');

// Global interceptors will be processed for all requests
restful.prehandlers.push(options => {
  return {
    params: Object.assign({}, options.params, {hello: 'world'}),
  };
});
restful.posthandlers.push(res => {
  console.log('Intercepted:', res);
  return res;
});

// Model interceptors will be processed only for the model itself
// Model prehandlers will execute BEFORE global prehandlers
model.prehandlers.push(options => {
  return {
    headers: {
      'X-From-Model': true,
    },
  };
});
// Model posthandlers will be processed AFTER global posthandlers
model.posthandlers.push(data => {
  return data || 'empty';
});

model.overrides = {
  posthandlers: [],   // disable global posthandlers
};