-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
80 lines (76 loc) · 1.74 KB
/
app.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
/*
* Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
*
* This file is part of the Restate Examples for the Node.js/TypeScript SDK,
* which is released under the MIT license.
*
* You can find a copy of the license in the file LICENSE
* in the root directory of this repository or package or at
* https://github.com/restatedev/examples/blob/main/LICENSE
*/
import * as restate from "@restatedev/restate-sdk";
import { xstate, fromPromise } from "@restatedev/xstate";
import { createMachine, sendTo } from "xstate";
const authServerMachine = createMachine(
{
id: "server",
initial: "waitingForCode",
states: {
waitingForCode: {
on: {
CODE: {
target: "process",
},
},
},
process: {
invoke: {
id: "process",
src: "authorise",
onDone: {
actions: sendTo(
({ self }) => self._parent!,
{ type: "TOKEN" },
{ delay: 1000 }
),
},
},
},
},
},
{
actors: {
authorise: fromPromise(
() => new Promise((resolve) => setTimeout(resolve, 5000))
),
},
}
);
const authClientMachine = createMachine({
id: "client",
initial: "idle",
states: {
idle: {
on: {
AUTH: { target: "authorizing" },
},
},
authorizing: {
invoke: {
id: "auth-server",
src: authServerMachine,
},
entry: sendTo("auth-server", ({ self }) => ({
type: "CODE",
sender: self,
})),
on: {
TOKEN: { target: "authorized" },
},
},
authorized: {
type: "final",
},
},
});
await restate.endpoint().bind(xstate("auth", authClientMachine)).listen();