localize-api

helpful resource for getting external api's and making them local

I needed a way to have more control over external API's as well as keep the REST API functionality. This module allows you to localize external API's along with a simple cache layer/garbarge collection utilizing nedb, I plan to build in mongodb as well.

  • express api mount/router for external resources
  • 100% coffeescript, hate it or love it
  • negates the whole clientside cors issue for some people by using request, and localizes to a RESTful route
npm install git+https://github.com/dhigginbotham/localize-api --save

You'll also need CoffeeScript, it's easy though: npm install coffeescript -g

This example depends on having express.js and nedb available:

npm install express nedb --save

var express = require('express');
var app = express();
 
var server = require("http").createServer(app);
 
var path = require('path');
var DataStore = require('nedb');
var localize = require('localize-api');
 
app.set('port', 1337);
app.use(express.bodyParser());
app.use(express.methodOverride());
 
// simple example 
var github = new localize();
 
github.mount(app);
 
// full example 
var ds = new DataStore({
  filename: path.join(__dirname, 'db', 'fileStorage.db');
});
 
var customRoute = function (reqres) {
  res.send(req.__coderbits);
};
 
var middleOne = function (reqresnext) {
  console.log("I am the middleOne middleware :)");
  next();
};
 
var middleTwo = function (reqresnext) {
  console.log("I am the middleTwo middleware :) :)");
  next();
};
 
var opts = {
  path: 'coderbits',
  uri: 'https://coderbits.com',
  customKey: '__coderbits',
  stale: '5m',
  cache: true,
  ds: ds,
  middleware: [middleOne, middleTwo],
  customRoute: customRoute
};
 
var coderbits = new localize(opts);
 
coderbits.mount(app);
 
server.listen(app.get('port'), function () {
  console.log('listening on port ' + app.get('port'));
});
 
// GET http://localhost:1337/coderbits/dhz.json 
NameDefaultsInfo
accepted['post', 'put', 'delete', 'get']accepted methods to run external requests against, expects an array
cachefalseuses nedb currently, still finishing mongodb
customKey__localizedoverride the default req object addition
customRoutenullallows you to pass a custom route through as your endpoint, helpful if you want to use the output to template a file
dsDataStoreyou'll get one of these from nedb
middleware[]allows you to add custom middleware to your api, good for authentication/ensureLogin etc
pathgithubdefaults to github which is the default api to get this going quickly
stale1muses ms module for ez times eg: 1s, 1m, 5m, 1h, 10h, 1d, etc
urihttps://api.github.comapi path to localize
The MIT License (MIT)
 
Copyright (c) 2013 David Higginbotham 
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.