@iobxt/api-creator

1.3.0 • Public • Published

api-creator

An express wrapper which allows you to quickly and dynamically create api endpoints, without having to worry about boilerplate code.

Installation

npm i @iobxt/api-creator

Usage

Create a directory structure, something like this:

api
  |- foo
    |- doSomething.js

Your api endpoints must look something like this:

// doSomething.js
const init = (router, config, resources) => {
  router.get('/hello', (req, res) => res.send('hello'));
};

module.exports = { init };

Initialise the library with something like this:

const apiCreator = require('api-creator');
const path = require('path');

const apiPath = path.resolve('./api/');

apiCreator.init(apiPath, config, resources);

Your endpoints will be accessible on:

http://localhost:8080/foo/

As a lambda

Create a lambda.js file and export the module like so;

const api = apiCreator.init(apiPath, config, resources);

module.exports = api.handler;

Configuration

You can control the creator behaviour with the following environment varibles:

Name Description Default value Possible values
APP_PORT the port to listen on 8080
PLATFORM How the api runs development development, lambda

You can provide endpoint specific configuration by passing in a config param.

const config = {
  corsOptions: {
    origin: ['http://localhost:3000']
  },
  bodyParserEnabled: true,
  foo: {
    some: "config";
  }
}

This will configure everything under the foo directory.

Publishing a new version

  1. Commit changes
  2. Run npm publish:version major|minor|patch
  3. Profit

Dependencies (7)

Dev Dependencies (7)

Package Sidebar

Install

npm i @iobxt/api-creator

Weekly Downloads

8

Version

1.3.0

License

ISC

Unpacked Size

26.5 kB

Total Files

17

Last publish

Collaborators

  • iobxt