forked from ErikWittern/openapi-snippet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
193 lines (170 loc) · 4.98 KB
/
index.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
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
191
192
193
/**
* swagger-snippet
* Generate code snippets for given Swagger / Open API Specification files
*
* Author: Erik Wittern
* License: MIT
*/
'use strict'
var SwaggerToHar = require('./swagger-to-har.js')
var HTTPSnippet = require('httpsnippet')
var getEndpointSnippets = function (swagger, path, method, targets, values) {
// if optional parameter is not provided, set it to empty object
if (typeof values === 'undefined') {
values = {}
}
var har = SwaggerToHar.getEndpoint(swagger, path, method, values)
var snippet = new HTTPSnippet(har)
var snippets = []
for (var j in targets) {
var target = formatTarget(targets[j])
if (!target) throw new Error('Invalid target: ' + targets[j])
snippets.push({
id: targets[j],
title: target.title,
content: snippet.convert(target.language, typeof target.library !== 'undefined' ? target.library : null)
})
}
return {
method: har.method,
url: har.url,
description: har.description,
resource: getResourceName(har.url),
snippets: snippets
}
}
var getSwaggerSnippets = function (swagger, targets) {
var harList = SwaggerToHar.getAll(swagger)
var results = []
for (var i in harList) {
// create HTTPSnippet object:
var har = harList[i]
var snippet = new HTTPSnippet(har.har)
var snippets = []
for (var j in targets) {
var target = formatTarget(targets[j])
if (!target) throw new Error('Invalid target: ' + targets[j])
snippets.push({
id: targets[j],
title: target.title,
content: snippet.convert(target.language, typeof target.library !== 'undefined' ? target.library : null)
})
}
results.push({
method: har.method,
url: har.url,
description: har.description,
resource: getResourceName(har.url),
snippets: snippets
})
}
// sort results:
results.sort(function (a, b) {
if (a.resource < b.resource) {
return -1
} else if (a.resource > b.resource) {
return 1
} else {
return getMethodOrder(a.method.toLowerCase(), b.method.toLowerCase())
}
})
return results
}
/**
* Determine the order of HTTP methods.
*
* @param {string} a One HTTP verb in lower case
* @param {string} b Another HTTP verb in lower case
* @return {number} The order instruction for the given HTTP verbs
*/
var getMethodOrder = function (a, b) {
var order = ['get', 'post', 'put', 'delete', 'patch']
if (order.indexOf(a) === -1) {
return 1
} else if (order.indexOf(b) === -1) {
return -1
} else if (order.indexOf(a) < order.indexOf(b)) {
return -1
} else if (order.indexOf(a) > order.indexOf(b)) {
return 1
} else {
return 0
}
}
/**
* Determines the name of the resource exposed by the method.
* E.g., ../users/{userId} --> users
*
* @param {string} urlStr The Swagger path definition
* @return {string} The determined resource name
*/
var getResourceName = function (urlStr) {
var pathComponents = urlStr.split('/')
for (var i = pathComponents.length - 1; i >= 0; i--) {
var cand = pathComponents[i]
if (cand !== '' && !/^{/.test(cand)) {
return cand
}
}
}
/**
* Format the given target by splitting up language and library and making sure
* that HTTP Snippet supports them.
*
* @param {string} targetStr String defining a target, e.g., node_request
* @return {object} Object with formatted target, or null
*/
var formatTarget = function (targetStr) {
var language = targetStr.split('_')[0]
var title = capitalizeFirstLetter(language)
var library = targetStr.split('_')[1]
var validTargets = HTTPSnippet.availableTargets()
var validLanguage = false
var validLibrary = false
for (var i in validTargets) {
var target = validTargets[i]
if (language === target.key) {
validLanguage = true
if (typeof library === 'undefined') {
library = target.default
validLibrary = true
} else {
for (var j in target.clients) {
var client = target.clients[j]
if (library === client.key) {
validLibrary = true
break
}
}
}
}
}
if (!validLanguage || !validLibrary) {
return null
}
return {
title: typeof library !== 'undefined' ? title + ' + ' + capitalizeFirstLetter(library) : title,
language: language,
library: library
}
}
var capitalizeFirstLetter = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
module.exports = {
getSwaggerSnippets: getSwaggerSnippets,
getEndpointSnippets: getEndpointSnippets
}
// The if is only for when this is run from the browser
if (typeof window !== 'undefined') {
// grab existing namespace object, or create a blank object
// if it doesn't exist
var SwaggerSnippet = window.SwaggerSnippet || {}
// define that object
SwaggerSnippet = {
getSwaggerSnippets: getSwaggerSnippets,
getEndpointSnippets: getEndpointSnippets
}
// replace/create the global namespace
window.SwaggerSnippet = SwaggerSnippet
}