From 80b8a358482d159f8feba62dab86c3c75a2bf713 Mon Sep 17 00:00:00 2001 From: maruware Date: Mon, 19 Nov 2018 13:51:45 +0900 Subject: [PATCH] Add @types/koa-bouncer support (#30453) --- types/koa-bouncer/index.d.ts | 91 ++++++++++++++++++++++++++ types/koa-bouncer/koa-bouncer-tests.ts | 40 +++++++++++ types/koa-bouncer/tsconfig.json | 23 +++++++ types/koa-bouncer/tslint.json | 79 ++++++++++++++++++++++ 4 files changed, 233 insertions(+) create mode 100644 types/koa-bouncer/index.d.ts create mode 100644 types/koa-bouncer/koa-bouncer-tests.ts create mode 100644 types/koa-bouncer/tsconfig.json create mode 100644 types/koa-bouncer/tslint.json diff --git a/types/koa-bouncer/index.d.ts b/types/koa-bouncer/index.d.ts new file mode 100644 index 0000000000..7b881f2b5c --- /dev/null +++ b/types/koa-bouncer/index.d.ts @@ -0,0 +1,91 @@ +// Type definitions for koa-bouncer 6.0.4 +// Project: https://github.com/danneu/koa-bouncer +// Definitions by: maruware +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +import { Middleware, Context } from 'koa'; + +declare namespace KoaBouncer { + export class ValidatorError extends Error { + name: string; + message: string; + bouncer: { + key: string, + message: string + } + constructor(key: string, message: string); + } + + export class Validator { + constructor(props: {ctx: Context, key: string, vals: any}) + required(tip?: string): Validator + optional(): Validator + check(result: any, tip?: string): Validator + checkNot(result: any, tip?: string): Validator + checkPred(pred: any, tip?: string): Validator + checkNotPred(pred: any, tip?: string): Validator + tap(f: (arg: any) => any, tip?: string): Validator + isIn(arr: any[], tip?: string): Validator + isNotIn(arr: any[], tip?: string): Validator + isArray(tip?: string): Validator + eq(otherVal: string, tip?: string): Validator + gt(otherVal: string, tip?: string): Validator + gte(otherVal: string, tip?: string): Validator + lt(otherVal: string, tip?: string): Validator + lte(otherVal: string, tip?: string): Validator + isLength(min: number, max: number, tip?: string): Validator + defaultTo(valueOrFunction: any): Validator + isString(tip?: string): Validator + isInt(tip?: string): Validator + toInt(tip?: string): Validator + isFiniteNumber(tip?: string): Validator + toArray(): Validator + toInts(tip?: string): Validator + uniq(): Validator + toBoolean(): Validator + toDecimal(tip?: string): Validator + toFloat(tip?: string): Validator + toFiniteFloat(): Validator + toString(): Validator + trim(): Validator + match(regexp: RegExp, tip?: string): Validator + notMatch(regexp: RegExp, tip?: string): Validator + fromJson(tip?: string): Validator + isAlpha(tip?: string): Validator + isAlphanumeric(tip?: string): Validator + isNumeric(tip?: string): Validator + isAscii(tip?: string): Validator + isBase64(tip?: string): Validator + isEmail(tip?: string): Validator + isHexColor(tip?: string): Validator + isUuid(tip?: string): Validator + isJson(tip?: string): Validator + encodeBase64(tip?: string): Validator + clamp(min: number, max: number): Validator + + static addMethod(name: string, fn: Function): void; + } + + interface MiddlewareOption { + getParams?: (ctx: Context) => any; + getQuery?: (ctx: Context) => any; + getBody?: (ctx: Context) => any; + } + + export function middleware(opts?: MiddlewareOption): Middleware; + export function isSafeInteger(n: number): boolean; +} + +declare module "koa" { + interface Context { + vals: any; + validateBody: (key: string) => KoaBouncer.Validator; + validateQuery: (key: string) => KoaBouncer.Validator; + validateParam: (key: string) => KoaBouncer.Validator; + check: (result: any, tip?: string) => void; + checkNot: (result: any, tip?: string) => KoaBouncer.Validator; + } +} + +export = KoaBouncer; \ No newline at end of file diff --git a/types/koa-bouncer/koa-bouncer-tests.ts b/types/koa-bouncer/koa-bouncer-tests.ts new file mode 100644 index 0000000000..0f91658a85 --- /dev/null +++ b/types/koa-bouncer/koa-bouncer-tests.ts @@ -0,0 +1,40 @@ +import Koa = require("koa"); +import Router = require("koa-router"); +import bouncer = require("koa-bouncer"); + +const app = new Koa(); + +app.use(bouncer.middleware()); +const router = new Router() + +router.post('/users', async (ctx) => { + ctx.validateBody('uname') + .required('Username required') + .isString() + .trim() + + ctx.validateBody('email') + .optional() + .isString() + .trim() + .isEmail('Invalid email format') + + ctx.validateBody('password1') + .required('Password required') + .isString() + .isLength(6, 100, 'Password must be 6-100 chars') + + ctx.validateBody('password2') + .required('Password confirmation required') + .isString() + .eq(ctx.vals.password1, 'Passwords must match') + + console.log(ctx.vals) +}) + +app + .use(router.routes()) + .use(router.allowedMethods()); + + +app.listen(80); diff --git a/types/koa-bouncer/tsconfig.json b/types/koa-bouncer/tsconfig.json new file mode 100644 index 0000000000..d789d6411a --- /dev/null +++ b/types/koa-bouncer/tsconfig.json @@ -0,0 +1,23 @@ +{ + "files": [ + "index.d.ts", + "koa-bouncer-tests.ts" + ], + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + } +} \ No newline at end of file diff --git a/types/koa-bouncer/tslint.json b/types/koa-bouncer/tslint.json new file mode 100644 index 0000000000..a41bf5d19a --- /dev/null +++ b/types/koa-bouncer/tslint.json @@ -0,0 +1,79 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "adjacent-overload-signatures": false, + "array-type": false, + "arrow-return-shorthand": false, + "ban-types": false, + "callable-types": false, + "comment-format": false, + "dt-header": false, + "eofline": false, + "export-just-namespace": false, + "import-spacing": false, + "interface-name": false, + "interface-over-type-literal": false, + "jsdoc-format": false, + "max-line-length": false, + "member-access": false, + "new-parens": false, + "no-any-union": false, + "no-boolean-literal-compare": false, + "no-conditional-assignment": false, + "no-consecutive-blank-lines": false, + "no-construct": false, + "no-declare-current-package": false, + "no-duplicate-imports": false, + "no-duplicate-variable": false, + "no-empty-interface": false, + "no-for-in-array": false, + "no-inferrable-types": false, + "no-internal-module": false, + "no-irregular-whitespace": false, + "no-mergeable-namespace": false, + "no-misused-new": false, + "no-namespace": false, + "no-object-literal-type-assertion": false, + "no-padding": false, + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": false, + "no-redundant-undefined": false, + "no-reference-import": false, + "no-relative-import-in-test": false, + "no-self-import": false, + "no-single-declare-module": false, + "no-string-throw": false, + "no-unnecessary-callback-wrapper": false, + "no-unnecessary-class": false, + "no-unnecessary-generics": false, + "no-unnecessary-qualifier": false, + "no-unnecessary-type-assertion": false, + "no-useless-files": false, + "no-var-keyword": false, + "no-var-requires": false, + "no-void-expression": false, + "no-trailing-whitespace": false, + "object-literal-key-quotes": false, + "object-literal-shorthand": false, + "one-line": false, + "one-variable-per-declaration": false, + "only-arrow-functions": false, + "prefer-conditional-expression": false, + "prefer-const": false, + "prefer-declare-function": false, + "prefer-for-of": false, + "prefer-method-signature": false, + "prefer-template": false, + "radix": false, + "semicolon": false, + "space-before-function-paren": false, + "space-within-parens": false, + "strict-export-declare-modifiers": false, + "trim-file": false, + "triple-equals": false, + "typedef-whitespace": false, + "unified-signatures": false, + "void-return": false, + "whitespace": false + } +}