@scalar/snippetz 0.7.4 → 0.7.6
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/clients/index.js +168 -169
- package/dist/httpsnippet-lite/helpers/code-builder.js +86 -74
- package/dist/httpsnippet-lite/helpers/escape.js +69 -44
- package/dist/httpsnippet-lite/helpers/headers.js +17 -14
- package/dist/httpsnippet-lite/helpers/reducer.js +1 -1
- package/dist/httpsnippet-lite/helpers/shell.js +13 -13
- package/dist/httpsnippet-lite/helpers/snippet-request.js +1 -1
- package/dist/httpsnippet-lite/helpers/url.js +4 -8
- package/dist/httpsnippet-lite/targets/c/libcurl/client.js +69 -67
- package/dist/httpsnippet-lite/targets/clojure/clj_http/client.js +165 -154
- package/dist/httpsnippet-lite/targets/csharp/restsharp/client.js +35 -38
- package/dist/httpsnippet-lite/targets/go/native/client.js +151 -147
- package/dist/httpsnippet-lite/targets/java/asynchttp/client.js +32 -35
- package/dist/httpsnippet-lite/targets/java/nethttp/client.js +40 -39
- package/dist/httpsnippet-lite/targets/java/okhttp/client.js +83 -73
- package/dist/httpsnippet-lite/targets/java/unirest/client.js +41 -34
- package/dist/httpsnippet-lite/targets/javascript/axios/client.js +81 -76
- package/dist/httpsnippet-lite/targets/javascript/jquery/client.js +77 -71
- package/dist/httpsnippet-lite/targets/javascript/xhr/client.js +73 -71
- package/dist/httpsnippet-lite/targets/kotlin/okhttp/client.js +83 -73
- package/dist/httpsnippet-lite/targets/node/axios/client.js +74 -69
- package/dist/httpsnippet-lite/targets/objc/helpers.js +52 -33
- package/dist/httpsnippet-lite/targets/objc/nsurlsession/client.js +123 -121
- package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js +60 -49
- package/dist/httpsnippet-lite/targets/powershell/common.js +46 -56
- package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js +9 -13
- package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js +9 -13
- package/dist/httpsnippet-lite/targets/python/python3/client.js +85 -68
- package/dist/httpsnippet-lite/targets/r/httr/client.js +114 -95
- package/dist/httpsnippet-lite/targets/ruby/native/client.js +66 -70
- package/dist/httpsnippet-lite/targets/shell/httpie/client.js +121 -107
- package/dist/httpsnippet-lite/targets/shell/wget/client.js +48 -43
- package/dist/httpsnippet-lite/targets/swift/helpers.js +62 -44
- package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js +126 -112
- package/dist/httpsnippet-lite/targets/target.js +1 -1
- package/dist/index.js +2 -9
- package/dist/libs/http.js +68 -56
- package/dist/libs/javascript.js +77 -60
- package/dist/libs/php.js +59 -49
- package/dist/libs/rust.js +60 -33
- package/dist/libs/shell.js +14 -5
- package/dist/plugins/c/libcurl/index.js +1 -5
- package/dist/plugins/c/libcurl/libcurl.js +13 -13
- package/dist/plugins/clojure/clj_http/clj_http.js +13 -13
- package/dist/plugins/clojure/clj_http/index.js +1 -5
- package/dist/plugins/csharp/httpclient/httpclient.js +206 -150
- package/dist/plugins/csharp/httpclient/index.js +1 -5
- package/dist/plugins/csharp/restsharp/index.js +1 -5
- package/dist/plugins/csharp/restsharp/restsharp.js +13 -13
- package/dist/plugins/dart/http/http.js +95 -93
- package/dist/plugins/dart/http/index.js +1 -5
- package/dist/plugins/fsharp/httpclient/httpclient.js +169 -122
- package/dist/plugins/fsharp/httpclient/index.js +1 -5
- package/dist/plugins/go/native/index.js +1 -5
- package/dist/plugins/go/native/native.js +13 -13
- package/dist/plugins/http/http11/http11.d.ts.map +1 -1
- package/dist/plugins/http/http11/http11.js +104 -85
- package/dist/plugins/http/http11/index.js +1 -5
- package/dist/plugins/java/asynchttp/asynchttp.js +13 -13
- package/dist/plugins/java/asynchttp/index.js +1 -5
- package/dist/plugins/java/nethttp/index.js +1 -5
- package/dist/plugins/java/nethttp/nethttp.js +13 -13
- package/dist/plugins/java/okhttp/index.js +1 -5
- package/dist/plugins/java/okhttp/okhttp.js +13 -13
- package/dist/plugins/java/unirest/index.js +1 -5
- package/dist/plugins/java/unirest/unirest.js +13 -13
- package/dist/plugins/js/axios/axios.js +13 -13
- package/dist/plugins/js/axios/index.js +1 -5
- package/dist/plugins/js/fetch/fetch.js +82 -68
- package/dist/plugins/js/fetch/index.js +1 -5
- package/dist/plugins/js/jquery/index.js +1 -5
- package/dist/plugins/js/jquery/jquery.js +13 -13
- package/dist/plugins/js/ofetch/index.js +1 -5
- package/dist/plugins/js/ofetch/ofetch.js +61 -48
- package/dist/plugins/js/xhr/index.js +1 -5
- package/dist/plugins/js/xhr/xhr.js +13 -13
- package/dist/plugins/kotlin/okhttp/index.js +1 -5
- package/dist/plugins/kotlin/okhttp/okhttp.js +13 -13
- package/dist/plugins/node/axios/axios.js +13 -13
- package/dist/plugins/node/axios/index.js +1 -5
- package/dist/plugins/node/fetch/fetch.js +86 -74
- package/dist/plugins/node/fetch/index.js +1 -5
- package/dist/plugins/node/ofetch/index.js +1 -5
- package/dist/plugins/node/ofetch/ofetch.js +61 -48
- package/dist/plugins/node/undici/index.js +1 -5
- package/dist/plugins/node/undici/undici.js +60 -47
- package/dist/plugins/objc/nsurlsession/index.js +1 -5
- package/dist/plugins/objc/nsurlsession/nsurlsession.js +13 -13
- package/dist/plugins/ocaml/cohttp/cohttp.js +13 -13
- package/dist/plugins/ocaml/cohttp/index.js +1 -5
- package/dist/plugins/php/curl/curl.js +143 -101
- package/dist/plugins/php/curl/index.js +1 -5
- package/dist/plugins/php/guzzle/guzzle.d.ts.map +1 -1
- package/dist/plugins/php/guzzle/guzzle.js +109 -83
- package/dist/plugins/php/guzzle/index.js +1 -5
- package/dist/plugins/powershell/restmethod/index.js +1 -5
- package/dist/plugins/powershell/restmethod/restmethod.js +13 -13
- package/dist/plugins/powershell/webrequest/index.js +1 -5
- package/dist/plugins/powershell/webrequest/webrequest.js +13 -13
- package/dist/plugins/python/httpx/async.js +17 -15
- package/dist/plugins/python/httpx/index.js +2 -7
- package/dist/plugins/python/httpx/sync.js +11 -12
- package/dist/plugins/python/python3/index.js +1 -5
- package/dist/plugins/python/python3/python3.js +13 -13
- package/dist/plugins/python/requests/index.js +1 -5
- package/dist/plugins/python/requests/requests.js +11 -12
- package/dist/plugins/python/requestsLike.d.ts.map +1 -1
- package/dist/plugins/python/requestsLike.js +145 -112
- package/dist/plugins/r/httr/httr.js +13 -13
- package/dist/plugins/r/httr/index.js +1 -5
- package/dist/plugins/ruby/native/index.js +1 -5
- package/dist/plugins/ruby/native/native.js +13 -13
- package/dist/plugins/rust/reqwest/index.js +1 -5
- package/dist/plugins/rust/reqwest/reqwest.js +126 -83
- package/dist/plugins/shell/curl/curl.d.ts.map +1 -1
- package/dist/plugins/shell/curl/curl.js +129 -97
- package/dist/plugins/shell/curl/index.js +1 -5
- package/dist/plugins/shell/httpie/httpie.js +13 -13
- package/dist/plugins/shell/httpie/index.js +1 -5
- package/dist/plugins/shell/wget/index.js +1 -5
- package/dist/plugins/shell/wget/wget.js +13 -13
- package/dist/plugins/swift/nsurlsession/index.js +1 -5
- package/dist/plugins/swift/nsurlsession/nsurlsession.js +13 -13
- package/dist/snippetz.js +25 -28
- package/dist/utils/convertWithHttpSnippetLite.js +100 -96
- package/dist/utils/index.js +1 -5
- package/package.json +8 -12
- package/dist/clients/index.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/code-builder.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/escape.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/headers.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/reducer.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/shell.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/snippet-request.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/url.js.map +0 -7
- package/dist/httpsnippet-lite/targets/c/libcurl/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/clojure/clj_http/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/csharp/restsharp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/go/native/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/asynchttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/nethttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/okhttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/unirest/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/javascript/axios/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/javascript/jquery/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/javascript/xhr/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/kotlin/okhttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/node/axios/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/objc/helpers.js.map +0 -7
- package/dist/httpsnippet-lite/targets/objc/nsurlsession/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/powershell/common.js.map +0 -7
- package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/python/python3/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/r/httr/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/ruby/native/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/shell/httpie/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/shell/wget/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/swift/helpers.js.map +0 -7
- package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/target.js.map +0 -7
- package/dist/index.js.map +0 -7
- package/dist/libs/http.js.map +0 -7
- package/dist/libs/javascript.js.map +0 -7
- package/dist/libs/php.js.map +0 -7
- package/dist/libs/rust.js.map +0 -7
- package/dist/libs/shell.js.map +0 -7
- package/dist/plugins/c/libcurl/index.js.map +0 -7
- package/dist/plugins/c/libcurl/libcurl.js.map +0 -7
- package/dist/plugins/clojure/clj_http/clj_http.js.map +0 -7
- package/dist/plugins/clojure/clj_http/index.js.map +0 -7
- package/dist/plugins/csharp/httpclient/httpclient.js.map +0 -7
- package/dist/plugins/csharp/httpclient/index.js.map +0 -7
- package/dist/plugins/csharp/restsharp/index.js.map +0 -7
- package/dist/plugins/csharp/restsharp/restsharp.js.map +0 -7
- package/dist/plugins/dart/http/http.js.map +0 -7
- package/dist/plugins/dart/http/index.js.map +0 -7
- package/dist/plugins/fsharp/httpclient/httpclient.js.map +0 -7
- package/dist/plugins/fsharp/httpclient/index.js.map +0 -7
- package/dist/plugins/go/native/index.js.map +0 -7
- package/dist/plugins/go/native/native.js.map +0 -7
- package/dist/plugins/http/http11/http11.js.map +0 -7
- package/dist/plugins/http/http11/index.js.map +0 -7
- package/dist/plugins/java/asynchttp/asynchttp.js.map +0 -7
- package/dist/plugins/java/asynchttp/index.js.map +0 -7
- package/dist/plugins/java/nethttp/index.js.map +0 -7
- package/dist/plugins/java/nethttp/nethttp.js.map +0 -7
- package/dist/plugins/java/okhttp/index.js.map +0 -7
- package/dist/plugins/java/okhttp/okhttp.js.map +0 -7
- package/dist/plugins/java/unirest/index.js.map +0 -7
- package/dist/plugins/java/unirest/unirest.js.map +0 -7
- package/dist/plugins/js/axios/axios.js.map +0 -7
- package/dist/plugins/js/axios/index.js.map +0 -7
- package/dist/plugins/js/fetch/fetch.js.map +0 -7
- package/dist/plugins/js/fetch/index.js.map +0 -7
- package/dist/plugins/js/jquery/index.js.map +0 -7
- package/dist/plugins/js/jquery/jquery.js.map +0 -7
- package/dist/plugins/js/ofetch/index.js.map +0 -7
- package/dist/plugins/js/ofetch/ofetch.js.map +0 -7
- package/dist/plugins/js/xhr/index.js.map +0 -7
- package/dist/plugins/js/xhr/xhr.js.map +0 -7
- package/dist/plugins/kotlin/okhttp/index.js.map +0 -7
- package/dist/plugins/kotlin/okhttp/okhttp.js.map +0 -7
- package/dist/plugins/node/axios/axios.js.map +0 -7
- package/dist/plugins/node/axios/index.js.map +0 -7
- package/dist/plugins/node/fetch/fetch.js.map +0 -7
- package/dist/plugins/node/fetch/index.js.map +0 -7
- package/dist/plugins/node/ofetch/index.js.map +0 -7
- package/dist/plugins/node/ofetch/ofetch.js.map +0 -7
- package/dist/plugins/node/undici/index.js.map +0 -7
- package/dist/plugins/node/undici/undici.js.map +0 -7
- package/dist/plugins/objc/nsurlsession/index.js.map +0 -7
- package/dist/plugins/objc/nsurlsession/nsurlsession.js.map +0 -7
- package/dist/plugins/ocaml/cohttp/cohttp.js.map +0 -7
- package/dist/plugins/ocaml/cohttp/index.js.map +0 -7
- package/dist/plugins/php/curl/curl.js.map +0 -7
- package/dist/plugins/php/curl/index.js.map +0 -7
- package/dist/plugins/php/guzzle/guzzle.js.map +0 -7
- package/dist/plugins/php/guzzle/index.js.map +0 -7
- package/dist/plugins/powershell/restmethod/index.js.map +0 -7
- package/dist/plugins/powershell/restmethod/restmethod.js.map +0 -7
- package/dist/plugins/powershell/webrequest/index.js.map +0 -7
- package/dist/plugins/powershell/webrequest/webrequest.js.map +0 -7
- package/dist/plugins/python/httpx/async.js.map +0 -7
- package/dist/plugins/python/httpx/index.js.map +0 -7
- package/dist/plugins/python/httpx/sync.js.map +0 -7
- package/dist/plugins/python/python3/index.js.map +0 -7
- package/dist/plugins/python/python3/python3.js.map +0 -7
- package/dist/plugins/python/requests/index.js.map +0 -7
- package/dist/plugins/python/requests/requests.js.map +0 -7
- package/dist/plugins/python/requestsLike.js.map +0 -7
- package/dist/plugins/r/httr/httr.js.map +0 -7
- package/dist/plugins/r/httr/index.js.map +0 -7
- package/dist/plugins/ruby/native/index.js.map +0 -7
- package/dist/plugins/ruby/native/native.js.map +0 -7
- package/dist/plugins/rust/reqwest/index.js.map +0 -7
- package/dist/plugins/rust/reqwest/reqwest.js.map +0 -7
- package/dist/plugins/shell/curl/curl.js.map +0 -7
- package/dist/plugins/shell/curl/index.js.map +0 -7
- package/dist/plugins/shell/httpie/httpie.js.map +0 -7
- package/dist/plugins/shell/httpie/index.js.map +0 -7
- package/dist/plugins/shell/wget/index.js.map +0 -7
- package/dist/plugins/shell/wget/wget.js.map +0 -7
- package/dist/plugins/swift/nsurlsession/index.js.map +0 -7
- package/dist/plugins/swift/nsurlsession/nsurlsession.js.map +0 -7
- package/dist/snippetz.js.map +0 -7
- package/dist/utils/convertWithHttpSnippetLite.js.map +0 -7
- package/dist/utils/index.js.map +0 -7
|
@@ -1,114 +1,128 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
`let postData = NSMutableData(data: "${head.name}=${head.value}".data(using: String.Encoding.utf8)!)`
|
|
38
|
-
);
|
|
39
|
-
tail.forEach(({ name, value }) => {
|
|
40
|
-
push(`postData.append("&${name}=${value}".data(using: String.Encoding.utf8)!)`);
|
|
41
|
-
});
|
|
42
|
-
} else {
|
|
43
|
-
req.hasBody = false;
|
|
44
|
-
}
|
|
45
|
-
break;
|
|
46
|
-
case "application/json":
|
|
47
|
-
if (postData.jsonObj) {
|
|
48
|
-
push(`${literalDeclaration("parameters", postData.jsonObj, opts)} as [String : Any]`);
|
|
1
|
+
/**
|
|
2
|
+
* @description
|
|
3
|
+
* HTTP code snippet generator for Swift using NSURLSession.
|
|
4
|
+
*
|
|
5
|
+
* @author
|
|
6
|
+
* @thibaultCha
|
|
7
|
+
*
|
|
8
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
+
*/
|
|
10
|
+
import { CodeBuilder } from '../../../../httpsnippet-lite/helpers/code-builder.js';
|
|
11
|
+
import { literalDeclaration } from '../../../../httpsnippet-lite/targets/swift/helpers.js';
|
|
12
|
+
export const nsurlsession = {
|
|
13
|
+
info: {
|
|
14
|
+
key: 'nsurlsession',
|
|
15
|
+
title: 'NSURLSession',
|
|
16
|
+
link: 'https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html',
|
|
17
|
+
description: "Foundation's NSURLSession request",
|
|
18
|
+
},
|
|
19
|
+
convert: ({ allHeaders, postData, fullUrl, method }, options) => {
|
|
20
|
+
let _a;
|
|
21
|
+
const opts = {
|
|
22
|
+
indent: ' ',
|
|
23
|
+
pretty: true,
|
|
24
|
+
timeout: '10',
|
|
25
|
+
...options,
|
|
26
|
+
};
|
|
27
|
+
const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
|
|
28
|
+
// Markers for headers to be created as litteral objects and later be set on the NSURLRequest if exist
|
|
29
|
+
const req = {
|
|
30
|
+
hasHeaders: false,
|
|
31
|
+
hasBody: false,
|
|
32
|
+
};
|
|
33
|
+
// We just want to make sure people understand that is the only dependency
|
|
34
|
+
push('import Foundation');
|
|
35
|
+
if (Object.keys(allHeaders).length) {
|
|
36
|
+
req.hasHeaders = true;
|
|
49
37
|
blank();
|
|
50
|
-
push(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
38
|
+
push(literalDeclaration('headers', allHeaders, opts));
|
|
39
|
+
}
|
|
40
|
+
if (postData && (postData.text || postData.jsonObj || postData.params)) {
|
|
41
|
+
req.hasBody = true;
|
|
42
|
+
switch (postData.mimeType) {
|
|
43
|
+
case 'application/x-www-form-urlencoded':
|
|
44
|
+
// By appending parameters one by one in the resulting snippet,
|
|
45
|
+
// we make it easier for the user to edit it according to his or her needs after pasting.
|
|
46
|
+
// The user can just add/remove lines adding/removing body parameters.
|
|
47
|
+
blank();
|
|
48
|
+
if ((_a = postData.params) === null || _a === void 0 ? void 0 : _a.length) {
|
|
49
|
+
const [head, ...tail] = postData.params;
|
|
50
|
+
push(`let postData = NSMutableData(data: "${head.name}=${head.value}".data(using: String.Encoding.utf8)!)`);
|
|
51
|
+
tail.forEach(({ name, value }) => {
|
|
52
|
+
push(`postData.append("&${name}=${value}".data(using: String.Encoding.utf8)!)`);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
req.hasBody = false;
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case 'application/json':
|
|
60
|
+
if (postData.jsonObj) {
|
|
61
|
+
push(`${literalDeclaration('parameters', postData.jsonObj, opts)} as [String : Any]`);
|
|
62
|
+
blank();
|
|
63
|
+
push('let postData = JSONSerialization.data(withJSONObject: parameters, options: [])');
|
|
64
|
+
}
|
|
65
|
+
break;
|
|
66
|
+
case 'multipart/form-data':
|
|
67
|
+
/**
|
|
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
|
+
*/
|
|
72
|
+
push(literalDeclaration('parameters', postData.params, opts));
|
|
73
|
+
blank();
|
|
74
|
+
push(`let boundary = "${postData.boundary}"`);
|
|
75
|
+
blank();
|
|
76
|
+
push('var body = ""');
|
|
77
|
+
push('var error: NSError? = nil');
|
|
78
|
+
push('for param in parameters {');
|
|
79
|
+
push('let paramName = param["name"]!', 1);
|
|
80
|
+
push('body += "--\\(boundary)\\r\\n"', 1);
|
|
81
|
+
push('body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""', 1);
|
|
82
|
+
push('if let filename = param["fileName"] {', 1);
|
|
83
|
+
push('let contentType = param["content-type"]!', 2);
|
|
84
|
+
push('let fileContent = String(contentsOfFile: filename, encoding: String.Encoding.utf8)', 2);
|
|
85
|
+
push('if (error != nil) {', 2);
|
|
86
|
+
push('print(error as Any)', 3);
|
|
87
|
+
push('}', 2);
|
|
88
|
+
push('body += "; filename=\\"\\(filename)\\"\\r\\n"', 2);
|
|
89
|
+
push('body += "Content-Type: \\(contentType)\\r\\n\\r\\n"', 2);
|
|
90
|
+
push('body += fileContent', 2);
|
|
91
|
+
push('} else if let paramValue = param["value"] {', 1);
|
|
92
|
+
push('body += "\\r\\n\\r\\n\\(paramValue)"', 2);
|
|
93
|
+
push('}', 1);
|
|
94
|
+
push('}');
|
|
95
|
+
break;
|
|
96
|
+
default:
|
|
97
|
+
blank();
|
|
98
|
+
push(`let postData = NSData(data: "${postData.text}".data(using: String.Encoding.utf8)!)`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
blank();
|
|
102
|
+
// NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
|
|
103
|
+
push(`let request = NSMutableURLRequest(url: NSURL(string: "${fullUrl}")! as URL,`);
|
|
104
|
+
push(' cachePolicy: .useProtocolCachePolicy,');
|
|
105
|
+
push(` timeoutInterval: ${Number.parseInt(opts.timeout, 10).toFixed(1)})`);
|
|
106
|
+
push(`request.httpMethod = "${method}"`);
|
|
107
|
+
if (req.hasHeaders) {
|
|
108
|
+
push('request.allHTTPHeaderFields = headers');
|
|
109
|
+
}
|
|
110
|
+
if (req.hasBody) {
|
|
111
|
+
push('request.httpBody = postData as Data');
|
|
112
|
+
}
|
|
113
|
+
blank();
|
|
114
|
+
// Retrieving the shared session will be less verbose than creating a new one.
|
|
115
|
+
push('let session = URLSession.shared');
|
|
116
|
+
push('let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in');
|
|
117
|
+
push('if (error != nil) {', 1);
|
|
118
|
+
push('print(error as Any)', 2);
|
|
119
|
+
push('} else {', 1); // Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status .
|
|
120
|
+
push('let httpResponse = response as? HTTPURLResponse', 2);
|
|
121
|
+
push('print(httpResponse)', 2);
|
|
122
|
+
push('}', 1);
|
|
123
|
+
push('})');
|
|
124
|
+
blank();
|
|
125
|
+
push('dataTask.resume()');
|
|
126
|
+
return join();
|
|
127
|
+
},
|
|
110
128
|
};
|
|
111
|
-
export {
|
|
112
|
-
nsurlsession
|
|
113
|
-
};
|
|
114
|
-
//# sourceMappingURL=client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
} from "@scalar/types/snippetz";
|
|
4
|
-
import { snippetz } from "./snippetz.js";
|
|
5
|
-
export {
|
|
6
|
-
AVAILABLE_CLIENTS,
|
|
7
|
-
snippetz
|
|
8
|
-
};
|
|
9
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export { AVAILABLE_CLIENTS, } from '@scalar/types/snippetz';
|
|
2
|
+
export { snippetz } from './snippetz.js';
|
package/dist/libs/http.js
CHANGED
|
@@ -1,63 +1,75 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Normalizes the request object with defaults
|
|
3
|
+
*/
|
|
4
|
+
export function normalizeRequest(request) {
|
|
5
|
+
return {
|
|
6
|
+
...request,
|
|
7
|
+
method: (request.method || 'GET').toUpperCase(),
|
|
8
|
+
};
|
|
6
9
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Builds the query string from request parameters
|
|
12
|
+
*/
|
|
13
|
+
export function buildQueryString(queryParams) {
|
|
14
|
+
if (!queryParams?.length) {
|
|
15
|
+
return '';
|
|
16
|
+
}
|
|
17
|
+
const queryPairs = queryParams.map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value)}`);
|
|
18
|
+
return `?${queryPairs.join('&')}`;
|
|
13
19
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new URL search params object and sets query params so we can handle arrays
|
|
22
|
+
*/
|
|
23
|
+
export const createSearchParams = (query = []) => {
|
|
24
|
+
const searchParams = new URLSearchParams();
|
|
25
|
+
query.forEach((q) => {
|
|
26
|
+
searchParams.append(q.name, q.value);
|
|
27
|
+
});
|
|
28
|
+
return searchParams;
|
|
20
29
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
acc[name]
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Reduces query parameters into an object while preserving repeated keys as arrays.
|
|
32
|
+
*/
|
|
33
|
+
export function reduceQueryParams(query = []) {
|
|
34
|
+
return query.reduce((acc, { name, value }) => {
|
|
35
|
+
const existingValue = acc[name];
|
|
36
|
+
if (existingValue === undefined) {
|
|
37
|
+
acc[name] = value;
|
|
38
|
+
}
|
|
39
|
+
else if (Array.isArray(existingValue)) {
|
|
40
|
+
existingValue.push(value);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
acc[name] = [existingValue, value];
|
|
44
|
+
}
|
|
45
|
+
return acc;
|
|
46
|
+
}, {});
|
|
36
47
|
}
|
|
37
|
-
|
|
38
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Builds the complete URL with query string
|
|
50
|
+
*/
|
|
51
|
+
export function buildUrl(baseUrl, queryString) {
|
|
52
|
+
return `${baseUrl}${queryString}`;
|
|
39
53
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Processes headers and cookies into a headers object
|
|
56
|
+
*/
|
|
57
|
+
export function processHeaders(request) {
|
|
58
|
+
const headers = {};
|
|
59
|
+
// Process regular headers
|
|
60
|
+
if (request.headers) {
|
|
61
|
+
for (const header of request.headers) {
|
|
62
|
+
if (header.value && !/[; ]/.test(header.name)) {
|
|
63
|
+
headers[header.name] = header.value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Process cookies
|
|
68
|
+
if (request.cookies && request.cookies.length > 0) {
|
|
69
|
+
const cookieString = request.cookies
|
|
70
|
+
.map((cookie) => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`)
|
|
71
|
+
.join('; ');
|
|
72
|
+
headers['Cookie'] = cookieString;
|
|
47
73
|
}
|
|
48
|
-
|
|
49
|
-
if (request.cookies && request.cookies.length > 0) {
|
|
50
|
-
const cookieString = request.cookies.map((cookie) => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`).join("; ");
|
|
51
|
-
headers["Cookie"] = cookieString;
|
|
52
|
-
}
|
|
53
|
-
return headers;
|
|
74
|
+
return headers;
|
|
54
75
|
}
|
|
55
|
-
export {
|
|
56
|
-
buildQueryString,
|
|
57
|
-
buildUrl,
|
|
58
|
-
createSearchParams,
|
|
59
|
-
normalizeRequest,
|
|
60
|
-
processHeaders,
|
|
61
|
-
reduceQueryParams
|
|
62
|
-
};
|
|
63
|
-
//# sourceMappingURL=http.js.map
|
package/dist/libs/javascript.js
CHANGED
|
@@ -1,69 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a key needs to be wrapped in quotes when used as an object property
|
|
3
|
+
*
|
|
4
|
+
* Returns true if the key contains spaces or hyphens
|
|
5
|
+
*/
|
|
1
6
|
function needsQuotes(key) {
|
|
2
|
-
|
|
7
|
+
return /\s|-/.test(key);
|
|
3
8
|
}
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Represents a raw code that should not be quoted, e.g. `JSON.stringify(...)`.
|
|
11
|
+
* If consists of multiple lines, they will be indented properly.
|
|
12
|
+
*/
|
|
13
|
+
export class Raw {
|
|
14
|
+
value;
|
|
15
|
+
constructor(value) {
|
|
16
|
+
this.value = value;
|
|
17
|
+
}
|
|
8
18
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Converts an object into a string representation with proper formatting and indentation
|
|
21
|
+
*
|
|
22
|
+
* Handles nested objects, arrays, and special string values
|
|
23
|
+
*/
|
|
24
|
+
export function objectToString(obj, indent = 0) {
|
|
25
|
+
const parts = [];
|
|
26
|
+
const indentation = ' '.repeat(indent);
|
|
27
|
+
const innerIndentation = ' '.repeat(indent + 2);
|
|
28
|
+
if (Object.keys(obj).length === 0) {
|
|
29
|
+
return '{}';
|
|
30
|
+
}
|
|
31
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
32
|
+
const formattedKey = needsQuotes(key) ? `'${key}'` : key;
|
|
33
|
+
if (value instanceof Raw) {
|
|
34
|
+
const lines = value.value.split('\n');
|
|
35
|
+
let formattedValue = `${value.value}`;
|
|
36
|
+
if (lines.length > 1) {
|
|
37
|
+
formattedValue = lines
|
|
38
|
+
.map((line, index) => {
|
|
39
|
+
if (index === 0) {
|
|
40
|
+
return line;
|
|
41
|
+
}
|
|
42
|
+
return `${innerIndentation}${line}`;
|
|
43
|
+
})
|
|
44
|
+
.join('\n');
|
|
45
|
+
}
|
|
46
|
+
parts.push(`${innerIndentation}${formattedKey}: ${formattedValue}`);
|
|
47
|
+
}
|
|
48
|
+
else if (Array.isArray(value)) {
|
|
49
|
+
const items = value.map((item) => {
|
|
50
|
+
if (typeof item === 'string') {
|
|
51
|
+
return `'${item}'`;
|
|
52
|
+
}
|
|
53
|
+
if (item && typeof item === 'object') {
|
|
54
|
+
return objectToString(item);
|
|
55
|
+
}
|
|
56
|
+
return JSON.stringify(item);
|
|
57
|
+
});
|
|
58
|
+
if (items.some((item) => item.includes('\n'))) {
|
|
59
|
+
// format vertically if any array element contains a newline
|
|
60
|
+
const arrayString = items.map((item) => indentString(item, indent + 4)).join(',\n');
|
|
61
|
+
parts.push(`${innerIndentation}${formattedKey}: [\n${arrayString}\n${innerIndentation}]`);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
parts.push(`${innerIndentation}${formattedKey}: [${items.join(', ')}]`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else if (value && typeof value === 'object') {
|
|
68
|
+
parts.push(`${innerIndentation}${formattedKey}: ${objectToString(value, indent + 2)}`);
|
|
69
|
+
}
|
|
70
|
+
else if (typeof value === 'string') {
|
|
71
|
+
const formattedValue = `'${value}'`;
|
|
72
|
+
parts.push(`${innerIndentation}${formattedKey}: ${formattedValue}`);
|
|
34
73
|
}
|
|
35
|
-
|
|
36
|
-
|
|
74
|
+
else {
|
|
75
|
+
parts.push(`${innerIndentation}${formattedKey}: ${value}`);
|
|
37
76
|
}
|
|
38
|
-
return JSON.stringify(item);
|
|
39
|
-
});
|
|
40
|
-
if (items.some((item) => item.includes("\n"))) {
|
|
41
|
-
const arrayString = items.map((item) => indentString(item, indent + 4)).join(",\n");
|
|
42
|
-
parts.push(`${innerIndentation}${formattedKey}: [
|
|
43
|
-
${arrayString}
|
|
44
|
-
${innerIndentation}]`);
|
|
45
|
-
} else {
|
|
46
|
-
parts.push(`${innerIndentation}${formattedKey}: [${items.join(", ")}]`);
|
|
47
|
-
}
|
|
48
|
-
} else if (value && typeof value === "object") {
|
|
49
|
-
parts.push(`${innerIndentation}${formattedKey}: ${objectToString(value, indent + 2)}`);
|
|
50
|
-
} else if (typeof value === "string") {
|
|
51
|
-
const formattedValue = `'${value}'`;
|
|
52
|
-
parts.push(`${innerIndentation}${formattedKey}: ${formattedValue}`);
|
|
53
|
-
} else {
|
|
54
|
-
parts.push(`${innerIndentation}${formattedKey}: ${value}`);
|
|
55
77
|
}
|
|
56
|
-
|
|
57
|
-
return `{
|
|
58
|
-
${parts.join(",\n")}
|
|
59
|
-
${indentation}}`;
|
|
78
|
+
return `{\n${parts.join(',\n')}\n${indentation}}`;
|
|
60
79
|
}
|
|
61
80
|
function indentString(str, indent) {
|
|
62
|
-
|
|
63
|
-
|
|
81
|
+
const indentation = ' '.repeat(indent);
|
|
82
|
+
return str
|
|
83
|
+
.split('\n')
|
|
84
|
+
.map((line) => `${indentation}${line}`)
|
|
85
|
+
.join('\n');
|
|
64
86
|
}
|
|
65
|
-
export {
|
|
66
|
-
Raw,
|
|
67
|
-
objectToString
|
|
68
|
-
};
|
|
69
|
-
//# sourceMappingURL=javascript.js.map
|