/
fn.js
171 lines (154 loc) · 4.99 KB
/
fn.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
import { Map } from "immutable"
import win from "../../window"
/**
* if duplicate key name existed from FormData entries,
* we mutated the key name by appending a hashIdx
* @param {String} k - possibly mutated key name
* @return {String} - src key name
*/
const extractKey = (k) => {
const hashIdx = "_**[]"
if (k.indexOf(hashIdx) < 0) {
return k
}
return k.split(hashIdx)[0].trim()
}
const escapeShell = (str) => {
if (str === "-d ") {
return str
}
// eslint-disable-next-line no-useless-escape
if (!/^[_\/-]/g.test(str))
return ("'" + str
.replace(/'/g, "'\\''") + "'")
else
return str
}
const escapeCMD = (str) => {
str = str
.replace(/\^/g, "^^")
.replace(/\\"/g, "\\\\\"")
.replace(/"/g, "\"\"")
.replace(/\n/g, "^\n")
if (str === "-d ") {
return str
.replace(/-d /g, "-d ^\n")
}
// eslint-disable-next-line no-useless-escape
if (!/^[_\/-]/g.test(str))
return "\"" + str + "\""
else
return str
}
const escapePowershell = (str) => {
if (str === "-d ") {
return str
}
if (/\n/.test(str)) {
const escaped = str.replace(/`/g, "``").replace(/\$/g, "`$")
return `@"\n${escaped}\n"@`
}
if (!/^[_\/-]/.test(str)) { // eslint-disable-line no-useless-escape
const escaped = str.replace(/'/g, "''")
return `'${escaped}'`
}
return str
}
function getStringBodyOfMap(request) {
let curlifyToJoin = []
for (let [k, v] of request.get("body").entrySeq()) {
let extractedKey = extractKey(k)
if (v instanceof win.File) {
curlifyToJoin.push(` "${extractedKey}": {\n "name": "${v.name}"${v.type ? `,\n "type": "${v.type}"` : ""}\n }`)
} else {
curlifyToJoin.push(` "${extractedKey}": ${JSON.stringify(v, null, 2).replace(/(\r\n|\r|\n)/g, "\n ")}`)
}
}
return `{\n${curlifyToJoin.join(",\n")}\n}`
}
const curlify = (request, escape, newLine, ext = "") => {
let isMultipartFormDataRequest = false
let curlified = ""
const addWords = (...args) => curlified += " " + args.map(escape).join(" ")
const addWordsWithoutLeadingSpace = (...args) => curlified += args.map(escape).join(" ")
const addNewLine = () => curlified += ` ${newLine}`
const addIndent = (level = 1) => curlified += " ".repeat(level)
let headers = request.get("headers")
curlified += "curl" + ext
if (request.has("curlOptions")) {
addWords(...request.get("curlOptions"))
}
addWords("-X", request.get("method"))
addNewLine()
addIndent()
addWordsWithoutLeadingSpace(`${request.get("url")}`)
if (headers && headers.size) {
for (let p of request.get("headers").entries()) {
addNewLine()
addIndent()
let [h, v] = p
addWordsWithoutLeadingSpace("-H", `${h}: ${v}`)
isMultipartFormDataRequest = isMultipartFormDataRequest || /^content-type$/i.test(h) && /^multipart\/form-data$/i.test(v)
}
}
const body = request.get("body")
if (body) {
if (isMultipartFormDataRequest && ["POST", "PUT", "PATCH"].includes(request.get("method"))) {
for (let [k, v] of body.entrySeq()) {
let extractedKey = extractKey(k)
addNewLine()
addIndent()
addWordsWithoutLeadingSpace("-F")
/**
* SwaggerClient produces specialized sub-class of File class, that only
* accepts string data and retain this data in `data`
* public property throughout the lifecycle of its instances.
*
* This sub-class is exclusively used only when Encoding Object
* is defined within the Media Type Object (OpenAPI 3.x.y).
*/
if (v instanceof win.File && typeof v.valueOf() === "string") {
addWords(`${extractedKey}=${v.data}${v.type ? `;type=${v.type}` : ""}`)
} else if (v instanceof win.File) {
addWords(`${extractedKey}=@${v.name}${v.type ? `;type=${v.type}` : ""}`)
} else {
addWords(`${extractedKey}=${v}`)
}
}
} else if(body instanceof win.File) {
addNewLine()
addIndent()
addWordsWithoutLeadingSpace(`--data-binary '@${body.name}'`)
} else {
addNewLine()
addIndent()
addWordsWithoutLeadingSpace("-d ")
let reqBody = body
if (!Map.isMap(reqBody)) {
if (typeof reqBody !== "string") {
reqBody = JSON.stringify(reqBody)
}
addWordsWithoutLeadingSpace(reqBody)
} else {
addWordsWithoutLeadingSpace(getStringBodyOfMap(request))
}
}
} else if (!body && request.get("method") === "POST") {
addNewLine()
addIndent()
addWordsWithoutLeadingSpace("-d ''")
}
return curlified
}
// eslint-disable-next-line camelcase
export const requestSnippetGenerator_curl_powershell = (request) => {
return curlify(request, escapePowershell, "`\n", ".exe")
}
// eslint-disable-next-line camelcase
export const requestSnippetGenerator_curl_bash = (request) => {
return curlify(request, escapeShell, "\\\n")
}
// eslint-disable-next-line camelcase
export const requestSnippetGenerator_curl_cmd = (request) => {
return curlify(request, escapeCMD, "^\n")
}