Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

restful-js

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

restful-js

Simple and generic JavaScript class to handle REST API calls

  • 0.7.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-78.57%
Maintainers
1
Weekly downloads
 
Created
Source

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!

Keywords

FAQs

Package last updated on 07 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc