@readme/httpsnippet 3.1.3 → 4.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 +63 -62
- package/dist/package.json +86 -0
- package/dist/src/fixtures/customTarget.js +15 -0
- package/dist/src/fixtures/mimetypes.js +77 -0
- package/dist/src/fixtures/requests/application-form-encoded.js +69 -0
- package/dist/src/fixtures/requests/application-json.js +72 -0
- package/dist/src/fixtures/requests/cookies.js +51 -0
- package/dist/src/fixtures/requests/custom-method.js +41 -0
- package/dist/src/fixtures/requests/full.js +99 -0
- package/dist/src/fixtures/requests/headers.js +56 -0
- package/dist/src/fixtures/requests/http-insecure.js +45 -0
- package/dist/src/fixtures/requests/jsonObj-multiline.js +59 -0
- package/dist/src/fixtures/requests/jsonObj-null-value.js +61 -0
- package/dist/src/fixtures/requests/multipart-data.js +72 -0
- package/dist/src/fixtures/requests/multipart-file.js +65 -0
- package/dist/src/fixtures/requests/multipart-form-data-no-params.js +56 -0
- package/dist/src/fixtures/requests/multipart-form-data.js +64 -0
- package/dist/src/fixtures/requests/nested.js +64 -0
- package/dist/src/fixtures/requests/query-encoded.js +59 -0
- package/dist/src/fixtures/requests/query.js +64 -0
- package/dist/src/fixtures/requests/short.js +45 -0
- package/dist/src/fixtures/requests/text-plain.js +57 -0
- package/dist/src/fixtures/runCustomFixtures.js +77 -0
- package/dist/src/helpers/code-builder.js +91 -0
- package/dist/src/helpers/form-data.js +157 -0
- package/dist/src/helpers/har-validator.js +76 -0
- package/dist/src/helpers/headers.js +33 -0
- package/dist/src/helpers/reducer.js +19 -0
- package/dist/src/helpers/shell.js +20 -0
- package/dist/src/helpers/utils.js +21 -0
- package/dist/src/index.js +285 -0
- package/dist/src/targets/c/libcurl/client.js +43 -0
- package/dist/src/targets/c/target.js +15 -0
- package/dist/src/targets/clojure/clj_http/client.js +184 -0
- package/dist/src/targets/clojure/target.js +15 -0
- package/dist/src/targets/csharp/httpclient/client.js +162 -0
- package/dist/src/targets/csharp/restsharp/client.js +38 -0
- package/dist/src/targets/csharp/target.js +17 -0
- package/dist/src/targets/go/native/client.js +122 -0
- package/dist/src/targets/go/target.js +15 -0
- package/dist/src/targets/http/http1.1/client.js +79 -0
- package/dist/src/targets/http/target.js +15 -0
- package/dist/src/targets/java/asynchttp/client.js +53 -0
- package/dist/src/targets/java/nethttp/client.js +52 -0
- package/dist/src/targets/java/okhttp/client.js +79 -0
- package/dist/src/targets/java/target.js +21 -0
- package/dist/src/targets/java/unirest/client.js +53 -0
- package/dist/src/targets/javascript/axios/client.js +101 -0
- package/dist/src/targets/javascript/axios/fixtures/application-form-encoded.js +22 -0
- package/dist/src/targets/javascript/axios/fixtures/application-json.js +26 -0
- package/dist/src/targets/javascript/axios/fixtures/cookies.js +18 -0
- package/dist/src/targets/javascript/axios/fixtures/custom-method.js +14 -0
- package/dist/src/targets/javascript/axios/fixtures/full.js +26 -0
- package/dist/src/targets/javascript/axios/fixtures/headers.js +18 -0
- package/dist/src/targets/javascript/axios/fixtures/http-insecure.js +14 -0
- package/dist/src/targets/javascript/axios/fixtures/jsonObj-multiline.js +19 -0
- package/dist/src/targets/javascript/axios/fixtures/jsonObj-null-value.js +19 -0
- package/dist/src/targets/javascript/axios/fixtures/multipart-data.js +22 -0
- package/dist/src/targets/javascript/axios/fixtures/multipart-file.js +21 -0
- package/dist/src/targets/javascript/axios/fixtures/multipart-form-data-no-params.js +18 -0
- package/dist/src/targets/javascript/axios/fixtures/multipart-form-data.js +21 -0
- package/dist/src/targets/javascript/axios/fixtures/nested.js +18 -0
- package/dist/src/targets/javascript/axios/fixtures/query-encoded.js +21 -0
- package/dist/src/targets/javascript/axios/fixtures/query.js +18 -0
- package/dist/src/targets/javascript/axios/fixtures/short.js +14 -0
- package/dist/src/targets/javascript/axios/fixtures/text-plain.js +19 -0
- package/dist/src/targets/javascript/fetch/client.js +115 -0
- package/dist/src/targets/javascript/fetch/fixtures/application-form-encoded.js +9 -0
- package/dist/src/targets/javascript/fetch/fixtures/application-json.js +16 -0
- package/dist/src/targets/javascript/fetch/fixtures/cookies.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/custom-method.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/full.js +13 -0
- package/dist/src/targets/javascript/fetch/fixtures/headers.js +8 -0
- package/dist/src/targets/javascript/fetch/fixtures/http-insecure.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/jsonObj-multiline.js +9 -0
- package/dist/src/targets/javascript/fetch/fixtures/jsonObj-null-value.js +9 -0
- package/dist/src/targets/javascript/fetch/fixtures/multipart-data.js +9 -0
- package/dist/src/targets/javascript/fetch/fixtures/multipart-file.js +8 -0
- package/dist/src/targets/javascript/fetch/fixtures/multipart-form-data-no-params.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/multipart-form-data.js +8 -0
- package/dist/src/targets/javascript/fetch/fixtures/nested.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/query-encoded.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/query.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/short.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/text-plain.js +5 -0
- package/dist/src/targets/javascript/jquery/client.js +93 -0
- package/dist/src/targets/javascript/jquery/fixtures/application-form-encoded.js +17 -0
- package/dist/src/targets/javascript/jquery/fixtures/application-json.js +15 -0
- package/dist/src/targets/javascript/jquery/fixtures/cookies.js +13 -0
- package/dist/src/targets/javascript/jquery/fixtures/custom-method.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/full.js +18 -0
- package/dist/src/targets/javascript/jquery/fixtures/headers.js +15 -0
- package/dist/src/targets/javascript/jquery/fixtures/http-insecure.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/jsonObj-multiline.js +15 -0
- package/dist/src/targets/javascript/jquery/fixtures/jsonObj-null-value.js +15 -0
- package/dist/src/targets/javascript/jquery/fixtures/multipart-data.js +18 -0
- package/dist/src/targets/javascript/jquery/fixtures/multipart-file.js +17 -0
- package/dist/src/targets/javascript/jquery/fixtures/multipart-form-data-no-params.js +13 -0
- package/dist/src/targets/javascript/jquery/fixtures/multipart-form-data.js +17 -0
- package/dist/src/targets/javascript/jquery/fixtures/nested.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/query-encoded.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/query.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/short.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/text-plain.js +14 -0
- package/dist/src/targets/javascript/target.js +21 -0
- package/dist/src/targets/javascript/xhr/client.js +90 -0
- package/dist/src/targets/javascript/xhr/fixtures/application-form-encoded.js +12 -0
- package/dist/src/targets/javascript/xhr/fixtures/application-json.js +31 -0
- package/dist/src/targets/javascript/xhr/fixtures/cookies.js +12 -0
- package/dist/src/targets/javascript/xhr/fixtures/cors.js +10 -0
- package/dist/src/targets/javascript/xhr/fixtures/custom-method.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/full.js +14 -0
- package/dist/src/targets/javascript/xhr/fixtures/headers.js +14 -0
- package/dist/src/targets/javascript/xhr/fixtures/http-insecure.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/jsonObj-multiline.js +14 -0
- package/dist/src/targets/javascript/xhr/fixtures/jsonObj-null-value.js +14 -0
- package/dist/src/targets/javascript/xhr/fixtures/multipart-data.js +13 -0
- package/dist/src/targets/javascript/xhr/fixtures/multipart-file.js +12 -0
- package/dist/src/targets/javascript/xhr/fixtures/multipart-form-data-no-params.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/multipart-form-data.js +12 -0
- package/dist/src/targets/javascript/xhr/fixtures/nested.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/query-encoded.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/query.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/short.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/text-plain.js +12 -0
- package/dist/src/targets/kotlin/okhttp/client.js +79 -0
- package/dist/src/targets/kotlin/target.js +15 -0
- package/dist/src/targets/node/axios/client.js +87 -0
- package/dist/src/targets/node/axios/fixtures/application-form-encoded.js +19 -0
- package/dist/src/targets/node/axios/fixtures/application-json.js +22 -0
- package/dist/src/targets/node/axios/fixtures/cookies.js +14 -0
- package/dist/src/targets/node/axios/fixtures/custom-method.js +10 -0
- package/dist/src/targets/node/axios/fixtures/full.js +22 -0
- package/dist/src/targets/node/axios/fixtures/headers.js +14 -0
- package/dist/src/targets/node/axios/fixtures/http-insecure.js +10 -0
- package/dist/src/targets/node/axios/fixtures/jsonObj-multiline.js +15 -0
- package/dist/src/targets/node/axios/fixtures/jsonObj-null-value.js +15 -0
- package/dist/src/targets/node/axios/fixtures/multipart-data.js +15 -0
- package/dist/src/targets/node/axios/fixtures/multipart-file.js +15 -0
- package/dist/src/targets/node/axios/fixtures/multipart-form-data-no-params.js +14 -0
- package/dist/src/targets/node/axios/fixtures/multipart-form-data.js +15 -0
- package/dist/src/targets/node/axios/fixtures/nested.js +13 -0
- package/dist/src/targets/node/axios/fixtures/query-encoded.js +13 -0
- package/dist/src/targets/node/axios/fixtures/query.js +13 -0
- package/dist/src/targets/node/axios/fixtures/short.js +10 -0
- package/dist/src/targets/node/axios/fixtures/text-plain.js +15 -0
- package/dist/src/targets/node/fetch/client.js +149 -0
- package/dist/src/targets/node/fetch/fixtures/application-form-encoded.js +15 -0
- package/dist/src/targets/node/fetch/fixtures/application-json.js +18 -0
- package/dist/src/targets/node/fetch/fixtures/cookies.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/custom-method.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/full.js +18 -0
- package/dist/src/targets/node/fetch/fixtures/headers.js +10 -0
- package/dist/src/targets/node/fetch/fixtures/http-insecure.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/jsonObj-multiline.js +11 -0
- package/dist/src/targets/node/fetch/fixtures/jsonObj-null-value.js +11 -0
- package/dist/src/targets/node/fetch/fixtures/multipart-data.js +13 -0
- package/dist/src/targets/node/fetch/fixtures/multipart-file.js +12 -0
- package/dist/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/multipart-form-data.js +11 -0
- package/dist/src/targets/node/fetch/fixtures/nested.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/query-encoded.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/query.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/short.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/text-plain.js +7 -0
- package/dist/src/targets/node/native/client.js +91 -0
- package/dist/src/targets/node/native/fixtures/application-form-encoded.js +24 -0
- package/dist/src/targets/node/native/fixtures/application-json.js +30 -0
- package/dist/src/targets/node/native/fixtures/cookies.js +22 -0
- package/dist/src/targets/node/native/fixtures/custom-method.js +20 -0
- package/dist/src/targets/node/native/fixtures/full.js +26 -0
- package/dist/src/targets/node/native/fixtures/headers.js +24 -0
- package/dist/src/targets/node/native/fixtures/http-insecure.js +20 -0
- package/dist/src/targets/node/native/fixtures/jsonObj-multiline.js +23 -0
- package/dist/src/targets/node/native/fixtures/jsonObj-null-value.js +23 -0
- package/dist/src/targets/node/native/fixtures/multipart-data.js +23 -0
- package/dist/src/targets/node/native/fixtures/multipart-file.js +23 -0
- package/dist/src/targets/node/native/fixtures/multipart-form-data-no-params.js +22 -0
- package/dist/src/targets/node/native/fixtures/multipart-form-data.js +23 -0
- package/dist/src/targets/node/native/fixtures/nested.js +20 -0
- package/dist/src/targets/node/native/fixtures/query-encoded.js +20 -0
- package/dist/src/targets/node/native/fixtures/query.js +20 -0
- package/dist/src/targets/node/native/fixtures/short.js +20 -0
- package/dist/src/targets/node/native/fixtures/text-plain.js +23 -0
- package/dist/src/targets/node/request/client.js +116 -0
- package/dist/src/targets/node/request/fixtures/application-form-encoded.js +13 -0
- package/dist/src/targets/node/request/fixtures/application-json.js +21 -0
- package/dist/src/targets/node/request/fixtures/cookies.js +11 -0
- package/dist/src/targets/node/request/fixtures/custom-method.js +8 -0
- package/dist/src/targets/node/request/fixtures/full.js +20 -0
- package/dist/src/targets/node/request/fixtures/headers.js +12 -0
- package/dist/src/targets/node/request/fixtures/http-insecure.js +8 -0
- package/dist/src/targets/node/request/fixtures/jsonObj-multiline.js +14 -0
- package/dist/src/targets/node/request/fixtures/jsonObj-null-value.js +14 -0
- package/dist/src/targets/node/request/fixtures/multipart-data.js +20 -0
- package/dist/src/targets/node/request/fixtures/multipart-file.js +19 -0
- package/dist/src/targets/node/request/fixtures/multipart-form-data-no-params.js +12 -0
- package/dist/src/targets/node/request/fixtures/multipart-form-data.js +13 -0
- package/dist/src/targets/node/request/fixtures/nested.js +11 -0
- package/dist/src/targets/node/request/fixtures/query-encoded.js +11 -0
- package/dist/src/targets/node/request/fixtures/query.js +11 -0
- package/dist/src/targets/node/request/fixtures/short.js +8 -0
- package/dist/src/targets/node/request/fixtures/text-plain.js +13 -0
- package/dist/src/targets/node/target.js +24 -0
- package/dist/src/targets/node/unirest/client.js +119 -0
- package/dist/src/targets/node/unirest/fixtures/application-form-encoded.js +15 -0
- package/dist/src/targets/node/unirest/fixtures/application-json.js +32 -0
- package/dist/src/targets/node/unirest/fixtures/cookies.js +12 -0
- package/dist/src/targets/node/unirest/fixtures/custom-method.js +8 -0
- package/dist/src/targets/node/unirest/fixtures/full.js +27 -0
- package/dist/src/targets/node/unirest/fixtures/headers.js +13 -0
- package/dist/src/targets/node/unirest/fixtures/http-insecure.js +8 -0
- package/dist/src/targets/node/unirest/fixtures/jsonObj-multiline.js +15 -0
- package/dist/src/targets/node/unirest/fixtures/jsonObj-null-value.js +15 -0
- package/dist/src/targets/node/unirest/fixtures/multipart-data.js +20 -0
- package/dist/src/targets/node/unirest/fixtures/multipart-file.js +18 -0
- package/dist/src/targets/node/unirest/fixtures/multipart-form-data-no-params.js +11 -0
- package/dist/src/targets/node/unirest/fixtures/multipart-form-data.js +16 -0
- package/dist/src/targets/node/unirest/fixtures/nested.js +13 -0
- package/dist/src/targets/node/unirest/fixtures/query-encoded.js +12 -0
- package/dist/src/targets/node/unirest/fixtures/query.js +16 -0
- package/dist/src/targets/node/unirest/fixtures/short.js +8 -0
- package/dist/src/targets/node/unirest/fixtures/text-plain.js +12 -0
- package/dist/src/targets/objc/helpers.js +59 -0
- package/dist/src/targets/objc/nsurlsession/client.js +151 -0
- package/dist/src/targets/objc/target.js +15 -0
- package/dist/src/targets/ocaml/cohttp/client.js +72 -0
- package/dist/src/targets/ocaml/target.js +15 -0
- package/dist/src/targets/php/curl/client.js +135 -0
- package/dist/src/targets/php/guzzle/client.js +126 -0
- package/dist/src/targets/php/helpers.js +63 -0
- package/dist/src/targets/php/http1/client.js +90 -0
- package/dist/src/targets/php/http2/client.js +128 -0
- package/dist/src/targets/php/target.js +22 -0
- package/dist/src/targets/powershell/common.js +49 -0
- package/dist/src/targets/powershell/restmethod/client.js +13 -0
- package/dist/src/targets/powershell/target.js +17 -0
- package/dist/src/targets/powershell/webrequest/client.js +13 -0
- package/dist/src/targets/python/helpers.js +70 -0
- package/dist/src/targets/python/requests/client.js +161 -0
- package/dist/src/targets/python/target.js +16 -0
- package/dist/src/targets/r/httr/client.js +125 -0
- package/dist/src/targets/r/target.js +15 -0
- package/dist/src/targets/ruby/native/client.js +69 -0
- package/dist/src/targets/ruby/target.js +15 -0
- package/dist/src/targets/shell/curl/client.js +171 -0
- package/dist/src/targets/shell/httpie/client.js +111 -0
- package/dist/src/targets/shell/target.js +20 -0
- package/dist/src/targets/shell/wget/client.js +55 -0
- package/dist/src/targets/swift/helpers.js +80 -0
- package/dist/src/targets/swift/nsurlsession/client.js +156 -0
- package/dist/src/targets/swift/target.js +15 -0
- package/dist/src/targets/targets.js +130 -0
- package/package.json +26 -21
- package/src/helpers/code-builder.js +0 -108
- package/src/helpers/form-data.js +0 -107
- package/src/helpers/headers.js +0 -48
- package/src/helpers/reducer.js +0 -20
- package/src/index.js +0 -361
- package/src/targets/c/index.js +0 -10
- package/src/targets/c/libcurl.js +0 -45
- package/src/targets/clojure/clj_http.js +0 -176
- package/src/targets/clojure/index.js +0 -10
- package/src/targets/csharp/httpclient.js +0 -157
- package/src/targets/csharp/index.js +0 -11
- package/src/targets/csharp/restsharp.js +0 -49
- package/src/targets/go/index.js +0 -10
- package/src/targets/go/native.js +0 -130
- package/src/targets/http/http1.1.js +0 -89
- package/src/targets/http/index.js +0 -10
- package/src/targets/index.js +0 -20
- package/src/targets/java/asynchttp.js +0 -54
- package/src/targets/java/index.js +0 -13
- package/src/targets/java/nethttp.js +0 -59
- package/src/targets/java/okhttp.js +0 -78
- package/src/targets/java/unirest.js +0 -57
- package/src/targets/javascript/axios.js +0 -101
- package/src/targets/javascript/fetch.js +0 -129
- package/src/targets/javascript/index.js +0 -13
- package/src/targets/javascript/jquery.js +0 -89
- package/src/targets/javascript/xhr.js +0 -86
- package/src/targets/kotlin/index.js +0 -10
- package/src/targets/kotlin/okhttp.js +0 -78
- package/src/targets/node/axios.js +0 -76
- package/src/targets/node/fetch.js +0 -145
- package/src/targets/node/index.js +0 -15
- package/src/targets/node/native.js +0 -92
- package/src/targets/node/request.js +0 -126
- package/src/targets/node/unirest.js +0 -120
- package/src/targets/objc/helpers.js +0 -80
- package/src/targets/objc/index.js +0 -10
- package/src/targets/objc/nsurlsession.js +0 -164
- package/src/targets/ocaml/cohttp.js +0 -73
- package/src/targets/ocaml/index.js +0 -10
- package/src/targets/php/curl.js +0 -142
- package/src/targets/php/guzzle.js +0 -135
- package/src/targets/php/helpers.js +0 -87
- package/src/targets/php/http1.js +0 -85
- package/src/targets/php/http2.js +0 -131
- package/src/targets/php/index.js +0 -14
- package/src/targets/powershell/common.js +0 -60
- package/src/targets/powershell/index.js +0 -11
- package/src/targets/powershell/restmethod.js +0 -8
- package/src/targets/powershell/webrequest.js +0 -8
- package/src/targets/python/helpers.js +0 -85
- package/src/targets/python/index.js +0 -11
- package/src/targets/python/requests.js +0 -163
- package/src/targets/r/httr.js +0 -134
- package/src/targets/r/index.js +0 -10
- package/src/targets/ruby/index.js +0 -10
- package/src/targets/ruby/native.js +0 -71
- package/src/targets/shell/curl.js +0 -157
- package/src/targets/shell/helpers.js +0 -23
- package/src/targets/shell/httpie.js +0 -129
- package/src/targets/shell/index.js +0 -13
- package/src/targets/shell/wget.js +0 -52
- package/src/targets/swift/helpers.js +0 -93
- package/src/targets/swift/index.js +0 -10
- package/src/targets/swift/nsurlsession.js +0 -152
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator for Java using Unirest.
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @shashiranjan84
|
|
7
|
-
*
|
|
8
|
-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
|
-
|
|
13
|
-
module.exports = function (source, options) {
|
|
14
|
-
const opts = {
|
|
15
|
-
indent: ' ',
|
|
16
|
-
...options,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const code = new CodeBuilder(opts.indent);
|
|
20
|
-
|
|
21
|
-
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
|
|
22
|
-
|
|
23
|
-
if (methods.indexOf(source.method.toUpperCase()) === -1) {
|
|
24
|
-
code.push(
|
|
25
|
-
'HttpResponse<String> response = Unirest.customMethod("%s","%s")',
|
|
26
|
-
source.method.toUpperCase(),
|
|
27
|
-
source.fullUrl
|
|
28
|
-
);
|
|
29
|
-
} else {
|
|
30
|
-
code.push('HttpResponse<String> response = Unirest.%s("%s")', source.method.toLowerCase(), source.fullUrl);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Add headers, including the cookies
|
|
34
|
-
const headers = Object.keys(source.allHeaders);
|
|
35
|
-
|
|
36
|
-
// construct headers
|
|
37
|
-
if (headers.length) {
|
|
38
|
-
headers.forEach(function (key) {
|
|
39
|
-
code.push(1, '.header("%s", "%s")', key, source.allHeaders[key]);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (source.postData.text) {
|
|
44
|
-
code.push(1, '.body(%s)', JSON.stringify(source.postData.text));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
code.push(1, '.asString();');
|
|
48
|
-
|
|
49
|
-
return code.join();
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
module.exports.info = {
|
|
53
|
-
key: 'unirest',
|
|
54
|
-
title: 'Unirest',
|
|
55
|
-
link: 'http://unirest.io/java.html',
|
|
56
|
-
description: 'Lightweight HTTP Request Client Library',
|
|
57
|
-
};
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator for Javascript & Node.js using Axios.
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @rohit-gohri
|
|
7
|
-
*
|
|
8
|
-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const stringifyObject = require('stringify-object');
|
|
12
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
13
|
-
|
|
14
|
-
module.exports = function (source, options) {
|
|
15
|
-
const opts = {
|
|
16
|
-
indent: ' ',
|
|
17
|
-
...options,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const code = new CodeBuilder(opts.indent);
|
|
21
|
-
|
|
22
|
-
code.push('import axios from "axios";').blank();
|
|
23
|
-
|
|
24
|
-
const reqOpts = {
|
|
25
|
-
method: source.method,
|
|
26
|
-
url: source.url,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
if (Object.keys(source.queryObj).length) {
|
|
30
|
-
reqOpts.params = source.queryObj;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (Object.keys(source.allHeaders).length) {
|
|
34
|
-
reqOpts.headers = source.allHeaders;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
switch (source.postData.mimeType) {
|
|
38
|
-
case 'application/x-www-form-urlencoded':
|
|
39
|
-
code.push('const encodedParams = new URLSearchParams();');
|
|
40
|
-
source.postData.params.forEach(function (param) {
|
|
41
|
-
code.push(`encodedParams.set('${param.name}', '${param.value}');`);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
code.blank();
|
|
45
|
-
|
|
46
|
-
reqOpts.data = 'encodedParams';
|
|
47
|
-
break;
|
|
48
|
-
|
|
49
|
-
case 'application/json':
|
|
50
|
-
if (source.postData.jsonObj) {
|
|
51
|
-
reqOpts.data = source.postData.jsonObj;
|
|
52
|
-
}
|
|
53
|
-
break;
|
|
54
|
-
|
|
55
|
-
case 'multipart/form-data':
|
|
56
|
-
if (source.postData.params) {
|
|
57
|
-
code.push('const form = new FormData();');
|
|
58
|
-
|
|
59
|
-
source.postData.params.forEach(function (param) {
|
|
60
|
-
code.push(
|
|
61
|
-
'form.append(%s, %s);',
|
|
62
|
-
JSON.stringify(param.name),
|
|
63
|
-
JSON.stringify(param.value || param.fileName || '')
|
|
64
|
-
);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
code.blank();
|
|
68
|
-
|
|
69
|
-
reqOpts.data = '[form]';
|
|
70
|
-
}
|
|
71
|
-
break;
|
|
72
|
-
|
|
73
|
-
default:
|
|
74
|
-
if (source.postData.text) {
|
|
75
|
-
reqOpts.data = source.postData.text;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
code
|
|
80
|
-
.push(
|
|
81
|
-
'const options = %s;',
|
|
82
|
-
stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }).replace('"[form]"', 'form')
|
|
83
|
-
)
|
|
84
|
-
.blank();
|
|
85
|
-
|
|
86
|
-
code
|
|
87
|
-
.push('axios.request(options).then(function (response) {')
|
|
88
|
-
.push(1, 'console.log(response.data);')
|
|
89
|
-
.push('}).catch(%s', 'function (error) {')
|
|
90
|
-
.push(1, 'console.error(error);')
|
|
91
|
-
.push('});');
|
|
92
|
-
|
|
93
|
-
return code.join().replace(/'encodedParams'/, 'encodedParams,');
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
module.exports.info = {
|
|
97
|
-
key: 'axios',
|
|
98
|
-
title: 'Axios',
|
|
99
|
-
link: 'https://github.com/axios/axios',
|
|
100
|
-
description: 'Promise based HTTP client for the browser and node.js',
|
|
101
|
-
};
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator for fetch
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @pmdroid
|
|
7
|
-
*
|
|
8
|
-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const stringifyObject = require('stringify-object');
|
|
12
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
13
|
-
const { getHeaderName } = require('../../helpers/headers');
|
|
14
|
-
|
|
15
|
-
module.exports = function (source, options) {
|
|
16
|
-
const opts = {
|
|
17
|
-
indent: ' ',
|
|
18
|
-
credentials: null,
|
|
19
|
-
...options,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const code = new CodeBuilder(opts.indent);
|
|
23
|
-
|
|
24
|
-
const reqOptions = {
|
|
25
|
-
method: source.method,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
if (Object.keys(source.allHeaders).length) {
|
|
29
|
-
reqOptions.headers = source.allHeaders;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (opts.credentials !== null) {
|
|
33
|
-
reqOptions.credentials = opts.credentials;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
switch (source.postData.mimeType) {
|
|
37
|
-
case 'application/x-www-form-urlencoded':
|
|
38
|
-
reqOptions.body = source.postData.paramsObj ? source.postData.paramsObj : source.postData.text;
|
|
39
|
-
break;
|
|
40
|
-
|
|
41
|
-
case 'application/json':
|
|
42
|
-
if (source.postData.jsonObj) {
|
|
43
|
-
reqOptions.body = source.postData.jsonObj;
|
|
44
|
-
}
|
|
45
|
-
break;
|
|
46
|
-
|
|
47
|
-
case 'multipart/form-data':
|
|
48
|
-
if (source.postData.params) {
|
|
49
|
-
// The FormData API automatically adds a `Content-Type` header for `multipart/form-data`
|
|
50
|
-
// content and if we add our own here data won't be correctly transmitted.
|
|
51
|
-
if (reqOptions.headers) {
|
|
52
|
-
const contentTypeHeader = getHeaderName(reqOptions.headers, 'content-type');
|
|
53
|
-
if (contentTypeHeader) {
|
|
54
|
-
delete reqOptions.headers[contentTypeHeader];
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
code.push('const form = new FormData();');
|
|
59
|
-
|
|
60
|
-
source.postData.params.forEach(function (param) {
|
|
61
|
-
code.push(
|
|
62
|
-
'form.append(%s, %s);',
|
|
63
|
-
JSON.stringify(param.name),
|
|
64
|
-
JSON.stringify(param.value || param.fileName || '')
|
|
65
|
-
);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
code.blank();
|
|
69
|
-
}
|
|
70
|
-
break;
|
|
71
|
-
|
|
72
|
-
default:
|
|
73
|
-
if (source.postData.text) {
|
|
74
|
-
reqOptions.body = source.postData.text;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// If we ultimately don't have any headers to send then we shouldn't add an empty object into the
|
|
79
|
-
// request options.
|
|
80
|
-
if (reqOptions.headers && !Object.keys(reqOptions.headers).length) {
|
|
81
|
-
delete reqOptions.headers;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
code
|
|
85
|
-
.push(
|
|
86
|
-
'const options = %s;',
|
|
87
|
-
stringifyObject(reqOptions, {
|
|
88
|
-
indent: opts.indent,
|
|
89
|
-
inlineCharacterLimit: 80,
|
|
90
|
-
|
|
91
|
-
// The Fetch API body only accepts string parameters, but stringified JSON can be difficult to
|
|
92
|
-
// read, so we keep the object as a literal and use this transform function to wrap the literal
|
|
93
|
-
// in a `JSON.stringify` call.
|
|
94
|
-
transform: (object, property, originalResult) => {
|
|
95
|
-
if (property === 'body') {
|
|
96
|
-
if (source.postData.mimeType === 'application/x-www-form-urlencoded') {
|
|
97
|
-
return `new URLSearchParams(${originalResult})`;
|
|
98
|
-
} else if (source.postData.mimeType === 'application/json') {
|
|
99
|
-
return `JSON.stringify(${originalResult})`;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return originalResult;
|
|
104
|
-
},
|
|
105
|
-
})
|
|
106
|
-
)
|
|
107
|
-
.blank();
|
|
108
|
-
|
|
109
|
-
if (source.postData.mimeType === 'multipart/form-data') {
|
|
110
|
-
if (source.postData.params) {
|
|
111
|
-
code.push('options.body = form;').blank();
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
code
|
|
116
|
-
.push("fetch('%s', options)", source.fullUrl)
|
|
117
|
-
.push(1, '.then(response => response.json())')
|
|
118
|
-
.push(1, '.then(response => console.log(response))')
|
|
119
|
-
.push(1, '.catch(err => console.error(err));');
|
|
120
|
-
|
|
121
|
-
return code.join();
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
module.exports.info = {
|
|
125
|
-
key: 'fetch',
|
|
126
|
-
title: 'fetch',
|
|
127
|
-
link: 'https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch',
|
|
128
|
-
description: 'Perform asynchronous HTTP requests with the Fetch API',
|
|
129
|
-
};
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator for native XMLHttpRequest
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @AhmadNassri
|
|
7
|
-
*
|
|
8
|
-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
|
-
const helpers = require('../../helpers/headers');
|
|
13
|
-
|
|
14
|
-
module.exports = function (source, options) {
|
|
15
|
-
const opts = {
|
|
16
|
-
indent: ' ',
|
|
17
|
-
...options,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const code = new CodeBuilder(opts.indent);
|
|
21
|
-
|
|
22
|
-
const settings = {
|
|
23
|
-
async: true,
|
|
24
|
-
crossDomain: true,
|
|
25
|
-
url: source.fullUrl,
|
|
26
|
-
method: source.method,
|
|
27
|
-
headers: source.allHeaders,
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
switch (source.postData.mimeType) {
|
|
31
|
-
case 'application/x-www-form-urlencoded':
|
|
32
|
-
settings.data = source.postData.paramsObj ? source.postData.paramsObj : source.postData.text;
|
|
33
|
-
break;
|
|
34
|
-
|
|
35
|
-
case 'application/json':
|
|
36
|
-
settings.processData = false;
|
|
37
|
-
settings.data = source.postData.text;
|
|
38
|
-
break;
|
|
39
|
-
|
|
40
|
-
case 'multipart/form-data':
|
|
41
|
-
if (source.postData.params) {
|
|
42
|
-
code.push('const form = new FormData();');
|
|
43
|
-
|
|
44
|
-
source.postData.params.forEach(function (param) {
|
|
45
|
-
code.push(
|
|
46
|
-
'form.append(%s, %s);',
|
|
47
|
-
JSON.stringify(param.name),
|
|
48
|
-
JSON.stringify(param.value || param.fileName || '')
|
|
49
|
-
);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
settings.processData = false;
|
|
53
|
-
settings.contentType = false;
|
|
54
|
-
settings.mimeType = 'multipart/form-data';
|
|
55
|
-
settings.data = '[form]';
|
|
56
|
-
|
|
57
|
-
// remove the contentType header
|
|
58
|
-
if (helpers.hasHeader(settings.headers, 'content-type')) {
|
|
59
|
-
if (helpers.getHeader(settings.headers, 'content-type').indexOf('boundary')) {
|
|
60
|
-
delete settings.headers[helpers.getHeaderName(settings.headers, 'content-type')];
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
code.blank();
|
|
65
|
-
}
|
|
66
|
-
break;
|
|
67
|
-
|
|
68
|
-
default:
|
|
69
|
-
if (source.postData.text) {
|
|
70
|
-
settings.data = source.postData.text;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
code
|
|
75
|
-
.push(`const settings = ${JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form')};`)
|
|
76
|
-
.blank()
|
|
77
|
-
.push('$.ajax(settings).done(function (response) {')
|
|
78
|
-
.push(1, 'console.log(response);')
|
|
79
|
-
.push('});');
|
|
80
|
-
|
|
81
|
-
return code.join();
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
module.exports.info = {
|
|
85
|
-
key: 'jquery',
|
|
86
|
-
title: 'jQuery',
|
|
87
|
-
link: 'http://api.jquery.com/jquery.ajax/',
|
|
88
|
-
description: 'Perform an asynchronous HTTP (Ajax) requests with jQuery',
|
|
89
|
-
};
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator for native XMLHttpRequest
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @AhmadNassri
|
|
7
|
-
*
|
|
8
|
-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
|
-
const helpers = require('../../helpers/headers');
|
|
13
|
-
|
|
14
|
-
module.exports = function (source, options) {
|
|
15
|
-
const opts = {
|
|
16
|
-
indent: ' ',
|
|
17
|
-
cors: true,
|
|
18
|
-
...options,
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const code = new CodeBuilder(opts.indent);
|
|
22
|
-
|
|
23
|
-
switch (source.postData.mimeType) {
|
|
24
|
-
case 'application/json':
|
|
25
|
-
code.push('const data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent)).blank();
|
|
26
|
-
break;
|
|
27
|
-
|
|
28
|
-
case 'multipart/form-data':
|
|
29
|
-
if (source.postData.params) {
|
|
30
|
-
code.push('const data = new FormData();');
|
|
31
|
-
|
|
32
|
-
source.postData.params.forEach(function (param) {
|
|
33
|
-
code.push(
|
|
34
|
-
'data.append(%s, %s);',
|
|
35
|
-
JSON.stringify(param.name),
|
|
36
|
-
JSON.stringify(param.value || param.fileName || '')
|
|
37
|
-
);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
// remove the contentType header
|
|
41
|
-
if (helpers.hasHeader(source.allHeaders, 'content-type')) {
|
|
42
|
-
if (helpers.getHeader(source.allHeaders, 'content-type').indexOf('boundary')) {
|
|
43
|
-
// eslint-disable-next-line no-param-reassign
|
|
44
|
-
delete source.allHeaders[helpers.getHeaderName(source.allHeaders, 'content-type')];
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
code.blank();
|
|
49
|
-
}
|
|
50
|
-
break;
|
|
51
|
-
|
|
52
|
-
default:
|
|
53
|
-
code.push('const data = %s;', JSON.stringify(source.postData.text || null)).blank();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
code.push('const xhr = new XMLHttpRequest();');
|
|
57
|
-
|
|
58
|
-
if (opts.cors) {
|
|
59
|
-
code.push('xhr.withCredentials = true;');
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
code
|
|
63
|
-
.blank()
|
|
64
|
-
.push('xhr.addEventListener("readystatechange", function () {')
|
|
65
|
-
.push(1, 'if (this.readyState === this.DONE) {')
|
|
66
|
-
.push(2, 'console.log(this.responseText);')
|
|
67
|
-
.push(1, '}')
|
|
68
|
-
.push('});')
|
|
69
|
-
.blank()
|
|
70
|
-
.push('xhr.open(%s, %s);', JSON.stringify(source.method), JSON.stringify(source.fullUrl));
|
|
71
|
-
|
|
72
|
-
Object.keys(source.allHeaders).forEach(function (key) {
|
|
73
|
-
code.push('xhr.setRequestHeader(%s, %s);', JSON.stringify(key), JSON.stringify(source.allHeaders[key]));
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
code.blank().push('xhr.send(data);');
|
|
77
|
-
|
|
78
|
-
return code.join();
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
module.exports.info = {
|
|
82
|
-
key: 'xhr',
|
|
83
|
-
title: 'XMLHttpRequest',
|
|
84
|
-
link: 'https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest',
|
|
85
|
-
description: 'W3C Standard API that provides scripted client functionality',
|
|
86
|
-
};
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator for Kotlin using OkHttp.
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @seanghay
|
|
7
|
-
*
|
|
8
|
-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
|
-
|
|
13
|
-
module.exports = function (source, options) {
|
|
14
|
-
const opts = {
|
|
15
|
-
indent: ' ',
|
|
16
|
-
...options,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const code = new CodeBuilder(opts.indent);
|
|
20
|
-
|
|
21
|
-
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];
|
|
22
|
-
|
|
23
|
-
const methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH'];
|
|
24
|
-
|
|
25
|
-
code.push('val client = OkHttpClient()').blank();
|
|
26
|
-
|
|
27
|
-
if (source.postData.text) {
|
|
28
|
-
if (source.postData.boundary) {
|
|
29
|
-
code.push(
|
|
30
|
-
'val mediaType = MediaType.parse("%s; boundary=%s")',
|
|
31
|
-
source.postData.mimeType,
|
|
32
|
-
source.postData.boundary
|
|
33
|
-
);
|
|
34
|
-
} else {
|
|
35
|
-
code.push('val mediaType = MediaType.parse("%s")', source.postData.mimeType);
|
|
36
|
-
}
|
|
37
|
-
code.push('val body = RequestBody.create(mediaType, %s)', JSON.stringify(source.postData.text));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
code.push('val request = Request.Builder()');
|
|
41
|
-
code.push(1, '.url("%s")', source.fullUrl);
|
|
42
|
-
if (methods.indexOf(source.method.toUpperCase()) === -1) {
|
|
43
|
-
if (source.postData.text) {
|
|
44
|
-
code.push(1, '.method("%s", body)', source.method.toUpperCase());
|
|
45
|
-
} else {
|
|
46
|
-
code.push(1, '.method("%s", null)', source.method.toUpperCase());
|
|
47
|
-
}
|
|
48
|
-
} else if (methodsWithBody.indexOf(source.method.toUpperCase()) >= 0) {
|
|
49
|
-
if (source.postData.text) {
|
|
50
|
-
code.push(1, '.%s(body)', source.method.toLowerCase());
|
|
51
|
-
} else {
|
|
52
|
-
code.push(1, '.%s(null)', source.method.toLowerCase());
|
|
53
|
-
}
|
|
54
|
-
} else {
|
|
55
|
-
code.push(1, '.%s()', source.method.toLowerCase());
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Add headers, including the cookies
|
|
59
|
-
const headers = Object.keys(source.allHeaders);
|
|
60
|
-
|
|
61
|
-
// construct headers
|
|
62
|
-
if (headers.length) {
|
|
63
|
-
headers.forEach(function (key) {
|
|
64
|
-
code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key]);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
code.push(1, '.build()').blank().push('val response = client.newCall(request).execute()');
|
|
69
|
-
|
|
70
|
-
return code.join();
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
module.exports.info = {
|
|
74
|
-
key: 'okhttp',
|
|
75
|
-
title: 'OkHttp',
|
|
76
|
-
link: 'http://square.github.io/okhttp/',
|
|
77
|
-
description: 'An HTTP Request Client Library',
|
|
78
|
-
};
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator for Javascript & Node.js using Axios.
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @rohit-gohri
|
|
7
|
-
*
|
|
8
|
-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const stringifyObject = require('stringify-object');
|
|
12
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
13
|
-
|
|
14
|
-
module.exports = function (source, options) {
|
|
15
|
-
const opts = {
|
|
16
|
-
indent: ' ',
|
|
17
|
-
...options,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const code = new CodeBuilder(opts.indent);
|
|
21
|
-
|
|
22
|
-
code.push('const axios = require("axios").default;');
|
|
23
|
-
|
|
24
|
-
const reqOpts = {
|
|
25
|
-
method: source.method,
|
|
26
|
-
url: source.fullUrl,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
if (Object.keys(source.allHeaders).length) {
|
|
30
|
-
reqOpts.headers = source.allHeaders;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
switch (source.postData.mimeType) {
|
|
34
|
-
case 'application/x-www-form-urlencoded':
|
|
35
|
-
code.push("const { URLSearchParams } = require('url');");
|
|
36
|
-
code.blank();
|
|
37
|
-
|
|
38
|
-
code.push('const encodedParams = new URLSearchParams();');
|
|
39
|
-
source.postData.params.forEach(function (param) {
|
|
40
|
-
code.push(`encodedParams.set('${param.name}', '${param.value}');`);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
reqOpts.data = 'encodedParams';
|
|
44
|
-
break;
|
|
45
|
-
|
|
46
|
-
case 'application/json':
|
|
47
|
-
if (source.postData.jsonObj) {
|
|
48
|
-
reqOpts.data = source.postData.jsonObj;
|
|
49
|
-
}
|
|
50
|
-
break;
|
|
51
|
-
|
|
52
|
-
default:
|
|
53
|
-
if (source.postData.text) {
|
|
54
|
-
reqOpts.data = source.postData.text;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
code.blank();
|
|
59
|
-
code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 })).blank();
|
|
60
|
-
|
|
61
|
-
code
|
|
62
|
-
.push('axios.request(options).then(function (response) {')
|
|
63
|
-
.push(1, 'console.log(response.data);')
|
|
64
|
-
.push('}).catch(%s', 'function (error) {')
|
|
65
|
-
.push(1, 'console.error(error);')
|
|
66
|
-
.push('});');
|
|
67
|
-
|
|
68
|
-
return code.join().replace(/'encodedParams'/, 'encodedParams,');
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
module.exports.info = {
|
|
72
|
-
key: 'axios',
|
|
73
|
-
title: 'Axios',
|
|
74
|
-
link: 'https://github.com/axios/axios',
|
|
75
|
-
description: 'Promise based HTTP client for the browser and node.js',
|
|
76
|
-
};
|