@miqro/core 1.5.3 → 1.5.8
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 +88 -14
- package/dist/api-router.js +1 -0
- package/dist/api-router.js.map +1 -0
- package/dist/app.d.ts +1 -1
- package/dist/app.js +12 -8
- package/dist/app.js.map +1 -0
- package/dist/common/api-router-helpers.d.ts +7 -7
- package/dist/common/api-router-helpers.js +1 -0
- package/dist/common/api-router-helpers.js.map +1 -0
- package/dist/common/api-router-utils.d.ts +2 -0
- package/dist/common/api-router-utils.js +23 -2
- package/dist/common/api-router-utils.js.map +1 -0
- package/dist/common/config/config.js +1 -0
- package/dist/common/config/config.js.map +1 -0
- package/dist/common/config/featuretoggle.js +1 -0
- package/dist/common/config/featuretoggle.js.map +1 -0
- package/dist/common/config/index.js +1 -0
- package/dist/common/config/index.js.map +1 -0
- package/dist/common/config/loader.js +3 -3
- package/dist/common/config/loader.js.map +1 -0
- package/dist/common/context.js +2 -1
- package/dist/common/context.js.map +1 -0
- package/dist/common/error/badrequesterror.js +1 -0
- package/dist/common/error/badrequesterror.js.map +1 -0
- package/dist/common/error/configfilenotfound.js +1 -0
- package/dist/common/error/configfilenotfound.js.map +1 -0
- package/dist/common/error/forbiddenerror.js +1 -0
- package/dist/common/error/forbiddenerror.js.map +1 -0
- package/dist/common/error/index.js +1 -0
- package/dist/common/error/index.js.map +1 -0
- package/dist/common/error/notfound.js +1 -0
- package/dist/common/error/notfound.js.map +1 -0
- package/dist/common/error/notimplemented.js +1 -0
- package/dist/common/error/notimplemented.js.map +1 -0
- package/dist/common/error/unauthorizederror.js +1 -0
- package/dist/common/error/unauthorizederror.js.map +1 -0
- package/dist/common/helpers.js +1 -0
- package/dist/common/helpers.js.map +1 -0
- package/dist/common/index.d.ts +0 -1
- package/dist/common/index.js +1 -1
- package/dist/common/index.js.map +1 -0
- package/dist/common/logger/common.js +1 -0
- package/dist/common/logger/common.js.map +1 -0
- package/dist/common/logger/factory.js +1 -0
- package/dist/common/logger/factory.js.map +1 -0
- package/dist/common/logger/formatter.js +1 -0
- package/dist/common/logger/formatter.js.map +1 -0
- package/dist/common/logger/index.js +1 -0
- package/dist/common/logger/index.js.map +1 -0
- package/dist/common/logger/logger.js +1 -0
- package/dist/common/logger/logger.js.map +1 -0
- package/dist/common/logger/transport.js +1 -0
- package/dist/common/logger/transport.js.map +1 -0
- package/dist/common/router-helpers.js +1 -0
- package/dist/common/router-helpers.js.map +1 -0
- package/dist/common/router-utils.d.ts +9 -7
- package/dist/common/router-utils.js +38 -33
- package/dist/common/router-utils.js.map +1 -0
- package/dist/common/session/common.d.ts +2 -2
- package/dist/common/session/common.js +1 -0
- package/dist/common/session/common.js.map +1 -0
- package/dist/common/session/index.js +1 -0
- package/dist/common/session/index.js.map +1 -0
- package/dist/common/session/service.d.ts +1 -1
- package/dist/common/session/service.js +6 -5
- package/dist/common/session/service.js.map +1 -0
- package/dist/common/tokenize-match.js +1 -0
- package/dist/common/tokenize-match.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/buffer.js +2 -0
- package/dist/middleware/buffer.js.map +1 -0
- package/dist/middleware/cookie.js +2 -0
- package/dist/middleware/cookie.js.map +1 -0
- package/dist/middleware/cors.js +4 -3
- package/dist/middleware/cors.js.map +1 -0
- package/dist/middleware/form.js +4 -1
- package/dist/middleware/form.js.map +1 -0
- package/dist/middleware/group.js +1 -0
- package/dist/middleware/group.js.map +1 -0
- package/dist/middleware/index.js +1 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/json.js +2 -0
- package/dist/middleware/json.js.map +1 -0
- package/dist/middleware/logger.js +1 -0
- package/dist/middleware/logger.js.map +1 -0
- package/dist/middleware/parse.d.ts +6 -6
- package/dist/middleware/parse.js +14 -12
- package/dist/middleware/parse.js.map +1 -0
- package/dist/middleware/session.js +4 -2
- package/dist/middleware/session.js.map +1 -0
- package/dist/middleware/tag.js +1 -0
- package/dist/middleware/tag.js.map +1 -0
- package/dist/middleware/text.js +3 -2
- package/dist/middleware/text.js.map +1 -0
- package/dist/proxy.d.ts +17 -6
- package/dist/proxy.js +40 -31
- package/dist/proxy.js.map +1 -0
- package/dist/router.d.ts +3 -3
- package/dist/router.js +51 -8
- package/dist/router.js.map +1 -0
- package/dist/static.d.ts +11 -0
- package/dist/static.js +192 -0
- package/dist/static.js.map +1 -0
- package/package.json +9 -7
- package/dist/common/parser/common.d.ts +0 -69
- package/dist/common/parser/common.js +0 -2
- package/dist/common/parser/error.d.ts +0 -4
- package/dist/common/parser/error.js +0 -11
- package/dist/common/parser/get.d.ts +0 -3
- package/dist/common/parser/get.js +0 -43
- package/dist/common/parser/helpers.d.ts +0 -1
- package/dist/common/parser/helpers.js +0 -9
- package/dist/common/parser/index.d.ts +0 -4
- package/dist/common/parser/index.js +0 -16
- package/dist/common/parser/parser.d.ts +0 -35
- package/dist/common/parser/parser.js +0 -197
- package/dist/common/parser/types/any.d.ts +0 -2
- package/dist/common/parser/types/any.js +0 -10
- package/dist/common/parser/types/array.d.ts +0 -2
- package/dist/common/parser/types/array.js +0 -58
- package/dist/common/parser/types/boolean.d.ts +0 -2
- package/dist/common/parser/types/boolean.js +0 -11
- package/dist/common/parser/types/email.d.ts +0 -2
- package/dist/common/parser/types/email.js +0 -22
- package/dist/common/parser/types/enum.d.ts +0 -2
- package/dist/common/parser/types/enum.js +0 -25
- package/dist/common/parser/types/index.d.ts +0 -12
- package/dist/common/parser/types/index.js +0 -24
- package/dist/common/parser/types/multiple.d.ts +0 -2
- package/dist/common/parser/types/multiple.js +0 -32
- package/dist/common/parser/types/nested.d.ts +0 -2
- package/dist/common/parser/types/nested.js +0 -15
- package/dist/common/parser/types/number.d.ts +0 -2
- package/dist/common/parser/types/number.js +0 -28
- package/dist/common/parser/types/object.d.ts +0 -2
- package/dist/common/parser/types/object.js +0 -10
- package/dist/common/parser/types/regex.d.ts +0 -2
- package/dist/common/parser/types/regex.js +0 -15
- package/dist/common/parser/types/string.d.ts +0 -2
- package/dist/common/parser/types/string.js +0 -18
- package/dist/common/parser/types/url.d.ts +0 -2
- package/dist/common/parser/types/url.js +0 -34
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
export interface ParseOptions {
|
|
2
|
-
description?: string;
|
|
3
|
-
options: ParseOption[] | ParseOptionMap;
|
|
4
|
-
mode?: ParseOptionsMode;
|
|
5
|
-
ignoreUndefined?: boolean;
|
|
6
|
-
}
|
|
7
|
-
export interface SimpleMap<T> {
|
|
8
|
-
[key: string]: T;
|
|
9
|
-
}
|
|
10
|
-
export declare type ParseOptionsMode = "remove_extra" | "add_extra" | "no_extra";
|
|
11
|
-
export declare type SimpleTypes = string | boolean | number | Array<SimpleTypes> | SimpleMap<SimpleTypes>;
|
|
12
|
-
export declare type ParseOptionTypeWithOutOptions = "string" | "boolean" | "number" | "object" | "any" | "array" | string;
|
|
13
|
-
export declare type ParseOptionType = "regex" | "nested" | "enum" | "multiple" | ParseOptionTypeWithOutOptions;
|
|
14
|
-
export declare type ParseOptionMap = SimpleMap<NoNameParseOption | ParseOptionTypeWithOutOptions>;
|
|
15
|
-
interface ParseOptionsBase {
|
|
16
|
-
type: ParseOptionType;
|
|
17
|
-
options?: any;
|
|
18
|
-
regex?: string;
|
|
19
|
-
multipleOptions?: BasicParseOption[];
|
|
20
|
-
forceArray?: boolean;
|
|
21
|
-
allowNull?: boolean;
|
|
22
|
-
arrayType?: ParseOptionType;
|
|
23
|
-
arrayMinLength?: number;
|
|
24
|
-
arrayMaxLength?: number;
|
|
25
|
-
numberMax?: number;
|
|
26
|
-
numberMaxDecimals?: number;
|
|
27
|
-
numberMinDecimals?: number;
|
|
28
|
-
numberMin?: number;
|
|
29
|
-
stringMaxLength?: number;
|
|
30
|
-
stringMinLength?: number;
|
|
31
|
-
nestedOptions?: {
|
|
32
|
-
ignoreUndefined?: boolean;
|
|
33
|
-
options: ParseOption[] | ParseOptionMap;
|
|
34
|
-
mode?: ParseOptionsMode;
|
|
35
|
-
};
|
|
36
|
-
enumValues?: string[];
|
|
37
|
-
parseJSON?: boolean;
|
|
38
|
-
}
|
|
39
|
-
export interface BasicParseOption extends ParseOptionsBase {
|
|
40
|
-
description?: string;
|
|
41
|
-
required?: boolean;
|
|
42
|
-
}
|
|
43
|
-
export interface NoNameParseOption extends ParseOptionsBase {
|
|
44
|
-
defaultValue?: any;
|
|
45
|
-
description?: string;
|
|
46
|
-
required?: boolean;
|
|
47
|
-
}
|
|
48
|
-
export interface ParseOption extends ParseOptionsBase {
|
|
49
|
-
name: string;
|
|
50
|
-
description?: string;
|
|
51
|
-
defaultValue?: any;
|
|
52
|
-
required?: boolean;
|
|
53
|
-
}
|
|
54
|
-
export interface ParseValueArgs extends ParseOptionsBase {
|
|
55
|
-
name: string;
|
|
56
|
-
attrName: string;
|
|
57
|
-
value: any;
|
|
58
|
-
}
|
|
59
|
-
export interface ParseValueValidatorResponse {
|
|
60
|
-
isType: boolean;
|
|
61
|
-
parsedValue: any;
|
|
62
|
-
message?: string;
|
|
63
|
-
}
|
|
64
|
-
export declare type ParseValueValidator = (args: ParseValueArgs, parser: ParserInterface) => ParseValueValidatorResponse;
|
|
65
|
-
export interface ParserInterface {
|
|
66
|
-
parseValue(args: ParseValueArgs): ParseValueValidatorResponse;
|
|
67
|
-
parse(name: string, arg: any, options: ParseOption[] | ParseOptionMap, mode?: ParseOptionsMode, ignoreUndefined?: boolean): any;
|
|
68
|
-
}
|
|
69
|
-
export {};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ParseOptionsError = void 0;
|
|
4
|
-
class ParseOptionsError extends Error {
|
|
5
|
-
constructor(message = "BAD REQUEST", argAttr) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.argAttr = argAttr;
|
|
8
|
-
this.name = "ParseOptionsError";
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
exports.ParseOptionsError = ParseOptionsError;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.get = void 0;
|
|
4
|
-
const parser_1 = require("./parser");
|
|
5
|
-
const error_1 = require("./error");
|
|
6
|
-
/*
|
|
7
|
-
usage const name = get(obj, "user.info.name", defaultValue, {type: "string"}, parser);
|
|
8
|
-
*/
|
|
9
|
-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
10
|
-
const get = (obj, attrPath, defaultValue, option, parser) => {
|
|
11
|
-
if (!obj || typeof obj !== "object") {
|
|
12
|
-
return defaultValue !== undefined ? defaultValue : undefined;
|
|
13
|
-
}
|
|
14
|
-
if (typeof attrPath !== "string") {
|
|
15
|
-
throw new Error(`attrPath must be typeof string`);
|
|
16
|
-
}
|
|
17
|
-
const path = attrPath.split(".").reverse();
|
|
18
|
-
let value = obj;
|
|
19
|
-
while (path.length > 0) {
|
|
20
|
-
const p = path.pop();
|
|
21
|
-
if (value[p] === undefined) {
|
|
22
|
-
return defaultValue !== undefined ? defaultValue : undefined;
|
|
23
|
-
}
|
|
24
|
-
value = value[p];
|
|
25
|
-
}
|
|
26
|
-
if (!option) {
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
const args = {
|
|
30
|
-
name: attrPath,
|
|
31
|
-
attrName: attrPath,
|
|
32
|
-
value,
|
|
33
|
-
...option
|
|
34
|
-
};
|
|
35
|
-
const parserResult = parser ? parser.parseValue(args) : (0, parser_1.parseValue)(args);
|
|
36
|
-
if (parserResult.isType) {
|
|
37
|
-
return parserResult.parsedValue;
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
throw new error_1.ParseOptionsError(parserResult.message ? parserResult.message : `${attrPath} not ${args.type}`, attrPath);
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
exports.get = get;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const newURL: (input: string, base?: string | any) => any;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.newURL = void 0;
|
|
4
|
-
const newURL = (input, base) => {
|
|
5
|
-
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
return new URL(input, base);
|
|
8
|
-
};
|
|
9
|
-
exports.newURL = newURL;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
-
};
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
__exportStar(require("./parser"), exports);
|
|
14
|
-
__exportStar(require("./common"), exports);
|
|
15
|
-
__exportStar(require("./error"), exports);
|
|
16
|
-
__exportStar(require("./get"), exports);
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { ParserInterface, ParseValueValidator, ParseValueValidatorResponse } from ".";
|
|
2
|
-
import { ParseOption, ParseValueArgs, ParseOptionMap, ParseOptionsMode } from "./common";
|
|
3
|
-
/**
|
|
4
|
-
* # Parser
|
|
5
|
-
*
|
|
6
|
-
* ```typescript
|
|
7
|
-
*import { Parser } from "@miqro/core";
|
|
8
|
-
*const parser = new Parser();
|
|
9
|
-
*
|
|
10
|
-
*const parsedValue = parser.parse("body", req.body, {
|
|
11
|
-
* name: "string",
|
|
12
|
-
* optional: {
|
|
13
|
-
* type: "string",
|
|
14
|
-
* required: false
|
|
15
|
-
* },
|
|
16
|
-
* mandatory: {
|
|
17
|
-
* type: "boolean",
|
|
18
|
-
* required: true
|
|
19
|
-
* }
|
|
20
|
-
*}, "no_extra");
|
|
21
|
-
*
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export declare class Parser implements ParserInterface {
|
|
25
|
-
protected parsers: {
|
|
26
|
-
[name: string]: ParseValueValidator;
|
|
27
|
-
};
|
|
28
|
-
constructor();
|
|
29
|
-
registerParser(t: string, parser: ParseValueValidator): void;
|
|
30
|
-
unRegisterParser(t: string): void;
|
|
31
|
-
parseValue(args: ParseValueArgs): ParseValueValidatorResponse;
|
|
32
|
-
parse(name: string, arg: any, options: ParseOption[] | ParseOptionMap, mode?: ParseOptionsMode, ignoreUndefined?: boolean): any;
|
|
33
|
-
}
|
|
34
|
-
export declare const parseValue: (args: ParseValueArgs) => ParseValueValidatorResponse;
|
|
35
|
-
export declare const parse: (name: string, arg: any, options: ParseOption[] | ParseOptionMap, mode?: ParseOptionsMode, ignoreUndefined?: boolean) => any;
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parse = exports.parseValue = exports.Parser = void 0;
|
|
4
|
-
const error_1 = require("./error");
|
|
5
|
-
const types_1 = require("./types");
|
|
6
|
-
const parseOptionMap2ParseOptionList = (map) => {
|
|
7
|
-
return Object.keys(map).map(name => {
|
|
8
|
-
const val = map[name];
|
|
9
|
-
return typeof val !== "object" ? {
|
|
10
|
-
name,
|
|
11
|
-
required: true,
|
|
12
|
-
type: val
|
|
13
|
-
} : val.required === undefined ? {
|
|
14
|
-
...val,
|
|
15
|
-
required: true,
|
|
16
|
-
name
|
|
17
|
-
} : {
|
|
18
|
-
...val,
|
|
19
|
-
name
|
|
20
|
-
};
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* # Parser
|
|
25
|
-
*
|
|
26
|
-
* ```typescript
|
|
27
|
-
*import { Parser } from "@miqro/core";
|
|
28
|
-
*const parser = new Parser();
|
|
29
|
-
*
|
|
30
|
-
*const parsedValue = parser.parse("body", req.body, {
|
|
31
|
-
* name: "string",
|
|
32
|
-
* optional: {
|
|
33
|
-
* type: "string",
|
|
34
|
-
* required: false
|
|
35
|
-
* },
|
|
36
|
-
* mandatory: {
|
|
37
|
-
* type: "boolean",
|
|
38
|
-
* required: true
|
|
39
|
-
* }
|
|
40
|
-
*}, "no_extra");
|
|
41
|
-
*
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
class Parser {
|
|
45
|
-
constructor() {
|
|
46
|
-
this.parsers = {};
|
|
47
|
-
this.registerParser("any", types_1.parseAny);
|
|
48
|
-
this.registerParser("array", types_1.parseArray);
|
|
49
|
-
this.registerParser("boolean", types_1.parseBoolean);
|
|
50
|
-
this.registerParser("enum", types_1.parseEnum);
|
|
51
|
-
this.registerParser("multiple", types_1.parseMultiple);
|
|
52
|
-
this.registerParser("nested", types_1.parseNested);
|
|
53
|
-
this.registerParser("number", types_1.parseNumber);
|
|
54
|
-
this.registerParser("object", types_1.parseObject);
|
|
55
|
-
this.registerParser("string", types_1.parseString);
|
|
56
|
-
this.registerParser("regex", types_1.parseRegex);
|
|
57
|
-
this.registerParser("url", types_1.parseURL);
|
|
58
|
-
this.registerParser("email", types_1.parseEmail);
|
|
59
|
-
}
|
|
60
|
-
registerParser(t, parser) {
|
|
61
|
-
if (this.parsers[t]) {
|
|
62
|
-
throw new Error("already registered!");
|
|
63
|
-
}
|
|
64
|
-
this.parsers[t] = parser;
|
|
65
|
-
}
|
|
66
|
-
unRegisterParser(t) {
|
|
67
|
-
if (!this.parsers[t]) {
|
|
68
|
-
throw new Error("not registered!");
|
|
69
|
-
}
|
|
70
|
-
delete this.parsers[t];
|
|
71
|
-
}
|
|
72
|
-
parseValue(args) {
|
|
73
|
-
const { type, forceArray, parseJSON, allowNull } = args;
|
|
74
|
-
// check parsers
|
|
75
|
-
const parser = this.parsers[type];
|
|
76
|
-
if (parser === undefined) {
|
|
77
|
-
throw new error_1.ParseOptionsError(`unsupported type ${type}`);
|
|
78
|
-
}
|
|
79
|
-
// prepare args
|
|
80
|
-
if (parseJSON) {
|
|
81
|
-
if (typeof args.value !== "string") {
|
|
82
|
-
throw new error_1.ParseOptionsError(`parseJSON not available to non string value`);
|
|
83
|
-
}
|
|
84
|
-
try {
|
|
85
|
-
args.value = JSON.parse(args.value);
|
|
86
|
-
}
|
|
87
|
-
catch (e) {
|
|
88
|
-
throw new error_1.ParseOptionsError(`value not json!`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
if (allowNull && args.value === null) {
|
|
92
|
-
return {
|
|
93
|
-
isType: true,
|
|
94
|
-
parsedValue: null
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
if (forceArray && type === "array" && !(args.value instanceof Array)) {
|
|
99
|
-
args.value = [args.value];
|
|
100
|
-
}
|
|
101
|
-
else if (forceArray && type !== "array") {
|
|
102
|
-
throw new error_1.ParseOptionsError(`unsupported type ${type} with forceArray`);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
// run parser
|
|
106
|
-
return parser(args, this);
|
|
107
|
-
}
|
|
108
|
-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
109
|
-
parse(name, arg, options, mode = "no_extra", ignoreUndefined = false) {
|
|
110
|
-
const ret = {};
|
|
111
|
-
if (!arg || typeof arg !== "object") {
|
|
112
|
-
throw new error_1.ParseOptionsError(`invalid ${name}`, name);
|
|
113
|
-
}
|
|
114
|
-
if (!(options instanceof Array)) {
|
|
115
|
-
options = parseOptionMap2ParseOptionList(options);
|
|
116
|
-
}
|
|
117
|
-
for (const option of options) {
|
|
118
|
-
const value = arg[option.name];
|
|
119
|
-
if (value === undefined && option.defaultValue !== undefined) {
|
|
120
|
-
ret[option.name] = option.defaultValue;
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
const exists = ignoreUndefined ? value !== undefined : arg.hasOwnProperty(option.name);
|
|
124
|
-
if (!exists && option.required === false) {
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
else if (!exists && (option.required === true || option.required === undefined)) {
|
|
128
|
-
throw new error_1.ParseOptionsError(`${name}.${option.name} not defined`, `${name}.${option.name}`);
|
|
129
|
-
}
|
|
130
|
-
const { isType, parsedValue, message } = this.parseValue({
|
|
131
|
-
name,
|
|
132
|
-
attrName: option.name,
|
|
133
|
-
type: option.type,
|
|
134
|
-
regex: option.regex,
|
|
135
|
-
value,
|
|
136
|
-
numberMaxDecimals: option.numberMaxDecimals,
|
|
137
|
-
numberMinDecimals: option.numberMinDecimals,
|
|
138
|
-
numberMin: option.numberMin,
|
|
139
|
-
numberMax: option.numberMax,
|
|
140
|
-
allowNull: option.allowNull,
|
|
141
|
-
options: option.options,
|
|
142
|
-
multipleOptions: option.multipleOptions,
|
|
143
|
-
stringMinLength: option.stringMinLength,
|
|
144
|
-
stringMaxLength: option.stringMaxLength,
|
|
145
|
-
arrayType: option.arrayType,
|
|
146
|
-
nestedOptions: option.nestedOptions,
|
|
147
|
-
enumValues: option.enumValues,
|
|
148
|
-
parseJSON: option.parseJSON,
|
|
149
|
-
arrayMaxLength: option.arrayMaxLength,
|
|
150
|
-
arrayMinLength: option.arrayMinLength,
|
|
151
|
-
forceArray: option.forceArray
|
|
152
|
-
});
|
|
153
|
-
if (!isType) {
|
|
154
|
-
throw new error_1.ParseOptionsError(`${name}.${option.name} not ${option.type}${message ? `: ${message}.` : ""}` +
|
|
155
|
-
`${option.type === "number" && option.numberMin !== undefined ? `${option.numberMin}:` : ""}${option.type === "number" && option.numberMax !== undefined ? `:${option.numberMax}` : ""}${option.numberMinDecimals !== undefined ? ` min decimals[${option.numberMinDecimals}]` : ""}${option.numberMaxDecimals !== undefined ? ` max decimals[${option.numberMaxDecimals}]` : ""}` +
|
|
156
|
-
`${option.type === "string" && option.stringMinLength !== undefined ? `${option.stringMinLength}:` : ""}${option.type === "string" && option.stringMaxLength !== undefined ? `:${option.stringMaxLength}` : ""}` +
|
|
157
|
-
`${option.type === "array" && option.arrayMinLength !== undefined ? `${option.arrayMinLength}:` : ""}${option.type === "array" && option.arrayMaxLength !== undefined ? `:${option.arrayMaxLength}` : ""}` +
|
|
158
|
-
`${option.type === "array" && option.arrayType ? (option.arrayType !== "enum" ? ` of ${option.arrayType}` : ` of ${option.arrayType} as defined. valid values [${option.enumValues}]`) : ""}` +
|
|
159
|
-
`${option.type === "nested" ? ` as defined!` : ""}` +
|
|
160
|
-
`${option.type === "enum" ? ` as defined. valid values [${option.enumValues}]` : ""}` +
|
|
161
|
-
`${option.type === "multiple" ? ` as defined.` : ""}`, `${name}.${option.name}`);
|
|
162
|
-
}
|
|
163
|
-
ret[option.name] = parsedValue;
|
|
164
|
-
}
|
|
165
|
-
switch (mode) {
|
|
166
|
-
case "no_extra":
|
|
167
|
-
const argKeys = Object.keys(arg);
|
|
168
|
-
for (const argKey of argKeys) {
|
|
169
|
-
if (ignoreUndefined && arg[argKey] === undefined) {
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
else if (!ret.hasOwnProperty(argKey)) {
|
|
173
|
-
throw new error_1.ParseOptionsError(`${name}.${argKey} option not valid [${argKey}]`, `${name}.${argKey}`);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
return ret;
|
|
177
|
-
case "add_extra":
|
|
178
|
-
return {
|
|
179
|
-
...arg,
|
|
180
|
-
...ret
|
|
181
|
-
};
|
|
182
|
-
case "remove_extra":
|
|
183
|
-
return ret;
|
|
184
|
-
default:
|
|
185
|
-
throw new error_1.ParseOptionsError(`unsupported mode ${mode}`);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
exports.Parser = Parser;
|
|
190
|
-
const defaultParser = new Parser();
|
|
191
|
-
const parseValue = (args) => {
|
|
192
|
-
return defaultParser.parseValue(args);
|
|
193
|
-
};
|
|
194
|
-
exports.parseValue = parseValue;
|
|
195
|
-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
196
|
-
const parse = (name, arg, options, mode = "no_extra", ignoreUndefined = false) => defaultParser.parse(name, arg, options, mode, ignoreUndefined);
|
|
197
|
-
exports.parse = parse;
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseArray = void 0;
|
|
4
|
-
const parseArray = ({ value, arrayType, name, attrName, numberMin, numberMax, allowNull, multipleOptions, stringMinLength, numberMaxDecimals, numberMinDecimals, stringMaxLength, nestedOptions, enumValues, options, regex, arrayMaxLength, arrayMinLength }, parser) => {
|
|
5
|
-
const parsedList = [];
|
|
6
|
-
let isType = value instanceof Array;
|
|
7
|
-
if (isType) {
|
|
8
|
-
if (arrayType === undefined) {
|
|
9
|
-
for (const v of value) {
|
|
10
|
-
parsedList.push(v);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
else {
|
|
14
|
-
for (let i = 0; i < value.length; i++) {
|
|
15
|
-
const v = value[i];
|
|
16
|
-
const aiType = parser.parseValue({
|
|
17
|
-
name: `${name}.${attrName}`,
|
|
18
|
-
attrName: `[${i}]`,
|
|
19
|
-
type: arrayType,
|
|
20
|
-
options,
|
|
21
|
-
regex,
|
|
22
|
-
forceArray: false,
|
|
23
|
-
value: v,
|
|
24
|
-
numberMin,
|
|
25
|
-
numberMax,
|
|
26
|
-
numberMaxDecimals,
|
|
27
|
-
numberMinDecimals,
|
|
28
|
-
allowNull,
|
|
29
|
-
multipleOptions,
|
|
30
|
-
stringMinLength,
|
|
31
|
-
stringMaxLength,
|
|
32
|
-
nestedOptions,
|
|
33
|
-
enumValues,
|
|
34
|
-
arrayMaxLength,
|
|
35
|
-
arrayMinLength,
|
|
36
|
-
});
|
|
37
|
-
if (!aiType.isType) {
|
|
38
|
-
isType = false;
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
parsedList.push(aiType.parsedValue);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if (isType && arrayMinLength !== undefined && parsedList.length < arrayMinLength) {
|
|
48
|
-
isType = false;
|
|
49
|
-
}
|
|
50
|
-
if (isType && arrayMaxLength !== undefined && parsedList.length > arrayMaxLength) {
|
|
51
|
-
isType = false;
|
|
52
|
-
}
|
|
53
|
-
return {
|
|
54
|
-
isType,
|
|
55
|
-
parsedValue: isType ? parsedList : value
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
exports.parseArray = parseArray;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseBoolean = void 0;
|
|
4
|
-
const parseBoolean = ({ value }) => {
|
|
5
|
-
const parsedValue = value === "true" || value === true ? true : value === "false" || value === false ? false : null;
|
|
6
|
-
return {
|
|
7
|
-
isType: parsedValue !== null,
|
|
8
|
-
parsedValue: parsedValue !== null ? parsedValue : value
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
exports.parseBoolean = parseBoolean;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseEmail = void 0;
|
|
4
|
-
const regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
|
5
|
-
const parseEmail = ({ value, stringMinLength, stringMaxLength }) => {
|
|
6
|
-
let isType = typeof value === "string";
|
|
7
|
-
const parsedValue = value;
|
|
8
|
-
if (isType && stringMinLength !== undefined && parsedValue.length < stringMinLength) {
|
|
9
|
-
isType = false;
|
|
10
|
-
}
|
|
11
|
-
if (isType && stringMaxLength !== undefined && parsedValue.length > stringMaxLength) {
|
|
12
|
-
isType = false;
|
|
13
|
-
}
|
|
14
|
-
if (isType) {
|
|
15
|
-
isType = regex.test(String(value).toLowerCase());
|
|
16
|
-
}
|
|
17
|
-
return {
|
|
18
|
-
isType,
|
|
19
|
-
parsedValue: value
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
exports.parseEmail = parseEmail;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseEnum = void 0;
|
|
4
|
-
const error_1 = require("../error");
|
|
5
|
-
const parseEnum = ({ value, attrName, name, enumValues }, parser) => {
|
|
6
|
-
const enumCheck = parser.parseValue({
|
|
7
|
-
name: `${name}.${attrName}`,
|
|
8
|
-
attrName: `enumList`,
|
|
9
|
-
forceArray: false,
|
|
10
|
-
type: "array",
|
|
11
|
-
value: enumValues,
|
|
12
|
-
arrayType: "string"
|
|
13
|
-
});
|
|
14
|
-
if (enumCheck.isType && enumValues && enumValues.length > 0) {
|
|
15
|
-
enumCheck.isType = enumValues.indexOf(value) !== -1;
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
throw new error_1.ParseOptionsError(`options.enumValues not a string array`);
|
|
19
|
-
}
|
|
20
|
-
return {
|
|
21
|
-
isType: enumCheck.isType,
|
|
22
|
-
parsedValue: value
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
exports.parseEnum = parseEnum;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export * from "./any";
|
|
2
|
-
export * from "./array";
|
|
3
|
-
export * from "./boolean";
|
|
4
|
-
export * from "./email";
|
|
5
|
-
export * from "./enum";
|
|
6
|
-
export * from "./multiple";
|
|
7
|
-
export * from "./nested";
|
|
8
|
-
export * from "./number";
|
|
9
|
-
export * from "./object";
|
|
10
|
-
export * from "./regex";
|
|
11
|
-
export * from "./string";
|
|
12
|
-
export * from "./url";
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
-
};
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
__exportStar(require("./any"), exports);
|
|
14
|
-
__exportStar(require("./array"), exports);
|
|
15
|
-
__exportStar(require("./boolean"), exports);
|
|
16
|
-
__exportStar(require("./email"), exports);
|
|
17
|
-
__exportStar(require("./enum"), exports);
|
|
18
|
-
__exportStar(require("./multiple"), exports);
|
|
19
|
-
__exportStar(require("./nested"), exports);
|
|
20
|
-
__exportStar(require("./number"), exports);
|
|
21
|
-
__exportStar(require("./object"), exports);
|
|
22
|
-
__exportStar(require("./regex"), exports);
|
|
23
|
-
__exportStar(require("./string"), exports);
|
|
24
|
-
__exportStar(require("./url"), exports);
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseMultiple = void 0;
|
|
4
|
-
const error_1 = require("../error");
|
|
5
|
-
const parseMultiple = ({ multipleOptions, name, attrName, forceArray, type, value }, parser) => {
|
|
6
|
-
if (!multipleOptions) {
|
|
7
|
-
throw new error_1.ParseOptionsError(`unsupported type ${type} without multipleOptions`);
|
|
8
|
-
}
|
|
9
|
-
for (let i = 0; i < multipleOptions.length; i++) {
|
|
10
|
-
const basicOption = multipleOptions[i];
|
|
11
|
-
try {
|
|
12
|
-
const aiType = parser.parseValue({
|
|
13
|
-
...basicOption,
|
|
14
|
-
forceArray,
|
|
15
|
-
name,
|
|
16
|
-
attrName,
|
|
17
|
-
value
|
|
18
|
-
});
|
|
19
|
-
if (aiType.isType) {
|
|
20
|
-
return aiType;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
catch (e) {
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return {
|
|
28
|
-
isType: false,
|
|
29
|
-
parsedValue: value
|
|
30
|
-
};
|
|
31
|
-
};
|
|
32
|
-
exports.parseMultiple = parseMultiple;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseNested = void 0;
|
|
4
|
-
const error_1 = require("../error");
|
|
5
|
-
const parseNested = ({ nestedOptions, name, attrName, type, value }, parser) => {
|
|
6
|
-
if (!nestedOptions) {
|
|
7
|
-
throw new error_1.ParseOptionsError(`unsupported type ${type} without nestedOptions`);
|
|
8
|
-
}
|
|
9
|
-
const pValue = parser.parse(`${name}.${attrName}`, value, nestedOptions.options, nestedOptions.mode, nestedOptions.ignoreUndefined);
|
|
10
|
-
return {
|
|
11
|
-
isType: pValue !== null,
|
|
12
|
-
parsedValue: pValue === null ? value : pValue
|
|
13
|
-
};
|
|
14
|
-
};
|
|
15
|
-
exports.parseNested = parseNested;
|