@readme/httpsnippet 2.5.0 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -9
- package/package.json +24 -28
- package/src/helpers/code-builder.js +85 -80
- package/src/helpers/form-data.js +27 -25
- package/src/helpers/headers.js +10 -19
- package/src/helpers/reducer.js +10 -14
- package/src/index.js +167 -146
- package/src/targets/c/index.js +3 -5
- package/src/targets/c/libcurl.js +18 -23
- package/src/targets/clojure/clj_http.js +103 -87
- package/src/targets/clojure/index.js +3 -5
- package/src/targets/csharp/httpclient.js +90 -90
- package/src/targets/csharp/index.js +3 -5
- package/src/targets/csharp/restsharp.js +20 -22
- package/src/targets/go/index.js +3 -5
- package/src/targets/go/native.js +46 -53
- package/src/targets/http/http1.1.js +26 -36
- package/src/targets/http/index.js +3 -5
- package/src/targets/index.js +2 -4
- package/src/targets/java/asynchttp.js +22 -23
- package/src/targets/java/index.js +3 -5
- package/src/targets/java/nethttp.js +20 -24
- package/src/targets/java/okhttp.js +31 -31
- package/src/targets/java/unirest.js +22 -19
- package/src/targets/javascript/axios.js +43 -38
- package/src/targets/javascript/fetch.js +66 -68
- package/src/targets/javascript/index.js +3 -5
- package/src/targets/javascript/jquery.js +46 -40
- package/src/targets/javascript/xhr.js +42 -38
- package/src/targets/kotlin/index.js +3 -5
- package/src/targets/kotlin/okhttp.js +31 -31
- package/src/targets/node/axios.js +33 -30
- package/src/targets/node/fetch.js +89 -83
- package/src/targets/node/index.js +4 -5
- package/src/targets/node/native.js +36 -30
- package/src/targets/node/request.js +64 -64
- package/src/targets/node/unirest.js +56 -55
- package/src/targets/objc/helpers.js +24 -23
- package/src/targets/objc/index.js +3 -5
- package/src/targets/objc/nsurlsession.js +73 -45
- package/src/targets/ocaml/cohttp.js +29 -27
- package/src/targets/ocaml/index.js +3 -5
- package/src/targets/php/curl.js +86 -82
- package/src/targets/php/guzzle.js +135 -0
- package/src/targets/php/helpers.js +29 -28
- package/src/targets/php/http1.js +33 -44
- package/src/targets/php/http2.js +70 -71
- package/src/targets/php/index.js +5 -5
- package/src/targets/powershell/common.js +35 -30
- package/src/targets/powershell/index.js +3 -5
- package/src/targets/powershell/restmethod.js +3 -5
- package/src/targets/powershell/webrequest.js +3 -5
- package/src/targets/python/helpers.js +37 -34
- package/src/targets/python/index.js +4 -5
- package/src/targets/python/python3.js +33 -44
- package/src/targets/python/requests.js +47 -72
- package/src/targets/r/httr.js +59 -70
- package/src/targets/r/index.js +3 -5
- package/src/targets/ruby/index.js +3 -5
- package/src/targets/ruby/native.js +39 -33
- package/src/targets/shell/curl.js +78 -51
- package/src/targets/shell/helpers.js +7 -9
- package/src/targets/shell/httpie.js +45 -39
- package/src/targets/shell/index.js +4 -5
- package/src/targets/shell/wget.js +20 -22
- package/src/targets/swift/helpers.js +35 -32
- package/src/targets/swift/index.js +3 -5
- package/src/targets/swift/nsurlsession.js +53 -39
- package/src/.DS_Store +0 -0
package/src/index.js
CHANGED
|
@@ -1,106 +1,113 @@
|
|
|
1
|
-
|
|
1
|
+
const es = require('event-stream');
|
|
2
|
+
const MultiPartForm = require('form-data');
|
|
3
|
+
const qs = require('qs');
|
|
4
|
+
const reducer = require('./helpers/reducer');
|
|
5
|
+
const helpers = require('./helpers/headers');
|
|
6
|
+
const targets = require('./targets');
|
|
7
|
+
const url = require('url');
|
|
8
|
+
const validate = require('har-validator/lib/async');
|
|
2
9
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const es = require('event-stream')
|
|
6
|
-
const MultiPartForm = require('form-data')
|
|
7
|
-
const qs = require('qs')
|
|
8
|
-
const reducer = require('./helpers/reducer')
|
|
9
|
-
const helpers = require('./helpers/headers')
|
|
10
|
-
const targets = require('./targets')
|
|
11
|
-
const url = require('url')
|
|
12
|
-
const validate = require('har-validator/lib/async')
|
|
13
|
-
|
|
14
|
-
const { formDataIterator, isBlob } = require('./helpers/form-data.js')
|
|
10
|
+
const { formDataIterator, isBlob } = require('./helpers/form-data');
|
|
15
11
|
|
|
16
12
|
// constructor
|
|
17
13
|
const HTTPSnippet = function (data, opts = {}) {
|
|
18
|
-
let entries
|
|
19
|
-
const self = this
|
|
20
|
-
const input =
|
|
14
|
+
let entries;
|
|
15
|
+
const self = this;
|
|
16
|
+
const input = { ...data };
|
|
21
17
|
|
|
22
|
-
const options =
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
const options = {
|
|
19
|
+
harIsAlreadyEncoded: false,
|
|
20
|
+
...opts,
|
|
21
|
+
};
|
|
25
22
|
|
|
26
23
|
// prep the main container
|
|
27
|
-
self.requests = []
|
|
24
|
+
self.requests = [];
|
|
28
25
|
|
|
29
26
|
// is it har?
|
|
30
27
|
if (input.log && input.log.entries) {
|
|
31
|
-
entries = input.log.entries
|
|
28
|
+
entries = input.log.entries;
|
|
32
29
|
} else {
|
|
33
|
-
entries = [
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
entries = [
|
|
31
|
+
{
|
|
32
|
+
request: input,
|
|
33
|
+
},
|
|
34
|
+
];
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
entries.forEach(function (entry) {
|
|
38
|
+
const request = { ...entry.request };
|
|
39
|
+
|
|
39
40
|
// add optional properties to make validation successful
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
validate.request(
|
|
41
|
+
request.httpVersion = request.httpVersion || 'HTTP/1.1';
|
|
42
|
+
request.queryString = request.queryString || [];
|
|
43
|
+
request.headers = request.headers || [];
|
|
44
|
+
request.cookies = request.cookies || [];
|
|
45
|
+
request.postData = request.postData || {};
|
|
46
|
+
request.postData.mimeType = request.postData.mimeType || 'application/octet-stream';
|
|
47
|
+
|
|
48
|
+
request.bodySize = 0;
|
|
49
|
+
request.headersSize = 0;
|
|
50
|
+
request.postData.size = 0;
|
|
51
|
+
|
|
52
|
+
validate.request(request, function (err, valid) {
|
|
52
53
|
if (!valid) {
|
|
53
|
-
throw err
|
|
54
|
+
throw err;
|
|
54
55
|
}
|
|
55
56
|
|
|
56
|
-
self.requests.push(self.prepare(
|
|
57
|
-
})
|
|
58
|
-
})
|
|
59
|
-
}
|
|
57
|
+
self.requests.push(self.prepare(request, options));
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
};
|
|
60
61
|
|
|
61
62
|
HTTPSnippet.prototype.prepare = function (request, options) {
|
|
62
63
|
// construct utility properties
|
|
63
|
-
request.queryObj = {}
|
|
64
|
-
request.headersObj = {}
|
|
65
|
-
request.cookiesObj = {}
|
|
66
|
-
request.allHeaders = {}
|
|
67
|
-
request.postData.jsonObj = false
|
|
68
|
-
request.postData.paramsObj = false
|
|
64
|
+
request.queryObj = {};
|
|
65
|
+
request.headersObj = {};
|
|
66
|
+
request.cookiesObj = {};
|
|
67
|
+
request.allHeaders = {};
|
|
68
|
+
request.postData.jsonObj = false;
|
|
69
|
+
request.postData.paramsObj = false;
|
|
69
70
|
|
|
70
71
|
// construct query objects
|
|
71
72
|
if (request.queryString && request.queryString.length) {
|
|
72
|
-
request.queryObj = request.queryString.reduce(reducer, {})
|
|
73
|
+
request.queryObj = request.queryString.reduce(reducer, {});
|
|
73
74
|
}
|
|
74
75
|
|
|
75
76
|
// construct headers objects
|
|
76
77
|
if (request.headers && request.headers.length) {
|
|
77
|
-
const http2VersionRegex = /^HTTP\/2
|
|
78
|
+
const http2VersionRegex = /^HTTP\/2/;
|
|
78
79
|
request.headersObj = request.headers.reduce(function (headers, header) {
|
|
79
|
-
let headerName = header.name
|
|
80
|
+
let headerName = header.name;
|
|
80
81
|
if (request.httpVersion.match(http2VersionRegex)) {
|
|
81
|
-
headerName = headerName.toLowerCase()
|
|
82
|
+
headerName = headerName.toLowerCase();
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
// eslint-disable-next-line no-param-reassign
|
|
86
|
+
headers[headerName] = header.value;
|
|
87
|
+
return headers;
|
|
88
|
+
}, {});
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
// construct headers objects
|
|
90
92
|
if (request.cookies && request.cookies.length) {
|
|
91
93
|
request.cookiesObj = request.cookies.reduceRight(function (cookies, cookie) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
94
|
+
// eslint-disable-next-line no-param-reassign
|
|
95
|
+
cookies[cookie.name] = cookie.value;
|
|
96
|
+
return cookies;
|
|
97
|
+
}, {});
|
|
95
98
|
}
|
|
96
99
|
|
|
97
100
|
// construct Cookie header
|
|
98
101
|
const cookies = request.cookies.map(function (cookie) {
|
|
99
|
-
|
|
100
|
-
|
|
102
|
+
if (options.harIsAlreadyEncoded) {
|
|
103
|
+
return `${cookie.name}=${cookie.value}`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`;
|
|
107
|
+
});
|
|
101
108
|
|
|
102
109
|
if (cookies.length) {
|
|
103
|
-
request.allHeaders.cookie = cookies.join('; ')
|
|
110
|
+
request.allHeaders.cookie = cookies.join('; ');
|
|
104
111
|
}
|
|
105
112
|
|
|
106
113
|
switch (request.postData.mimeType) {
|
|
@@ -109,11 +116,11 @@ HTTPSnippet.prototype.prepare = function (request, options) {
|
|
|
109
116
|
case 'multipart/form-data':
|
|
110
117
|
case 'multipart/alternative':
|
|
111
118
|
// reset values
|
|
112
|
-
request.postData.text = ''
|
|
113
|
-
request.postData.mimeType = 'multipart/form-data'
|
|
119
|
+
request.postData.text = '';
|
|
120
|
+
request.postData.mimeType = 'multipart/form-data';
|
|
114
121
|
|
|
115
122
|
if (request.postData.params) {
|
|
116
|
-
const form = new MultiPartForm()
|
|
123
|
+
const form = new MultiPartForm();
|
|
117
124
|
|
|
118
125
|
// The `form-data` module returns one of two things: a native FormData object, or its own polyfill. Since the
|
|
119
126
|
// polyfill does not support the full API of the native FormData object, when this library is running in a
|
|
@@ -129,212 +136,226 @@ HTTPSnippet.prototype.prepare = function (request, options) {
|
|
|
129
136
|
// This hack is pretty awful but it's the only way we can use this library in the browser as if we code this
|
|
130
137
|
// against just the native FormData object, we can't polyfill that back into Node because Blob and File objects,
|
|
131
138
|
// which something like `formdata-polyfill` requires, don't exist there.
|
|
132
|
-
const isNativeFormData =
|
|
139
|
+
const isNativeFormData = typeof form[Symbol.iterator] === 'function';
|
|
133
140
|
|
|
134
141
|
// easter egg
|
|
135
|
-
const boundary = '---011000010111000001101001'
|
|
142
|
+
const boundary = '---011000010111000001101001';
|
|
136
143
|
if (!isNativeFormData) {
|
|
137
|
-
form._boundary = boundary
|
|
144
|
+
form._boundary = boundary;
|
|
138
145
|
}
|
|
139
146
|
|
|
140
147
|
request.postData.params.forEach(function (param) {
|
|
141
|
-
const name = param.name
|
|
142
|
-
const value = param.value || ''
|
|
143
|
-
const filename = param.fileName || null
|
|
148
|
+
const name = param.name;
|
|
149
|
+
const value = param.value || '';
|
|
150
|
+
const filename = param.fileName || null;
|
|
144
151
|
|
|
145
152
|
if (isNativeFormData) {
|
|
146
153
|
if (isBlob(value)) {
|
|
147
|
-
form.append(name, value, filename)
|
|
154
|
+
form.append(name, value, filename);
|
|
148
155
|
} else {
|
|
149
|
-
form.append(name, value)
|
|
156
|
+
form.append(name, value);
|
|
150
157
|
}
|
|
151
158
|
} else {
|
|
152
159
|
form.append(name, value, {
|
|
153
160
|
filename: filename,
|
|
154
|
-
contentType: param.contentType || null
|
|
155
|
-
})
|
|
161
|
+
contentType: param.contentType || null,
|
|
162
|
+
});
|
|
156
163
|
}
|
|
157
|
-
})
|
|
164
|
+
});
|
|
158
165
|
|
|
159
166
|
if (isNativeFormData) {
|
|
167
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
160
168
|
for (const data of formDataIterator(form, boundary)) {
|
|
161
|
-
request.postData.text += data
|
|
169
|
+
request.postData.text += data;
|
|
162
170
|
}
|
|
163
171
|
} else {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
172
|
+
form.pipe(
|
|
173
|
+
// eslint-disable-next-line array-callback-return
|
|
174
|
+
es.map(function (data) {
|
|
175
|
+
request.postData.text += data;
|
|
176
|
+
})
|
|
177
|
+
);
|
|
168
178
|
}
|
|
169
179
|
|
|
170
|
-
request.postData.boundary = boundary
|
|
180
|
+
request.postData.boundary = boundary;
|
|
171
181
|
|
|
172
182
|
// Since headers are case-sensitive we need to see if there's an existing `Content-Type` header that we can
|
|
173
183
|
// override.
|
|
174
184
|
const contentTypeHeader = helpers.hasHeader(request.headersObj, 'content-type')
|
|
175
185
|
? helpers.getHeaderName(request.headersObj, 'content-type')
|
|
176
|
-
: 'content-type'
|
|
186
|
+
: 'content-type';
|
|
177
187
|
|
|
178
|
-
request.headersObj[contentTypeHeader] =
|
|
188
|
+
request.headersObj[contentTypeHeader] = `multipart/form-data; boundary=${boundary}`;
|
|
179
189
|
}
|
|
180
|
-
break
|
|
190
|
+
break;
|
|
181
191
|
|
|
182
192
|
case 'application/x-www-form-urlencoded':
|
|
183
193
|
if (!request.postData.params) {
|
|
184
|
-
request.postData.text = ''
|
|
194
|
+
request.postData.text = '';
|
|
185
195
|
} else {
|
|
186
|
-
request.postData.paramsObj = request.postData.params.reduce(reducer, {})
|
|
196
|
+
request.postData.paramsObj = request.postData.params.reduce(reducer, {});
|
|
187
197
|
|
|
188
198
|
// always overwrite
|
|
189
|
-
request.postData.text = qs.stringify(request.postData.paramsObj)
|
|
199
|
+
request.postData.text = qs.stringify(request.postData.paramsObj);
|
|
190
200
|
}
|
|
191
|
-
break
|
|
201
|
+
break;
|
|
192
202
|
|
|
193
203
|
case 'text/json':
|
|
194
204
|
case 'text/x-json':
|
|
195
205
|
case 'application/json':
|
|
196
206
|
case 'application/x-json':
|
|
197
|
-
request.postData.mimeType = 'application/json'
|
|
207
|
+
request.postData.mimeType = 'application/json';
|
|
198
208
|
|
|
199
209
|
if (request.postData.text) {
|
|
200
210
|
try {
|
|
201
|
-
request.postData.jsonObj = JSON.parse(request.postData.text)
|
|
211
|
+
request.postData.jsonObj = JSON.parse(request.postData.text);
|
|
202
212
|
} catch (e) {
|
|
203
213
|
// force back to text/plain
|
|
204
214
|
// if headers have proper content-type value, then this should also work
|
|
205
|
-
request.postData.mimeType = 'text/plain'
|
|
215
|
+
request.postData.mimeType = 'text/plain';
|
|
206
216
|
}
|
|
207
217
|
}
|
|
208
|
-
break
|
|
218
|
+
break;
|
|
219
|
+
|
|
220
|
+
default:
|
|
221
|
+
// no-op
|
|
209
222
|
}
|
|
210
223
|
|
|
211
224
|
// create allHeaders object
|
|
212
|
-
request.allHeaders = Object.assign(request.allHeaders, request.headersObj)
|
|
225
|
+
request.allHeaders = Object.assign(request.allHeaders, request.headersObj);
|
|
213
226
|
|
|
214
227
|
// deconstruct the uri
|
|
215
228
|
// eslint-disable-next-line node/no-deprecated-api
|
|
216
|
-
request.uriObj = url.parse(request.url, true, true)
|
|
229
|
+
request.uriObj = url.parse(request.url, true, true);
|
|
217
230
|
|
|
218
231
|
// merge all possible queryString values
|
|
219
|
-
request.queryObj = Object.assign(request.queryObj, request.uriObj.query)
|
|
232
|
+
request.queryObj = Object.assign(request.queryObj, request.uriObj.query);
|
|
220
233
|
|
|
221
234
|
// reset uriObj values for a clean url
|
|
222
|
-
request.uriObj.query = null
|
|
223
|
-
request.uriObj.search = null
|
|
224
|
-
request.uriObj.path = request.uriObj.pathname
|
|
235
|
+
request.uriObj.query = null;
|
|
236
|
+
request.uriObj.search = null;
|
|
237
|
+
request.uriObj.path = request.uriObj.pathname;
|
|
225
238
|
|
|
226
239
|
// keep the base url clean of queryString
|
|
227
|
-
request.url = url.format(request.uriObj)
|
|
240
|
+
request.url = url.format(request.uriObj);
|
|
228
241
|
|
|
229
242
|
// update the uri object
|
|
230
|
-
request.uriObj.query = request.queryObj
|
|
231
|
-
if (options.
|
|
243
|
+
request.uriObj.query = request.queryObj;
|
|
244
|
+
if (options.harIsAlreadyEncoded) {
|
|
232
245
|
request.uriObj.search = qs.stringify(request.queryObj, {
|
|
233
|
-
|
|
234
|
-
|
|
246
|
+
encode: false,
|
|
247
|
+
indices: false,
|
|
248
|
+
});
|
|
235
249
|
} else {
|
|
236
250
|
request.uriObj.search = qs.stringify(request.queryObj, {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
})
|
|
251
|
+
indices: false,
|
|
252
|
+
});
|
|
240
253
|
}
|
|
241
254
|
|
|
242
255
|
if (request.uriObj.search) {
|
|
243
|
-
request.uriObj.path = request.uriObj.pathname
|
|
256
|
+
request.uriObj.path = `${request.uriObj.pathname}?${request.uriObj.search}`;
|
|
244
257
|
}
|
|
245
258
|
|
|
246
259
|
// construct a full url
|
|
247
|
-
request.fullUrl = url.format(request.uriObj)
|
|
260
|
+
request.fullUrl = url.format(request.uriObj);
|
|
248
261
|
|
|
249
|
-
return request
|
|
250
|
-
}
|
|
262
|
+
return request;
|
|
263
|
+
};
|
|
251
264
|
|
|
252
265
|
HTTPSnippet.prototype.convert = function (target, client, opts) {
|
|
253
266
|
if (!opts && client) {
|
|
254
|
-
|
|
267
|
+
// eslint-disable-next-line no-param-reassign
|
|
268
|
+
opts = client;
|
|
255
269
|
}
|
|
256
270
|
|
|
257
|
-
const func = this._matchTarget(target, client)
|
|
271
|
+
const func = this._matchTarget(target, client);
|
|
258
272
|
if (func) {
|
|
259
273
|
const results = this.requests.map(function (request) {
|
|
260
|
-
return func(request, opts)
|
|
261
|
-
})
|
|
274
|
+
return func(request, opts);
|
|
275
|
+
});
|
|
262
276
|
|
|
263
|
-
return results.length === 1 ? results[0] : results
|
|
277
|
+
return results.length === 1 ? results[0] : results;
|
|
264
278
|
}
|
|
265
279
|
|
|
266
|
-
return false
|
|
267
|
-
}
|
|
280
|
+
return false;
|
|
281
|
+
};
|
|
268
282
|
|
|
269
283
|
HTTPSnippet.prototype._matchTarget = function (target, client) {
|
|
270
284
|
// does it exist?
|
|
271
285
|
// eslint-disable-next-line no-prototype-builtins
|
|
272
286
|
if (!targets.hasOwnProperty(target)) {
|
|
273
|
-
return false
|
|
287
|
+
return false;
|
|
274
288
|
}
|
|
275
289
|
|
|
276
290
|
// shorthand
|
|
277
291
|
if (typeof client === 'string' && typeof targets[target][client] === 'function') {
|
|
278
|
-
return targets[target][client]
|
|
292
|
+
return targets[target][client];
|
|
279
293
|
}
|
|
280
294
|
|
|
281
295
|
// default target
|
|
282
|
-
return targets[target][targets[target].info.default]
|
|
283
|
-
}
|
|
296
|
+
return targets[target][targets[target].info.default];
|
|
297
|
+
};
|
|
284
298
|
|
|
285
299
|
// exports
|
|
286
|
-
module.exports = HTTPSnippet
|
|
300
|
+
module.exports = HTTPSnippet;
|
|
287
301
|
|
|
288
302
|
module.exports.addTarget = function (target) {
|
|
289
303
|
if (!('info' in target)) {
|
|
290
|
-
throw new Error('The supplied custom target must contain an `info` object.')
|
|
291
|
-
} else if (
|
|
292
|
-
|
|
293
|
-
|
|
304
|
+
throw new Error('The supplied custom target must contain an `info` object.');
|
|
305
|
+
} else if (
|
|
306
|
+
!('key' in target.info) ||
|
|
307
|
+
!('title' in target.info) ||
|
|
308
|
+
!('extname' in target.info) ||
|
|
309
|
+
!('default' in target.info)
|
|
310
|
+
) {
|
|
311
|
+
throw new Error(
|
|
312
|
+
'The supplied custom target must have an `info` object with a `key`, `title`, `extname`, and `default` property.'
|
|
313
|
+
);
|
|
314
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
294
315
|
} else if (targets.hasOwnProperty(target.info.key)) {
|
|
295
|
-
throw new Error('The supplied custom target already exists.')
|
|
316
|
+
throw new Error('The supplied custom target already exists.');
|
|
296
317
|
} else if (Object.keys(target).length === 1) {
|
|
297
|
-
throw new Error('A custom target must have a client defined on it.')
|
|
318
|
+
throw new Error('A custom target must have a client defined on it.');
|
|
298
319
|
}
|
|
299
320
|
|
|
300
|
-
targets[target.info.key] = target
|
|
301
|
-
}
|
|
321
|
+
targets[target.info.key] = target;
|
|
322
|
+
};
|
|
302
323
|
|
|
303
324
|
module.exports.addTargetClient = function (target, client) {
|
|
304
325
|
// eslint-disable-next-line no-prototype-builtins
|
|
305
326
|
if (!targets.hasOwnProperty(target)) {
|
|
306
|
-
throw new Error(`Sorry, but no ${target} target exists to add clients to.`)
|
|
327
|
+
throw new Error(`Sorry, but no ${target} target exists to add clients to.`);
|
|
307
328
|
} else if (!('info' in client)) {
|
|
308
|
-
throw new Error('The supplied custom target client must contain an `info` object.')
|
|
329
|
+
throw new Error('The supplied custom target client must contain an `info` object.');
|
|
309
330
|
} else if (!('key' in client.info) || !('title' in client.info)) {
|
|
310
|
-
throw new Error('The supplied custom target client must have an `info` object with a `key` and `title` property.')
|
|
311
|
-
|
|
331
|
+
throw new Error('The supplied custom target client must have an `info` object with a `key` and `title` property.');
|
|
332
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
312
333
|
} else if (targets[target].hasOwnProperty(client.info.key)) {
|
|
313
|
-
throw new Error('The supplied custom target client already exists, please use a different key')
|
|
334
|
+
throw new Error('The supplied custom target client already exists, please use a different key');
|
|
314
335
|
}
|
|
315
336
|
|
|
316
|
-
targets[target][client.info.key] = client
|
|
317
|
-
}
|
|
337
|
+
targets[target][client.info.key] = client;
|
|
338
|
+
};
|
|
318
339
|
|
|
319
340
|
module.exports.availableTargets = function () {
|
|
320
341
|
return Object.keys(targets).map(function (key) {
|
|
321
|
-
const target =
|
|
342
|
+
const target = { ...targets[key].info };
|
|
322
343
|
const clients = Object.keys(targets[key])
|
|
323
344
|
.filter(function (prop) {
|
|
324
|
-
return
|
|
345
|
+
return !['info', 'index'].includes(prop);
|
|
325
346
|
})
|
|
326
347
|
.map(function (client) {
|
|
327
|
-
return targets[key][client].info
|
|
328
|
-
})
|
|
348
|
+
return targets[key][client].info;
|
|
349
|
+
});
|
|
329
350
|
|
|
330
351
|
if (clients.length) {
|
|
331
|
-
target.clients = clients
|
|
352
|
+
target.clients = clients;
|
|
332
353
|
}
|
|
333
354
|
|
|
334
|
-
return target
|
|
335
|
-
})
|
|
336
|
-
}
|
|
355
|
+
return target;
|
|
356
|
+
});
|
|
357
|
+
};
|
|
337
358
|
|
|
338
359
|
module.exports.extname = function (target) {
|
|
339
|
-
return targets[target] ? targets[target].info.extname : ''
|
|
340
|
-
}
|
|
360
|
+
return targets[target] ? targets[target].info.extname : '';
|
|
361
|
+
};
|
package/src/targets/c/index.js
CHANGED
package/src/targets/c/libcurl.js
CHANGED
|
@@ -1,50 +1,45 @@
|
|
|
1
|
-
|
|
1
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
module.exports = function (source) {
|
|
4
|
+
const code = new CodeBuilder();
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
code.push('CURL *hnd = curl_easy_init();')
|
|
6
|
+
code
|
|
7
|
+
.push('CURL *hnd = curl_easy_init();')
|
|
9
8
|
.blank()
|
|
10
9
|
.push('curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "%s");', source.method.toUpperCase())
|
|
11
|
-
.push('curl_easy_setopt(hnd, CURLOPT_URL, "%s");', source.fullUrl)
|
|
10
|
+
.push('curl_easy_setopt(hnd, CURLOPT_URL, "%s");', source.fullUrl);
|
|
12
11
|
|
|
13
12
|
// Add headers, including the cookies
|
|
14
|
-
const headers = Object.keys(source.headersObj)
|
|
13
|
+
const headers = Object.keys(source.headersObj);
|
|
15
14
|
|
|
16
15
|
// construct headers
|
|
17
16
|
if (headers.length) {
|
|
18
|
-
code.blank()
|
|
19
|
-
.push('struct curl_slist *headers = NULL;')
|
|
17
|
+
code.blank().push('struct curl_slist *headers = NULL;');
|
|
20
18
|
|
|
21
19
|
headers.forEach(function (key) {
|
|
22
|
-
code.push('headers = curl_slist_append(headers, "%s: %s");', key, source.headersObj[key])
|
|
23
|
-
})
|
|
20
|
+
code.push('headers = curl_slist_append(headers, "%s: %s");', key, source.headersObj[key]);
|
|
21
|
+
});
|
|
24
22
|
|
|
25
|
-
code.push('curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);')
|
|
23
|
+
code.push('curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);');
|
|
26
24
|
}
|
|
27
25
|
|
|
28
26
|
// construct cookies
|
|
29
27
|
if (source.allHeaders.cookie) {
|
|
30
|
-
code.blank()
|
|
31
|
-
.push('curl_easy_setopt(hnd, CURLOPT_COOKIE, "%s");', source.allHeaders.cookie)
|
|
28
|
+
code.blank().push('curl_easy_setopt(hnd, CURLOPT_COOKIE, "%s");', source.allHeaders.cookie);
|
|
32
29
|
}
|
|
33
30
|
|
|
34
31
|
if (source.postData.text) {
|
|
35
|
-
code.blank()
|
|
36
|
-
.push('curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, %s);', JSON.stringify(source.postData.text))
|
|
32
|
+
code.blank().push('curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, %s);', JSON.stringify(source.postData.text));
|
|
37
33
|
}
|
|
38
34
|
|
|
39
|
-
code.blank()
|
|
40
|
-
.push('CURLcode ret = curl_easy_perform(hnd);')
|
|
35
|
+
code.blank().push('CURLcode ret = curl_easy_perform(hnd);');
|
|
41
36
|
|
|
42
|
-
return code.join()
|
|
43
|
-
}
|
|
37
|
+
return code.join();
|
|
38
|
+
};
|
|
44
39
|
|
|
45
40
|
module.exports.info = {
|
|
46
41
|
key: 'libcurl',
|
|
47
42
|
title: 'Libcurl',
|
|
48
43
|
link: 'http://curl.haxx.se/libcurl/',
|
|
49
|
-
description: 'Simple REST and HTTP API Client for C'
|
|
50
|
-
}
|
|
44
|
+
description: 'Simple REST and HTTP API Client for C',
|
|
45
|
+
};
|