-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbus.ts
executable file
·108 lines (100 loc) · 2.06 KB
/
bus.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
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env -S deno run
import {
Application,
codecs,
dapr,
DaprServerV1,
Events,
HTTPResponse,
log,
People,
RestModule,
unauthenticated,
} from "./iota.ts";
const app = new Application("dapr-example", "0.0.1")
.spec("apex.axdl")
.use(new RestModule(":8080"));
const statestore = "statestore";
const pubsub = "pubsub";
app.transport(
"dapr",
DaprServerV1({
address: ":19090",
subscriptions: [
{
pubsub: "pubsub",
topic: "welcome",
codec: codecs.CloudEventsJSON,
handler: Events.onWelcome,
},
],
}),
);
People.authorize(app, {
get: unauthenticated,
create: unauthenticated,
});
People.register(app, {
get: ({ input, flow }) =>
flow.then(
"Get state",
() =>
dapr.GetState({
store: statestore,
key: input.id,
notFoundError: "person_not_found",
}),
),
create: ({ input, flow }) =>
flow.then(
"Save state",
() =>
dapr.SetState({
store: statestore,
items: [{
key: input.id,
value: "input",
}],
}),
).then(
"Publish greeting message",
() =>
dapr.Publish({
pubsub: pubsub,
topic: "welcome",
data: `
{
"type": "welcome.v1",
"data": {
"message": "Welcome, " + ${input.firstName} + " " + ${input.lastName} + "!",
},
}`,
codec: codecs.CloudEventsJSON,
}),
).then(
"201 Created",
() =>
HTTPResponse({
status: 201,
headers: [
{
name: "Location",
value: `"{{host}}/people/" + ${input.id}`,
},
],
}),
),
});
Events.register(app, {
onWelcome: ({ input, flow }) =>
flow.then(
"Log the welcome message",
() => log("Event received: [%s] %q", input.type, input.data.message),
),
});
app.error("person_not_found", {
type: "PersonNotFound",
code: "not_found",
title: "Person not found",
message: "Person for id {{ .key }} is not found",
});