-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
index.ts
97 lines (85 loc) 路 1.85 KB
/
index.ts
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import * as Hemera from './../../../packages/hemera'
import * as nats from 'nats'
type FileResult = {
do: string
isDone: boolean
}
const hemera = new Hemera(nats.connect('nats://127.0.0.1:4242'), {
logLevel: 'debug'
})
hemera.ready(async (err: Error) => {
// callback-style
hemera.ext('onAct', function(hemera, next) {
// some code
next()
})
hemera.ext('onActFinished', function(hemera, next) {
// some code
next()
})
hemera.ext('preHandler', function(hemera, request, reply, next) {
// some code
next()
})
hemera.ext('onRequest', function(hemera, request, reply, next) {
// some code
next()
})
hemera.ext('onSend', function(hemera, request, reply, next) {
// some code
next()
})
hemera.ext('onResponse', function(hemera, reply, next) {
// some code
next()
})
// async/await
hemera.ext('preHandler', async function(hemera, request, reply) {
// some code
})
hemera.ext('onAct', async hemera => {
// some code
})
hemera.ext('onResponse', async function(hemera, reply) {
// some code
})
hemera.ext('onClose', async addDefinition => {
// some code
})
hemera.ext('onAdd', async addDefinition => {
// some code
})
hemera.add(
{
topic: 'test'
},
function(request: Hemera.ServerPattern, cb: Hemera.NodeCallback) {
cb(null, true)
}
)
hemera.add(
{
topic: 'testAsync'
},
async function(request: Hemera.ServerPattern) {
return true
}
)
hemera.act(
{
topic: 'test'
},
function(error: Error, response: any) {
this.log.info(error, response)
this.log.info(this.trace$)
}
)
const response1 = await hemera.act({
topic: 'test'
})
const response2 = await hemera.act<FileResult>({
topic: 'test'
})
response2.data.do
response1.context.log.info(response1.data)
})