-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoSiren.js
50 lines (49 loc) · 1.44 KB
/
toSiren.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
function toSiren(service, annotate = {}) {
const current = service.state
return {
class: service.id,
title: current.meta.title || (service.machine.meta && service.machine.meta.title) || current.id,
properties: current.context,
links: [
{
rel: ["self"],
href: annotate.href("")
}
],
entities: [
{
class: "state",
properties: current
}
],
actions: current.nextEvents.reduce((actions, event) => {
let meta = current.meta[`${service.id}.${current.toStrings()[0]}`]
if (!meta) {
meta = { on: { [event]: {} } }
}
const { fields, ...rest} = ((meta.on||{})[event] || [])
if (Array.isArray(fields) || service.machine.transition(current, event).changed) {
let action = {
name: event,
href: annotate.href && annotate.href(event),
method: "PUT",
fields: (fields||[]).map(field => {
return Object.keys(field).reduce((field, key) => {
const resolver = ((service.machine.options.meta||{}).values||{})[field[key]]
if (resolver) {
if (typeof resolver === "function") {
field[key] = resolver(current.context)
}
}
return field
}, field)
}),
...rest
}
actions.push(action)
}
return actions
}, [])
}
}
module.exports = toSiren