forked from W-Games/Paw-APIBlueprintGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIBlueprintGenerator.coffee
121 lines (104 loc) · 3.37 KB
/
APIBlueprintGenerator.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
# 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 = ->
# 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
@generate = (context) ->
paw_request = context.getCurrentRequest()
url = paw_request.url
template = readFile("apiblueprint.mustache")
Mustache.render(template,
method: paw_request.method,
path: @path(url),
request: @request(paw_request),
response: @response(paw_request.getLastExchange()),
)
return
APIBlueprintGenerator.identifier = "io.apiary.PawExtensions.APIBlueprintGenerator"
APIBlueprintGenerator.title = "API Blueprint Generator"
APIBlueprintGenerator.fileExtension = "md"
registerCodeGenerator APIBlueprintGenerator