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

restproxy

v1.1.0

Published

Model your REST APIs as an object's properties/functions with no boilerplate.

Downloads

7

Readme

RestProxy

Model your REST APIs as an object's properties/functions with no boilerplate.

Installation

npm install restproxy

Getting Started

Let's say you have a JSON REST API at api.example.com and you have a couple endpoints such as:

  • GET /users to list users
  • GET /user/:id to fetch a user
  • POST /user:id to update a user

It's as simple as this.

const restproxy = require('restproxy');

const api = restproxy('api.example.com');
// or const api = new restproxy('api.example.com');

// GET /users
api
  .users
  .get();

// GET /user/1234
api
  .user(1234)
  .get();

// POST /user/1234 with some JSON
api
  .user(1234)
  .post({ name: 'John Smith' });

No boilerplate. No need to describe each endpoint and resource with classes or functions or objects. If your API changes, you simply change your RestProxy calls to match.

Interfaces

RestProxy supports callbacks and promises.

Promises

api
  .users
  .put({ name: 'John Smith' })
  .then((data) => {
    // ...
  });

Callbacks

api
  .users
  .put({ name: 'John Smith' }, (err, data) => {

  });

Aliases

To make code easier to understand, we bundle a few default aliases like so.

let user = { name: 'John Smith' };

// GET
api.users.get();
api.users.fetch();

// POST
api.user(1234).post(user);
api.user(1234).update(user);
api.user(1234).modify(user);

// PUT
api.users.put(user);
api.users.create(user);
api.users.add(user);

// DELETE
api.user(1234).delete();
api.user(1234).remove();

These are built-in by default but can be overridden or removed completely using the RestProxy instance's options object.

const options = {
  // Remove all aliases
  methodAliases: {} ,

  // Custom aliases
  methodAliases: {
    'explode': 'delete'
  }
}

const api = restproxy('api.example.com', options);

Headers

You can easily set default headers on your RestProxy object as well as append headers for individual requests.

const options = {
  headers: {
    // Unnecessary, this one is included by default
    'Content-Type': 'application/json',
    // But you can add other global headers as well
    'X-Token': 'my-token'
  }
};

const api = restproxy('api.example.com', options);

api
  .users
  .get(); // Content-Type + X-Token headers

// You can also specify headers on individual requests
api
  .header('Accept', 'text/html')
  .users
  .get();

// Subsequent requests on the same API object will retain global headers
// but NOT headers on previous requests, avoiding unintended side-effects.
//
// i.e. No 'Accept: text/html' header
api
  .users
  .get();

HTTP library

RestProxy uses superagent to perform requests. I'm looking into possibly supporting other libraries as well, PRs are welcome.