@loomcore/api 0.1.50 → 0.1.52
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.
|
@@ -10,7 +10,7 @@ export declare class MigrationRunner {
|
|
|
10
10
|
private parseSql;
|
|
11
11
|
private getMigrator;
|
|
12
12
|
private wipeDatabase;
|
|
13
|
-
run(command
|
|
13
|
+
run(command?: 'up' | 'down' | 'reset', target?: string): Promise<void>;
|
|
14
14
|
private closeConnection;
|
|
15
15
|
create(name: string): Promise<void>;
|
|
16
16
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
2
|
export interface RequestLifecycleOptions {
|
|
3
3
|
onRequestStart?: (req: Request, res: Response) => void | Promise<void>;
|
|
4
|
-
onRequestEnd?: (req: Request, res: Response, error?: Error) => void | Promise<void>;
|
|
4
|
+
onRequestEnd?: (req: Request, res: Response, duration: number, error?: Error) => void | Promise<void>;
|
|
5
5
|
}
|
|
6
6
|
export declare const requestLifecycle: (options?: RequestLifecycleOptions) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const requestLifecycle = (options = {}) => {
|
|
2
2
|
const { onRequestStart, onRequestEnd } = options;
|
|
3
3
|
return async (req, res, next) => {
|
|
4
|
-
|
|
4
|
+
const startTime = Date.now();
|
|
5
5
|
if (onRequestStart) {
|
|
6
6
|
try {
|
|
7
7
|
await onRequestStart(req, res);
|
|
@@ -17,7 +17,8 @@ export const requestLifecycle = (options = {}) => {
|
|
|
17
17
|
endCallbackCalled = true;
|
|
18
18
|
if (onRequestEnd) {
|
|
19
19
|
try {
|
|
20
|
-
|
|
20
|
+
const duration = Date.now() - startTime;
|
|
21
|
+
await onRequestEnd(req, res, duration, error);
|
|
21
22
|
}
|
|
22
23
|
catch (callbackError) {
|
|
23
24
|
console.error('Error in onRequestEnd callback:', callbackError);
|
|
@@ -25,21 +26,13 @@ export const requestLifecycle = (options = {}) => {
|
|
|
25
26
|
}
|
|
26
27
|
};
|
|
27
28
|
res.on('finish', () => {
|
|
28
|
-
|
|
29
|
-
callOnRequestEnd(error);
|
|
29
|
+
callOnRequestEnd();
|
|
30
30
|
});
|
|
31
31
|
res.on('close', () => {
|
|
32
32
|
if (!res.writableEnded && !endCallbackCalled) {
|
|
33
|
-
|
|
34
|
-
callOnRequestEnd(error);
|
|
33
|
+
callOnRequestEnd(new Error('Request closed before completion'));
|
|
35
34
|
}
|
|
36
35
|
});
|
|
37
|
-
|
|
38
|
-
await next();
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
req.lifecycleError = error instanceof Error ? error : new Error(String(error));
|
|
42
|
-
throw error;
|
|
43
|
-
}
|
|
36
|
+
next();
|
|
44
37
|
};
|
|
45
38
|
};
|