Skip to content

Commit

Permalink
#56 add TypeScript declaration file and add types property to package…
Browse files Browse the repository at this point in the history
….json

ignore package-lock.json
  • Loading branch information
naxmefy committed Aug 17, 2017
1 parent fdf4120 commit 1bca7c2
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
node_modules
npm-debug.log
yarn.lock
package-lock.json
135 changes: 135 additions & 0 deletions index.d.ts
@@ -0,0 +1,135 @@
import * as Koa from "koa";

declare module "koa" {
interface Request {
body: any;
}
interface Context {
body: any;
}
}

declare namespace koaBody {
interface IKoaBodyFormidableOptions {
/**
* {Integer} The expected number of bytes in this form, default null
*/
bytesExpected?: number

/**
* {Integer} Limits the number of fields that the querystring parser will decode, default 1000
*/
maxFields?: number;

/**
* {Integer} Limits the amount of memory all fields together (except files) can allocate in bytes.
* If this value is exceeded, an 'error' event is emitted, default 2mb (2 * 1024 * 1024)
*/
maxFieldsSize?: number;

/**
* {String} Sets the directory for placing file uploads in, default os.tmpDir()
*/
uploadDir?: string;

/**
* {Boolean} Files written to uploadDir will include the extensions of the original files, default false
*/
keepExtensions?: boolean;

/**
* {String} If you want checksums calculated for incoming files, set this to either 'sha1' or 'md5', default false
*/
hash?: string;

/**
* {Boolean} Multiple file uploads or no, default true
*/
multiples?: boolean;
/**
* {Function} Special callback on file begin. The function is executed directly by formidable.
* It can be used to rename files before saving them to disk. See https://github.com/felixge/node-formidable#filebegin
*/
onFileBegin?: (name: string, file: any) => void;
}
interface IKoaBodyOptions {
/**
* {Boolean} Patch request body to Node's ctx.req, default false
*
* Note: You can patch request body to Node or Koa in same time if you want.
*/
patchNode?: boolean;

/**
* {Boolean} Patch request body to Koa's ctx.request, default true
*
* Note: You can patch request body to Node or Koa in same time if you want.
*/
patchKoa?: boolean;

/**
* {String|Integer} The byte (if integer) limit of the JSON body, default 1mb
*/
jsonLimit?: string|number;

/**
* {String|Integer} The byte (if integer) limit of the form body, default 56kb
*/
formLimit?: string|number;

/**
* {String|Integer} The byte (if integer) limit of the text body, default 56kb
*/
textLimit?: string|number;

/**
* {String} Sets encoding for incoming form fields, default utf-8
*/
encoding?: string;

/**
* {Boolean} Parse multipart bodies, default false
*/
multipart?: boolean;

/**
* {Boolean} Parse urlencoded bodies, default true
*/
urlencoded?: boolean;

/**
* {Boolean} Parse text bodies, default true
*/
text?: boolean;

/**
* {Boolean} Parse json bodies, default true
*/
json?: boolean;

/**
* {Object} Options to pass to the formidable multipart parser
*/
formidable?: IKoaBodyFormidableOptions;

/**
* {Function} Custom error handle, if throw an error, you can customize the response - onError(error, context), default will throw
*/
onError?: (err: Error, ctx: Koa.Context) => void;

/**
* {Boolean} If enabled, don't parse GET, HEAD, DELETE requests, default true
*
* GET, HEAD, and DELETE requests have no defined semantics for the request body,
* but this doesn't mean they may not be valid in certain use cases.
* koa-body is strict by default
*
* see http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-19#section-6.3
*/
strict?: boolean;
}
}

declare function koaBody (options?: koaBody.IKoaBodyOptions): Koa.Middleware;

export = koaBody;
10 changes: 6 additions & 4 deletions package.json
Expand Up @@ -3,6 +3,7 @@
"version": "2.3.0",
"description": "A koa body parser middleware. Support multipart, urlencoded and json request bodies.",
"main": "index.js",
"types": "./index.d.ts",
"scripts": {
"test": "node_modules/.bin/mocha",
"examples-multer": "node --harmony examples/multer.js",
Expand Down Expand Up @@ -38,17 +39,18 @@
"test.js"
],
"dependencies": {
"co-body": "*",
"formidable": "1.1.1"
"co-body": "^5.1.1",
"formidable": "^1.1.1"
},
"devDependencies": {
"@types/koa": "^2.0.39",
"koa": "^2.0.0",
"koa-router": "^7.0.1",
"lodash": "^3.3.1",
"mocha": "*",
"multiline": "*",
"should": "*",
"supertest": "2.0.0",
"koa": "^2.0.0"
"supertest": "2.0.0"
},
"contributors": [
{
Expand Down

0 comments on commit 1bca7c2

Please sign in to comment.