forked from feathersjs-ecosystem/feathers-hooks-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.ts
More file actions
executable file
·55 lines (45 loc) · 1.46 KB
/
debug.ts
File metadata and controls
executable file
·55 lines (45 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* eslint-disable no-console */
import type { HookContext } from '@feathersjs/feathers';
/**
* Display the current hook context for debugging.
* @see https://hooks-common.feathersjs.com/hooks.html#debug
*/
export function debug<H extends HookContext = HookContext>(msg: string, ...fieldNames: string[]) {
return (context: H) => {
// display timestamp
const now = new Date();
console.log(
`${now.getFullYear()}-${
now.getMonth() + 1
}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`,
);
if (msg) {
console.log(msg);
}
// display service, method & type of hook (before/after/error)
console.log(`${context.type} service('${context.path}').${context.method}()`);
// display id for get, patch, update & remove
if (!['find', 'create'].includes(context.method) && 'id' in context) {
console.log('id:', context.id);
}
if (context.data) {
console.log('data:', context.data);
}
if (context.params?.query) {
console.log('query:', context.params.query);
}
if (context.result) {
console.log('result:', context.result);
}
// display additional params
const params = context.params || {};
console.log('params props:', Object.keys(params).sort());
fieldNames.forEach(name => {
// @ts-ignore
console.log(`params.${name}:`, params[name]);
});
if (context.error) {
console.log('error', context.error);
}
};
}