apicluster

1.0.5 • Public • Published

Api Cluster

Endpoint Library

NPM version Downloads Build Status

NPM

What is API Cluster?

API Cluster is free and open source Javascript library for organizing endpoint in efficient way.

Why API Cluster?

Add multiple groups for versioning.
Quit messing with concatenation on URL with + operator.
Available in (Node) Server side and (browser) Client Side.

Getting Started

1. Install api cluster globally (server side) or include the apicluster.js in your file.

$ npm install --global apicluster
<script src="ApiCluster.js"></script>

2. Create Config and define endpoints.

ApiCluster
  .defaults({
      name: 'mydefault',
 
      config: {
        'employee': 'emp',
        'details': 'details',
        'timesheet': 'timesheet'
      },
 
      endpoints: {
        "empDetails": "_employee_/_details_/:empId/profile"
      }
  });

2. Add multiple groups.

ApiCluster
  .defaults({
      name: 'mydefault',
 
      config: {
        'employee': 'emp',
        'details': 'details',
        'timesheet': 'timesheet'
      },
 
      endpoints: {
        "empDetails": "_employee_/_details_/:empId/profile"
      }
  })
  .addAnother({
      name: 'v1',
 
      config: {
        'employee': 'emp/v1',
        'details': 'detailed',
        'timesheet': 'timesheet'
      },
 
      endpoints: {
        "empDetails": "_employee_/_details_/:empId/profile"
      }
  })
  .addAnother({
      name: 'v2',
 
      config: {
        'employee': 'emp/v2',
        'details': 'detailed',
        'timesheet': 'timesheet'
      },
 
      endpoints: {
        "empDetails": "_employee_/_details_/:empId/profile"
      }
  });

4. Get your dynamic Endpoint URL from the Configured Endpoint list from defaults() method.

var empDetails = ApiCluster
                  .get('empDetails')
                  .arg({
                    'empId': 1000 
                  })
                  .query({
                    'confirm': 'yes',
                    'testAccount': 'yes'
                  })
                  .url();
Expected Output: emp/details/1000/profile?confirm=yes&testAccount=yes

5. Get Endpoint URL from the Configured Endpoint list from v1 Group defined in addAnother() method.

var empDetails = ApiCluster
                  .use('v1')
                  .get('empDetails')
                  .arg({
                    'empId': 1000 
                  })
                  .query({
                    'confirm': 'yes',
                    'testAccount': 'yes'
                  })
                  .url();
Expected Output: emp/v1/detailed/1000/profile?confirm=yes&testAccount=yes

Example on how to use it in Node.

var http = require('http'),
    ApiCluster = require('apicluster');
 
ApiCluster
  .defaults({
      name: 'mydefault',
 
      config: {
        'employee': 'emp',
        'details': 'details',
        'timesheet': 'timesheet'
      },
 
      endpoints: {
        "empDetails": "_employee_/_details_/:empId/profile"
      }
  });
 
//Lets define a port we want to listen to
const PORT = 9000; 
 
// Function which handles requests and send response
function handleRequest(request, response) {
    var empDetailURL = ApiCluster
                  .get('empDetails')
                  .arg({
                    'empId': 1000 
                  })
                  .query({
                    'confirm': 'yes',
                    'testAccount': 'yes'
                  })
                  .url();
    response.end('<h1>Generated Endpoint URL:<br> '
     + empDetailURL + '</h1>');
}
 
//Create a server
var server = http.createServer(handleRequest);
 
//Lets start our server
server.listen(PORT, function(){
    // Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening on: http://localhost:%s", PORT);
});

Want to contribute?

Anyone can help make this project better - check out the Contributing guide!

Bitdeli Badge

Package Sidebar

Install

npm i apicluster

Weekly Downloads

6

Version

1.0.5

License

ISC

Last publish

Collaborators

  • ramsunvtech