From fdbf262aa93f9e994037c0055a2c9db2694cff09 Mon Sep 17 00:00:00 2001 From: Mudkip Date: Tue, 6 Sep 2016 22:06:12 +0800 Subject: [PATCH] Add type definition for koa-json-error (#10951) * Add type definition for koa-json-error * Consist with import statement --- koa-json-error/koa-json-error-tests.ts | 13 ++++++++++ koa-json-error/koa-json-error.d.ts | 36 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 koa-json-error/koa-json-error-tests.ts create mode 100644 koa-json-error/koa-json-error.d.ts diff --git a/koa-json-error/koa-json-error-tests.ts b/koa-json-error/koa-json-error-tests.ts new file mode 100644 index 0000000000..37aae696b0 --- /dev/null +++ b/koa-json-error/koa-json-error-tests.ts @@ -0,0 +1,13 @@ +/// +/// +/// + +import * as Koa from "koa"; +import * as error from "koa-json-error"; +import { assign } from "lodash"; + +const app = new Koa(); + +app.use(error({ + preFormat: err => assign({}, err) +})); \ No newline at end of file diff --git a/koa-json-error/koa-json-error.d.ts b/koa-json-error/koa-json-error.d.ts new file mode 100644 index 0000000000..491ead463e --- /dev/null +++ b/koa-json-error/koa-json-error.d.ts @@ -0,0 +1,36 @@ +// Type definitions for koa-json-error v3.x +// Project: https://github.com/koajs/json-error +// Definitions by: Mudkip +// Definitions: https://github.com/mudkipme/DefinitelyTyped + +/// + +declare module "koa-json-error" { + + import * as Koa from "koa"; + + interface JSONErrorOptions { + /** + * Perform some task before calling `options.format`. Must be a function with the original err as its only argument. + */ + preFormat?: { (err: Error): any }; + + /** + * Runs inmediatly after `options.preFormat`. It receives two arguments: the original `err` and the output of `options.preFormat`. It should `return` a newly formatted error. + */ + format?: { (err: Error, obj: any): any }; + + /** + * Runs inmediatly after `options.format`. It receives two arguments: the original `err` and the output of `options.format`. It should `return` a newly formatted error. + */ + postFormat?: { (err: Error, obj: any): any }; + } + + /** + * Error handler for pure Koa 2.0.0+ JSON apps + */ + function jsonError(options?: JSONErrorOptions) : { (ctx: Koa.Context, next?: () => any): any }; + + namespace jsonError {} + export = jsonError; +} \ No newline at end of file