@syntay/fastay 0.1.5 → 0.1.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.
- package/dist/index.d.ts +1 -1
- package/dist/router.js +56 -24
- package/dist/types/index.d.ts +22 -2
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/router.ts +82 -26
- package/src/types/index.ts +28 -2
package/dist/index.d.ts
CHANGED
package/dist/router.js
CHANGED
|
@@ -42,6 +42,62 @@ function wrapHandler(fn, routePath, filePath) {
|
|
|
42
42
|
return;
|
|
43
43
|
if (result === undefined)
|
|
44
44
|
return;
|
|
45
|
+
// Suporte a status e cookies customizado
|
|
46
|
+
// if (
|
|
47
|
+
// typeof result === 'object' &&
|
|
48
|
+
// 'status' in result &&
|
|
49
|
+
// 'body' in result &&
|
|
50
|
+
// typeof result.status === 'number'
|
|
51
|
+
// ) {
|
|
52
|
+
// return res.status(result.status).json(result.body);
|
|
53
|
+
// }
|
|
54
|
+
if (typeof result === 'object' && result !== null) {
|
|
55
|
+
const typedResult = result;
|
|
56
|
+
// redirect
|
|
57
|
+
if (typedResult.redirect) {
|
|
58
|
+
return res.redirect(typedResult.status ?? 302, typedResult.redirect);
|
|
59
|
+
}
|
|
60
|
+
//headers
|
|
61
|
+
if (typedResult.headers) {
|
|
62
|
+
for (const [h, v] of Object.entries(typedResult.headers)) {
|
|
63
|
+
res.setHeader(h, v);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//file
|
|
67
|
+
if (typedResult.file) {
|
|
68
|
+
const { path, filename, options } = typedResult.file;
|
|
69
|
+
if (filename) {
|
|
70
|
+
return res.download(path, filename, options);
|
|
71
|
+
}
|
|
72
|
+
return res.download(path, options);
|
|
73
|
+
}
|
|
74
|
+
// stream
|
|
75
|
+
if (typedResult.stream) {
|
|
76
|
+
if (typedResult.headers) {
|
|
77
|
+
for (const [h, v] of Object.entries(typedResult.headers))
|
|
78
|
+
res.setHeader(h, v);
|
|
79
|
+
}
|
|
80
|
+
return typedResult.stream.pipe(res);
|
|
81
|
+
}
|
|
82
|
+
// raw
|
|
83
|
+
if (typedResult.raw) {
|
|
84
|
+
if (typedResult.headers) {
|
|
85
|
+
for (const [h, v] of Object.entries(typedResult.headers))
|
|
86
|
+
res.setHeader(h, v);
|
|
87
|
+
}
|
|
88
|
+
return res.status(typedResult.status ?? 200).send(typedResult.raw);
|
|
89
|
+
}
|
|
90
|
+
if (typedResult.cookies) {
|
|
91
|
+
for (const [name, data] of Object.entries(typedResult.cookies)) {
|
|
92
|
+
res.cookie(name, data.value, data.options || {});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (typeof typedResult.status === 'number') {
|
|
96
|
+
return res.status(typedResult.status).json(typedResult.body ?? {});
|
|
97
|
+
}
|
|
98
|
+
return res.json(result);
|
|
99
|
+
}
|
|
100
|
+
// Suporte a retorno simples
|
|
45
101
|
if (typeof result === 'string')
|
|
46
102
|
return res.send(result);
|
|
47
103
|
if (typeof result === 'number')
|
|
@@ -50,31 +106,7 @@ function wrapHandler(fn, routePath, filePath) {
|
|
|
50
106
|
}
|
|
51
107
|
catch (err) {
|
|
52
108
|
const stack = err?.stack?.split('\n').slice(0, 3).join('\n') || '';
|
|
53
|
-
// logger.error(
|
|
54
|
-
// `✗ Runtime Error in route [${req.method} ${routePath}]\n` +
|
|
55
|
-
// ` File: ${filePath}\n` +
|
|
56
|
-
// ` ${err.name}: ${err.message || 'Unknown error'}\n` +
|
|
57
|
-
// ` Stack: ${stack}`
|
|
58
|
-
// );
|
|
59
|
-
let fileInfo = '';
|
|
60
|
-
if (err.stack) {
|
|
61
|
-
const stackLine = err.stack.split('\n')[1]; // pega primeira linha depois do erro
|
|
62
|
-
const match = stackLine.match(/\((.*):(\d+):(\d+)\)/);
|
|
63
|
-
if (match) {
|
|
64
|
-
const [_, file, line, col] = match;
|
|
65
|
-
fileInfo = `${file}:${line}:${col}`;
|
|
66
|
-
// Tenta mostrar o trecho da linha que deu erro
|
|
67
|
-
if (fs.existsSync(file)) {
|
|
68
|
-
const codeLines = fs.readFileSync(file, 'utf-8').split('\n');
|
|
69
|
-
const codeSnippet = codeLines[parseInt(line) - 1].trim();
|
|
70
|
-
fileInfo += ` → ${codeSnippet}`;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
// logger.group(`✗ Runtime Error in route [${req.method} ${routePath}]`);
|
|
75
109
|
logger.error(`${err.name}: ${err.message}`);
|
|
76
|
-
if (fileInfo)
|
|
77
|
-
logger.error(`Location: ${fileInfo}`);
|
|
78
110
|
next(err);
|
|
79
111
|
}
|
|
80
112
|
};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
import { Request as ExpressRequest, Response as ExpressResponse, NextFunction } from 'express';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* Podem ser usados nos handlers do usuário
|
|
3
|
+
* Tipos do Express reexportados para os usuários
|
|
5
4
|
*/
|
|
6
5
|
export type Request = ExpressRequest;
|
|
7
6
|
export type Response = ExpressResponse;
|
|
8
7
|
export type Next = NextFunction;
|
|
8
|
+
export {};
|
|
9
|
+
declare global {
|
|
10
|
+
type FastayResponse = {
|
|
11
|
+
status?: number;
|
|
12
|
+
body?: any;
|
|
13
|
+
cookies?: Record<string, {
|
|
14
|
+
value: string;
|
|
15
|
+
options?: any;
|
|
16
|
+
}>;
|
|
17
|
+
headers?: Record<string, string>;
|
|
18
|
+
redirect?: string;
|
|
19
|
+
file?: {
|
|
20
|
+
path: string;
|
|
21
|
+
filename?: string;
|
|
22
|
+
options?: any;
|
|
23
|
+
};
|
|
24
|
+
stream?: NodeJS.ReadableStream;
|
|
25
|
+
raw?: Buffer | string;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export type RouteHandler = (() => FastayResponse | any) | ((req: Request) => FastayResponse | any) | ((req: Request, res: Response) => FastayResponse | any);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
package/src/router.ts
CHANGED
|
@@ -47,41 +47,97 @@ function wrapHandler(fn: Function, routePath: string, filePath: string) {
|
|
|
47
47
|
return async (req: Request, res: Response, next: NextFunction) => {
|
|
48
48
|
try {
|
|
49
49
|
const result = fn.length >= 2 ? await fn(req, res) : await fn(req);
|
|
50
|
+
|
|
50
51
|
if (res.headersSent) return;
|
|
51
52
|
if (result === undefined) return;
|
|
52
|
-
if (typeof result === 'string') return res.send(result);
|
|
53
|
-
if (typeof result === 'number') return res.send(String(result));
|
|
54
|
-
return res.json(result);
|
|
55
|
-
} catch (err: any) {
|
|
56
|
-
const stack = err?.stack?.split('\n').slice(0, 3).join('\n') || '';
|
|
57
|
-
// logger.error(
|
|
58
|
-
// `✗ Runtime Error in route [${req.method} ${routePath}]\n` +
|
|
59
|
-
// ` File: ${filePath}\n` +
|
|
60
|
-
// ` ${err.name}: ${err.message || 'Unknown error'}\n` +
|
|
61
|
-
// ` Stack: ${stack}`
|
|
62
|
-
// );
|
|
63
53
|
|
|
64
|
-
|
|
65
|
-
if (
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
54
|
+
// Suporte a status e cookies customizado
|
|
55
|
+
// if (
|
|
56
|
+
// typeof result === 'object' &&
|
|
57
|
+
// 'status' in result &&
|
|
58
|
+
// 'body' in result &&
|
|
59
|
+
// typeof result.status === 'number'
|
|
60
|
+
// ) {
|
|
61
|
+
// return res.status(result.status).json(result.body);
|
|
62
|
+
// }
|
|
63
|
+
if (typeof result === 'object' && result !== null) {
|
|
64
|
+
const typedResult = result as {
|
|
65
|
+
status?: number;
|
|
66
|
+
body?: any;
|
|
67
|
+
|
|
68
|
+
cookies?: Record<string, { value: string; options?: any }>;
|
|
69
|
+
headers?: Record<string, string>;
|
|
70
|
+
redirect?: string;
|
|
71
|
+
file?: {
|
|
72
|
+
path: string;
|
|
73
|
+
filename?: string;
|
|
74
|
+
options?: any;
|
|
75
|
+
};
|
|
76
|
+
stream?: NodeJS.ReadableStream;
|
|
77
|
+
raw?: Buffer | string;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// redirect
|
|
81
|
+
if (typedResult.redirect) {
|
|
82
|
+
return res.redirect(typedResult.status ?? 302, typedResult.redirect);
|
|
83
|
+
}
|
|
71
84
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
85
|
+
//headers
|
|
86
|
+
if (typedResult.headers) {
|
|
87
|
+
for (const [h, v] of Object.entries(typedResult.headers)) {
|
|
88
|
+
res.setHeader(h, v);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
//file
|
|
93
|
+
if (typedResult.file) {
|
|
94
|
+
const { path, filename, options } = typedResult.file;
|
|
95
|
+
|
|
96
|
+
if (filename) {
|
|
97
|
+
return res.download(path, filename, options);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return res.download(path, options);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// stream
|
|
104
|
+
if (typedResult.stream) {
|
|
105
|
+
if (typedResult.headers) {
|
|
106
|
+
for (const [h, v] of Object.entries(typedResult.headers))
|
|
107
|
+
res.setHeader(h, v);
|
|
108
|
+
}
|
|
109
|
+
return typedResult.stream.pipe(res);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// raw
|
|
113
|
+
if (typedResult.raw) {
|
|
114
|
+
if (typedResult.headers) {
|
|
115
|
+
for (const [h, v] of Object.entries(typedResult.headers))
|
|
116
|
+
res.setHeader(h, v);
|
|
77
117
|
}
|
|
118
|
+
return res.status(typedResult.status ?? 200).send(typedResult.raw);
|
|
78
119
|
}
|
|
120
|
+
|
|
121
|
+
if (typedResult.cookies) {
|
|
122
|
+
for (const [name, data] of Object.entries(typedResult.cookies)) {
|
|
123
|
+
res.cookie(name, data.value, data.options || {});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (typeof typedResult.status === 'number') {
|
|
128
|
+
return res.status(typedResult.status).json(typedResult.body ?? {});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return res.json(result);
|
|
79
132
|
}
|
|
80
133
|
|
|
81
|
-
//
|
|
134
|
+
// Suporte a retorno simples
|
|
135
|
+
if (typeof result === 'string') return res.send(result);
|
|
136
|
+
if (typeof result === 'number') return res.send(String(result));
|
|
137
|
+
return res.json(result);
|
|
138
|
+
} catch (err: any) {
|
|
139
|
+
const stack = err?.stack?.split('\n').slice(0, 3).join('\n') || '';
|
|
82
140
|
logger.error(`${err.name}: ${err.message}`);
|
|
83
|
-
if (fileInfo) logger.error(`Location: ${fileInfo}`);
|
|
84
|
-
|
|
85
141
|
next(err);
|
|
86
142
|
}
|
|
87
143
|
};
|
package/src/types/index.ts
CHANGED
|
@@ -5,9 +5,35 @@ import {
|
|
|
5
5
|
} from 'express';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
* Podem ser usados nos handlers do usuário
|
|
8
|
+
* Tipos do Express reexportados para os usuários
|
|
10
9
|
*/
|
|
11
10
|
export type Request = ExpressRequest;
|
|
12
11
|
export type Response = ExpressResponse;
|
|
13
12
|
export type Next = NextFunction;
|
|
13
|
+
|
|
14
|
+
export {};
|
|
15
|
+
|
|
16
|
+
declare global {
|
|
17
|
+
type FastayResponse = {
|
|
18
|
+
status?: number;
|
|
19
|
+
body?: any;
|
|
20
|
+
|
|
21
|
+
cookies?: Record<string, { value: string; options?: any }>;
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
redirect?: string;
|
|
24
|
+
|
|
25
|
+
file?: {
|
|
26
|
+
path: string;
|
|
27
|
+
filename?: string;
|
|
28
|
+
options?: any;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
stream?: NodeJS.ReadableStream;
|
|
32
|
+
raw?: Buffer | string;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type RouteHandler =
|
|
37
|
+
| (() => FastayResponse | any)
|
|
38
|
+
| ((req: Request) => FastayResponse | any)
|
|
39
|
+
| ((req: Request, res: Response) => FastayResponse | any);
|