From 3899bf56bc974317c02ab2a4360eb681b07e9e52 Mon Sep 17 00:00:00 2001 From: nashaofu Date: Thu, 18 Jan 2018 20:33:52 +0800 Subject: [PATCH] release 1.0.0 --- README.md | 28 ++++------------------------ package.json | 2 +- test/index.test.ts | 14 +++++--------- 3 files changed, 10 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index d941024..884103b 100644 --- a/README.md +++ b/README.md @@ -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') @@ -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) => { - if (ctx.request.body !== undefined) { - ctx.body = ctx.request.body - } - await next() -}) - app.listen(port) console.error(`listening on port ${port}`) ``` @@ -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 @@ -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 })) ``` diff --git a/package.json b/package.json index d554017..9573a44 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/index.test.ts b/test/index.test.ts index 04b021d..f2014b3 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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 @@ -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) }) @@ -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) }) @@ -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 @@ -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) }) @@ -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) @@ -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) } }))