mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-12 04:50:18 +00:00
Add @types/koa-bouncer support (#30453)
This commit is contained in:
committed by
Pranav Senthilnathan
parent
37c5d42f4c
commit
80b8a35848
Vendored
+91
@@ -0,0 +1,91 @@
|
||||
// Type definitions for koa-bouncer 6.0.4
|
||||
// Project: https://github.com/danneu/koa-bouncer
|
||||
// Definitions by: maruware <https://github.com/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;
|
||||
@@ -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);
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user