1.3.0 • Published 2 months ago

@iobxt/api-creator v1.3.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 months ago

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:

NameDescriptionDefault valuePossible values
APP_PORTthe port to listen on8080
PLATFORMHow the api runsdevelopmentdevelopment, 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
1.3.0

2 months ago

1.2.3

8 months ago

1.2.2

8 months ago

1.2.1

8 months ago

1.2.0

8 months ago

1.0.1

9 months ago

1.1.0

9 months ago

1.0.0

10 months ago