@readme/httpsnippet 2.4.5 → 3.0.1
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 -27
- package/src/helpers/code-builder.js +85 -80
- package/src/helpers/form-data.js +28 -26
- package/src/helpers/headers.js +18 -8
- package/src/helpers/reducer.js +10 -14
- package/src/index.js +175 -151
- package/src/targets/c/index.js +3 -5
- package/src/targets/c/libcurl.js +20 -25
- package/src/targets/clojure/clj_http.js +126 -102
- package/src/targets/clojure/index.js +3 -5
- package/src/targets/csharp/httpclient.js +93 -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 +47 -54
- package/src/targets/http/http1.1.js +27 -37
- 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 +47 -42
- package/src/targets/javascript/fetch.js +66 -68
- package/src/targets/javascript/index.js +3 -5
- package/src/targets/javascript/jquery.js +50 -44
- package/src/targets/javascript/xhr.js +48 -44
- package/src/targets/kotlin/index.js +3 -5
- package/src/targets/kotlin/okhttp.js +31 -31
- package/src/targets/node/axios.js +37 -34
- package/src/targets/node/fetch.js +94 -87
- package/src/targets/node/index.js +4 -5
- package/src/targets/node/native.js +51 -45
- package/src/targets/node/request.js +69 -69
- package/src/targets/node/unirest.js +70 -68
- package/src/targets/objc/helpers.js +32 -25
- package/src/targets/objc/index.js +3 -5
- package/src/targets/objc/nsurlsession.js +108 -79
- package/src/targets/ocaml/cohttp.js +32 -30
- 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 +31 -29
- package/src/targets/php/http1.js +38 -49
- package/src/targets/php/http2.js +75 -75
- 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 +42 -37
- package/src/targets/python/index.js +4 -5
- package/src/targets/python/python3.js +33 -45
- package/src/targets/python/requests.js +48 -72
- package/src/targets/r/httr.js +60 -73
- package/src/targets/r/index.js +3 -5
- package/src/targets/ruby/index.js +3 -5
- package/src/targets/ruby/native.js +41 -35
- package/src/targets/shell/curl.js +108 -43
- package/src/{helpers/shell.js → 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 +44 -35
- package/src/targets/swift/index.js +3 -5
- package/src/targets/swift/nsurlsession.js +93 -79
- 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
|
-
var es = require('event-stream')
|
|
6
|
-
var MultiPartForm = require('form-data')
|
|
7
|
-
var qs = require('qs')
|
|
8
|
-
var reducer = require('./helpers/reducer')
|
|
9
|
-
var helpers = require('./helpers/headers')
|
|
10
|
-
var targets = require('./targets')
|
|
11
|
-
var url = require('url')
|
|
12
|
-
var 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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
const HTTPSnippet = function (data, opts = {}) {
|
|
14
|
+
let entries;
|
|
15
|
+
const self = this;
|
|
16
|
+
const input = { ...data };
|
|
21
17
|
|
|
22
|
-
|
|
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
|
-
|
|
78
|
+
const http2VersionRegex = /^HTTP\/2/;
|
|
78
79
|
request.headersObj = request.headers.reduce(function (headers, header) {
|
|
79
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
+
const cookies = request.cookies.map(function (cookie) {
|
|
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
|
-
|
|
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,209 +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) {
|
|
160
|
-
|
|
161
|
-
|
|
167
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
168
|
+
for (const data of formDataIterator(form, boundary)) {
|
|
169
|
+
request.postData.text += data;
|
|
162
170
|
}
|
|
163
171
|
} else {
|
|
164
|
-
form.pipe(
|
|
165
|
-
|
|
166
|
-
|
|
172
|
+
form.pipe(
|
|
173
|
+
// eslint-disable-next-line array-callback-return
|
|
174
|
+
es.map(function (data) {
|
|
175
|
+
request.postData.text += data;
|
|
176
|
+
})
|
|
177
|
+
);
|
|
167
178
|
}
|
|
168
179
|
|
|
169
|
-
request.postData.boundary = boundary
|
|
180
|
+
request.postData.boundary = boundary;
|
|
170
181
|
|
|
171
182
|
// Since headers are case-sensitive we need to see if there's an existing `Content-Type` header that we can
|
|
172
183
|
// override.
|
|
173
184
|
const contentTypeHeader = helpers.hasHeader(request.headersObj, 'content-type')
|
|
174
185
|
? helpers.getHeaderName(request.headersObj, 'content-type')
|
|
175
|
-
: 'content-type'
|
|
186
|
+
: 'content-type';
|
|
176
187
|
|
|
177
|
-
request.headersObj[contentTypeHeader] =
|
|
188
|
+
request.headersObj[contentTypeHeader] = `multipart/form-data; boundary=${boundary}`;
|
|
178
189
|
}
|
|
179
|
-
break
|
|
190
|
+
break;
|
|
180
191
|
|
|
181
192
|
case 'application/x-www-form-urlencoded':
|
|
182
193
|
if (!request.postData.params) {
|
|
183
|
-
request.postData.text = ''
|
|
194
|
+
request.postData.text = '';
|
|
184
195
|
} else {
|
|
185
|
-
request.postData.paramsObj = request.postData.params.reduce(reducer, {})
|
|
196
|
+
request.postData.paramsObj = request.postData.params.reduce(reducer, {});
|
|
186
197
|
|
|
187
198
|
// always overwrite
|
|
188
|
-
request.postData.text = qs.stringify(request.postData.paramsObj)
|
|
199
|
+
request.postData.text = qs.stringify(request.postData.paramsObj);
|
|
189
200
|
}
|
|
190
|
-
break
|
|
201
|
+
break;
|
|
191
202
|
|
|
192
203
|
case 'text/json':
|
|
193
204
|
case 'text/x-json':
|
|
194
205
|
case 'application/json':
|
|
195
206
|
case 'application/x-json':
|
|
196
|
-
request.postData.mimeType = 'application/json'
|
|
207
|
+
request.postData.mimeType = 'application/json';
|
|
197
208
|
|
|
198
209
|
if (request.postData.text) {
|
|
199
210
|
try {
|
|
200
|
-
request.postData.jsonObj = JSON.parse(request.postData.text)
|
|
211
|
+
request.postData.jsonObj = JSON.parse(request.postData.text);
|
|
201
212
|
} catch (e) {
|
|
202
213
|
// force back to text/plain
|
|
203
214
|
// if headers have proper content-type value, then this should also work
|
|
204
|
-
request.postData.mimeType = 'text/plain'
|
|
215
|
+
request.postData.mimeType = 'text/plain';
|
|
205
216
|
}
|
|
206
217
|
}
|
|
207
|
-
break
|
|
218
|
+
break;
|
|
219
|
+
|
|
220
|
+
default:
|
|
221
|
+
// no-op
|
|
208
222
|
}
|
|
209
223
|
|
|
210
224
|
// create allHeaders object
|
|
211
|
-
request.allHeaders = Object.assign(request.allHeaders, request.headersObj)
|
|
225
|
+
request.allHeaders = Object.assign(request.allHeaders, request.headersObj);
|
|
212
226
|
|
|
213
227
|
// deconstruct the uri
|
|
214
|
-
|
|
228
|
+
// eslint-disable-next-line node/no-deprecated-api
|
|
229
|
+
request.uriObj = url.parse(request.url, true, true);
|
|
215
230
|
|
|
216
231
|
// merge all possible queryString values
|
|
217
|
-
request.queryObj = Object.assign(request.queryObj, request.uriObj.query)
|
|
232
|
+
request.queryObj = Object.assign(request.queryObj, request.uriObj.query);
|
|
218
233
|
|
|
219
234
|
// reset uriObj values for a clean url
|
|
220
|
-
request.uriObj.query = null
|
|
221
|
-
request.uriObj.search = null
|
|
222
|
-
request.uriObj.path = request.uriObj.pathname
|
|
235
|
+
request.uriObj.query = null;
|
|
236
|
+
request.uriObj.search = null;
|
|
237
|
+
request.uriObj.path = request.uriObj.pathname;
|
|
223
238
|
|
|
224
239
|
// keep the base url clean of queryString
|
|
225
|
-
request.url = url.format(request.uriObj)
|
|
240
|
+
request.url = url.format(request.uriObj);
|
|
226
241
|
|
|
227
242
|
// update the uri object
|
|
228
|
-
request.uriObj.query = request.queryObj
|
|
229
|
-
if (options.
|
|
243
|
+
request.uriObj.query = request.queryObj;
|
|
244
|
+
if (options.harIsAlreadyEncoded) {
|
|
230
245
|
request.uriObj.search = qs.stringify(request.queryObj, {
|
|
231
|
-
|
|
232
|
-
|
|
246
|
+
encode: false,
|
|
247
|
+
indices: false,
|
|
248
|
+
});
|
|
233
249
|
} else {
|
|
234
250
|
request.uriObj.search = qs.stringify(request.queryObj, {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
})
|
|
251
|
+
indices: false,
|
|
252
|
+
});
|
|
238
253
|
}
|
|
239
254
|
|
|
240
255
|
if (request.uriObj.search) {
|
|
241
|
-
request.uriObj.path = request.uriObj.pathname
|
|
256
|
+
request.uriObj.path = `${request.uriObj.pathname}?${request.uriObj.search}`;
|
|
242
257
|
}
|
|
243
258
|
|
|
244
259
|
// construct a full url
|
|
245
|
-
request.fullUrl = url.format(request.uriObj)
|
|
260
|
+
request.fullUrl = url.format(request.uriObj);
|
|
246
261
|
|
|
247
|
-
return request
|
|
248
|
-
}
|
|
262
|
+
return request;
|
|
263
|
+
};
|
|
249
264
|
|
|
250
265
|
HTTPSnippet.prototype.convert = function (target, client, opts) {
|
|
251
266
|
if (!opts && client) {
|
|
252
|
-
|
|
267
|
+
// eslint-disable-next-line no-param-reassign
|
|
268
|
+
opts = client;
|
|
253
269
|
}
|
|
254
270
|
|
|
255
|
-
|
|
256
|
-
|
|
271
|
+
const func = this._matchTarget(target, client);
|
|
257
272
|
if (func) {
|
|
258
|
-
|
|
259
|
-
return func(request, opts)
|
|
260
|
-
})
|
|
273
|
+
const results = this.requests.map(function (request) {
|
|
274
|
+
return func(request, opts);
|
|
275
|
+
});
|
|
261
276
|
|
|
262
|
-
return results.length === 1 ? results[0] : results
|
|
277
|
+
return results.length === 1 ? results[0] : results;
|
|
263
278
|
}
|
|
264
279
|
|
|
265
|
-
return false
|
|
266
|
-
}
|
|
280
|
+
return false;
|
|
281
|
+
};
|
|
267
282
|
|
|
268
283
|
HTTPSnippet.prototype._matchTarget = function (target, client) {
|
|
269
284
|
// does it exist?
|
|
285
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
270
286
|
if (!targets.hasOwnProperty(target)) {
|
|
271
|
-
return false
|
|
287
|
+
return false;
|
|
272
288
|
}
|
|
273
289
|
|
|
274
290
|
// shorthand
|
|
275
291
|
if (typeof client === 'string' && typeof targets[target][client] === 'function') {
|
|
276
|
-
return targets[target][client]
|
|
292
|
+
return targets[target][client];
|
|
277
293
|
}
|
|
278
294
|
|
|
279
295
|
// default target
|
|
280
|
-
return targets[target][targets[target].info.default]
|
|
281
|
-
}
|
|
296
|
+
return targets[target][targets[target].info.default];
|
|
297
|
+
};
|
|
282
298
|
|
|
283
299
|
// exports
|
|
284
|
-
module.exports = HTTPSnippet
|
|
300
|
+
module.exports = HTTPSnippet;
|
|
285
301
|
|
|
286
302
|
module.exports.addTarget = function (target) {
|
|
287
303
|
if (!('info' in target)) {
|
|
288
|
-
throw new Error('The supplied custom target must contain an `info` object.')
|
|
289
|
-
} else if (
|
|
290
|
-
|
|
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
|
|
291
315
|
} else if (targets.hasOwnProperty(target.info.key)) {
|
|
292
|
-
throw new Error('The supplied custom target already exists.')
|
|
316
|
+
throw new Error('The supplied custom target already exists.');
|
|
293
317
|
} else if (Object.keys(target).length === 1) {
|
|
294
|
-
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.');
|
|
295
319
|
}
|
|
296
320
|
|
|
297
|
-
targets[target.info.key] = target
|
|
298
|
-
}
|
|
321
|
+
targets[target.info.key] = target;
|
|
322
|
+
};
|
|
299
323
|
|
|
300
324
|
module.exports.addTargetClient = function (target, client) {
|
|
325
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
301
326
|
if (!targets.hasOwnProperty(target)) {
|
|
302
|
-
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.`);
|
|
303
328
|
} else if (!('info' in client)) {
|
|
304
|
-
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.');
|
|
305
330
|
} else if (!('key' in client.info) || !('title' in client.info)) {
|
|
306
|
-
throw new Error('The supplied custom target client must have an `info` object with a `key` and `title` property.')
|
|
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
|
|
307
333
|
} else if (targets[target].hasOwnProperty(client.info.key)) {
|
|
308
|
-
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');
|
|
309
335
|
}
|
|
310
336
|
|
|
311
|
-
targets[target][client.info.key] = client
|
|
312
|
-
}
|
|
337
|
+
targets[target][client.info.key] = client;
|
|
338
|
+
};
|
|
313
339
|
|
|
314
340
|
module.exports.availableTargets = function () {
|
|
315
341
|
return Object.keys(targets).map(function (key) {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
342
|
+
const target = { ...targets[key].info };
|
|
343
|
+
const clients = Object.keys(targets[key])
|
|
319
344
|
.filter(function (prop) {
|
|
320
|
-
return
|
|
345
|
+
return !['info', 'index'].includes(prop);
|
|
321
346
|
})
|
|
322
|
-
|
|
323
347
|
.map(function (client) {
|
|
324
|
-
return targets[key][client].info
|
|
325
|
-
})
|
|
348
|
+
return targets[key][client].info;
|
|
349
|
+
});
|
|
326
350
|
|
|
327
351
|
if (clients.length) {
|
|
328
|
-
target.clients = clients
|
|
352
|
+
target.clients = clients;
|
|
329
353
|
}
|
|
330
354
|
|
|
331
|
-
return target
|
|
332
|
-
})
|
|
333
|
-
}
|
|
355
|
+
return target;
|
|
356
|
+
});
|
|
357
|
+
};
|
|
334
358
|
|
|
335
359
|
module.exports.extname = function (target) {
|
|
336
|
-
return targets[target] ? targets[target].info.extname : ''
|
|
337
|
-
}
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
.push('curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "%s");', source.method.toUpperCase())
|
|
11
|
-
.push('curl_easy_setopt(hnd, CURLOPT_URL, "%s");', source.fullUrl)
|
|
6
|
+
code
|
|
7
|
+
.push('CURL *hnd = curl_easy_init();')
|
|
8
|
+
.blank()
|
|
9
|
+
.push('curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "%s");', source.method.toUpperCase())
|
|
10
|
+
.push('curl_easy_setopt(hnd, CURLOPT_URL, "%s");', source.fullUrl);
|
|
12
11
|
|
|
13
12
|
// Add headers, including the cookies
|
|
14
|
-
|
|
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
|
+
};
|