-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatedWorkflowAPIsTest.js
60 lines (55 loc) · 1.74 KB
/
StatedWorkflowAPIsTest.js
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
import path from "path";
import fs from "fs";
import {StatedWorkflow} from "./src/workflow/StatedWorkflow.js";
import StatedREPL from "stated-js/dist/src/StatedREPL.js";
import {startApi, stopApi} from "./example/workflow-demo.js";
const apiPromise = startApi();
const createWorkflow = async () => {
const templateStr = fs.readFileSync(path.join('example', 'wfHttp01.json'), 'utf8');
return fetch('http://localhost:8080/workflow', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: templateStr,
}).then(response => response.json())
.then(data => {
console.log('Success:', data);
}).catch((error) => {
console.error('Error:', error);
});
}
const sendEvent = async () => {
const requestContext = {
url: 'http://localhost:8080/event',
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify([{
type: 'myType',
subscriberId: 'mySubscriberId',
data: {
key: 'value'
}
}])
};
return fetch(requestContext.url, requestContext)
.then(response => {
console.log(`sendEvent response: ${StatedREPL.stringify(response)}`);
response.json()
}).catch((error) => {
console.error('Error:', error);
});
}
apiPromise.then(async (result) => {
console.log(`apiPromise resolved with ${result}`);
await createWorkflow();
await sendEvent();
}).catch((error) => {
console.log(`apiPromise rejected with ${error}`);
}).finally(async () => {
console.log(`apiPromise finally`);
// await stopApi();
});