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

Commit ed03b79

Browse files
authoredJul 28, 2020
Merge pull request #61 from mulesoft-labs/rework_webapi_parser
Rework using webapi-parser
2 parents 88bb62c + db17dae commit ed03b79

File tree

6 files changed

+2700
-2402
lines changed

6 files changed

+2700
-2402
lines changed
 

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ install:
99
node_js:
1010
- "10"
1111
- "12"
12-
- "13"
12+
- "14"
1313
- "node"
1414

1515
cache:

‎README.md

+20-41
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,29 @@ npm install osprey-method-handler --save
3737
## Usage
3838

3939
```js
40-
var express = require('express')
41-
var handler = require('osprey-method-handler')
42-
var app = express()
43-
44-
app.post('/users', handler({
45-
headers: {},
46-
responses: {
47-
'200': {
48-
body: {
49-
'application/json': {
50-
schema: '...',
51-
example: '...'
52-
}
53-
}
54-
}
55-
},
56-
body: {
57-
'application/json': {
58-
schema: '...'
59-
}
40+
const express = require('express')
41+
const handler = require('osprey-method-handler')
42+
const utils = require('./utils')
43+
44+
const app = express()
45+
46+
// webapi-parser.Operation
47+
const methodObj = utils.getMethodObj()
48+
const options = {}
49+
50+
app.post(
51+
'/users',
52+
handler(methodObj, '/users', 'POST', options),
53+
function (req, res) {
54+
res.send('success')
6055
}
61-
}, '/users', 'POST', { /* ... */ }), function (req, res) {
62-
res.send('success')
63-
})
56+
)
6457
```
65-
66-
Accepts the RAML schema as the first argument, method and path in subsequent arguments (mostly for debugging) and options as the final argument.
58+
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.
6759

6860
**Options**
6961

70-
* `ajv` Custom [Ajv](https://github.com/epoberezkin/ajv) instance to be used for JSON validation
62+
* `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)
7163
* `discardUnknownBodies` Discard undefined request streams (default: `true`)
7264
* `discardUnknownQueryParameters` Discard undefined query parameters (default: `true`)
7365
* `discardUnknownHeaders` Discard undefined header parameters (always includes known headers) (default: `true`)
@@ -76,32 +68,19 @@ Accepts the RAML schema as the first argument, method and path in subsequent arg
7668
* `limit` The [maximum bytes](https://github.com/expressjs/body-parser#limit-2) for XML, JSON and URL-encoded endpoints (default: `'100kb'`)
7769
* `parameterLimit` The [maximum number](https://github.com/expressjs/body-parser#parameterlimit) of URL-encoded parameters (default: `1000`)
7870
* `busboyLimits` The multipart limits defined by [Busboy](https://github.com/mscdex/busboy#busboy-methods)
79-
* `RAMLVersion` The RAML version passed to [raml-validate](https://github.com/mulesoft/node-raml-validate) (default: `'RAML08'`)
8071

8172
### Adding JSON schemas
8273

8374
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.
8475

8576
`handler.addJsonSchema()` accepts a third (optional) `options` argument. Supported `options` are:
86-
* `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.
77+
* `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.
8778

8879
### Validation Errors
8980

9081
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)).
9182

92-
The errors object format is:
93-
94-
```ts
95-
interface Error {
96-
type: 'json' | 'form' | 'headers' | 'query' | 'xml'
97-
message: string
98-
keyword: string
99-
dataPath: string
100-
data: any
101-
schema: any
102-
meta?: Object
103-
}
104-
```
83+
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.
10584

10685
**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`).
10786

0 commit comments

Comments
 (0)
This repository has been archived.