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

restful-js

v0.7.1

Published

Simple and generic JavaScript class to handle REST API calls

Downloads

19

Readme

Restful JS

Build Status npm version

A simple and generic way to handle RESTful API calls in your JavaScript application. There are many libraries and utilities classes which provide RESTful implementation in JavaScript to handle API calls to server. Unfortunately, those other solutions, as good as they are, not always suits for everyone. Sometimes, the willing to do everything, just makes it more complex and harder to use. This restful-js solution, keeps stuff the most simple there is, and exposes a nice and easy interface for use in your app. In case of customized logic, there are some hook methods you can override

Install

npm install restful-js --save

How to use

fetch(url, options) or get(url, options) Sends HTTP GET request with the following parameters: url: String - the URL resource. options: Object - All of the ajax options you would like to provide.

post(url, data, options) Sends HTTP POST request with the following parameters: url: String - the URL resource. data: Any - The data you would like to send to the server. options: Object - All of the ajax options you would like to provide.

put(url, data, options) Sends HTTP PUT request with the following parameters: url: String - the URL resource. data: Any - The data you would like to send to the server. options: Object - All of the ajax options you would like to provide.

destroy(url, options) Sends HTTP DELETE request with the following parameters: url: String - the URL resource. options: Object - All of the ajax options you would like to provide.

Different ways to use

As a Singelton

By default, an instance of the restful-js object is exported and ready to use. For example:

import RestApi from 'restful-js';
// later on...
RestApi.fetch('some/url/resource').then(response => { /* handle response */ });
As a Class

You may also get a class and instantiate it yourself and manage it's life-cycle. For example:

import {RestfulAPI} from 'restful-js';
// later on...
let restfulAPI = new RestfulAPI();
restfulAPI.fetch(`${baseUrl}/users`).then(response => { /* handle response */ });
Add event handlers

You may instantiate a class and give different handlers for XHR events. For example:

import {RestfulAPI} from 'restful-js';
// later on...
let restfulAPI = new RestfulAPI({
	errorResponseHandler: (xhr, textStatus, errorThrown) => {...}
	ajaxStartHandler: () => {...},
	ajaxStopHandler: () => {...}
});
Inherit and add you own customization

You can easily customize some behavioural stuff by inheriting from the exported RestfulAPI and add your own logic. For example:

import {RestfulAPI} from 'restful-js';

class MyRestfulAPI extends RestfulAPI {
    applySecurity(options, data){
        // add some security logic
    }
}
Get a jQuery reference

You may get a jQuery reference for your own usage and event handling For example:

import {RestfulAPI} from 'restful-js';

const restfulAPI = new RestAPIUtil();

// jQuery binary transport to download files through XHR
restfulAPI.$.ajaxTransport('+binary', () => {...})

Having some trouble? Have an issue?

For bugs and issues, please use the issues page.

Road map

  • Add support for more HTTP methods
  • Remove the need of jQuery.ajax and use the global fetch way! (waiting for full browser support)

Contribute

Sure! just fork this repository and join in!