Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Rework using webapi-parser #61

Merged
merged 67 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
b49834b
Fix latest standard linting notes
postatum Oct 29, 2019
128dc63
Replace vars with let/const
postatum Nov 1, 2019
64c6f8c
Refactor and fix tests
postatum Nov 1, 2019
fcfd66a
Refactor vars in code
postatum Nov 1, 2019
349db37
Rename osprey-router var in tests for readability
postatum Nov 1, 2019
0e3c41a
Rename params in methods
postatum Nov 1, 2019
9d9764c
Rework headerHandler
postatum Nov 4, 2019
235a6b9
Partially rework bodyHandler
postatum Nov 4, 2019
445de87
Rework jsonBodyHandler
postatum Nov 5, 2019
ca38b71
Rework xmlBodyHandler
postatum Nov 5, 2019
d47f0f9
Rework urlencodedBodyHandler
postatum Nov 5, 2019
eb7ff20
Reworked formDataBodyHandler
postatum Nov 5, 2019
32f30ef
Rework acceptsHandler
postatum Nov 6, 2019
0de0dff
Finish initial rework. Remove deps
postatum Nov 6, 2019
cab6e52
Start reworking tests
postatum Nov 8, 2019
c4ecbfb
Finish initial tests rework
postatum Nov 10, 2019
70a01a0
Start fixing bugs after rework
postatum Nov 10, 2019
a6fd746
Fix few bugs discovered while fixing tests
postatum Nov 11, 2019
6b2ce1c
Fix more issues and tests
postatum Nov 11, 2019
654906c
Fix more tests and issues
postatum Nov 11, 2019
4ed77b6
Fix majority of tests
postatum Nov 12, 2019
30fc49d
Remove RAML 0.8-specific code, add tests
postatum Nov 13, 2019
c30f6d2
Replace number type definition
postatum Nov 13, 2019
0e0c21b
Check for req.readableEnded
postatum Nov 15, 2019
ded5435
Reimplement sanitization.
postatum Nov 18, 2019
b191c6d
Fix jsdoc comments
postatum Nov 20, 2019
8eaa2ce
Fix few tests
postatum Nov 26, 2019
de75421
Fix failing urlencoded test
postatum Nov 27, 2019
fde26be
Fix form fields sanitization
postatum Nov 27, 2019
646f18c
Fix files handling in forms
postatum Nov 28, 2019
ab6cbd9
Fix sanitization order in forms handling
postatum Nov 28, 2019
ad2df57
Fix security issues
postatum Nov 28, 2019
edd7ce5
Call webapi-parser.init on default headers creation
postatum Nov 28, 2019
523184d
Update dependencies
postatum Nov 28, 2019
c5956e3
Update popsicle and popsicle-server
postatum Nov 29, 2019
ea3c09a
Not use instanceof to check amf types
postatum Dec 3, 2019
4b540cf
Fix additionalProperties and anyshape handling
postatum Dec 3, 2019
c8e9258
Fix additionalProperties more. Add tests
postatum Dec 3, 2019
4be3743
Rework queryParameters and headers validation
postatum Dec 3, 2019
4aaf117
Update webapi-parser
postatum Dec 4, 2019
11f4a1d
Link osprey-router, raml-sanitize from github
postatum Dec 20, 2019
56a36a2
Rework Object.fromEntries usage
postatum Dec 20, 2019
a197842
Update readme
postatum Dec 23, 2019
23e38df
Update osprey-router version from gh
postatum Dec 24, 2019
b318dee
Unlink gh dependencies
postatum Dec 24, 2019
be8129b
Optimize code to call wap.init() only during setup
postatum Dec 24, 2019
37a13c1
Rework default headers handling to not call webapi-parser.init()
postatum Dec 24, 2019
51d8327
Rename confusing function
postatum Dec 24, 2019
83dfbb2
Optimize headerHandler to call webapi-parser init on start
postatum Dec 25, 2019
1003789
Fix query params picking bug
postatum Dec 25, 2019
c856429
Fix status codes in tests
postatum Feb 3, 2020
677307e
Rework to validate with ajv instead of wap
postatum Feb 3, 2020
ec430bd
Update webapi-parser to 0.5.0
postatum Feb 5, 2020
2c7b80f
Downgrade webapi-parser
postatum Feb 5, 2020
2d0129e
Add unit test for nodeShapeFromParams
postatum Feb 6, 2020
272375c
Update deps
postatum Feb 27, 2020
8ef20f0
Restore ajv option
postatum Feb 28, 2020
abe4d1e
Restore addJsonSchema API
postatum Feb 28, 2020
e2a3cb1
Merge master. Fix conflicts
postatum May 4, 2020
7973950
Update deps
postatum Jul 7, 2020
7adb162
Merge fresh master
postatum Jul 8, 2020
f4b65c4
Address readme notes
postatum Jul 14, 2020
a877fea
Revert error format change
postatum Jul 14, 2020
44fb3af
Remove input schemas check from jsonBodyHandler
postatum Jul 14, 2020
18c614d
Add RAML unions validation test
postatum Jul 15, 2020
4c0ba24
Clarify readme
postatum Jul 15, 2020
db17dae
Update dependencies
postatum Jul 28, 2020
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ install:
node_js:
- "10"
- "12"
- "13"
- "14"
- "node"

cache:
Expand Down
61 changes: 20 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,29 @@ npm install osprey-method-handler --save
## Usage

```js
var express = require('express')
var handler = require('osprey-method-handler')
var app = express()

app.post('/users', handler({
headers: {},
responses: {
'200': {
body: {
'application/json': {
schema: '...',
example: '...'
}
}
}
},
body: {
'application/json': {
schema: '...'
}
const express = require('express')
const handler = require('osprey-method-handler')
const utils = require('./utils')

const app = express()

// webapi-parser.Operation
const methodObj = utils.getMethodObj()
const options = {}

app.post(
'/users',
handler(methodObj, '/users', 'POST', options),
function (req, res) {
res.send('success')
}
}, '/users', 'POST', { /* ... */ }), function (req, res) {
res.send('success')
})
)
```

Accepts the RAML schema as the first argument, method and path in subsequent arguments (mostly for debugging) and options as the final argument.
Accepts [webapi-parser](https://github.com/raml-org/webapi-parser) `Operation` object as first argument, path string as second argument, method name as third and options object as final argument.

**Options**

* `ajv` Custom [Ajv](https://github.com/epoberezkin/ajv) instance to be used for JSON validation
* `ajv` Custom [Ajv](https://github.com/epoberezkin/ajv) instance to be used to validate query strings, request headers and request bodied (url-encoded, form-data, json)
* `discardUnknownBodies` Discard undefined request streams (default: `true`)
* `discardUnknownQueryParameters` Discard undefined query parameters (default: `true`)
* `discardUnknownHeaders` Discard undefined header parameters (always includes known headers) (default: `true`)
Expand All @@ -76,32 +68,19 @@ Accepts the RAML schema as the first argument, method and path in subsequent arg
* `limit` The [maximum bytes](https://github.com/expressjs/body-parser#limit-2) for XML, JSON and URL-encoded endpoints (default: `'100kb'`)
* `parameterLimit` The [maximum number](https://github.com/expressjs/body-parser#parameterlimit) of URL-encoded parameters (default: `1000`)
* `busboyLimits` The multipart limits defined by [Busboy](https://github.com/mscdex/busboy#busboy-methods)
* `RAMLVersion` The RAML version passed to [raml-validate](https://github.com/mulesoft/node-raml-validate) (default: `'RAML08'`)
postatum marked this conversation as resolved.
Show resolved Hide resolved

### Adding JSON schemas

If you are using external JSON schemas with `$ref`, you can add them to the module before you compile the middleware. Use `handler.addJsonSchema(schema, key)` to compile automatically when used.

`handler.addJsonSchema()` accepts a third (optional) `options` argument. Supported `options` are:
* `ajv` Custom [Ajv](https://github.com/epoberezkin/ajv) instance. E.g. `handler.addJsonSchema(schema, key, {ajv: myAjvInstance})`. The provided ajv instance can later be passed as an option to the handler to perform JSON validation.
* `ajv` Custom [Ajv](https://github.com/epoberezkin/ajv) instance. E.g. `handler.addJsonSchema(schema, key, {ajv: myAjvInstance})`. The provided ajv instance can later be passed as an option to the handler to perform validation.

### Validation Errors

The library intercepts incoming requests and does validation. It will respond with `400`, `406` or `415` error instances from [http-errors](https://github.com/jshttp/http-errors). Validation errors are attached to `400` instances and noted using `ramlValidation = true` and `requestErrors = []` (an array of errors that were found, compatible with [request-error-handler](https://github.com/mulesoft-labs/node-request-error-handler)).
postatum marked this conversation as resolved.
Show resolved Hide resolved

The errors object format is:

```ts
interface Error {
type: 'json' | 'form' | 'headers' | 'query' | 'xml'
message: string
keyword: string
dataPath: string
data: any
schema: any
meta?: Object
}
```
See [the code](https://github.com/mulesoft-labs/osprey-method-handler/blob/7adb162035e4e593a5bbda8b3e83b1996adc2174/osprey-method-handler.js#L705-L751) for a complete list of errors formats.
postatum marked this conversation as resolved.
Show resolved Hide resolved

**Please note:** XML validation does not have a way to get the `keyword`, `dataPath`, `data` or `schema`. Instead, it has a `meta` object that contains information from `libxmljs` (`domain`, `code`, `level`, `column`, `line`).
jstoiko marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Loading