Skip to content

musement/api-client

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WIP - api-client - isomorphic API client

This API client supports node and browser environments. It uses isomorphic-fetch under the hood, and provides a flexible interface, yet simple.

Features

  • middleware based: it comes already with basic middlewares, but it is possible to add custom ones.
  • mapping: through object-mapper offers automatic mapping of API responses to given models.
  • fallback values: for each call it is possible to set a fallback return value (in case of 404 or any other error).
  • mock values: for each call it is possible to set a mock value (that overrides the server response).
  • mock server: if swagger option is specified, the given spec file will be read and used to run a mock server thanks to swagger-mock-api, based on chanchejs.
  • logging and debug mode

Usage

import {init, call} from 'api-client'

// init with global params
init('http://your.api.basepath.com', () => {'Content-Type': 'application/json'})

// then perform the simplest HTTP request (GET http://your.api.basepath.com/events )
call('events')

// or more complex ones
call('events/:idEvent', {
    params: {
        idEvents: 432,
        sortBy: '-relevance'
    },
    fallback: {name: 'fake event'},
    mock: true,
// see object-mapper options for further detail
    model: {
       'about': 'about',
       'city.name': 'cityName'
   }
})


API

See API documentation

Middlewares

It is possible to add custom middlewares by specifying them through the init() method.

A middleware should be a function with the following signature:

function middleware (req: Request, res: response) : (Promise<Response>|Response)

It can:

  • return a promise
  • return a value/object
  • throw an error (only in this case, all other middleware are skipped and the API call will result in an error)

The order they are added in the promise chain matters.

Models

TBD

import { city } from './city'

export const venueRes = {
    'about': 'about',
    'city': {
        key: 'city',
        model: city
    }
}

Tests

npm run test

Contribution

contribution is always welcome! Documentation is automatically generated and tests have to be kept up to date.

  • develop
  • update tests
  • npm run build
  • npm run generate-md
  • open a PR

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%