Giter Club home page Giter Club logo

cork's Introduction

cork

Build Status

Cork is a minimal layer that sits on top of the request module and adds a few patterns that make working with 3rd party APIs simpler.

Installation

npm install cork

Basic Use

var cork = require('cork');

// First, let's register a new service with the name "buzz" and a few defaults
cork.register('buzz', {
    'method':   'get',
    'headers':  {
        'x-api-key': 'someApiKeyGoesHere'
    },
    'json':     {}
    'timeout':  20000
});

// Next, let's make a request based on that service
cork.request('buzz', {
    'uri':      'https://api.geek.com/some/resource'
}, function (err, body) {
    console.dir(body); // Woo!
});

// Neato! But let's say we want to create another request and override some of the defaults
cork.request('buzz', {
    'method':   'post'
    'uri':      'https://api.geek.com/some/other/resource',
    'json':     {
        'foo': 'bar'
    }
}, function (err, body) {
    console.dir(body); // Huzzah!
});

Base URIs

A base URI can be defined during registration by passing the optional base parameter:

cork.register('dork', {
    base: 'http://api.nerd.com',
});

cork.request('dork', {
    uri: '/yet/another/resource'
}, function (err, body) {
    // Weee! 
});

API Limits / Throttling

When registering a service, Cork accepts an optional throttle parameter which represents a request limit expressed in milliseconds. Any requests that subsequently hit the throttle limit will be queued and processed in FIFO order. For example, let's say that we are working with an API that only accepts 10 requests per second:

cork.register('geek', {
    throttle: 100   // 1 request per 100 milliseconds
});

Testing

npm test

cork's People

Contributors

thisandagain avatar

Watchers

James Cloos avatar Alex Davis avatar

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.