Skip to content

Commit

Permalink
release 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nashaofu committed Jan 18, 2018
1 parent ba8ba56 commit 3899bf5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 34 deletions.
28 changes: 4 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ a body parser for koa. support json, form(urlencoded), multipart and text type b

## Usage

1. javascript
```js
const Koa = require('koa')
const parser = require('koa-parser')
Expand All @@ -25,26 +24,6 @@ app.use(async (ctx, next) => {
await next()
})

app.listen(port)
console.error(`listening on port ${port}`)
```
2. typescript
```typescript
import * as Koa from 'koa'
import * as parser from 'koa-parser'

const port = 3000
const app = new Koa()

app.use(parser())

app.use(async (ctx: Koa.Context, next: () => Promise<void>) => {
if (ctx.request.body !== undefined) {
ctx.body = ctx.request.body
}
await next()
})

app.listen(port)
console.error(`listening on port ${port}`)
```
Expand All @@ -58,7 +37,7 @@ console.error(`listening on port ${port}`)
```typescript
app.use(parser({
encoding?: string, // requested encoding
error?: (err: any, ctx: any) => any, // support custom parser error handle
error?: (err: any, ctx: Koa.Context) => any, // support custom parser error handle
json?: string | string[], // support json parser types
multipart?: string | string[], // support multipart(form-data) parser types
text?: string | string[], // support text parser types
Expand All @@ -73,15 +52,16 @@ app.use(parser({
app.use(parser({
error (err, ctx) {
console.log(err)
ctx.throw(err, 422)
}
}))
```

* **json**: Extended support for json parsing options, The default supported types are ``['application/json', 'application/json-patch+json', 'application/vnd.api+json', 'application/csp-report']``. you can customize the type like:
```js
// json will be support default types and application/x-web-app-manifest+json
// json will be support default types and application/x-javascript
app.use(parser({
json: 'application/x-web-app-manifest+json' // You can also pass parameters in an array
json: 'application/x-javascript' // You can also pass parameters in an array
}))
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-parser",
"version": "0.4.2",
"version": "1.0.0",
"description": "a body parser for koa",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
14 changes: 5 additions & 9 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('test width default', () => {
app.use(async (ctx, next) => {
expect(ctx.request.body).toEqual({
str: 'str',
arr: ['arr1', 'arr2'],
obj: { key: 'val' }
})
ctx.body = ctx.request.body
Expand All @@ -31,11 +30,9 @@ describe('test width default', () => {
.post('/')
.send({
str: 'str',
arr: ['arr1', 'arr2'],
obj: { key: 'val' }
}).expect({
str: 'str',
arr: ['arr1', 'arr2'],
obj: { key: 'val' }
}, done)
})
Expand All @@ -51,7 +48,7 @@ describe('test width default', () => {
supertest(server)
.patch('/')
.set('Content-type', 'application/json-patch+json')
.send('[{"op": "add", "path": "/foo", "value": "bar"}]')
.send(JSON.stringify([{ op: 'add', path: '/foo', value: 'bar' }]))
.expect([{ op: 'add', path: '/foo', value: 'bar' }], done)
})

Expand All @@ -76,7 +73,6 @@ describe('test width default', () => {
app.use(async (ctx, next) => {
expect(ctx.request.body).toEqual({
str: 'str',
arr: ['arr1', 'arr2'],
obj: { key: 'val' }
})
ctx.body = ctx.request.body
Expand All @@ -88,11 +84,9 @@ describe('test width default', () => {
.post('/')
.send({
str: 'str',
arr: ['arr1', 'arr2'],
obj: { key: 'val' }
}).expect({
str: 'str',
arr: ['arr1', 'arr2'],
obj: { key: 'val' }
}, done)
})
Expand All @@ -118,7 +112,9 @@ describe('test width default', () => {
.attach('firstField', 'package.json')
.expect(200)
.end((err, res) => {
if (err) return done(err)
if (err) {
return done(err)
}
expect(res.body).toMatchObject({
names: expect.arrayContaining(['Paul', 'John']),
firstField: expect.any(Object)
Expand Down Expand Up @@ -175,7 +171,7 @@ describe('error and not parser', () => {

it('should get custom error message', done => {
app.use(parser({
error(err, ctx) {
error (err, ctx) {
ctx.throw('custom parse error', 422)
}
}))
Expand Down

0 comments on commit 3899bf5

Please sign in to comment.