Skip to content
This repository has been archived by the owner on Aug 22, 2019. It is now read-only.

zerowastemap/api-factory-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

[WIP] API factory generator

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

Usage

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

// set default options for each requests
const options = {
  mode: 'cors',
  domain: 'zerowastemap.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('[email protected]') works too if we have a schema
api.auth.login({ email: '[email protected]' }).then(response => {
  ...
})

See also

JavaScript Factory Functions vs Constructor Functions vs Classes

Testing

Tests won't work on your machine because it test against api which is not released yet

LICENSE

MIT

Author(s)