Feign.js Build Status Coverage

Declarative flexible Restclient-bridge that enables to easily define rest-client for node. It is based on the java-implementation of feign from netflix.

Remark: this library is not affiliated to netflix

Overview

Feign.js allows to define a rest-client api and staying independent of a specific client-implementation. It tries to solve three issues:

First point is to have a central location where you define all api-calls of a client, parameters, headers and so on, so in your business-code, you just call the generated function.

Second point is to stay independent of a client (e.g. you can later switch to another one that supports a feature you may need).

Third point is that you can now start implementing unforseen requirements like resilience (e.g. using a circuitBreaker) later on without changing your application at all (this feature is planned for later versions though).

Currently, following clients are supported:

Changelog

Installation

You need to install both feignjs and a client to be used for feign.

npm install feignjs
npm install feignjs-<client>

or with bower

bower install feignjs
bower install feignjs-<client>

Features:

var apiDescription = {
  getUsers: 'GET /users',
  getUser: 'GET /users/{id}',
  createPost: 'POST /posts',
  modifyPost: 'PUT /posts/{id}',
};

var client = feign.builder()
        .client(new FeignRequest())        
        .target(apiDescription, 'http://jsonplaceholder.typicode.com');


client.modifyPost(1, {content: 'new text'}).then(console.log)

see more examples in the samples-folder

Format

The description of clients is mostly intuitive. it can be given as plain string or object. The format supports uri-templates based on (rfc6570), so you can even use more complicate formats:

var apiDescription = {
  getUsers: 'GET /users',
  getUser: 'GET /users/{id}',
  getPosts: 'POST /posts{?count,order}',
  modifyPost: {
    method: 'PUT',
    uri: '/posts{/id}'
  }

};

Usage

The generated client contains methods to call the described api-endpoints. Depending on the Http-method and the path-parameters the format will vary.

client.method([path-parameters], [body/query/post-parameter-object], [callback-function]);

Some examples:

//GET /users/{id}
client.getUser(1)
client.getUser({id: 1})

//PUT /posts/{id}
client.modifyPost(1, newPost);
client.modifyPost({id: 1}, newPost);
//or if you configured callbacks:
client.modifyPost({id: 1}, newPost, onResult);

//POST /posts{?count,order}
client.getPosts({count: 10, order: 'ASC'});

## Options: an option-object can be fed into feign.builder() with following options:

Option Note default
promise crate a promise-based api. false for callback-based api. true

Extension

//TODO