This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAPIBlueprintGenerator.coffee
More file actions
155 lines (129 loc) · 4.34 KB
/
Copy pathAPIBlueprintGenerator.coffee
File metadata and controls
155 lines (129 loc) · 4.34 KB
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
# in API v0.2.0 and below (Paw 2.2.2 and below), require had no return value
((root) ->
if root.bundle?.minApiVersion('0.2.0')
root.Mustache = require("./mustache")
else
require("mustache.js")
)(this)
APIBlueprintGenerator = ->
templateAction = readFile("apiblueprint-action.mustache")
templateGroup = readFile("apiblueprint-group.mustache")
# Generate a response dictionary for the mustache context from a paw HTTPExchange
#
# @param [HTTPExchange] exchange The paw HTTP exchange for the response
#
# @return [Object] The template context object
#
@response = (exchange) ->
if !exchange
return null
headers = []
is_json = false
for key, value of exchange.responseHeaders
if key in ['Content-Type', 'Connection', 'Date', 'Via', 'Server', 'Content-Length']
if key == 'Content-Type'
is_json = value.search(/(json)/i) > -1
continue
headers.push({ key: key, value: value })
has_headers = (headers.length > 0)
body = exchange.responseBody
has_body = body.length > 0
if has_body
if is_json
body = JSON.stringify(JSON.parse(body), null, 4)
body_indentation = ' '
if has_headers
body_indentation += ' '
body = body.replace(/^/gm, body_indentation)
return {
statusCode: exchange.responseStatusCode,
contentType: exchange.responseHeaders['Content-Type'],
"headers?": has_headers,
headers: headers
"body?": has_headers && has_body,
body: body,
}
# Generate a request dictionary for the mustache context from a paw Request
#
# @param [Request] exchange The paw HTTP request
#
# @return [Object] The template context object
#
@request = (paw_request) ->
headers = []
is_json = false
for key, value of paw_request.headers
if key in ['Content-Type']
is_json = (value.search(/(json)/i) > -1)
continue
headers.push({ key: key, value: value })
has_headers = (headers.length > 0)
body = paw_request.body
has_body = body.length > 0
if has_body
if is_json
body = JSON.stringify(JSON.parse(body), null, 4)
body_indentation = ' '
if has_headers
body_indentation += ' '
body = body.replace(/^/gm, body_indentation)
description = paw_request.description
has_description = description && description.length > 0
if has_headers || has_body || paw_request.headers['Content-Type']
return {
"headers?": has_headers,
headers: headers,
contentType: paw_request.headers['Content-Type'],
"body?": has_headers && has_body,
body: body,
"description?": has_description,
description: description,
}
# Get a path from a URL
#
# @param [String] url The given URL
#
# @return [String] The path from the URL
@path = (url) ->
path = url.replace(/^https?:\/\/[^\/]+/i, '')
if !path
path = '/'
path
@renderPawItems = (items, level = 1) ->
sections = for item in items
@renderPawItem(item, level)
sections.join("\n")
@renderPawItem = (item, level = 1) ->
if item.toString().match(/^RequestGroup/)
@renderPawGroup(item, level)
else
@renderPawRequest(item, level)
@renderPawGroup = (paw_group, level = 1) ->
sections = []
sections.push Mustache.render(templateGroup,
level: "#".repeat(level),
name: paw_group.name
)
children = paw_group.getChildren().sort (a, b) -> a.order - b.order
sections = sections.concat @renderPawItems(children, level + 1)
sections.join("\n")
@renderPawRequest = (paw_request, level = 1) ->
url = paw_request.url
Mustache.render(templateAction,
level: "#".repeat(level),
name: paw_request.name.replace(/[\[\]\(\)]/g, ''),
method: paw_request.method,
path: @path(url),
request: @request(paw_request),
response: @response(paw_request.getLastExchange()),
)
@generate = (context, requests, options) ->
if context.runtimeInfo.task == 'exportAllRequests'
@renderPawItems(context.getRootRequestTreeItems())
else
@renderPawItems(requests)
return
APIBlueprintGenerator.identifier = "io.apiary.PawExtensions.APIBlueprintGenerator"
APIBlueprintGenerator.title = "API Blueprint Generator"
APIBlueprintGenerator.fileExtension = "md"
registerCodeGenerator APIBlueprintGenerator