diff --git a/co-body/co-body-tests.ts b/co-body/co-body-tests.ts new file mode 100644 index 0000000000..6011632399 --- /dev/null +++ b/co-body/co-body-tests.ts @@ -0,0 +1,44 @@ +/// +/// + +import * as koa from 'koa'; +import * as parse from 'co-body'; + +var app = new koa(); + +app.use(async function (ctx: koa.Context, next: Function): Promise { + var body: any; + + // application/json + body = await parse.json(ctx.req); + + // explicit limit + body = await parse.json(ctx.req, { limit: '10kb' }); + + // application/x-www-form-urlencoded + body = await parse.form(ctx.req); + + // text/plain + body = await parse.text(ctx.req); + + // either + body = await parse(ctx.req); + + // custom type + body = await parse(ctx.req, { textTypes: ['text', 'html'] }); + + // This lib also supports ctx.req in Koa (or other libraries), so that you may simply use this instead of this.req. + + // application/json + body = await parse.json(ctx); + + // application/x-www-form-urlencoded + body = await parse.form(ctx); + + // text/plain + body = await parse.text(ctx); + + // either + body = await parse(ctx); +}); + diff --git a/co-body/index.d.ts b/co-body/index.d.ts new file mode 100644 index 0000000000..0393db0a94 --- /dev/null +++ b/co-body/index.d.ts @@ -0,0 +1,37 @@ +// Type definitions for co-body +// Project: https://github.com/cojs/co-body +// Definitions by: Joshua DeVinney +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +/// +/// + +import * as http from 'http'; +import * as Koa from 'koa'; +import * as qs from 'qs'; + +declare namespace CoBody { + type Context = http.IncomingMessage | Koa.Context; + + export interface Parse { + (context: Context, options?: Options): Promise; + text: (context: Context, options?: Options) => Promise; + form: (context: Context, options?: Options) => Promise; + json: (context: Context, options?: Options) => Promise; + } + + export interface Options { + limit?: number | string; + strict?: boolean; + queryString?: qs.IParseOptions; + jsonTypes?: string[]; + formTypes?: string[]; + textTypes?: string[]; + encoding?: string; + length?: number; + } +} + +declare var CoBody: CoBody.Parse; +export = CoBody; diff --git a/co-body/tsconfig.json b/co-body/tsconfig.json new file mode 100644 index 0000000000..be5ee22788 --- /dev/null +++ b/co-body/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "co-body-tests.ts" + ] +} \ No newline at end of file