Skip to content

nachos/api-tailor

Repository files navigation

api-tailor

A tool to swiftly tailor a fitting api

Linux OSX Windows Coverage Dependencies DevDependencies
Coverage Status

Have a problem? Come chat with us!

Join the chat at https://gitter.im/nachos/api-tailor

Installation

$ [sudo] npm install api-tailor --save

Examples

var tailor = require('api-tailor');

var client = tailor({
    host: 'http://yourserver.com/api',
    resources: {
      customers: {
        all: {
          method: 'GET',
          path: '/'
        },
        add: {
          method: 'POST',
          path: '/'
        },
        byName: {
          method: 'GET',
          path: '/:name' // url parameter
        },
        downloadLog: {
          method: 'GET',
          path: '/log',
          stream: true // response will return as stream
        },
        uploadLog: {
          method: 'POST',
          path: '/log',
          data: 'form' // data given will be treated as form data
        }
      }
    }
  });

client.customers.all()
  .then(function(customers) {
    // customers -> all data from get request to http://yourserver.com/api/customers/all
  });
  
client.customers.add({ name: 'nacho', address: 'nachos home 25, dip mountain, taco-ville' })
  .then(function() {
    // -> post request to http://yourserver.com/api/customers/
  });
  
client.customers.byName({}, { name: 'nacho' })
  .then(function(customer) {
    // customer -> get request to http://yourserver.com/api/customers/nacho
  });
  
client.customers.downloadLog()
  .then(function(stream) {
    stream.pipe(process.stdout); // stream -> get request to http://yourserver.com/api/customers/log
  });
  
client.customers.uploadLog({file: fs.createReadStream('file.txt')})
  .then(function() {
    // -> post request to http://yourserver.com/api/customers/log
  });

Injectors

Use injectors to intercept outgoing or incoming data and manpulate it.

Token Injector Example

  var token;

  client.inject({
    request: function (request) {
      request.headers = request.headers || {};

      if (token) {
        request.headers.Authorization = 'Bearer ' + token;
      }

      return Q.resolve(request);
    }
  });

Run Tests

$ npm test

License

MIT

we never go out of style

About

A tool to swiftly tailor a fitting api

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •