From a077ff45b338358ada8f9837e60dbbb8941b20f7 Mon Sep 17 00:00:00 2001 From: Corey Butler Date: Thu, 13 Aug 2020 15:57:01 -0500 Subject: [PATCH] Add httpMethods list (convenience), updated docs. --- README.md | 27 +++++++++++++++++++++++++++ index.js | 5 +++++ package-lock.json | 2 +- package.json | 2 +- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 737e73c..74cf5c8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. @@ -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 @@ -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` diff --git a/index.js b/index.js index fa9cf29..f9315c4 100644 --- a/index.js +++ b/index.js @@ -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 () { @@ -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) diff --git a/package-lock.json b/package-lock.json index e2ac797..202b757 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@butlerlogic/common-api", - "version": "1.5.3", + "version": "1.5.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 924615f..0d59139 100644 --- a/package.json +++ b/package.json @@ -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": {