0.0.1 • Published 8 years ago

korridor v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

Korridor

Korridor is a small and lightweight registry for the services you use. It provides a transparent layer of abstraction to simplify accessing internal as well as external services in a uniform manner.

Services can be registered explicitly and by using one or multiple stores. A ServiceStore can fetch data from external sources and dynamically keeps your registry up-to-date.

Caution

This library should be considered a draft for now. It is under active development and will very likely change drastically. While I encourage you to test it, I strongly advice against using it in production during this phase.

Installation

$ npm install -S korridor

Usage

A minimal example to access reddit:

const korridor = require("korrridor")

const Reddit = korridor.registerService({
  name: "Reddit",
  endpoint: "https://www.reddit.com/r/",
  method: "GET"  // default: POST
})

Reddit
  .call("aww.json")
  .then(result => console.log(result.data.children))

Register A Service

The easiest way to register a service is by directly passing the configuration to korridor.registerService. An already registered service with the same name will be replaced.

const korridor = require("korridor")

// The registered service is returned.
// It will also be available at:
// korridor.services.Reddit
const Reddit = korridor.registerService({
  name: "Reddit",
  endpoint: "https://www.reddit.com/r/"
})

It is possible to create a Service in advance and then pass it to Korridor.

const Reddit = new korridor.Service({
  name: "Reddit",
  endpoint: "https://www.reddit.com/r/"
})

korridor.registerService(Reddit)

You can also register multiple services at once using korridor.registerServices.

korridor.registerServices([
  {
    name: "Reddit",
    endpoint: "https://www.reddit.com/r/"
  }
  {
    name: "GoogleMapsGeocode",
    endpoint: "https://maps.googleapis.com/maps/api/geocode/json",
    method: "GET"
  }
])

Invoking Service Functions

After a service is successfully registered, you can directly start using it. Korridor expects a json-response.

const { Reddit } = korridor.services

Reddit
  .call("aww.json", null, { method: "GET" })
  .then(result => console.log(result))
  .catch(e => console.error(e))

Register A Store

A ServiceStore has the capability to register and update services dynamically. This can be very helpful when your environment is using a registry like Consul. To create a new store, you have to extend from korridor.ServiceStore.

const { ServiceStore } = require("korridor")

class MyStore extends ServiceStore {

  constructor() {
    super({ name: "MyStore" })
  }

  /** 
   * Called when the store can start to register services.
   * Use this to open any connections to registry-servers.
   * You can also return a Promise.
   */
  open () {
    this.addService({
      name: "Reddit",
      endpoint: "https://www.reddit.com",
      method: "GET"
    })
  }

  /**
   * Clean up, e.g. close connections.
   * All services, that have been registered by this store, will be removed 
   * after the close-function has been executed.
   * You can also return a Promise.
   */
  close() {

  }

}

You can then create an instance of your store and register it to Korridor.

korridor.registerStore(new MyStore()).then(store => {
  /**
   * You can now access the services registered by the store.
   * The services are available at:
   * korridor.services (including all other services)
   * store.services (only the store-services)
   *
   * The store is also available at: korridor.stores.MyStore
   */

   const { Reddit } = store.services 
   Reddit.call("aww.json").then(result => console.log(result))
})

Reusable Services and Stores

If you wrote a service or store that could possibly benefit other projects as well, please feel encouraged to upload it and create a new Issue. I would happily link these projects here to extend the comfort of using Korridor.

License

MIT License

Copyright (c) 2016 Felix Bernhardt (felixbernhardt@yahoo.com)

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.

0.0.1

8 years ago