hono 2.1.4 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -14
- package/dist/cjs/compose.js +48 -0
- package/dist/cjs/context.js +148 -0
- package/dist/cjs/hono.js +154 -0
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/middleware/basic-auth/index.js +48 -0
- package/dist/cjs/middleware/bearer-auth/index.js +57 -0
- package/dist/cjs/middleware/cache/index.js +32 -0
- package/dist/cjs/middleware/compress/index.js +19 -0
- package/dist/cjs/middleware/cors/index.js +75 -0
- package/dist/cjs/middleware/etag/index.js +27 -0
- package/dist/cjs/middleware/html/index.js +36 -0
- package/dist/cjs/middleware/jsx/index.js +193 -0
- package/dist/cjs/middleware/jsx/jsx-dev-runtime.js +10 -0
- package/dist/cjs/middleware/jsx/jsx-runtime.js +7 -0
- package/dist/cjs/middleware/jwt/index.js +63 -0
- package/dist/cjs/middleware/logger/index.js +49 -0
- package/dist/cjs/middleware/powered-by/index.js +10 -0
- package/dist/cjs/middleware/pretty-json/index.js +11 -0
- package/dist/cjs/middleware/serve-static/bun.js +41 -0
- package/dist/cjs/middleware/serve-static/index.js +5 -0
- package/dist/cjs/middleware/serve-static/serve-static.js +40 -0
- package/dist/cjs/middleware/validator/index.js +5 -0
- package/dist/cjs/middleware/validator/middleware.js +56 -0
- package/dist/cjs/middleware/validator/rule.js +66 -0
- package/dist/cjs/middleware/validator/sanitizer.js +6 -0
- package/dist/cjs/middleware/validator/validator.js +195 -0
- package/dist/cjs/request.js +120 -0
- package/dist/cjs/router/reg-exp-router/index.js +5 -0
- package/dist/cjs/router/reg-exp-router/node.js +108 -0
- package/dist/cjs/router/reg-exp-router/router.js +161 -0
- package/dist/cjs/router/reg-exp-router/trie.js +42 -0
- package/dist/cjs/router/smart-router/index.js +5 -0
- package/dist/cjs/router/smart-router/router.js +57 -0
- package/dist/cjs/router/static-router/index.js +5 -0
- package/dist/cjs/router/static-router/router.js +72 -0
- package/dist/cjs/router/trie-router/index.js +5 -0
- package/dist/cjs/router/trie-router/node.js +175 -0
- package/dist/cjs/router/trie-router/router.js +24 -0
- package/dist/cjs/router.js +9 -0
- package/dist/cjs/utils/body.js +18 -0
- package/dist/cjs/utils/buffer.js +39 -0
- package/dist/cjs/utils/cloudflare.js +39 -0
- package/dist/cjs/utils/cookie.js +40 -0
- package/dist/cjs/utils/crypto.js +53 -0
- package/dist/cjs/utils/encode.js +80 -0
- package/dist/cjs/utils/filepath.js +25 -0
- package/dist/cjs/utils/html.js +38 -0
- package/dist/cjs/utils/http-status.js +50 -0
- package/dist/cjs/utils/json.js +22 -0
- package/dist/cjs/utils/jwt/index.js +27 -0
- package/dist/cjs/utils/jwt/jwt.js +101 -0
- package/dist/cjs/utils/jwt/types.js +49 -0
- package/dist/cjs/utils/mime.js +92 -0
- package/dist/cjs/utils/url.js +94 -0
- package/dist/compose.d.ts +2 -2
- package/dist/compose.js +3 -7
- package/dist/context.d.ts +14 -9
- package/dist/context.js +56 -25
- package/dist/hono.d.ts +28 -26
- package/dist/hono.js +23 -22
- package/dist/index.js +3 -6
- package/dist/middleware/basic-auth/index.d.ts +2 -3
- package/dist/middleware/basic-auth/index.js +7 -11
- package/dist/middleware/bearer-auth/index.d.ts +2 -3
- package/dist/middleware/bearer-auth/index.js +4 -12
- package/dist/middleware/cache/index.d.ts +2 -3
- package/dist/middleware/cache/index.js +1 -5
- package/dist/middleware/compress/index.d.ts +2 -3
- package/dist/middleware/compress/index.js +1 -5
- package/dist/middleware/cors/index.d.ts +3 -4
- package/dist/middleware/cors/index.js +16 -6
- package/dist/middleware/etag/index.d.ts +2 -3
- package/dist/middleware/etag/index.js +3 -7
- package/dist/middleware/html/index.js +6 -11
- package/dist/middleware/jsx/index.js +9 -15
- package/dist/middleware/jsx/jsx-dev-runtime.js +3 -7
- package/dist/middleware/jsx/jsx-runtime.js +2 -7
- package/dist/middleware/jwt/index.d.ts +2 -3
- package/dist/middleware/jwt/index.js +3 -7
- package/dist/middleware/logger/index.d.ts +2 -3
- package/dist/middleware/logger/index.js +3 -7
- package/dist/middleware/powered-by/index.d.ts +2 -3
- package/dist/middleware/powered-by/index.js +1 -5
- package/dist/middleware/pretty-json/index.d.ts +2 -3
- package/dist/middleware/pretty-json/index.js +1 -5
- package/dist/middleware/serve-static/bun.d.ts +2 -3
- package/dist/middleware/serve-static/bun.js +18 -19
- package/dist/middleware/serve-static/index.js +1 -5
- package/dist/middleware/serve-static/module.d.mts +1 -1
- package/dist/middleware/serve-static/serve-static.d.ts +2 -3
- package/dist/middleware/serve-static/serve-static.js +7 -11
- package/dist/middleware/validator/index.d.ts +2 -0
- package/dist/middleware/validator/index.js +2 -0
- package/dist/middleware/validator/middleware.d.ts +21 -0
- package/dist/middleware/validator/middleware.js +52 -0
- package/dist/middleware/validator/rule.d.ts +21 -0
- package/dist/middleware/validator/rule.js +63 -0
- package/dist/middleware/validator/sanitizer.d.ts +3 -0
- package/dist/middleware/validator/sanitizer.js +3 -0
- package/dist/middleware/validator/validator.d.ts +75 -0
- package/dist/middleware/validator/validator.js +186 -0
- package/dist/request.d.ts +15 -5
- package/dist/request.js +58 -28
- package/dist/router/reg-exp-router/index.js +1 -5
- package/dist/router/reg-exp-router/node.d.ts +1 -3
- package/dist/router/reg-exp-router/node.js +21 -17
- package/dist/router/reg-exp-router/router.d.ts +3 -27
- package/dist/router/reg-exp-router/router.js +105 -315
- package/dist/router/reg-exp-router/trie.d.ts +0 -4
- package/dist/router/reg-exp-router/trie.js +4 -8
- package/dist/router/smart-router/index.d.ts +1 -0
- package/dist/router/smart-router/index.js +1 -0
- package/dist/router/smart-router/router.d.ts +9 -0
- package/dist/router/smart-router/router.js +53 -0
- package/dist/router/static-router/index.d.ts +1 -0
- package/dist/router/static-router/index.js +1 -0
- package/dist/router/static-router/router.d.ts +8 -0
- package/dist/router/static-router/router.js +68 -0
- package/dist/router/trie-router/index.js +1 -5
- package/dist/router/trie-router/node.js +7 -11
- package/dist/router/trie-router/router.js +11 -7
- package/dist/router.d.ts +3 -0
- package/dist/router.js +5 -5
- package/dist/utils/body.d.ts +2 -1
- package/dist/utils/body.js +1 -5
- package/dist/utils/buffer.d.ts +1 -1
- package/dist/utils/buffer.js +5 -11
- package/dist/utils/cloudflare.d.ts +1 -1
- package/dist/utils/cloudflare.js +1 -5
- package/dist/utils/cookie.js +2 -7
- package/dist/utils/crypto.js +8 -15
- package/dist/utils/encode.js +10 -20
- package/dist/utils/filepath.js +1 -5
- package/dist/utils/html.js +1 -5
- package/dist/utils/http-status.js +1 -5
- package/dist/utils/json.d.ts +1 -0
- package/dist/utils/json.js +18 -0
- package/dist/utils/jwt/index.js +1 -27
- package/dist/utils/jwt/jwt.js +22 -28
- package/dist/utils/jwt/types.js +8 -16
- package/dist/utils/mime.js +1 -5
- package/dist/utils/url.d.ts +1 -1
- package/dist/utils/url.js +18 -22
- package/package.json +122 -35
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.jwt = void 0;
|
|
4
|
-
const jwt_1 = require("../../utils/jwt");
|
|
5
|
-
const jwt = (options) => {
|
|
1
|
+
import { Jwt } from '../../utils/jwt';
|
|
2
|
+
export const jwt = (options) => {
|
|
6
3
|
if (!options) {
|
|
7
4
|
throw new Error('JWT auth middleware requires options for "secret');
|
|
8
5
|
}
|
|
@@ -42,7 +39,7 @@ const jwt = (options) => {
|
|
|
42
39
|
let authorized = false;
|
|
43
40
|
let msg = '';
|
|
44
41
|
try {
|
|
45
|
-
authorized = await
|
|
42
|
+
authorized = await Jwt.verify(token, options.secret, options.alg);
|
|
46
43
|
}
|
|
47
44
|
catch (e) {
|
|
48
45
|
msg = `${e}`;
|
|
@@ -60,4 +57,3 @@ const jwt = (options) => {
|
|
|
60
57
|
await next();
|
|
61
58
|
};
|
|
62
59
|
};
|
|
63
|
-
exports.jwt = jwt;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Next } from '../../hono';
|
|
1
|
+
import type { MiddlewareHandler } from '../../hono';
|
|
3
2
|
declare type PrintFunc = (str: string, ...rest: string[]) => void;
|
|
4
|
-
export declare const logger: (fn?: PrintFunc) =>
|
|
3
|
+
export declare const logger: (fn?: PrintFunc) => MiddlewareHandler;
|
|
5
4
|
export {};
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.logger = void 0;
|
|
4
|
-
const url_1 = require("../../utils/url");
|
|
1
|
+
import { getPathFromURL } from '../../utils/url';
|
|
5
2
|
var LogPrefix;
|
|
6
3
|
(function (LogPrefix) {
|
|
7
4
|
LogPrefix["Outgoing"] = "-->";
|
|
@@ -36,14 +33,13 @@ function log(fn, prefix, method, path, status = 0, elapsed) {
|
|
|
36
33
|
: ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}`;
|
|
37
34
|
fn(out);
|
|
38
35
|
}
|
|
39
|
-
const logger = (fn = console.log) => {
|
|
36
|
+
export const logger = (fn = console.log) => {
|
|
40
37
|
return async (c, next) => {
|
|
41
38
|
const { method } = c.req;
|
|
42
|
-
const path =
|
|
39
|
+
const path = getPathFromURL(c.req.url);
|
|
43
40
|
log(fn, LogPrefix.Incoming, method, path);
|
|
44
41
|
const start = Date.now();
|
|
45
42
|
await next();
|
|
46
43
|
log(fn, LogPrefix.Outgoing, method, path, c.res.status, time(start));
|
|
47
44
|
};
|
|
48
45
|
};
|
|
49
|
-
exports.logger = logger;
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export declare const poweredBy: () => (c: Context, next: Next) => Promise<void>;
|
|
1
|
+
import type { MiddlewareHandler } from '../../hono';
|
|
2
|
+
export declare const poweredBy: () => MiddlewareHandler;
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.poweredBy = void 0;
|
|
4
|
-
const poweredBy = () => {
|
|
1
|
+
export const poweredBy = () => {
|
|
5
2
|
return async (c, next) => {
|
|
6
3
|
await next();
|
|
7
4
|
c.res.headers.append('X-Powered-By', 'Hono');
|
|
8
5
|
};
|
|
9
6
|
};
|
|
10
|
-
exports.poweredBy = poweredBy;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Next } from '../../hono';
|
|
1
|
+
import type { MiddlewareHandler } from '../../hono';
|
|
3
2
|
declare type prettyOptions = {
|
|
4
3
|
space: number;
|
|
5
4
|
};
|
|
6
|
-
export declare const prettyJSON: (options?: prettyOptions) =>
|
|
5
|
+
export declare const prettyJSON: (options?: prettyOptions) => MiddlewareHandler;
|
|
7
6
|
export {};
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.prettyJSON = void 0;
|
|
4
|
-
const prettyJSON = (options = { space: 2 }) => {
|
|
1
|
+
export const prettyJSON = (options = { space: 2 }) => {
|
|
5
2
|
return async (c, next) => {
|
|
6
3
|
const pretty = c.req.query('pretty') || c.req.query('pretty') === '' ? true : false;
|
|
7
4
|
c.pretty(pretty, options.space);
|
|
8
5
|
await next();
|
|
9
6
|
};
|
|
10
7
|
};
|
|
11
|
-
exports.prettyJSON = prettyJSON;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Next } from '../../hono';
|
|
1
|
+
import type { MiddlewareHandler } from '../../hono';
|
|
3
2
|
export declare type ServeStaticOptions = {
|
|
4
3
|
root?: string;
|
|
5
4
|
path?: string;
|
|
6
5
|
};
|
|
7
|
-
export declare const serveStatic: (options?: ServeStaticOptions) =>
|
|
6
|
+
export declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler;
|
|
@@ -1,38 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
// @denoify-ignore
|
|
2
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
3
|
+
import { existsSync } from 'fs';
|
|
4
|
+
import { getFilePath } from '../../utils/filepath';
|
|
5
|
+
import { getMimeType } from '../../utils/mime';
|
|
6
6
|
// @ts-ignore
|
|
7
7
|
const { file } = Bun;
|
|
8
8
|
const DEFAULT_DOCUMENT = 'index.html';
|
|
9
|
-
const serveStatic = (options = { root: '' }) => {
|
|
9
|
+
export const serveStatic = (options = { root: '' }) => {
|
|
10
10
|
return async (c, next) => {
|
|
11
11
|
// Do nothing if Response is already set
|
|
12
12
|
if (c.res && c.finalized) {
|
|
13
13
|
await next();
|
|
14
14
|
}
|
|
15
15
|
const url = new URL(c.req.url);
|
|
16
|
-
let path =
|
|
16
|
+
let path = getFilePath({
|
|
17
17
|
filename: options.path ?? url.pathname,
|
|
18
18
|
root: options.root,
|
|
19
19
|
defaultDocument: DEFAULT_DOCUMENT,
|
|
20
20
|
});
|
|
21
21
|
path = `./${path}`;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
if (existsSync(path)) {
|
|
23
|
+
const content = file(path);
|
|
24
|
+
if (content) {
|
|
25
|
+
const mimeType = getMimeType(path);
|
|
26
|
+
if (mimeType) {
|
|
27
|
+
c.header('Content-Type', mimeType);
|
|
28
|
+
}
|
|
29
|
+
// Return Response object
|
|
30
|
+
return c.body(content);
|
|
27
31
|
}
|
|
28
|
-
// Return Response object
|
|
29
|
-
return c.body(content);
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
console.warn(`Static file: ${path} is not found`);
|
|
33
|
-
await next();
|
|
34
32
|
}
|
|
33
|
+
console.warn(`Static file: ${path} is not found`);
|
|
34
|
+
await next();
|
|
35
35
|
return;
|
|
36
36
|
};
|
|
37
37
|
};
|
|
38
|
-
exports.serveStatic = serveStatic;
|
|
@@ -1,5 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.serveStatic = void 0;
|
|
4
|
-
var serve_static_1 = require("./serve-static");
|
|
5
|
-
Object.defineProperty(exports, "serveStatic", { enumerable: true, get: function () { return serve_static_1.serveStatic; } });
|
|
1
|
+
export { serveStatic } from './serve-static';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { ServeStaticOptions } from './serve-static';
|
|
2
|
-
declare const module: (options?: ServeStaticOptions) =>
|
|
2
|
+
declare const module: (options?: ServeStaticOptions) => import("../../hono").MiddlewareHandler;
|
|
3
3
|
export { module as serveStatic };
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import type {
|
|
3
|
-
import type { Next } from '../../hono';
|
|
2
|
+
import type { MiddlewareHandler } from '../../hono';
|
|
4
3
|
export declare type ServeStaticOptions = {
|
|
5
4
|
root?: string;
|
|
6
5
|
path?: string;
|
|
7
6
|
manifest?: object | string;
|
|
8
7
|
namespace?: KVNamespace;
|
|
9
8
|
};
|
|
10
|
-
export declare const serveStatic: (options?: ServeStaticOptions) =>
|
|
9
|
+
export declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler;
|
|
@@ -1,29 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const cloudflare_1 = require("../../utils/cloudflare");
|
|
5
|
-
const filepath_1 = require("../../utils/filepath");
|
|
6
|
-
const mime_1 = require("../../utils/mime");
|
|
1
|
+
import { getContentFromKVAsset } from '../../utils/cloudflare';
|
|
2
|
+
import { getFilePath } from '../../utils/filepath';
|
|
3
|
+
import { getMimeType } from '../../utils/mime';
|
|
7
4
|
const DEFAULT_DOCUMENT = 'index.html';
|
|
8
5
|
// This middleware is available only on Cloudflare Workers.
|
|
9
|
-
const serveStatic = (options = { root: '' }) => {
|
|
6
|
+
export const serveStatic = (options = { root: '' }) => {
|
|
10
7
|
return async (c, next) => {
|
|
11
8
|
// Do nothing if Response is already set
|
|
12
9
|
if (c.finalized) {
|
|
13
10
|
await next();
|
|
14
11
|
}
|
|
15
12
|
const url = new URL(c.req.url);
|
|
16
|
-
const path =
|
|
13
|
+
const path = getFilePath({
|
|
17
14
|
filename: options.path ?? url.pathname,
|
|
18
15
|
root: options.root,
|
|
19
16
|
defaultDocument: DEFAULT_DOCUMENT,
|
|
20
17
|
});
|
|
21
|
-
const content = await
|
|
18
|
+
const content = await getContentFromKVAsset(path, {
|
|
22
19
|
manifest: options.manifest,
|
|
23
20
|
namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : undefined,
|
|
24
21
|
});
|
|
25
22
|
if (content) {
|
|
26
|
-
const mimeType =
|
|
23
|
+
const mimeType = getMimeType(path);
|
|
27
24
|
if (mimeType) {
|
|
28
25
|
c.header('Content-Type', mimeType);
|
|
29
26
|
}
|
|
@@ -37,4 +34,3 @@ const serveStatic = (options = { root: '' }) => {
|
|
|
37
34
|
return;
|
|
38
35
|
};
|
|
39
36
|
};
|
|
40
|
-
exports.serveStatic = serveStatic;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Context } from '../../context';
|
|
2
|
+
import type { Environment, Handler } from '../../hono';
|
|
3
|
+
import { Validator } from './validator';
|
|
4
|
+
import type { VString, VNumber, VBoolean, VObject, ValidateResult } from './validator';
|
|
5
|
+
declare type ValidationFunction<T> = (v: Validator, c: Context) => T;
|
|
6
|
+
declare type Schema = {
|
|
7
|
+
[key: string]: VString | VNumber | VBoolean | VObject | Schema;
|
|
8
|
+
};
|
|
9
|
+
declare type SchemaToProp<T> = {
|
|
10
|
+
[K in keyof T]: T[K] extends VNumber ? number : T[K] extends VBoolean ? boolean : T[K] extends VString ? string : T[K] extends VObject ? object : T[K] extends Schema ? SchemaToProp<T[K]> : never;
|
|
11
|
+
};
|
|
12
|
+
declare type ResultSet = {
|
|
13
|
+
hasError: boolean;
|
|
14
|
+
messages: string[];
|
|
15
|
+
results: ValidateResult[];
|
|
16
|
+
};
|
|
17
|
+
declare type Done = (resultSet: ResultSet, context: Context) => Response | void;
|
|
18
|
+
export declare const validatorMiddleware: <T extends Schema>(validationFunction: ValidationFunction<T>, options?: {
|
|
19
|
+
done?: Done;
|
|
20
|
+
}) => Handler<string, Environment, SchemaToProp<T>>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { VBase, Validator } from './validator';
|
|
2
|
+
function getValidatorList(schema) {
|
|
3
|
+
const map = [];
|
|
4
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
5
|
+
if (value instanceof VBase) {
|
|
6
|
+
map.push([[key], value]);
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
const children = getValidatorList(value);
|
|
10
|
+
for (const [keys, validator] of children) {
|
|
11
|
+
map.push([[key, ...keys], validator]);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return map;
|
|
16
|
+
}
|
|
17
|
+
export const validatorMiddleware = (validationFunction, options) => {
|
|
18
|
+
const v = new Validator();
|
|
19
|
+
const handler = async (c, next) => {
|
|
20
|
+
const resultSet = {
|
|
21
|
+
hasError: false,
|
|
22
|
+
messages: [],
|
|
23
|
+
results: [],
|
|
24
|
+
};
|
|
25
|
+
const validatorList = getValidatorList(validationFunction(v, c));
|
|
26
|
+
for (const [keys, validator] of validatorList) {
|
|
27
|
+
const result = await validator.validate(c.req);
|
|
28
|
+
if (result.isValid) {
|
|
29
|
+
// Set data on request object
|
|
30
|
+
c.req.valid(keys, result.value);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
resultSet.hasError = true;
|
|
34
|
+
if (result.message !== undefined) {
|
|
35
|
+
resultSet.messages.push(result.message);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
resultSet.results.push(result);
|
|
39
|
+
}
|
|
40
|
+
if (options && options.done) {
|
|
41
|
+
const res = options.done(resultSet, c);
|
|
42
|
+
if (res) {
|
|
43
|
+
return res;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (resultSet.hasError) {
|
|
47
|
+
return c.text(resultSet.messages.join('\n'), 400);
|
|
48
|
+
}
|
|
49
|
+
await next();
|
|
50
|
+
};
|
|
51
|
+
return handler;
|
|
52
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const rule: {
|
|
2
|
+
isEmpty(value: string, options?: {
|
|
3
|
+
ignore_whitespace: boolean;
|
|
4
|
+
}): boolean;
|
|
5
|
+
isLength: (value: string, options: Partial<{
|
|
6
|
+
min: number;
|
|
7
|
+
max: number;
|
|
8
|
+
}> | number, arg2?: number) => boolean;
|
|
9
|
+
isAlpha: (value: string) => boolean;
|
|
10
|
+
isNumeric: (value: string) => boolean;
|
|
11
|
+
contains: (value: string, elem: string, options?: Partial<{
|
|
12
|
+
ignoreCase: boolean;
|
|
13
|
+
minOccurrences: number;
|
|
14
|
+
}>) => boolean;
|
|
15
|
+
isIn: (value: string, options: string[]) => boolean;
|
|
16
|
+
match: (value: string, regExp: RegExp) => boolean;
|
|
17
|
+
isGte: (value: number, min: number) => boolean;
|
|
18
|
+
isLte: (value: number, max: number) => boolean;
|
|
19
|
+
isTrue: (value: boolean) => boolean;
|
|
20
|
+
isFalse: (value: boolean) => boolean;
|
|
21
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Some validation rules is are based on Validator.js
|
|
2
|
+
// Validator.js
|
|
3
|
+
// License (MIT)
|
|
4
|
+
// Copyright (c) 2018 Chris O'Hara <cohara87@gmail.com>
|
|
5
|
+
// https://github.com/validatorjs/validator.js
|
|
6
|
+
export const rule = {
|
|
7
|
+
// string
|
|
8
|
+
isEmpty(value, options = { ignore_whitespace: false }) {
|
|
9
|
+
return (options.ignore_whitespace ? value.trim().length : value.length) === 0;
|
|
10
|
+
},
|
|
11
|
+
isLength: (value, options, arg2) => {
|
|
12
|
+
let min;
|
|
13
|
+
let max;
|
|
14
|
+
if (typeof options === 'object') {
|
|
15
|
+
min = options.min || 0;
|
|
16
|
+
max = options.max;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
// backwards compatibility: isLength(str, min [, max])
|
|
20
|
+
min = options || 0;
|
|
21
|
+
max = arg2;
|
|
22
|
+
}
|
|
23
|
+
const presentationSequences = value.match(/(\uFE0F|\uFE0E)/g) || [];
|
|
24
|
+
const surrogatePairs = value.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
|
|
25
|
+
const len = value.length - presentationSequences.length - surrogatePairs.length;
|
|
26
|
+
return len >= min && (typeof max === 'undefined' || len <= max);
|
|
27
|
+
},
|
|
28
|
+
isAlpha: (value) => {
|
|
29
|
+
return /^[A-Z]+$/i.test(value);
|
|
30
|
+
},
|
|
31
|
+
isNumeric: (value) => {
|
|
32
|
+
return /^[0-9]+$/.test(value);
|
|
33
|
+
},
|
|
34
|
+
contains: (value, elem, options = {
|
|
35
|
+
ignoreCase: false,
|
|
36
|
+
minOccurrences: 1,
|
|
37
|
+
}) => {
|
|
38
|
+
options.ignoreCase || (options.ignoreCase = false);
|
|
39
|
+
options.minOccurrences || (options.minOccurrences = 1);
|
|
40
|
+
if (options.ignoreCase) {
|
|
41
|
+
return value.toLowerCase().split(elem.toLowerCase()).length > options.minOccurrences;
|
|
42
|
+
}
|
|
43
|
+
return value.split(elem).length > options.minOccurrences;
|
|
44
|
+
},
|
|
45
|
+
isIn: (value, options) => {
|
|
46
|
+
if (typeof options === 'object') {
|
|
47
|
+
for (const elem of options) {
|
|
48
|
+
if (elem === value)
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
},
|
|
54
|
+
match: (value, regExp) => {
|
|
55
|
+
return regExp.test(value);
|
|
56
|
+
},
|
|
57
|
+
// number
|
|
58
|
+
isGte: (value, min) => min <= value,
|
|
59
|
+
isLte: (value, max) => value <= max,
|
|
60
|
+
// boolean
|
|
61
|
+
isTrue: (value) => value === true,
|
|
62
|
+
isFalse: (value) => value === false,
|
|
63
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
declare type Target = 'query' | 'header' | 'body' | 'json';
|
|
2
|
+
declare type Type = string | number | boolean | object | undefined;
|
|
3
|
+
declare type Rule = (value: Type) => boolean;
|
|
4
|
+
declare type Sanitizer = (value: Type) => Type;
|
|
5
|
+
export declare class Validator {
|
|
6
|
+
query: (key: string) => VString;
|
|
7
|
+
header: (key: string) => VString;
|
|
8
|
+
body: (key: string) => VString;
|
|
9
|
+
json: (key: string) => VString;
|
|
10
|
+
}
|
|
11
|
+
export declare type ValidateResult = {
|
|
12
|
+
isValid: boolean;
|
|
13
|
+
message: string | undefined;
|
|
14
|
+
target: Target;
|
|
15
|
+
key: string;
|
|
16
|
+
value: Type;
|
|
17
|
+
};
|
|
18
|
+
declare type VOptions = {
|
|
19
|
+
target: Target;
|
|
20
|
+
key: string;
|
|
21
|
+
type?: 'string' | 'number' | 'boolean' | 'object';
|
|
22
|
+
};
|
|
23
|
+
export declare abstract class VBase {
|
|
24
|
+
type: 'string' | 'number' | 'boolean' | 'object';
|
|
25
|
+
target: Target;
|
|
26
|
+
key: string;
|
|
27
|
+
rules: Rule[];
|
|
28
|
+
sanitizers: Sanitizer[];
|
|
29
|
+
private _message;
|
|
30
|
+
constructor(options: VOptions);
|
|
31
|
+
addRule: (rule: Rule) => this;
|
|
32
|
+
addSanitizer: (sanitizer: Sanitizer) => this;
|
|
33
|
+
isRequired: () => this;
|
|
34
|
+
isOptional: () => this;
|
|
35
|
+
isEqual: (comparison: unknown) => this;
|
|
36
|
+
asNumber: () => VNumber;
|
|
37
|
+
asBoolean: () => VBoolean;
|
|
38
|
+
asObject: () => VObject;
|
|
39
|
+
message(value: string): this;
|
|
40
|
+
validate: (req: Request) => Promise<ValidateResult>;
|
|
41
|
+
private validateValue;
|
|
42
|
+
}
|
|
43
|
+
export declare class VString extends VBase {
|
|
44
|
+
constructor(options: VOptions);
|
|
45
|
+
isEmpty: (options?: {
|
|
46
|
+
ignore_whitespace: boolean;
|
|
47
|
+
}) => this;
|
|
48
|
+
isLength: (options: Partial<{
|
|
49
|
+
min: number;
|
|
50
|
+
max: number;
|
|
51
|
+
}> | number, arg2?: number) => this;
|
|
52
|
+
isAlpha: () => this;
|
|
53
|
+
isNumeric: () => this;
|
|
54
|
+
contains: (elem: string, options?: Partial<{
|
|
55
|
+
ignoreCase: boolean;
|
|
56
|
+
minOccurrences: number;
|
|
57
|
+
}>) => this;
|
|
58
|
+
isIn: (options: string[]) => this;
|
|
59
|
+
match: (regExp: RegExp) => this;
|
|
60
|
+
trim: () => this;
|
|
61
|
+
}
|
|
62
|
+
export declare class VNumber extends VBase {
|
|
63
|
+
constructor(options: VOptions);
|
|
64
|
+
isGte: (min: number) => this;
|
|
65
|
+
isLte: (min: number) => this;
|
|
66
|
+
}
|
|
67
|
+
export declare class VBoolean extends VBase {
|
|
68
|
+
constructor(options: VOptions);
|
|
69
|
+
isTrue: () => this;
|
|
70
|
+
isFalse: () => this;
|
|
71
|
+
}
|
|
72
|
+
export declare class VObject extends VBase {
|
|
73
|
+
constructor(options: VOptions);
|
|
74
|
+
}
|
|
75
|
+
export {};
|