fockeror 0.1.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/LICENSE +21 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +7 -0
- package/dist/exception.d.ts +8 -0
- package/dist/exception.js +17 -0
- package/dist/factory.d.ts +9 -0
- package/dist/factory.js +41 -0
- package/dist/feror.d.ts +21 -0
- package/dist/feror.js +119 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +21 -0
- package/dist/types.d.ts +40 -0
- package/dist/types.js +2 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 FOCKUSTY
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CLEAN_REGEX_REPLACER = exports.CLEAN_SEARCH_REGEX = exports.DEFAULT_HTTP_STATUS = exports.PLACEHOLDER_PATTERN = void 0;
|
|
4
|
+
exports.PLACEHOLDER_PATTERN = /\$\{\{\s{1}([^}\s]+)\s{1}\}\}/g;
|
|
5
|
+
exports.DEFAULT_HTTP_STATUS = 500;
|
|
6
|
+
exports.CLEAN_SEARCH_REGEX = /[.*+?^${}()|[\]\\]/g;
|
|
7
|
+
exports.CLEAN_REGEX_REPLACER = "\\$&";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ExceptionFormatterClass, ExceptionOptions } from "./types";
|
|
2
|
+
export declare class Exception {
|
|
3
|
+
readonly response: string | Record<string, unknown>;
|
|
4
|
+
readonly status: number;
|
|
5
|
+
readonly options?: ExceptionOptions | undefined;
|
|
6
|
+
constructor(response: string | Record<string, unknown>, status: number, options?: ExceptionOptions | undefined);
|
|
7
|
+
format<T>(formatterClass: ExceptionFormatterClass<T>): T;
|
|
8
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Exception = void 0;
|
|
4
|
+
class Exception {
|
|
5
|
+
response;
|
|
6
|
+
status;
|
|
7
|
+
options;
|
|
8
|
+
constructor(response, status, options) {
|
|
9
|
+
this.response = response;
|
|
10
|
+
this.status = status;
|
|
11
|
+
this.options = options;
|
|
12
|
+
}
|
|
13
|
+
format(formatterClass) {
|
|
14
|
+
return new formatterClass(this.response, this.status, this.options);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.Exception = Exception;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ErrorTemplateInput, ExceptionFormatterClass, Ferors, Logger } from "./types";
|
|
2
|
+
export declare class FerorFactory<FormatterClass> {
|
|
3
|
+
private readonly logger;
|
|
4
|
+
private readonly formatterClass;
|
|
5
|
+
constructor(logger: Logger, formatterClass: ExceptionFormatterClass<FormatterClass>);
|
|
6
|
+
execute<const Templates extends Record<string, ErrorTemplateInput>>(prefix: string, templates: Templates): Ferors<Templates, FormatterClass>;
|
|
7
|
+
private generateCode;
|
|
8
|
+
private defineError;
|
|
9
|
+
}
|
package/dist/factory.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FerorFactory = void 0;
|
|
4
|
+
const constants_1 = require("./constants");
|
|
5
|
+
const feror_1 = require("./feror");
|
|
6
|
+
class FerorFactory {
|
|
7
|
+
logger;
|
|
8
|
+
formatterClass;
|
|
9
|
+
constructor(logger, formatterClass) {
|
|
10
|
+
this.logger = logger;
|
|
11
|
+
this.formatterClass = formatterClass;
|
|
12
|
+
}
|
|
13
|
+
execute(prefix, templates) {
|
|
14
|
+
const templateKeys = Object.keys(templates);
|
|
15
|
+
const entries = templateKeys.map((key, index) => {
|
|
16
|
+
const input = templates[key];
|
|
17
|
+
const errorTemplate = this.defineError(input);
|
|
18
|
+
const code = this.generateCode(prefix, key, index);
|
|
19
|
+
this.logger.execute(`Загрузка ошибки ${prefix} ${code} : ${errorTemplate.message}`);
|
|
20
|
+
const prefixed = {
|
|
21
|
+
...errorTemplate,
|
|
22
|
+
message: `${code} : ${errorTemplate.message}`,
|
|
23
|
+
};
|
|
24
|
+
return [key, new feror_1.Feror(prefixed, this.logger, this.formatterClass)];
|
|
25
|
+
});
|
|
26
|
+
return Object.fromEntries(entries);
|
|
27
|
+
}
|
|
28
|
+
generateCode(prefix, key, index) {
|
|
29
|
+
const prefixEncoded = Buffer.from(prefix).toString("base64url");
|
|
30
|
+
const suffix = Buffer.from(`${key}:${index}`).toString("hex");
|
|
31
|
+
return `${prefixEncoded}:${suffix}`;
|
|
32
|
+
}
|
|
33
|
+
defineError(error) {
|
|
34
|
+
const combined = `${error.message} ${error.description}`;
|
|
35
|
+
const matches = combined.match(constants_1.PLACEHOLDER_PATTERN);
|
|
36
|
+
const allKeys = matches?.map((m) => m.slice(1, -1)) ?? [];
|
|
37
|
+
const uniqueKeys = Array.from(new Set(allKeys));
|
|
38
|
+
return { ...error, placeholders: uniqueKeys };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.FerorFactory = FerorFactory;
|
package/dist/feror.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ErrorTemplate, ExceptionFormatterClass, Logger, PlaceholderObject } from "./types";
|
|
2
|
+
export declare class Feror<const Placeholders extends string[], FormatterClass> {
|
|
3
|
+
readonly template: ErrorTemplate<Placeholders>;
|
|
4
|
+
private readonly logger;
|
|
5
|
+
private readonly formatterClass;
|
|
6
|
+
private readonly placeholderRegexes;
|
|
7
|
+
constructor(template: ErrorTemplate<Placeholders>, logger: Logger, formatterClass: ExceptionFormatterClass<FormatterClass>);
|
|
8
|
+
execute(placeholders: PlaceholderObject<Placeholders>, cause?: Error): FormatterClass;
|
|
9
|
+
execute(cause?: Error): FormatterClass;
|
|
10
|
+
throw(placeholders: PlaceholderObject<Placeholders>, cause?: Error): never;
|
|
11
|
+
throw(cause?: Error): never;
|
|
12
|
+
private createStaticException;
|
|
13
|
+
private createDynamicException;
|
|
14
|
+
private formatTemplate;
|
|
15
|
+
private createException;
|
|
16
|
+
private collectPlaceholdersFromText;
|
|
17
|
+
private validatePlaceholders;
|
|
18
|
+
private buildRegexMap;
|
|
19
|
+
private extractPlaceholders;
|
|
20
|
+
private createRegEx;
|
|
21
|
+
}
|
package/dist/feror.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Feror = void 0;
|
|
4
|
+
const constants_1 = require("./constants");
|
|
5
|
+
const exception_1 = require("./exception");
|
|
6
|
+
const PLACEHOLDER_PATTERN = /\$\{\{\s{1}([^}\s]+)\s{1}\}\}/g;
|
|
7
|
+
class Feror {
|
|
8
|
+
template;
|
|
9
|
+
logger;
|
|
10
|
+
formatterClass;
|
|
11
|
+
placeholderRegexes;
|
|
12
|
+
constructor(template, logger, formatterClass) {
|
|
13
|
+
this.template = template;
|
|
14
|
+
this.logger = logger;
|
|
15
|
+
this.formatterClass = formatterClass;
|
|
16
|
+
this.placeholderRegexes = this.extractPlaceholders(template);
|
|
17
|
+
}
|
|
18
|
+
execute(placeholdersOrCause, cause) {
|
|
19
|
+
let placeholders = undefined;
|
|
20
|
+
let actualCause;
|
|
21
|
+
if (placeholdersOrCause instanceof Error) {
|
|
22
|
+
actualCause = placeholdersOrCause;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
placeholders = placeholdersOrCause;
|
|
26
|
+
actualCause = cause;
|
|
27
|
+
}
|
|
28
|
+
if (!placeholders || Object.keys(placeholders).length === 0) {
|
|
29
|
+
return this.createStaticException(actualCause);
|
|
30
|
+
}
|
|
31
|
+
return this.createDynamicException(placeholders, actualCause);
|
|
32
|
+
}
|
|
33
|
+
throw(placeholdersOrCause, cause) {
|
|
34
|
+
if (placeholdersOrCause instanceof Error) {
|
|
35
|
+
throw this.execute(placeholdersOrCause);
|
|
36
|
+
}
|
|
37
|
+
throw this.execute(placeholdersOrCause, cause);
|
|
38
|
+
}
|
|
39
|
+
createStaticException(cause) {
|
|
40
|
+
return this.createException({ ...this.template, placeholders: [] }, cause);
|
|
41
|
+
}
|
|
42
|
+
createDynamicException(placeholders, cause) {
|
|
43
|
+
const error = this.formatTemplate(placeholders);
|
|
44
|
+
return this.createException(error, cause);
|
|
45
|
+
}
|
|
46
|
+
formatTemplate(placeholders) {
|
|
47
|
+
let message = this.template.message;
|
|
48
|
+
let description = this.template.description;
|
|
49
|
+
for (const [key, regex] of this.placeholderRegexes.entries()) {
|
|
50
|
+
const value = placeholders[key];
|
|
51
|
+
if (value === undefined) {
|
|
52
|
+
this.logger.error(new Error(`Placeholder {${key}} not provided in data`));
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
message = message.replace(regex, value);
|
|
56
|
+
description = description.replace(regex, value);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
...this.template,
|
|
60
|
+
message,
|
|
61
|
+
description,
|
|
62
|
+
placeholders: [],
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
createException(error, cause) {
|
|
66
|
+
const status = error.status ?? this.template.status ?? constants_1.DEFAULT_HTTP_STATUS;
|
|
67
|
+
return new exception_1.Exception(error.message, status, {
|
|
68
|
+
description: error.description,
|
|
69
|
+
cause: cause ?? error.cause,
|
|
70
|
+
}).format(this.formatterClass);
|
|
71
|
+
}
|
|
72
|
+
collectPlaceholdersFromText(template) {
|
|
73
|
+
const text = `${template.message} ${template.description}`;
|
|
74
|
+
const set = new Set();
|
|
75
|
+
for (const match of text.matchAll(PLACEHOLDER_PATTERN)) {
|
|
76
|
+
set.add(match[1]);
|
|
77
|
+
}
|
|
78
|
+
return set;
|
|
79
|
+
}
|
|
80
|
+
validatePlaceholders(collectedPlaceholders, declaredPlaceholders) {
|
|
81
|
+
if (!declaredPlaceholders) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const errors = [];
|
|
85
|
+
for (const placeholder of declaredPlaceholders) {
|
|
86
|
+
if (collectedPlaceholders.has(placeholder)) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
errors.push(new Error(`Placeholder "${placeholder}" not found in text`));
|
|
90
|
+
}
|
|
91
|
+
for (const placeholder of collectedPlaceholders) {
|
|
92
|
+
if (declaredPlaceholders.includes(placeholder)) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
errors.push(new Error(`Placeholder from text "${placeholder}" not found in placeholders list`));
|
|
96
|
+
}
|
|
97
|
+
if (errors.length !== 0) {
|
|
98
|
+
errors.forEach((e) => this.logger.error(e));
|
|
99
|
+
throw new Error("Placeholder validation failed.");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
buildRegexMap(keys) {
|
|
103
|
+
const placeholdersMap = new Map();
|
|
104
|
+
for (const key of keys) {
|
|
105
|
+
placeholdersMap.set(key, this.createRegEx(key));
|
|
106
|
+
}
|
|
107
|
+
return placeholdersMap;
|
|
108
|
+
}
|
|
109
|
+
extractPlaceholders(template) {
|
|
110
|
+
const collected = this.collectPlaceholdersFromText(template);
|
|
111
|
+
this.validatePlaceholders(collected, template.placeholders);
|
|
112
|
+
return this.buildRegexMap(collected);
|
|
113
|
+
}
|
|
114
|
+
createRegEx(key) {
|
|
115
|
+
const clean = key.replace(constants_1.CLEAN_SEARCH_REGEX, constants_1.CLEAN_REGEX_REPLACER);
|
|
116
|
+
return new RegExp(`\\$\\{\\{\\s{1}${clean}\\s{1}\\}\\}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.Feror = Feror;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
const factory_1 = require("./factory");
|
|
18
|
+
__exportStar(require("./feror"), exports);
|
|
19
|
+
__exportStar(require("./factory"), exports);
|
|
20
|
+
__exportStar(require("./exception"), exports);
|
|
21
|
+
exports.default = factory_1.FerorFactory;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Exception } from "./exception";
|
|
2
|
+
import type { Feror } from "./feror";
|
|
3
|
+
export interface Logger {
|
|
4
|
+
error: (message: Error) => unknown;
|
|
5
|
+
execute: (message: string) => unknown;
|
|
6
|
+
}
|
|
7
|
+
export type PlaceholderObject<Placeholders extends string[]> = {
|
|
8
|
+
[P in Placeholders[number]]: string;
|
|
9
|
+
};
|
|
10
|
+
export type ExtractPlaceholdersTuple<InputString extends string, Acc extends string[] = []> = InputString extends `${infer _}\${{ ${infer P} }}${infer Rest}` ? ExtractPlaceholdersTuple<Rest, [...Acc, P]> : Acc;
|
|
11
|
+
export type InferPlaceholders<T extends {
|
|
12
|
+
message: string;
|
|
13
|
+
description: string;
|
|
14
|
+
}> = ExtractPlaceholdersTuple<`${T["message"]} ${T["description"]}`>;
|
|
15
|
+
export type ErrorTemplateInput = {
|
|
16
|
+
message: string;
|
|
17
|
+
description: string;
|
|
18
|
+
cause?: Error;
|
|
19
|
+
status?: number;
|
|
20
|
+
options?: Record<string, unknown>;
|
|
21
|
+
};
|
|
22
|
+
export interface ErrorTemplate<Placeholders extends string[]> {
|
|
23
|
+
message: string;
|
|
24
|
+
description: string;
|
|
25
|
+
cause?: Error;
|
|
26
|
+
status?: number;
|
|
27
|
+
options?: Record<string, unknown>;
|
|
28
|
+
placeholders: Placeholders;
|
|
29
|
+
}
|
|
30
|
+
export type PlaceholderRegexMap<Placeholders extends string[]> = Map<keyof PlaceholderObject<Placeholders>, RegExp>;
|
|
31
|
+
export type FormattedErrorTemplate = ErrorTemplate<[]>;
|
|
32
|
+
export type Ferors<ErrorTemplate extends Record<string, ErrorTemplateInput>, FormatterClass> = {
|
|
33
|
+
[K in keyof ErrorTemplate]: Feror<InferPlaceholders<ErrorTemplate[K]>, FormatterClass>;
|
|
34
|
+
};
|
|
35
|
+
export interface ExceptionOptions {
|
|
36
|
+
cause?: unknown;
|
|
37
|
+
description?: string;
|
|
38
|
+
}
|
|
39
|
+
export type ExceptionConstuctorParameters = ConstructorParameters<typeof Exception>;
|
|
40
|
+
export type ExceptionFormatterClass<T> = new (...parameters: ExceptionConstuctorParameters) => T;
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "fockeror",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
"./package.json": "./package.json",
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/fockusty/fockeror.git"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/fockusty/fockeror#readme",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/fockusty/fockeror/issues"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"keywords": [],
|
|
29
|
+
"author": {
|
|
30
|
+
"name": "FOCKUSTY",
|
|
31
|
+
"email": "viserd.yt@gmail.com",
|
|
32
|
+
"url": "https://fockusty.vercel.app"
|
|
33
|
+
},
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"packageManager": "pnpm@10.33.0",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^25.5.2",
|
|
38
|
+
"typescript": "^6.0.2"
|
|
39
|
+
}
|
|
40
|
+
}
|