Git Product home page Git Product logo

request-ajax's Introduction

request-ajax npm version Build Status codecov.io

HTTP request client for APIs, based on request

Install

$ npm install --save request-ajax

Usage

Below is a example of usage.

var express = require('express');
var ajax = require('request-ajax');

var app = express();

app.get('/', function(req, res, next){
  ajax({
    // only JSON API is supported
    url: 'http://my-api.com/users.json'
    // your API auth
    clientId: process.env.CLIENT_ID,
    clientSecret: process.env.CLIENT_SECRET,
    // callbacks
    error: function(apiRequestError, apiResponse, statusCode) {
      next(apiRequestError || new Error('API error: ' + apiResponse.body));
    },
    success: function(data, apiResponse, statusCode) {
      var userCount = data.length;
      res.send('user count: ' + userCount);
    },
    complete: function(apiRequestError, isApiSuccess, apiResponse) {
      // ...
    }
  });
});

Options

url, required

API url, https will be removed from the url

prefilterUrl

default undefined

if function: this function will be called to let you replace the url with something different. For example: replacing https:// with http:// or using a local DNS/IP.

ajax({
  url: 'https://my-api.com/users.json',
  prefilterUrl: function(url) {
    return url && url.replace(new RegExp('^https://'), 'http://');
  }
});

method

default 'GET'

HTTP method to use for the request e.g. 'POST'

type

same as method

headers

default {}, custom headers

headers: {
  'X-FOO': 'BAR'
}

locale

default undefined, sets Accept-Language header

accessToken

default undefined, sets Bearer

requestOptions.auth = {
  bearer: options.accessToken
};

more about request auth option

apiClientId, origin

default undefined, sets Authorization header

requestOptions.headers.Authorization = 'Basic ' + new Buffer(options.apiClientId + ':').toString('base64');
requestOptions.headers.Origin = options.origin;

clientId, clientSecret

default undefined, sets Bearer

requestOptions.auth = {
  user: options.clientId,
  pass: options.clientSecret
};

more about request auth user/pass option

auth note:

accessToken wins over apiClientId, origin wins over clientId, clientSecret

timeout

default undefined, proxy to the request timeout option

Callbacks

success(json, apiResponse, statusCode)

error(moduleError, [apiResponse, statusCode])

complete(moduleError, isApiSuccess, [apiResponse, statusCode])


Backbone tests were copied form artsy/backbone-super-sync

request-ajax's People

Watchers

James Cloos avatar Ahmed Hassanein avatar

request-ajax's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.