Giter Site home page Giter Site logo

endpoints's Introduction

Endpoints

Build Status

Simple helper library for HTTP service clients. Endpoints works in the browser and in Node, using RequestAdapter to expose a common interface for Request Response objects, and superagent for Node and browser AJAX.

View the Endpoints API Reference.

tl;dr

var Endpoints = require('endpointsjs');

var myEndpoint = Endpoints.create('/some/url/pattern')
  .header('Content-Type', 'application/json')
  .methods(['get', 'post']);

myEndpoint.get()
  .send()          // Returns an Q Promise (Promises/A+)
  .invoke('text')  // You may invoke any method a RequestAdapter implements
  .done(console.log);

Install it

npm install endpointsjs

Usage

Use Endpoints to create an endpoint pattern, then call methods on that pattern and get promises back. No pyramid of doom. Code over configuration.

var Endpoints = require('endpointsjs');

var myEndpoint = Endpoints
  .create('/some/url/pattern')
  .header('Content-Type', 'application/json')
  .methods(['get', 'post']);

var promise = myEndpoint
  .get()
  .send(); // Returns an Q Promise (Promises/A+)

// You can do something like this
promise
.then(function(requestAdapter) {
  return requestAdapter.text();
})
.done(function(text) {
  console.log(text);
});

// Which is equivalent to
promise
.invoke('text')
.done(console.log);

Sending data to the server is also easy

var myOtherEndpoint = Endpoints.create('/some/other/url/pattern')
  .methods(['options', 'post', 'delete']);

myOtherEndpoint.post()
  .data({myData: 123})
  .send()
  ...
  .done();

It is also possible to build a URL by passing arguments

var myOtherEndpoint = Endpoints
  .create('/users/[userId]-[username]')
  .methods('get');

myOtherEndpoint
  .post()
  .param('userId', 123)
  .param('username', 'kahnjw')
  .send() // GETs the URL: /users/123-kahnjw
  ...
  .done();

Development

Bug fixes, new features, doc fixes are welcome and ecouraged. Open your pull early, make sure the code lints without error and the tests pass.

Setup

$ git clone [email protected]:kahnjw/endpoints.git
$ cd endpoints
$ npm install

Lint and test

$ gulp lint
$ gulp browserspec
$ gulp nodespec

Or just use the watch task

$ gulp watch

If new features are added or a bug is fixed, please cover them with new tests.

Footprint

Keep in mind this library is targeting both browser and Node environments. Footprint size should be as small as possible. To help with this there is a footprint task:

$ gulp footprint
[gulp] Using gulpfile ~/Documents/endpoints/gulpfile.js
[gulp] Starting 'footprint'...
[gulp] gulp-size: total 57.35 kB
index.js
└─┬ create.js
  ├─┬ http-method-helper.js
  │ └─┬ http-method.js
  │   ├── ../node_modules/lodash/dist/lodash.js
  │   ├─┬ ../node_modules/qagent/index.js
  │   │ └── ../node_modules/q/q.js
  │   └─┬ ../node_modules/superagent/lib/client.js
  │     ├── ../node_modules/superagent/node_modules/component-emitter/index.js
  │     └── ../node_modules/superagent/node_modules/reduce-component/index.js
  ├─┬ ../node_modules/requestadapter/src/index.js
  │ ├── ../node_modules/requestadapter/src/xhr-adapter.js
  │ └── ../node_modules/requestadapter/src/node-request-adapter.js
  └── ../node_modules/lodash/dist/lodash.js

[gulp] Finished 'footprint' after 1.4 s

This prints out the minified size of the library, and the dependency tree to ensure that no libraries are double included.

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.