@syntay/fastay 0.2.4 → 0.2.6

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.
@@ -1,95 +0,0 @@
1
- import { Request, Response, NextFunction } from 'express';
2
- import { logger } from '../logger.js';
3
-
4
- export type MiddlewareFn = (
5
- req: Request,
6
- res: Response,
7
- next: NextFunction
8
- ) => any;
9
-
10
- const color = {
11
- cyan: (s: string) => `\x1b[36m${s}\x1b[0m`,
12
- gray: (s: string) => `\x1b[90m${s}\x1b[0m`,
13
- red: (s: string) => `\x1b[31m${s}\x1b[0m`,
14
- yellow: (s: string) => `\x1b[33m${s}\x1b[0m`,
15
- green: (s: string) => `\x1b[32m${s}\x1b[0m`,
16
- white: (s: string) => `\x1b[37m${s}\x1b[0m`,
17
- };
18
-
19
- /**
20
- * Formata uma função para exibição bonitinha
21
- */
22
- function formatFunction(fn: Function): string {
23
- let code = fn.toString();
24
-
25
- // deixa com quebras de linha
26
- code = code.replace(/;/g, ';\n');
27
-
28
- // tenta dar indentação
29
- code = code
30
- .replace(/{/g, '{\n ')
31
- .replace(/}/g, '\n}')
32
- .replace(/\n\s*\n/g, '\n');
33
-
34
- return code.trim();
35
- }
36
-
37
- /**
38
- * Verifica se next() ou return existem
39
- */
40
- function validateMiddlewareCode(mw: MiddlewareFn) {
41
- const raw = mw.toString();
42
- const name = mw.name || 'anonymous';
43
-
44
- const cleaned = raw
45
- .replace(/\/\/.*$/gm, '')
46
- .replace(/\/\*[\s\S]*?\*\//gm, '');
47
-
48
- const hasNext = /next\s*\(/.test(cleaned);
49
- const hasReturn = /\breturn\b/.test(cleaned);
50
-
51
- if (!hasNext && !hasReturn) {
52
- const prettyCode = formatFunction(mw);
53
-
54
- const message = [
55
- `${color.red('⨯ Fastay Middleware Error')}`,
56
- ``,
57
- `The middleware ${color.yellow(
58
- `"${name}"`
59
- )} does not call next() or return any value.`,
60
- `This will halt the middleware chain and block the request pipeline.`,
61
- ``,
62
- `▌ Middleware Source`,
63
- color.gray(prettyCode),
64
- ``,
65
- `${color.cyan('▌ How to fix')}`,
66
- `Ensure your middleware ends with either:`,
67
- ` • ${color.green('next()')}`,
68
- ` • ${color.green('return ...')}`,
69
- ``,
70
- `Fastay cannot continue until this middleware is fixed.`,
71
- ].join('\n');
72
-
73
- const err = new Error(message);
74
- err.name = 'FastayMiddlewareError';
75
- throw err;
76
- }
77
- }
78
-
79
- /**
80
- * Wrapper final
81
- */
82
- export function wrapMiddleware(mw: MiddlewareFn): MiddlewareFn {
83
- const name = mw.name || 'anonymous';
84
- validateMiddlewareCode(mw);
85
-
86
- // retorna middleware "puro"
87
- return async (req: Request, res: Response, next: NextFunction) => {
88
- try {
89
- await mw(req, res, next);
90
- } catch (err) {
91
- logger.error(`[${name}] middleware error: ${err}`);
92
- next(err);
93
- }
94
- };
95
- }
@@ -1,14 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "dist",
5
- "declaration": true,
6
- "sourceMap": false,
7
- "rootDir": "src",
8
- "module": "ES2022",
9
- "target": "ES2020",
10
- "emitDeclarationOnly": false,
11
- "resolveJsonModule": true
12
- },
13
- "include": ["src", "src/types/**/*.d.ts"]
14
- }
package/tsconfig.json DELETED
@@ -1,22 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "CommonJS",
5
- "rootDir": "src",
6
- "outDir": "dist",
7
- "baseUrl": ".",
8
- "strict": true,
9
- "declaration": true,
10
- "emitDeclarationOnly": false,
11
- "declarationMap": true,
12
- "esModuleInterop": true,
13
- "forceConsistentCasingInFileNames": true,
14
- "skipLibCheck": true,
15
- "moduleResolution": "node",
16
- "resolveJsonModule": true,
17
- "allowSyntheticDefaultImports": true,
18
- "typeRoots": ["./node_modules/@types", "./src/types"]
19
- },
20
- "include": ["src", "src/types"],
21
- "exclude": ["node_modules", "dist"]
22
- }