/
refract-adapter.coffee
185 lines (140 loc) · 5.27 KB
/
refract-adapter.coffee
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
_ = require('./refract/helper')
blueprintApi = require('../blueprint-api')
markdown = require('./markdown')
getDescription = (element) ->
raw = _.chain(element).copy().first().content().value() or ''
html = if raw then markdown.toHtmlSync(raw) else ''
return {raw, html}
getHeaders = (element) ->
headers = {}
httpHeaders = _.get(element, 'attributes.headers')
return headers if not httpHeaders
_.content(httpHeaders).forEach((headerElement) ->
content = _.content(headerElement)
key = _.get(content, 'key.content')
value = _.get(content, 'value.content')
headers[key] = value if key
)
headers
transformResources = (element) ->
resources = []
_.resources(element).forEach((resourceElement) ->
resourceDescription = getDescription(resourceElement)
_.transitions(resourceElement).forEach((transitionElement) ->
description = getDescription(transitionElement)
resource = new blueprintApi.Resource({
url: _.get(resourceElement, 'attributes.href')
# uriTemplate
# method is set when iterating httpTransaction
name: _.get(resourceElement, 'meta.title')
# headers
# actionHeaders
description: resourceDescription.raw
htmlDescription: resourceDescription.html
actionName: _.get(transitionElement, 'meta.tite')
# model
# resourceParameters
# actionParameters
actionDescription: description.raw
actionHtmlDescription: description.html
# requests
# responses
# attributes
# resolvedAttributes
# actionRelation
# actionUriTemplate
})
requests = []
responses = []
_.httpTransactions(transitionElement).forEach((httpTransaction) ->
httpRequest = _.chain(httpTransaction).httpRequests().first().value()
httpResponse = _.chain(httpTransaction).httpResponses().first().value()
httpRequestBody = _.chain(httpRequest).messageBodies().first().value()
httpResponseBody = _.chain(httpResponse).messageBodies().first().value()
console.log JSON.stringify(httpResponse, null, 2), _.messageBodies(httpResponse)
httpRequestBodySchemas = _.chain(httpRequest).messageBodySchemas().first()
httpResponseBodySchemas = _.chain(httpResponse).messageBodySchemas().first()
httpRequestDescription = getDescription(httpRequest)
httpResponseDescription = getDescription(httpResponse)
# In refract just here we have ,ethod
resource.method = _.get(httpRequest, 'attributes.method')
request = new blueprintApi.Request({
name: _.get(httpRequest, 'meta.title')
description: httpRequestDescription.raw
htmlDescription: httpRequestDescription.html
headers: getHeaders(httpRequest)
# reference
body: _.content(httpRequestBody)
schema: _.content(httpRequestBodySchemas)
# exampleId
attributes: _.get(httpRequestBody, 'attributes')
# resolvedAttributes
})
response = new blueprintApi.Response({
status: _.get(httpResponse, 'attributes.statusCode')
description: httpResponseDescription.raw
htmlDescription: httpResponseDescription.html
headers: getHeaders(httpResponse)
# reference
body: _.content(httpResponseBody)
schema: _.content(httpResponseBodySchemas)
# exampleId
attributes: _.get(httpResponseBody, 'attributes')
# resolvedAttributes
})
requests.push(request)
responses.push(response)
)
resource.requests = requests
resource.request = requests[0]
resource.responses = responses
resources.push(resource)
)
)
resources
transformSections = (element) ->
resourceGroups = _.chain(element)
.content()
.filter({element: 'category', meta: {classes: ['resourceGroup']}})
.value()
resourceGroups.map((resourceGroupElement) ->
section = new blueprintApi.Section({
name: _.get(resourceGroupElement, 'meta.title')
})
description = getDescription(resourceGroupElement)
section.description = description.raw
section.htmlDescription = description.html
section.resources = transformResources(resourceGroupElement)
section
)
transformAst = (element) ->
category = _.chain(element).get('content').filter({element: 'category'}).first().value()
applicationAst = new blueprintApi.Blueprint({
name: _.get(category, 'meta.title')
metadata: []
})
# Metadata and location
metadata = []
_.chain(category)
.get('attributes.meta')
.filter({meta: {classes: ['user']}})
.value()
.forEach((entry) ->
content = _.content(entry)
name = _.get(content, 'key.content')
value = _.get(content, 'value.content')
metadata.push({name, value})
applicationAst.location = value if name is 'HOST'
)
applicationAst.metadata = metadata
# description
description = getDescription(category)
applicationAst.description = description.raw
applicationAst.htmlDescription = description.html
# Sections
applicationAst.sections = transformSections(category)
applicationAst
module.exports = {
transformAst
transformError: (source, err) -> err
}