2.1.4 • Published 5 years ago

@resonate/api-factory-generator v2.1.4

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

API factory generator

Project first published as @zerowastemap/api-factory-generator. Forked it to allow more specific configuration for resonate.

  • Uses fetch
  • Uses ajv for schema validation
  • Sane defaults
  • Sends token if provided
  • Each route can have specific options

Installation

$ npm i --save @resonate/api-factory-generator

Usage

const generator = require('@resonate/api-factory-generator')

// set default options for each requests
const options = {
  mode: 'cors',
  domain: 'resonate.localhost',
  scheme: 'https://',
  prefix: '/api',
  version: 1,
  token: 'some bearer token'
}

// configure your routes
// you can nest routes
const routes = {
  auth: {
    login: {
      path: '/auth/login',
      options: {
        method: 'POST'
      },
      schema: {
        type: 'object',
        required: ['email'],
        properties: {
          email: {
            type: 'string'
          }
        }
      }
    }
  },
  upload: {
    path: '/locate',
    options: {
      method: 'POST',
      auth: true, // will enable credentials
      multipart: true
    }
  }
  locate: {
    path: '/locate',
    schema: {
      type: 'object',
      required: ['longitude', 'latitude'],
      properties: {
        longitude: Longitude,
        latitude: Latitude
      }
    }
  }
}

const api = generator(routes, options) 

// api.auth.login('salut@zerowastemap.app') works too if we have a schema
api.auth.login({ email: 'salut@zerowastemap.app' }).then(response => {
  ...
})

Testing

Browser tests won't pass your machine because it test against api which is not released yet

See also

JavaScript Factory Functions vs Constructor Functions vs Classes

LICENSE

MIT

Author(s)

2.1.4

5 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.0.0

5 years ago