@scalar/snippetz 0.2.6 → 0.2.8
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/dist/_virtual/client.js +133 -0
- package/dist/_virtual/client10.js +121 -0
- package/dist/_virtual/client11.js +128 -0
- package/dist/_virtual/client12.js +64 -0
- package/dist/_virtual/client13.js +128 -0
- package/dist/_virtual/client14.js +71 -0
- package/dist/_virtual/client15.js +81 -0
- package/dist/_virtual/client16.js +76 -0
- package/dist/_virtual/client17.js +81 -0
- package/dist/_virtual/client18.js +87 -0
- package/dist/_virtual/client19.js +45 -0
- package/dist/_virtual/client2.js +52 -0
- package/dist/_virtual/client20.js +44 -0
- package/dist/_virtual/client21.js +71 -0
- package/dist/_virtual/client22.js +45 -0
- package/dist/_virtual/client23.js +71 -0
- package/dist/_virtual/client24.js +122 -0
- package/dist/_virtual/client25.js +37 -0
- package/dist/_virtual/client26.js +154 -0
- package/dist/_virtual/client27.js +181 -0
- package/dist/_virtual/client28.js +43 -0
- package/dist/_virtual/client3.js +113 -0
- package/dist/_virtual/client4.js +70 -0
- package/dist/_virtual/client5.js +123 -0
- package/dist/_virtual/client6.js +166 -0
- package/dist/_virtual/client7.js +91 -0
- package/dist/_virtual/client8.js +13 -0
- package/dist/_virtual/client9.js +13 -0
- package/dist/core/types.d.ts +29 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/utils/convertWithHttpSnippetLite.d.ts +7 -0
- package/dist/core/utils/convertWithHttpSnippetLite.d.ts.map +1 -0
- package/dist/core/utils/convertWithHttpSnippetLite.js +83 -0
- package/dist/httpsnippet-lite/dist/esm/helpers/code-builder.js +60 -0
- package/dist/httpsnippet-lite/dist/esm/helpers/escape.js +72 -0
- package/dist/httpsnippet-lite/dist/esm/helpers/headers.js +20 -0
- package/dist/httpsnippet-lite/dist/esm/helpers/shell.js +16 -0
- package/dist/httpsnippet-lite/dist/esm/targets/objc/helpers.js +56 -0
- package/dist/httpsnippet-lite/dist/esm/targets/php/helpers.js +35 -0
- package/dist/httpsnippet-lite/dist/esm/targets/powershell/common.js +51 -0
- package/dist/httpsnippet-lite/dist/esm/targets/python/helpers.js +61 -0
- package/dist/httpsnippet-lite/dist/esm/targets/swift/helpers.js +74 -0
- package/dist/plugins/c/libcurl/index.d.ts +2 -0
- package/dist/plugins/c/libcurl/index.d.ts.map +1 -0
- package/dist/plugins/c/libcurl/index.js +1 -0
- package/dist/plugins/c/libcurl/libcurl.d.ts +6 -0
- package/dist/plugins/c/libcurl/libcurl.d.ts.map +1 -0
- package/dist/plugins/c/libcurl/libcurl.js +16 -0
- package/dist/plugins/clojure/clj_http/clj_http.d.ts +6 -0
- package/dist/plugins/clojure/clj_http/clj_http.d.ts.map +1 -0
- package/dist/plugins/clojure/clj_http/clj_http.js +16 -0
- package/dist/plugins/clojure/clj_http/index.d.ts +2 -0
- package/dist/plugins/clojure/clj_http/index.d.ts.map +1 -0
- package/dist/plugins/clojure/clj_http/index.js +1 -0
- package/dist/plugins/csharp/httpclient/httpclient.d.ts +6 -0
- package/dist/plugins/csharp/httpclient/httpclient.d.ts.map +1 -0
- package/dist/plugins/csharp/httpclient/httpclient.js +16 -0
- package/dist/plugins/csharp/httpclient/index.d.ts +2 -0
- package/dist/plugins/csharp/httpclient/index.d.ts.map +1 -0
- package/dist/plugins/csharp/httpclient/index.js +1 -0
- package/dist/plugins/csharp/restsharp/index.d.ts +2 -0
- package/dist/plugins/csharp/restsharp/index.d.ts.map +1 -0
- package/dist/plugins/csharp/restsharp/index.js +1 -0
- package/dist/plugins/csharp/restsharp/restsharp.d.ts +6 -0
- package/dist/plugins/csharp/restsharp/restsharp.d.ts.map +1 -0
- package/dist/plugins/csharp/restsharp/restsharp.js +16 -0
- package/dist/plugins/go/native/index.d.ts +2 -0
- package/dist/plugins/go/native/index.d.ts.map +1 -0
- package/dist/plugins/go/native/index.js +1 -0
- package/dist/plugins/go/native/native.d.ts +6 -0
- package/dist/plugins/go/native/native.d.ts.map +1 -0
- package/dist/plugins/go/native/native.js +16 -0
- package/dist/plugins/http/http11/http11.d.ts +6 -0
- package/dist/plugins/http/http11/http11.d.ts.map +1 -0
- package/dist/plugins/http/http11/http11.js +16 -0
- package/dist/plugins/http/http11/index.d.ts +2 -0
- package/dist/plugins/http/http11/index.d.ts.map +1 -0
- package/dist/plugins/http/http11/index.js +1 -0
- package/dist/plugins/java/asynchttp/asynchttp.d.ts +6 -0
- package/dist/plugins/java/asynchttp/asynchttp.d.ts.map +1 -0
- package/dist/plugins/java/asynchttp/asynchttp.js +16 -0
- package/dist/plugins/java/asynchttp/index.d.ts +2 -0
- package/dist/plugins/java/asynchttp/index.d.ts.map +1 -0
- package/dist/plugins/java/asynchttp/index.js +1 -0
- package/dist/plugins/java/nethttp/index.d.ts +2 -0
- package/dist/plugins/java/nethttp/index.d.ts.map +1 -0
- package/dist/plugins/java/nethttp/index.js +1 -0
- package/dist/plugins/java/nethttp/nethttp.d.ts +6 -0
- package/dist/plugins/java/nethttp/nethttp.d.ts.map +1 -0
- package/dist/plugins/java/nethttp/nethttp.js +16 -0
- package/dist/plugins/java/okhttp/index.d.ts +2 -0
- package/dist/plugins/java/okhttp/index.d.ts.map +1 -0
- package/dist/plugins/java/okhttp/index.js +1 -0
- package/dist/plugins/java/okhttp/okhttp.d.ts +6 -0
- package/dist/plugins/java/okhttp/okhttp.d.ts.map +1 -0
- package/dist/plugins/java/okhttp/okhttp.js +16 -0
- package/dist/plugins/java/unirest/index.d.ts +2 -0
- package/dist/plugins/java/unirest/index.d.ts.map +1 -0
- package/dist/plugins/java/unirest/index.js +1 -0
- package/dist/plugins/java/unirest/unirest.d.ts +6 -0
- package/dist/plugins/java/unirest/unirest.d.ts.map +1 -0
- package/dist/plugins/java/unirest/unirest.js +16 -0
- package/dist/plugins/js/axios/axios.d.ts +6 -0
- package/dist/plugins/js/axios/axios.d.ts.map +1 -0
- package/dist/plugins/js/axios/axios.js +16 -0
- package/dist/plugins/js/axios/index.d.ts +2 -0
- package/dist/plugins/js/axios/index.d.ts.map +1 -0
- package/dist/plugins/js/axios/index.js +1 -0
- package/dist/plugins/js/jquery/index.d.ts +2 -0
- package/dist/plugins/js/jquery/index.d.ts.map +1 -0
- package/dist/plugins/js/jquery/index.js +1 -0
- package/dist/plugins/js/jquery/jquery.d.ts +6 -0
- package/dist/plugins/js/jquery/jquery.d.ts.map +1 -0
- package/dist/plugins/js/jquery/jquery.js +16 -0
- package/dist/plugins/js/xhr/index.d.ts +2 -0
- package/dist/plugins/js/xhr/index.d.ts.map +1 -0
- package/dist/plugins/js/xhr/index.js +1 -0
- package/dist/plugins/js/xhr/xhr.d.ts +6 -0
- package/dist/plugins/js/xhr/xhr.d.ts.map +1 -0
- package/dist/plugins/js/xhr/xhr.js +16 -0
- package/dist/plugins/kotlin/okhttp/index.d.ts +2 -0
- package/dist/plugins/kotlin/okhttp/index.d.ts.map +1 -0
- package/dist/plugins/kotlin/okhttp/index.js +1 -0
- package/dist/plugins/kotlin/okhttp/okhttp.d.ts +6 -0
- package/dist/plugins/kotlin/okhttp/okhttp.d.ts.map +1 -0
- package/dist/plugins/kotlin/okhttp/okhttp.js +16 -0
- package/dist/plugins/node/axios/axios.d.ts +6 -0
- package/dist/plugins/node/axios/axios.d.ts.map +1 -0
- package/dist/plugins/node/axios/axios.js +16 -0
- package/dist/plugins/node/axios/index.d.ts +2 -0
- package/dist/plugins/node/axios/index.d.ts.map +1 -0
- package/dist/plugins/node/axios/index.js +1 -0
- package/dist/plugins/objc/nsurlsession/index.d.ts +2 -0
- package/dist/plugins/objc/nsurlsession/index.d.ts.map +1 -0
- package/dist/plugins/objc/nsurlsession/index.js +1 -0
- package/dist/plugins/objc/nsurlsession/nsurlsession.d.ts +6 -0
- package/dist/plugins/objc/nsurlsession/nsurlsession.d.ts.map +1 -0
- package/dist/plugins/objc/nsurlsession/nsurlsession.js +16 -0
- package/dist/plugins/ocaml/cohttp/cohttp.d.ts +6 -0
- package/dist/plugins/ocaml/cohttp/cohttp.d.ts.map +1 -0
- package/dist/plugins/ocaml/cohttp/cohttp.js +16 -0
- package/dist/plugins/ocaml/cohttp/index.d.ts +2 -0
- package/dist/plugins/ocaml/cohttp/index.d.ts.map +1 -0
- package/dist/plugins/ocaml/cohttp/index.js +1 -0
- package/dist/plugins/php/curl/curl.d.ts +6 -0
- package/dist/plugins/php/curl/curl.d.ts.map +1 -0
- package/dist/plugins/php/curl/curl.js +16 -0
- package/dist/plugins/php/curl/index.d.ts +2 -0
- package/dist/plugins/php/curl/index.d.ts.map +1 -0
- package/dist/plugins/php/curl/index.js +1 -0
- package/dist/plugins/php/guzzle/guzzle.d.ts +6 -0
- package/dist/plugins/php/guzzle/guzzle.d.ts.map +1 -0
- package/dist/plugins/php/guzzle/guzzle.js +16 -0
- package/dist/plugins/php/guzzle/index.d.ts +2 -0
- package/dist/plugins/php/guzzle/index.d.ts.map +1 -0
- package/dist/plugins/php/guzzle/index.js +1 -0
- package/dist/plugins/powershell/restmethod/index.d.ts +2 -0
- package/dist/plugins/powershell/restmethod/index.d.ts.map +1 -0
- package/dist/plugins/powershell/restmethod/index.js +1 -0
- package/dist/plugins/powershell/restmethod/restmethod.d.ts +6 -0
- package/dist/plugins/powershell/restmethod/restmethod.d.ts.map +1 -0
- package/dist/plugins/powershell/restmethod/restmethod.js +16 -0
- package/dist/plugins/powershell/webrequest/index.d.ts +2 -0
- package/dist/plugins/powershell/webrequest/index.d.ts.map +1 -0
- package/dist/plugins/powershell/webrequest/index.js +1 -0
- package/dist/plugins/powershell/webrequest/webrequest.d.ts +6 -0
- package/dist/plugins/powershell/webrequest/webrequest.d.ts.map +1 -0
- package/dist/plugins/powershell/webrequest/webrequest.js +16 -0
- package/dist/plugins/python/python3/index.d.ts +2 -0
- package/dist/plugins/python/python3/index.d.ts.map +1 -0
- package/dist/plugins/python/python3/index.js +1 -0
- package/dist/plugins/python/python3/python3.d.ts +6 -0
- package/dist/plugins/python/python3/python3.d.ts.map +1 -0
- package/dist/plugins/python/python3/python3.js +16 -0
- package/dist/plugins/python/requests/index.d.ts +2 -0
- package/dist/plugins/python/requests/index.d.ts.map +1 -0
- package/dist/plugins/python/requests/index.js +1 -0
- package/dist/plugins/python/requests/requests.d.ts +6 -0
- package/dist/plugins/python/requests/requests.d.ts.map +1 -0
- package/dist/plugins/python/requests/requests.js +16 -0
- package/dist/plugins/r/httr/httr.d.ts +6 -0
- package/dist/plugins/r/httr/httr.d.ts.map +1 -0
- package/dist/plugins/r/httr/httr.js +16 -0
- package/dist/plugins/r/httr/index.d.ts +2 -0
- package/dist/plugins/r/httr/index.d.ts.map +1 -0
- package/dist/plugins/r/httr/index.js +1 -0
- package/dist/plugins/ruby/native/index.d.ts +2 -0
- package/dist/plugins/ruby/native/index.d.ts.map +1 -0
- package/dist/plugins/ruby/native/index.js +1 -0
- package/dist/plugins/ruby/native/native.d.ts +6 -0
- package/dist/plugins/ruby/native/native.d.ts.map +1 -0
- package/dist/plugins/ruby/native/native.js +16 -0
- package/dist/plugins/shell/httpie/httpie.d.ts +6 -0
- package/dist/plugins/shell/httpie/httpie.d.ts.map +1 -0
- package/dist/plugins/shell/httpie/httpie.js +16 -0
- package/dist/plugins/shell/httpie/index.d.ts +2 -0
- package/dist/plugins/shell/httpie/index.d.ts.map +1 -0
- package/dist/plugins/shell/httpie/index.js +1 -0
- package/dist/plugins/shell/wget/index.d.ts +2 -0
- package/dist/plugins/shell/wget/index.d.ts.map +1 -0
- package/dist/plugins/shell/wget/index.js +1 -0
- package/dist/plugins/shell/wget/wget.d.ts +6 -0
- package/dist/plugins/shell/wget/wget.d.ts.map +1 -0
- package/dist/plugins/shell/wget/wget.js +16 -0
- package/dist/plugins/swift/nsurlsession/index.d.ts +2 -0
- package/dist/plugins/swift/nsurlsession/index.d.ts.map +1 -0
- package/dist/plugins/swift/nsurlsession/index.js +1 -0
- package/dist/plugins/swift/nsurlsession/nsurlsession.d.ts +6 -0
- package/dist/plugins/swift/nsurlsession/nsurlsession.d.ts.map +1 -0
- package/dist/plugins/swift/nsurlsession/nsurlsession.js +16 -0
- package/dist/snippetz.d.ts +4 -4
- package/dist/snippetz.d.ts.map +1 -1
- package/dist/snippetz.js +60 -4
- package/package.json +145 -1
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
|
|
2
|
+
import { literalDeclaration } from '../httpsnippet-lite/dist/esm/targets/swift/helpers.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* HTTP code snippet generator for Swift using NSURLSession.
|
|
7
|
+
*
|
|
8
|
+
* @author
|
|
9
|
+
* @thibaultCha
|
|
10
|
+
*
|
|
11
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
12
|
+
*/
|
|
13
|
+
const nsurlsession = {
|
|
14
|
+
info: {
|
|
15
|
+
key: 'nsurlsession',
|
|
16
|
+
title: 'NSURLSession',
|
|
17
|
+
link: 'https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html',
|
|
18
|
+
description: "Foundation's NSURLSession request",
|
|
19
|
+
},
|
|
20
|
+
convert: ({ allHeaders, postData, fullUrl, method }, options) => {
|
|
21
|
+
var _a;
|
|
22
|
+
const opts = {
|
|
23
|
+
indent: ' ',
|
|
24
|
+
pretty: true,
|
|
25
|
+
timeout: '10',
|
|
26
|
+
...options,
|
|
27
|
+
};
|
|
28
|
+
const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
|
|
29
|
+
// Markers for headers to be created as litteral objects and later be set on the NSURLRequest if exist
|
|
30
|
+
const req = {
|
|
31
|
+
hasHeaders: false,
|
|
32
|
+
hasBody: false,
|
|
33
|
+
};
|
|
34
|
+
// We just want to make sure people understand that is the only dependency
|
|
35
|
+
push('import Foundation');
|
|
36
|
+
if (Object.keys(allHeaders).length) {
|
|
37
|
+
req.hasHeaders = true;
|
|
38
|
+
blank();
|
|
39
|
+
push(literalDeclaration('headers', allHeaders, opts));
|
|
40
|
+
}
|
|
41
|
+
if (postData && (postData.text || postData.jsonObj || postData.params)) {
|
|
42
|
+
req.hasBody = true;
|
|
43
|
+
switch (postData.mimeType) {
|
|
44
|
+
case 'application/x-www-form-urlencoded':
|
|
45
|
+
// By appending parameters one by one in the resulting snippet,
|
|
46
|
+
// we make it easier for the user to edit it according to his or her needs after pasting.
|
|
47
|
+
// The user can just add/remove lines adding/removing body parameters.
|
|
48
|
+
blank();
|
|
49
|
+
if ((_a = postData.params) === null || _a === void 0 ? void 0 : _a.length) {
|
|
50
|
+
const [head, ...tail] = postData.params;
|
|
51
|
+
push(`let postData = NSMutableData(data: "${head.name}=${head.value}".data(using: String.Encoding.utf8)!)`);
|
|
52
|
+
tail.forEach(({ name, value }) => {
|
|
53
|
+
push(`postData.append("&${name}=${value}".data(using: String.Encoding.utf8)!)`);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
req.hasBody = false;
|
|
58
|
+
}
|
|
59
|
+
break;
|
|
60
|
+
case 'application/json':
|
|
61
|
+
if (postData.jsonObj) {
|
|
62
|
+
push(`${literalDeclaration('parameters', postData.jsonObj, opts)} as [String : Any]`);
|
|
63
|
+
blank();
|
|
64
|
+
push('let postData = JSONSerialization.data(withJSONObject: parameters, options: [])');
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
case 'multipart/form-data':
|
|
68
|
+
/**
|
|
69
|
+
* By appending multipart parameters one by one in the resulting snippet,
|
|
70
|
+
* we make it easier for the user to edit it according to his or her needs after pasting.
|
|
71
|
+
* The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
|
|
72
|
+
*/
|
|
73
|
+
push(literalDeclaration('parameters', postData.params, opts));
|
|
74
|
+
blank();
|
|
75
|
+
push(`let boundary = "${postData.boundary}"`);
|
|
76
|
+
blank();
|
|
77
|
+
push('var body = ""');
|
|
78
|
+
push('var error: NSError? = nil');
|
|
79
|
+
push('for param in parameters {');
|
|
80
|
+
push('let paramName = param["name"]!', 1);
|
|
81
|
+
push('body += "--\\(boundary)\\r\\n"', 1);
|
|
82
|
+
push('body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""', 1);
|
|
83
|
+
push('if let filename = param["fileName"] {', 1);
|
|
84
|
+
push('let contentType = param["content-type"]!', 2);
|
|
85
|
+
push('let fileContent = String(contentsOfFile: filename, encoding: String.Encoding.utf8)', 2);
|
|
86
|
+
push('if (error != nil) {', 2);
|
|
87
|
+
push('print(error as Any)', 3);
|
|
88
|
+
push('}', 2);
|
|
89
|
+
push('body += "; filename=\\"\\(filename)\\"\\r\\n"', 2);
|
|
90
|
+
push('body += "Content-Type: \\(contentType)\\r\\n\\r\\n"', 2);
|
|
91
|
+
push('body += fileContent', 2);
|
|
92
|
+
push('} else if let paramValue = param["value"] {', 1);
|
|
93
|
+
push('body += "\\r\\n\\r\\n\\(paramValue)"', 2);
|
|
94
|
+
push('}', 1);
|
|
95
|
+
push('}');
|
|
96
|
+
break;
|
|
97
|
+
default:
|
|
98
|
+
blank();
|
|
99
|
+
push(`let postData = NSData(data: "${postData.text}".data(using: String.Encoding.utf8)!)`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
blank();
|
|
103
|
+
// NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
|
|
104
|
+
push(`let request = NSMutableURLRequest(url: NSURL(string: "${fullUrl}")! as URL,`);
|
|
105
|
+
push(' cachePolicy: .useProtocolCachePolicy,');
|
|
106
|
+
push(
|
|
107
|
+
// @ts-expect-error needs better types
|
|
108
|
+
` timeoutInterval: ${parseInt(opts.timeout, 10).toFixed(1)})`);
|
|
109
|
+
push(`request.httpMethod = "${method}"`);
|
|
110
|
+
if (req.hasHeaders) {
|
|
111
|
+
push('request.allHTTPHeaderFields = headers');
|
|
112
|
+
}
|
|
113
|
+
if (req.hasBody) {
|
|
114
|
+
push('request.httpBody = postData as Data');
|
|
115
|
+
}
|
|
116
|
+
blank();
|
|
117
|
+
// Retrieving the shared session will be less verbose than creating a new one.
|
|
118
|
+
push('let session = URLSession.shared');
|
|
119
|
+
push('let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in');
|
|
120
|
+
push('if (error != nil) {', 1);
|
|
121
|
+
push('print(error as Any)', 2);
|
|
122
|
+
push('} else {', 1); // Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status .
|
|
123
|
+
push('let httpResponse = response as? HTTPURLResponse', 2);
|
|
124
|
+
push('print(httpResponse)', 2);
|
|
125
|
+
push('}', 1);
|
|
126
|
+
push('})');
|
|
127
|
+
blank();
|
|
128
|
+
push('dataTask.resume()');
|
|
129
|
+
return join();
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export { nsurlsession };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
|
|
2
|
+
import { escapeForSingleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
|
|
3
|
+
import { hasHeader, getHeader, getHeaderName } from '../httpsnippet-lite/dist/esm/helpers/headers.js';
|
|
4
|
+
import { convertType } from '../httpsnippet-lite/dist/esm/targets/php/helpers.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @description
|
|
8
|
+
* HTTP code snippet generator for PHP using Guzzle.
|
|
9
|
+
*
|
|
10
|
+
* @author @RobertoArruda
|
|
11
|
+
* @author @erunion
|
|
12
|
+
*
|
|
13
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
14
|
+
*/
|
|
15
|
+
const guzzle = {
|
|
16
|
+
info: {
|
|
17
|
+
key: 'guzzle',
|
|
18
|
+
title: 'Guzzle',
|
|
19
|
+
link: 'http://docs.guzzlephp.org/en/stable/',
|
|
20
|
+
description: 'PHP with Guzzle',
|
|
21
|
+
},
|
|
22
|
+
convert: ({ postData, fullUrl, method, cookies, headersObj }, options) => {
|
|
23
|
+
var _a;
|
|
24
|
+
const opts = {
|
|
25
|
+
closingTag: false,
|
|
26
|
+
indent: ' ',
|
|
27
|
+
noTags: false,
|
|
28
|
+
shortTags: false,
|
|
29
|
+
...options,
|
|
30
|
+
};
|
|
31
|
+
const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
|
|
32
|
+
const { code: requestCode, push: requestPush, join: requestJoin, } = new CodeBuilder({ indent: opts.indent });
|
|
33
|
+
if (!opts.noTags) {
|
|
34
|
+
push(opts.shortTags ? '<?' : '<?php');
|
|
35
|
+
blank();
|
|
36
|
+
}
|
|
37
|
+
switch (postData === null || postData === void 0 ? void 0 : postData.mimeType) {
|
|
38
|
+
case 'application/x-www-form-urlencoded':
|
|
39
|
+
requestPush(`'form_params' => ${convertType(postData.paramsObj, opts.indent + opts.indent, opts.indent)},`, 1);
|
|
40
|
+
break;
|
|
41
|
+
case 'multipart/form-data': {
|
|
42
|
+
const fields = [];
|
|
43
|
+
if (postData.params) {
|
|
44
|
+
postData.params.forEach(function (param) {
|
|
45
|
+
if (param.fileName) {
|
|
46
|
+
const field = {
|
|
47
|
+
name: param.name,
|
|
48
|
+
filename: param.fileName,
|
|
49
|
+
contents: param.value,
|
|
50
|
+
};
|
|
51
|
+
if (param.contentType) {
|
|
52
|
+
field.headers = { 'Content-Type': param.contentType };
|
|
53
|
+
}
|
|
54
|
+
fields.push(field);
|
|
55
|
+
}
|
|
56
|
+
else if (param.value) {
|
|
57
|
+
fields.push({
|
|
58
|
+
name: param.name,
|
|
59
|
+
contents: param.value,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
if (fields.length) {
|
|
65
|
+
requestPush(`'multipart' => ${convertType(fields, opts.indent + opts.indent, opts.indent)}`, 1);
|
|
66
|
+
// Guzzle adds its own boundary for multipart requests.
|
|
67
|
+
if (hasHeader(headersObj, 'content-type')) {
|
|
68
|
+
if ((_a = getHeader(headersObj, 'content-type')) === null || _a === void 0 ? void 0 : _a.indexOf('boundary')) {
|
|
69
|
+
const headerName = getHeaderName(headersObj, 'content-type');
|
|
70
|
+
if (headerName) {
|
|
71
|
+
delete headersObj[headerName];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
default:
|
|
79
|
+
if (postData === null || postData === void 0 ? void 0 : postData.text) {
|
|
80
|
+
requestPush(`'body' => ${convertType(postData.text)},`, 1);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// construct headers
|
|
84
|
+
const headers = Object.keys(headersObj)
|
|
85
|
+
.sort()
|
|
86
|
+
.map(function (key) {
|
|
87
|
+
return `${opts.indent}${opts.indent}'${key}' => '${escapeForSingleQuotes(headersObj[key])}',`;
|
|
88
|
+
});
|
|
89
|
+
// construct cookies
|
|
90
|
+
const cookieString = cookies
|
|
91
|
+
.map(cookie => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`)
|
|
92
|
+
.join('; ');
|
|
93
|
+
if (cookieString.length) {
|
|
94
|
+
headers.push(`${opts.indent}${opts.indent}'cookie' => '${escapeForSingleQuotes(cookieString)}',`);
|
|
95
|
+
}
|
|
96
|
+
if (headers.length) {
|
|
97
|
+
requestPush("'headers' => [", 1);
|
|
98
|
+
requestPush(headers.join('\n'));
|
|
99
|
+
requestPush('],', 1);
|
|
100
|
+
}
|
|
101
|
+
push('$client = new \\GuzzleHttp\\Client();');
|
|
102
|
+
blank();
|
|
103
|
+
if (requestCode.length) {
|
|
104
|
+
push(`$response = $client->request('${method}', '${fullUrl}', [`);
|
|
105
|
+
push(requestJoin());
|
|
106
|
+
push(']);');
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
push(`$response = $client->request('${method}', '${fullUrl}');`);
|
|
110
|
+
}
|
|
111
|
+
blank();
|
|
112
|
+
push('echo $response->getBody();');
|
|
113
|
+
if (!opts.noTags && opts.closingTag) {
|
|
114
|
+
blank();
|
|
115
|
+
push('?>');
|
|
116
|
+
}
|
|
117
|
+
return join();
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export { guzzle };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
|
|
2
|
+
import { escapeForDoubleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
|
|
3
|
+
import { convertType } from '../httpsnippet-lite/dist/esm/targets/php/helpers.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* HTTP code snippet generator for PHP using curl-ext.
|
|
8
|
+
*
|
|
9
|
+
* @author
|
|
10
|
+
* @AhmadNassri
|
|
11
|
+
*
|
|
12
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
13
|
+
*/
|
|
14
|
+
const curl = {
|
|
15
|
+
info: {
|
|
16
|
+
key: 'curl',
|
|
17
|
+
title: 'cURL',
|
|
18
|
+
link: 'http://php.net/manual/en/book.curl.php',
|
|
19
|
+
description: 'PHP with ext-curl',
|
|
20
|
+
},
|
|
21
|
+
convert: ({ uriObj, postData, fullUrl, method, httpVersion, cookies, headersObj }, options = {}) => {
|
|
22
|
+
const { closingTag = false, indent = ' ', maxRedirects = 10, namedErrors = false, noTags = false, shortTags = false, timeout = 30, } = options;
|
|
23
|
+
const { push, blank, join } = new CodeBuilder({ indent });
|
|
24
|
+
if (!noTags) {
|
|
25
|
+
push(shortTags ? '<?' : '<?php');
|
|
26
|
+
blank();
|
|
27
|
+
}
|
|
28
|
+
push('$curl = curl_init();');
|
|
29
|
+
blank();
|
|
30
|
+
const curlOptions = [
|
|
31
|
+
{
|
|
32
|
+
escape: true,
|
|
33
|
+
name: 'CURLOPT_PORT',
|
|
34
|
+
value: uriObj.port === '' ? null : uriObj.port,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
escape: true,
|
|
38
|
+
name: 'CURLOPT_URL',
|
|
39
|
+
value: fullUrl,
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
escape: false,
|
|
43
|
+
name: 'CURLOPT_RETURNTRANSFER',
|
|
44
|
+
value: 'true',
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
escape: true,
|
|
48
|
+
name: 'CURLOPT_ENCODING',
|
|
49
|
+
value: '',
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
escape: false,
|
|
53
|
+
name: 'CURLOPT_MAXREDIRS',
|
|
54
|
+
value: maxRedirects,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
escape: false,
|
|
58
|
+
name: 'CURLOPT_TIMEOUT',
|
|
59
|
+
value: timeout,
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
escape: false,
|
|
63
|
+
name: 'CURLOPT_HTTP_VERSION',
|
|
64
|
+
value: httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1',
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
escape: true,
|
|
68
|
+
name: 'CURLOPT_CUSTOMREQUEST',
|
|
69
|
+
value: method,
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
escape: !(postData === null || postData === void 0 ? void 0 : postData.jsonObj),
|
|
73
|
+
name: 'CURLOPT_POSTFIELDS',
|
|
74
|
+
value: postData
|
|
75
|
+
? postData.jsonObj
|
|
76
|
+
? `json_encode(${convertType(postData.jsonObj, indent.repeat(2), indent)})`
|
|
77
|
+
: postData.text
|
|
78
|
+
: undefined,
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
push('curl_setopt_array($curl, [');
|
|
82
|
+
const curlopts = new CodeBuilder({ indent, join: `\n${indent}` });
|
|
83
|
+
curlOptions.forEach(({ value, name, escape }) => {
|
|
84
|
+
if (value !== null && value !== undefined) {
|
|
85
|
+
curlopts.push(`${name} => ${escape ? JSON.stringify(value) : value},`);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
// construct cookies
|
|
89
|
+
const curlCookies = cookies.map(cookie => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`);
|
|
90
|
+
if (curlCookies.length) {
|
|
91
|
+
curlopts.push(`CURLOPT_COOKIE => "${curlCookies.join('; ')}",`);
|
|
92
|
+
}
|
|
93
|
+
// construct cookies
|
|
94
|
+
const headers = Object.keys(headersObj)
|
|
95
|
+
.sort()
|
|
96
|
+
.map(key => `"${key}: ${escapeForDoubleQuotes(headersObj[key])}"`);
|
|
97
|
+
if (headers.length) {
|
|
98
|
+
curlopts.push('CURLOPT_HTTPHEADER => [');
|
|
99
|
+
curlopts.push(headers.join(`,\n${indent}${indent}`), 1);
|
|
100
|
+
curlopts.push('],');
|
|
101
|
+
}
|
|
102
|
+
push(curlopts.join(), 1);
|
|
103
|
+
push(']);');
|
|
104
|
+
blank();
|
|
105
|
+
push('$response = curl_exec($curl);');
|
|
106
|
+
push('$err = curl_error($curl);');
|
|
107
|
+
blank();
|
|
108
|
+
push('curl_close($curl);');
|
|
109
|
+
blank();
|
|
110
|
+
push('if ($err) {');
|
|
111
|
+
if (namedErrors) {
|
|
112
|
+
push('echo array_flip(get_defined_constants(true)["curl"])[$err];', 1);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
push('echo "cURL Error #:" . $err;', 1);
|
|
116
|
+
}
|
|
117
|
+
push('} else {');
|
|
118
|
+
push('echo $response;', 1);
|
|
119
|
+
push('}');
|
|
120
|
+
if (!noTags && closingTag) {
|
|
121
|
+
blank();
|
|
122
|
+
push('?>');
|
|
123
|
+
}
|
|
124
|
+
return join();
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export { curl };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
|
|
2
|
+
import { escapeForDoubleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* HTTP code snippet generator for OCaml using CoHTTP.
|
|
7
|
+
*
|
|
8
|
+
* @author
|
|
9
|
+
* @SGrondin
|
|
10
|
+
*
|
|
11
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
12
|
+
*/
|
|
13
|
+
const cohttp = {
|
|
14
|
+
info: {
|
|
15
|
+
key: 'cohttp',
|
|
16
|
+
title: 'CoHTTP',
|
|
17
|
+
link: 'https://github.com/mirage/ocaml-cohttp',
|
|
18
|
+
description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml',
|
|
19
|
+
},
|
|
20
|
+
convert: ({ fullUrl, allHeaders, postData, method }, options) => {
|
|
21
|
+
const opts = {
|
|
22
|
+
indent: ' ',
|
|
23
|
+
...options,
|
|
24
|
+
};
|
|
25
|
+
const methods = ['get', 'post', 'head', 'delete', 'patch', 'put', 'options'];
|
|
26
|
+
const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
|
|
27
|
+
push('open Cohttp_lwt_unix');
|
|
28
|
+
push('open Cohttp');
|
|
29
|
+
push('open Lwt');
|
|
30
|
+
blank();
|
|
31
|
+
push(`let uri = Uri.of_string "${fullUrl}" in`);
|
|
32
|
+
// Add headers, including the cookies
|
|
33
|
+
const headers = Object.keys(allHeaders);
|
|
34
|
+
if (headers.length === 1) {
|
|
35
|
+
push(`let headers = Header.add (Header.init ()) "${headers[0]}" "${escapeForDoubleQuotes(allHeaders[headers[0]])}" in`);
|
|
36
|
+
}
|
|
37
|
+
else if (headers.length > 1) {
|
|
38
|
+
push('let headers = Header.add_list (Header.init ()) [');
|
|
39
|
+
headers.forEach(key => {
|
|
40
|
+
push(`("${key}", "${escapeForDoubleQuotes(allHeaders[key])}");`, 1);
|
|
41
|
+
});
|
|
42
|
+
push('] in');
|
|
43
|
+
}
|
|
44
|
+
// Add body
|
|
45
|
+
if (postData === null || postData === void 0 ? void 0 : postData.text) {
|
|
46
|
+
// Just text
|
|
47
|
+
push(`let body = Cohttp_lwt_body.of_string ${JSON.stringify(postData.text)} in`);
|
|
48
|
+
}
|
|
49
|
+
// Do the request
|
|
50
|
+
blank();
|
|
51
|
+
const h = headers.length ? '~headers ' : '';
|
|
52
|
+
const b = (postData === null || postData === void 0 ? void 0 : postData.text) ? '~body ' : '';
|
|
53
|
+
const m = methods.includes(method.toLowerCase())
|
|
54
|
+
? `\`${method.toUpperCase()}`
|
|
55
|
+
: `(Code.method_of_string "${method}")`;
|
|
56
|
+
push(`Client.call ${h}${b}${m} uri`);
|
|
57
|
+
// Catch result
|
|
58
|
+
push('>>= fun (res, body_stream) ->');
|
|
59
|
+
push('(* Do stuff with the result *)', 1);
|
|
60
|
+
return join();
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export { cohttp };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
|
|
2
|
+
import { nsDeclaration } from '../httpsnippet-lite/dist/esm/targets/objc/helpers.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* HTTP code snippet generator for Objective-C using NSURLSession.
|
|
7
|
+
*
|
|
8
|
+
* @author
|
|
9
|
+
* @thibaultCha
|
|
10
|
+
*
|
|
11
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
12
|
+
*/
|
|
13
|
+
const nsurlsession = {
|
|
14
|
+
info: {
|
|
15
|
+
key: 'nsurlsession',
|
|
16
|
+
title: 'NSURLSession',
|
|
17
|
+
link: 'https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html',
|
|
18
|
+
description: "Foundation's NSURLSession request",
|
|
19
|
+
},
|
|
20
|
+
convert: ({ allHeaders, postData, method, fullUrl }, options) => {
|
|
21
|
+
var _a;
|
|
22
|
+
const opts = {
|
|
23
|
+
indent: ' ',
|
|
24
|
+
pretty: true,
|
|
25
|
+
timeout: 10,
|
|
26
|
+
...options,
|
|
27
|
+
};
|
|
28
|
+
const { push, join, blank } = new CodeBuilder({ indent: opts.indent });
|
|
29
|
+
// Markers for headers to be created as literal objects and later be set on the NSURLRequest if exist
|
|
30
|
+
const req = {
|
|
31
|
+
hasHeaders: false,
|
|
32
|
+
hasBody: false,
|
|
33
|
+
};
|
|
34
|
+
// We just want to make sure people understand that is the only dependency
|
|
35
|
+
push('#import <Foundation/Foundation.h>');
|
|
36
|
+
if (Object.keys(allHeaders).length) {
|
|
37
|
+
req.hasHeaders = true;
|
|
38
|
+
blank();
|
|
39
|
+
push(nsDeclaration('NSDictionary', 'headers', allHeaders, opts.pretty));
|
|
40
|
+
}
|
|
41
|
+
if (postData && (postData.text || postData.jsonObj || postData.params)) {
|
|
42
|
+
req.hasBody = true;
|
|
43
|
+
switch (postData.mimeType) {
|
|
44
|
+
case 'application/x-www-form-urlencoded':
|
|
45
|
+
if ((_a = postData.params) === null || _a === void 0 ? void 0 : _a.length) {
|
|
46
|
+
// By appending parameters one by one in the resulting snippet,
|
|
47
|
+
// we make it easier for the user to edit it according to his or her needs after pasting.
|
|
48
|
+
// The user can just add/remove lines adding/removing body parameters.
|
|
49
|
+
blank();
|
|
50
|
+
const [head, ...tail] = postData.params;
|
|
51
|
+
push(`NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"${head.name}=${head.value}" dataUsingEncoding:NSUTF8StringEncoding]];`);
|
|
52
|
+
tail.forEach(({ name, value }) => {
|
|
53
|
+
push(`[postData appendData:[@"&${name}=${value}" dataUsingEncoding:NSUTF8StringEncoding]];`);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
req.hasBody = false;
|
|
58
|
+
}
|
|
59
|
+
break;
|
|
60
|
+
case 'application/json':
|
|
61
|
+
if (postData.jsonObj) {
|
|
62
|
+
push(nsDeclaration('NSDictionary', 'parameters', postData.jsonObj, opts.pretty));
|
|
63
|
+
blank();
|
|
64
|
+
push('NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];');
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
case 'multipart/form-data':
|
|
68
|
+
// By appending multipart parameters one by one in the resulting snippet,
|
|
69
|
+
// we make it easier for the user to edit it according to his or her needs after pasting.
|
|
70
|
+
// The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
|
|
71
|
+
push(nsDeclaration('NSArray', 'parameters', postData.params || [], opts.pretty));
|
|
72
|
+
push(`NSString *boundary = @"${postData.boundary}";`);
|
|
73
|
+
blank();
|
|
74
|
+
push('NSError *error;');
|
|
75
|
+
push('NSMutableString *body = [NSMutableString string];');
|
|
76
|
+
push('for (NSDictionary *param in parameters) {');
|
|
77
|
+
push('[body appendFormat:@"--%@\\r\\n", boundary];', 1);
|
|
78
|
+
push('if (param[@"fileName"]) {', 1);
|
|
79
|
+
push('[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];', 2);
|
|
80
|
+
push('[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];', 2);
|
|
81
|
+
push('[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];', 2);
|
|
82
|
+
push('if (error) {', 2);
|
|
83
|
+
push('NSLog(@"%@", error);', 3);
|
|
84
|
+
push('}', 2);
|
|
85
|
+
push('} else {', 1);
|
|
86
|
+
push('[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];', 2);
|
|
87
|
+
push('[body appendFormat:@"%@", param[@"value"]];', 2);
|
|
88
|
+
push('}', 1);
|
|
89
|
+
push('}');
|
|
90
|
+
push('[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];');
|
|
91
|
+
push('NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];');
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
blank();
|
|
95
|
+
push(`NSData *postData = [[NSData alloc] initWithData:[@"${postData.text}" dataUsingEncoding:NSUTF8StringEncoding]];`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
blank();
|
|
99
|
+
push(`NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"${fullUrl}"]`);
|
|
100
|
+
// NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
|
|
101
|
+
push(' cachePolicy:NSURLRequestUseProtocolCachePolicy');
|
|
102
|
+
push(` timeoutInterval:${opts.timeout.toFixed(1)}];`);
|
|
103
|
+
push(`[request setHTTPMethod:@"${method}"];`);
|
|
104
|
+
if (req.hasHeaders) {
|
|
105
|
+
push('[request setAllHTTPHeaderFields:headers];');
|
|
106
|
+
}
|
|
107
|
+
if (req.hasBody) {
|
|
108
|
+
push('[request setHTTPBody:postData];');
|
|
109
|
+
}
|
|
110
|
+
blank();
|
|
111
|
+
// Retrieving the shared session will be less verbose than creating a new one.
|
|
112
|
+
push('NSURLSession *session = [NSURLSession sharedSession];');
|
|
113
|
+
push('NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request');
|
|
114
|
+
push(' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {');
|
|
115
|
+
push(' if (error) {', 1);
|
|
116
|
+
push(' NSLog(@"%@", error);', 2);
|
|
117
|
+
push(' } else {', 1);
|
|
118
|
+
// Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status .
|
|
119
|
+
push(' NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;', 2);
|
|
120
|
+
push(' NSLog(@"%@", httpResponse);', 2);
|
|
121
|
+
push(' }', 1);
|
|
122
|
+
push(' }];');
|
|
123
|
+
push('[dataTask resume];');
|
|
124
|
+
return join();
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export { nsurlsession };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
|
|
2
|
+
import { escapeForDoubleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* HTTP code snippet generator for Kotlin using OkHttp.
|
|
7
|
+
*
|
|
8
|
+
* @author
|
|
9
|
+
* @seanghay
|
|
10
|
+
*
|
|
11
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
12
|
+
*/
|
|
13
|
+
const okhttp = {
|
|
14
|
+
info: {
|
|
15
|
+
key: 'okhttp',
|
|
16
|
+
title: 'OkHttp',
|
|
17
|
+
link: 'http://square.github.io/okhttp/',
|
|
18
|
+
description: 'An HTTP Request Client Library',
|
|
19
|
+
},
|
|
20
|
+
convert: ({ postData, fullUrl, method, allHeaders }, options) => {
|
|
21
|
+
const opts = {
|
|
22
|
+
indent: ' ',
|
|
23
|
+
...options,
|
|
24
|
+
};
|
|
25
|
+
const { blank, join, push } = new CodeBuilder({ indent: opts.indent });
|
|
26
|
+
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];
|
|
27
|
+
const methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH'];
|
|
28
|
+
push('val client = OkHttpClient()');
|
|
29
|
+
blank();
|
|
30
|
+
if (postData === null || postData === void 0 ? void 0 : postData.text) {
|
|
31
|
+
if (postData.boundary) {
|
|
32
|
+
push(`val mediaType = MediaType.parse("${postData.mimeType}; boundary=${postData.boundary}")`);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
push(`val mediaType = MediaType.parse("${postData.mimeType}")`);
|
|
36
|
+
}
|
|
37
|
+
push(`val body = RequestBody.create(mediaType, ${JSON.stringify(postData.text)})`);
|
|
38
|
+
}
|
|
39
|
+
push('val request = Request.Builder()');
|
|
40
|
+
push(`.url("${fullUrl}")`, 1);
|
|
41
|
+
if (!methods.includes(method.toUpperCase())) {
|
|
42
|
+
if (postData === null || postData === void 0 ? void 0 : postData.text) {
|
|
43
|
+
push(`.method("${method.toUpperCase()}", body)`, 1);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
push(`.method("${method.toUpperCase()}", null)`, 1);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else if (methodsWithBody.includes(method.toUpperCase())) {
|
|
50
|
+
if (postData === null || postData === void 0 ? void 0 : postData.text) {
|
|
51
|
+
push(`.${method.toLowerCase()}(body)`, 1);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
push(`.${method.toLowerCase()}(null)`, 1);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
push(`.${method.toLowerCase()}()`, 1);
|
|
59
|
+
}
|
|
60
|
+
// Add headers, including the cookies
|
|
61
|
+
Object.keys(allHeaders).forEach(key => {
|
|
62
|
+
push(`.addHeader("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 1);
|
|
63
|
+
});
|
|
64
|
+
push('.build()', 1);
|
|
65
|
+
blank();
|
|
66
|
+
push('val response = client.newCall(request).execute()');
|
|
67
|
+
return join();
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export { okhttp };
|