-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.hook.ts
More file actions
executable file
·71 lines (60 loc) · 1.81 KB
/
Copy pathdebug.hook.ts
File metadata and controls
executable file
·71 lines (60 loc) · 1.81 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { HookContext, NextFunction } from '@feathersjs/feathers'
/**
* Logs the current hook context to the console for debugging purposes.
* Displays timestamp, service path, method, type, id, data, query, result, and
* any additional param fields you specify.
*
* @example
* ```ts
* import { debug } from 'feathers-utils/hooks'
*
* app.service('users').hooks({
* before: { find: [debug('before find', 'user')] }
* })
* ```
*
* @see https://utils.feathersjs.com/hooks/debug.html
*/
export const debug =
<H extends HookContext = HookContext>(msg: string, ...fieldNames: string[]) =>
async (context: H, next?: NextFunction): Promise<void> => {
if (next) {
await next()
}
// 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) => {
console.log(`params.${name}:`, params[name])
})
if (context.error) {
console.log('error', context.error)
}
}