/
RAMLImporter.coffee
190 lines (145 loc) · 6.67 KB
/
RAMLImporter.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
186
187
188
189
190
require "yaml.min.js"
# Hacks to use browser version since we can't use in Paw JS API 1.0
# raml = require "raml-parser.min.js"
this.window = this
this.setTimeout = (timeout) ->
return
require "raml-parser.min.js"
RAMLImporter = ->
# Create Paw requests from a RAML Request (object)
@createPawRequest = (context, ramlCollection, ramlRequestPath, ramlRequestMethod, ramlRequestValue) ->
if ramlRequestValue.summary
ramlRequestTitle = ramlRequestValue.summary
else
ramlRequestTitle = ramlRequestPath
headers = {}
queries = {}
formData = {}
body
# Extract contentType from Consumes and add the first one to Headers
if ramlRequestValue.consumes
for contentType in ramlRequestValue.consumes
headers["Content-Type"] = contentType
break
# Extract Headers and Query params
for index, ramlRequestParamValue of ramlRequestValue.parameters
# Add Queries
if ramlRequestParamValue.in == 'query' and ramlRequestParamValue.type == 'string'
queries[ramlRequestParamValue.name] = ramlRequestParamValue.name
# Add Headers
if ramlRequestParamValue.in == 'header' and ramlRequestParamValue.type == 'string'
headers[ramlRequestParamValue.name] = ramlRequestParamValue.name
# Add Url Encoded
if ramlRequestParamValue.in == 'formData' and ramlRequestParamValue.type == 'string'
formData[ramlRequestParamValue.name] = ramlRequestParamValue.name
# Add Body
if ramlRequestParamValue.in == 'body' #Only string
body = @json_from_definition_schema ramlCollection, ramlRequestParamValue.schema
ramlRequestUrl = @createRAMLRequestUrl ramlCollection, ramlRequestPath, queries
ramlRequestMethod = ramlRequestMethod.toUpperCase()
# Create Paw request
pawRequest = context.createRequest ramlRequestTitle, ramlRequestMethod, ramlRequestUrl
# Add Headers
for key, value of headers
pawRequest.setHeader key, value
# Add Basic Auth if required
pawRequest.setHeader "Authorization", "HTTP Basic Auth (Username/Password)" if @has_basic_auth ramlCollection, ramlRequestValue
# Set raw body
pawRequest.body = body if body
# Set Form URL-Encoded body
if Object.keys(formData).length > 0
# Set Form URL-Encoded body
if headers['Content-Type'] == "application/x-www-form-urlencoded"
pawRequest.urlEncodedBody = formData
# Set Multipart body
else if headers['Content-Type'] == "multipart/form-data"
pawRequest.multipartBody = formData
return pawRequest
@has_basic_auth = (ramlCollection, ramlRequestValue) ->
if ramlRequestValue.security
for security in ramlRequestValue.security
for own key, value of security
if ramlCollection.securityDefinitions[key] and ramlCollection.securityDefinitions[key].type == 'basic'
return true
break
return false
@json_from_definition_schema = (ramlCollection, property, indent = 0) ->
if property.type == 'string'
s = "\"string\""
else if property.type == 'integer'
s = "0"
else if property.type == 'boolean'
s = "true"
else if typeof(property) == 'object'
indent_str = Array(indent + 1).join(' ')
indent_str_children = Array(indent + 2).join(' ')
if property.items
property = property.items
s = "[\n" +
"#{indent_str_children}#{@json_from_definition_schema(ramlCollection, property, indent+1)}" +
"\n#{indent_str}]"
else
property = ramlCollection.definitions[property["$ref"].split('/').pop()] if property["$ref"]
property = property.properties if property.properties # Skip properties
s = "{\n" +
("#{indent_str_children}\"#{key}\" : #{@json_from_definition_schema(ramlCollection, value, indent+1)}" for key, value of property).join(',\n') +
"\n#{indent_str}}"
return s
@createRAMLRequestUrl = (ramlCollection, ramlRequestPath, queries) ->
# Build ramlRequestQueries
if Object.keys(queries).length > 0
ramlRequestQueries = []
for key, value of queries
ramlRequestQueries.push "#{key}=#{value}"
ramlRequestUrl = (if ramlCollection.schemes then ramlCollection.schemes[0] else 'http') +
'://' +
(ramlCollection.host or 'echo.luckymarmot.com') +
(ramlCollection.basePath or '') +
ramlRequestPath
if ramlRequestQueries
ramlRequestUrl = ramlRequestUrl + '?' + ramlRequestQueries.join('&')
return ramlRequestUrl
@createPawGroup = (context, ramlCollection, ramlRequestPathName, ramlRequestPathValue) ->
# Create Paw group
pawGroup = context.createRequestGroup ramlRequestPathName
for own ramlRequestMethod, ramlRequestValue of ramlRequestPathValue
# Create a Paw request
pawRequest = @createPawRequest context, ramlCollection, ramlRequestPathName, ramlRequestMethod, ramlRequestValue
# Add request to root group
pawGroup.appendChild pawRequest
return pawGroup
@importString = (context, string) ->
RAML.Parser.load(string).then ((data) ->
# Callback never called =(
ramlCollection = data
console.log 'Data: ' + data
return
), (error) ->
console.log 'Error parsing: ' + error
return
# try
# # Try YAML parse
# ramlCollection = yaml.load string
# catch yamlParseError
# throw new Error "Invalid RAML file format"
# if ramlCollection
#
# # Define host to localhost if not specified in file
# ramlCollection.host = if ramlCollection.host then ramlCollection.host else 'localhost'
#
# # Create a PawGroup
# pawRootGroup = context.createRequestGroup ramlCollection.info.title
#
# # Add RAML groups
# for own ramlRequestPathName, ramlRequestPathValue of ramlCollection.paths
#
# pawGroup = @createPawGroup context, ramlCollection, ramlRequestPathName, ramlRequestPathValue
#
# # Add group to root
# pawRootGroup.appendChild pawGroup
#
# return true
return
RAMLImporter.identifier = "com.luckymarmot.PawExtensions.RAMLImporter"
RAMLImporter.title = "RAML Importer"
registerImporter RAMLImporter