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

browser-resource

v0.5.3

Published

REST client for browsers

Downloads

5

Readme

browser-resource

A simple library for integrating with RESTful API's. It will seemlesly integrate with any RESTful API, as long as it follows the rules of such.

Installation

npm install --save browser-resource

Then require the Resrouce into your project, like so:

const Resource = require('browser-resource').Resource

or by using ES6 destructurizing syntax

const { Resource } = require('browser-resource');

Or if you can use ES6 imports

import { Resource } from 'browser-resource';
// all bellow examples will not be using this sytanx, but you can, if you want.

Configuration

Currently browser-resource supports only two global config options, in order to achive them, get the Config function, like so:

const { Config } = require('browser-resource');
Config(function (config) {
    // config is the object that you need to modify to setup the entire library
    // available options are listed below
});

namespace

  • string that will be prepended to every url before an request has been made

For example:

config.namespace = '/api/v1';

headers

  • headers has to be an array of arrays
  • those headers will be added to every request you send

For example:

config.headers = [['Authorization', 'Bearer super-secret-token']];

API

Instance of Resource has the following methods. Please note, that below methods return a Bluebird promise, for you to easily handle any async requests.

list(params: Object)

  • makes an GET request to the server for fetching a list of resources, the server should respond with an array of objectcs
  • params are optional query parameters that will be added to the request.

create(params: Object)

  • makes an POST request to the server
  • params is an object that will be send in the body of the request

update(params: Object)

  • makes an PATCH request to the server, in order to make PUT request please take a look at insert method
  • params will be sent as the body of the request. The id of the params object should be provided, this will be added to the url of the request. However, it will NOT be sent in the body of the request.

insert(params: Object)

  • makes an PUT request to the server, this can theoretically create an entire resource on the server (along with an id), if it is allowed by the server.
  • the id from params object will be added to the URL, but it will not be removed from the body of the request.

destroy(params: Object)

  • makes an DELETE request to the server.
  • params.id needs to be provided in order to make sure which resource should be deleted. id won't be added to the request body

action(name: String, params: Object, method = 'post')

Somtimes an custom action is defined on a resource. This will send any type of request and any action on any resource. For example: Resource url is /users and we want to send an POST request to email action on the server that sends email to all the users in the system. To do this we simply write:

resource.action('email', { message: 'Hi' }).then(function (res) {
  // handle response
}).catch(ErrorResponse, function (err) {
  console.error(err);
});

upload(name: String, file: Object, params: Object)

  • This is used for file upload, name will be appedned to the resource and create the final endpoint.
  • file should be an object that has file key and under it a File - this file will be uploaded.
  • params is an Object that will be added to the body of the request. Basically providing additonal parameters, if required.

This is an multipart request, so the body will not be a JSON. It will be form params/payload, for example:

const file = new File(); // this won't work since the File needs to args, its merley to show that a file is sent
const resource = new Resource('/users');
resource.upload('avatar', { file }, { filename: 'avatar.png' });

Error Handling

The returned Promise from any of the request methods is a Bluebird Promise. Meaning you can provide which errors you want to catch as a first argument of the catch function. I highly recommend doing so. If you will catch every error in this catch callback you will really have a hard time developing your application. browser-resource thankfuly exposes the Error klass which you can use to provide as the first agrugment

const { ErrorResponse, Resource } = require('browser-resource');

const resource = new Resource('/users');
resource.list().then(function (res) {
  // do what you need to do when a successful response has been received
}).catch(ErrorResponse, fuction (err) {
   // do the error handling
   // the error response is under err.err
});

One thing about the err object - it will keep the err response under the err property of the thrown error, for example: err.err.