Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
{
"presets": [
"stage-3",
"env"
],
"plugins": [
["transform-runtime", {
"polyfill": true,
"regenerator": true
}]
],
"env": {
"coverage": {
"plugins": [
"istanbul"
]
}
}
"extends": "./node_modules/@jordanforeman/babel-config/babel.config.node.js"
}
53 changes: 0 additions & 53 deletions .circleci/config.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .czrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"path": "@jordanforeman/commitizen-config"
}
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "@jordanforeman/eslint-config-btg"
"extends": "@jordanforeman/eslint-config"
}
6 changes: 6 additions & 0 deletions .huskyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"hooks": {
"prepare-commit-msg": "exec < /dev/tty && npx git-cz --hook || true",
"commit-msg": "npx commitlint -E HUSKY_GIT_PARAMS"
}
}
6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10.17.0
16 changes: 3 additions & 13 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"exclude": [
"test",
"lib"
],
"require": [
"babel-register"
],
"sourceMap": false,
"instrument": false,
"lines": 100,
"statements": 100,
"functions": 100,
"branches": 100
"nyc": {
"extends": "@jordanforeman/nyc-config"
}
}
3 changes: 3 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@jordanforeman/release-config"
}
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# api-framework
# `@jordanforeman/api-framework`

Everything you need to start creating a NodeJS based API Server. Handles common tasks such as:
Everything you need to start creating NodeJS based API Servers.

* Authentication
* Error handling
* Server configuration
<span class="badge-npmversion"><a href="https://www.npmjs.com/package/@jordanforeman/api-framework" title="View this project on NPM"><img src="https://img.shields.io/npm/v/@jordanforeman/api-framework.svg" alt="NPM version" /></a></span>
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
![](https://github.com/JordanForeman/api-framework/workflows/Semantic%20Release/badge.svg)
![](https://github.com/JordanForeman/api-framework/workflows/PR%20Verify/badge.svg)
[![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com/)

## Installation

```bash
$ npm i --save api-framework
$ npm i --save @jordanforeman/api-framework
```

## Usage

```js
import { start } from 'api-framework';
import { start } from '@jordanforeman/api-framework';
import controllers from './controllers';

const config = {
Expand All @@ -25,6 +27,14 @@ const config = {
start(controllers, config);
```

## Configuration Options

| Option | Description | Default |
|--------|-------------|---------|
| `port` | The port to run the server on | `8080` |
| `onStart` | (optional) a function to run when the server starts | **n/a** |
| `onError` | (optional) a function to run when the server encounters an error (takes a single parameter, which is the error) | **n/a** |

## Controller Syntax

This module is an opinionated wrapper around the Express framework, however controller syntax is heavily inspired by HapiJS. The `controllers` parameter passed to the `start` method is an array of objects with the following structure:
Expand All @@ -33,38 +43,32 @@ Property | Description
------------- | -------------
path | The API route/path
method | the standard HTTP method to use (maps to [Express router methods](https://expressjs.com/en/4x/api.html#router.METHOD))
config.middleware | `TODO` document this
config.handler | A single Express method callback with signature `(request, response, next)`
config.auth | (optional) the key of the authentication strategy to use for this controller
config.middleware | (optional) the [middleware](https://expressjs.com/en/guide/using-middleware.html) (or middlewares) to use for this controller
config.auth | (optional) the authentication strategy to use for this controller (see below)

```js
import { strategies } from 'api-framework';
import { auth as jwtAuth } from '@jordanforeman/jwt-authentication';

export const myController = {
path: '/',
method: 'GET',
config: {
auth: strategies.JWT,
auth: jwtAuth,
handler: getRoot
}
};
```

### Authentication Strategies

Authentication strategies are predefined Express middleware that handle authentication requirements for a given API route controller. Simply add: `auth: <strategy>` to your controller's `config`, and `api-framework` will handle the rest.

### Available Strategies

Name | Description
------------- | -------------
JWT | Reads 'x-access-token' from the request headers and verifies against the app secret defined in `config`. Immediately returns a 403 if token is invalid
## Authentication Strategies

### Default Middleware
Authentication strategies are published separately from this module. The following strategies are implemented to work with this module:

`TODO:` document this
* [`@jordanforeman/jwt-authentication`](https://github.com/jordanforeman/authentication-strategies/tree/master/packages/jwt-authentication)

## Helpful Links

* [Express Request](https://expressjs.com/en/4x/api.html#req)
* [Express Response](https://expressjs.com/en/4x/api.html#res)
* [Express Response](https://expressjs.com/en/4x/api.html#res)
* [Express Middleware](https://expressjs.com/en/guide/using-middleware.html)
* [Authentication Strategies](https://github.com/jordanforeman/authentication-strategies)
3 changes: 3 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@jordanforeman/commitlint-config']
};
3 changes: 0 additions & 3 deletions config/default.js

This file was deleted.

8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const server = require('./lib/server');

module.exports = {
start: require('./lib/server').default,
strategies: require('./lib/constant/strategies'),
middleware: require('./lib/middleware')
};
start: server.start
};
Loading