Skip to content

Commit

Permalink
Add httpMethods list (convenience), updated docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
coreybutler committed Aug 13, 2020
1 parent a104b02 commit a077ff4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const server = app.listen(() => console.log('Server is running.'))
- [applyRelativeUrl (req, route = '/' [, forceTLS = false])](#applyrelativeurl-req-route---forcetls--false)
- [errorType](#errorType)
- [commonHeaders](#commonHeaders)
- [httpMethods](#httpMethods)

## Middleware

Expand Down Expand Up @@ -186,6 +187,7 @@ app.get('/endpoint', (req, res) => { ...processing... }, checkResult)
```

### basicauth(user, password)

This method will perform basic authentication.
It will compare the authentication header credentials
with the username and password.
Expand Down Expand Up @@ -221,6 +223,7 @@ middleware or other handlers will be able to access the username
by referencing `req.user`.

### bearer(token)

This method looks for a bearer token in the `Authorization` request header. If the token does not match, a `401 (Unauthorized)` status is returned.

```javascript
Expand Down Expand Up @@ -666,4 +669,28 @@ API.errorType = 'json'

This is an array of the most common request headers used by HTTP clients. This is useful when constructing your own list of CORS headers using the `allowHeaders` method.

```javascript
console.log(API.commonHeaders)
```

Headers include: `Origin`, `X-Requested-With`, `Content-Type`, and `Accept`. This list may be updated from time to time.

### httpMethods

An array of the official HTTP methods.

```javascript
console.log(API.httpMethods)
```

Includes:

- `GET`
- `HEAD`
- `POST`
- `PUT`
- `DELETE`
- `CONNECT`
- `OPTIONS`
- `TRACE`
- `PATCH`
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function priv (value) {

const ERROR_TYPES = new Set(['json', 'text'])
const COMMON_HEADERS = new Set(['Origin', 'X-Requested-With', 'Content-Type', 'Accept'])
const HTTP_METHODS = new Set(['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'])

class Endpoint {
constructor () {
Expand Down Expand Up @@ -75,6 +76,10 @@ class Endpoint {
return Array.from(COMMON_HEADERS)
}

get httpMethods () {
return Array.from(HTTP_METHODS)
}

// Last argument must be a callback.
validateJsonBody () {
let args = Array.from(arguments)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@butlerlogic/common-api",
"version": "1.5.3",
"version": "1.5.4",
"description": "An API engineering productivity kit for Express.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit a077ff4

Please sign in to comment.