@scalar/snippetz 0.8.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/clients/index.d.ts.map +1 -1
- package/dist/clients/index.js +7 -5
- package/dist/libs/http.d.ts +29 -0
- package/dist/libs/http.d.ts.map +1 -1
- package/dist/libs/http.js +53 -0
- package/dist/libs/javascript.d.ts.map +1 -1
- package/dist/libs/javascript.js +6 -3
- package/dist/plugins/c/libcurl/libcurl.d.ts.map +1 -1
- package/dist/plugins/c/libcurl/libcurl.js +145 -5
- package/dist/plugins/go/native/native.d.ts.map +1 -1
- package/dist/plugins/go/native/native.js +150 -5
- package/dist/plugins/js/axios/axios.d.ts +1 -2
- package/dist/plugins/js/axios/axios.d.ts.map +1 -1
- package/dist/plugins/js/axios/axios.js +2 -11
- package/dist/plugins/node/axios/axios.d.ts +1 -2
- package/dist/plugins/node/axios/axios.d.ts.map +1 -1
- package/dist/plugins/node/axios/axios.js +2 -11
- package/dist/plugins/php/laravel/index.d.ts +2 -0
- package/dist/plugins/php/laravel/index.d.ts.map +1 -0
- package/dist/plugins/php/laravel/index.js +1 -0
- package/dist/plugins/php/laravel/laravel.d.ts +6 -0
- package/dist/plugins/php/laravel/laravel.d.ts.map +1 -0
- package/dist/plugins/php/laravel/laravel.js +134 -0
- package/dist/plugins/python/aiohttp/aiohttp.d.ts +6 -0
- package/dist/plugins/python/aiohttp/aiohttp.d.ts.map +1 -0
- package/dist/plugins/python/aiohttp/aiohttp.js +107 -0
- package/dist/plugins/python/aiohttp/index.d.ts +2 -0
- package/dist/plugins/python/aiohttp/index.d.ts.map +1 -0
- package/dist/plugins/python/aiohttp/index.js +1 -0
- package/dist/plugins/python/python3/python3.d.ts.map +1 -1
- package/dist/plugins/python/python3/python3.js +192 -5
- package/dist/plugins/python/requestsLike.d.ts +2 -0
- package/dist/plugins/python/requestsLike.d.ts.map +1 -1
- package/dist/plugins/python/requestsLike.js +9 -9
- package/dist/plugins/r/httr2/httr2.d.ts +6 -0
- package/dist/plugins/r/httr2/httr2.d.ts.map +1 -0
- package/dist/plugins/r/httr2/httr2.js +159 -0
- package/dist/plugins/r/httr2/index.d.ts +2 -0
- package/dist/plugins/r/httr2/index.d.ts.map +1 -0
- package/dist/plugins/r/httr2/index.js +1 -0
- package/dist/plugins/ruby/native/native.d.ts.map +1 -1
- package/dist/plugins/ruby/native/native.js +138 -5
- package/dist/plugins/shared/axios.d.ts +3 -0
- package/dist/plugins/shared/axios.d.ts.map +1 -0
- package/dist/plugins/shared/axios.js +148 -0
- package/dist/plugins/swift/nsurlsession/nsurlsession.d.ts.map +1 -1
- package/dist/plugins/swift/nsurlsession/nsurlsession.js +118 -5
- package/dist/snippetz.d.ts +2 -2
- package/package.json +17 -7
- package/dist/httpsnippet-lite/targets/c/libcurl/client.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/c/libcurl/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/c/libcurl/client.js +0 -74
- package/dist/httpsnippet-lite/targets/go/native/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/go/native/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/go/native/client.js +0 -157
- package/dist/httpsnippet-lite/targets/javascript/axios/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/javascript/axios/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/javascript/axios/client.js +0 -86
- package/dist/httpsnippet-lite/targets/node/axios/client.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/node/axios/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/node/axios/client.js +0 -78
- package/dist/httpsnippet-lite/targets/python/python3/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/python/python3/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/python/python3/client.js +0 -88
- package/dist/httpsnippet-lite/targets/r/httr/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/r/httr/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/r/httr/client.js +0 -115
- package/dist/httpsnippet-lite/targets/ruby/native/client.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/ruby/native/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/ruby/native/client.js +0 -67
- package/dist/httpsnippet-lite/targets/swift/helpers.d.ts +0 -15
- package/dist/httpsnippet-lite/targets/swift/helpers.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/swift/helpers.js +0 -70
- package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js +0 -128
- package/dist/plugins/r/httr/httr.d.ts +0 -6
- package/dist/plugins/r/httr/httr.d.ts.map +0 -1
- package/dist/plugins/r/httr/httr.js +0 -14
- package/dist/plugins/r/httr/index.d.ts +0 -2
- package/dist/plugins/r/httr/index.d.ts.map +0 -1
- package/dist/plugins/r/httr/index.js +0 -1
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator for R using httr
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @gabrielakoreeda
|
|
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 { escapeForDoubleQuotes, escapeForSingleQuotes } from '../../../../httpsnippet-lite/helpers/escape.js';
|
|
12
|
-
import { getHeader } from '../../../../httpsnippet-lite/helpers/headers.js';
|
|
13
|
-
export const httr = {
|
|
14
|
-
info: {
|
|
15
|
-
key: 'httr',
|
|
16
|
-
title: 'httr',
|
|
17
|
-
link: 'https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html',
|
|
18
|
-
description: 'httr: Tools for Working with URLs and HTTP',
|
|
19
|
-
},
|
|
20
|
-
convert: ({ url, queryObj, queryString, postData, allHeaders, method }, options = {}) => {
|
|
21
|
-
let _a, _b;
|
|
22
|
-
// Start snippet
|
|
23
|
-
const { push, blank, join } = new CodeBuilder({
|
|
24
|
-
indent: (_a = options.indent) !== null && _a !== void 0 ? _a : ' ',
|
|
25
|
-
});
|
|
26
|
-
// Import httr
|
|
27
|
-
push('library(httr)');
|
|
28
|
-
blank();
|
|
29
|
-
// Set URL
|
|
30
|
-
push(`url <- "${url}"`);
|
|
31
|
-
blank();
|
|
32
|
-
// Construct query string
|
|
33
|
-
const qs = queryObj;
|
|
34
|
-
delete queryObj.key;
|
|
35
|
-
const entries = Object.entries(qs).flatMap(([key, value]) => Array.isArray(value) ? value.map((item) => [key, item]) : [[key, value]]);
|
|
36
|
-
const entriesCount = entries.length;
|
|
37
|
-
if (entriesCount === 1) {
|
|
38
|
-
const entry = entries[0];
|
|
39
|
-
push(`queryString <- list(${entry[0]} = "${entry[1]}")`);
|
|
40
|
-
blank();
|
|
41
|
-
}
|
|
42
|
-
else if (entriesCount > 1) {
|
|
43
|
-
push('queryString <- list(');
|
|
44
|
-
entries.forEach(([key, value], i) => {
|
|
45
|
-
const isLastItem = i !== entriesCount - 1;
|
|
46
|
-
const maybeComma = isLastItem ? ',' : '';
|
|
47
|
-
push(`${key} = "${value}"${maybeComma}`, 1);
|
|
48
|
-
});
|
|
49
|
-
push(')');
|
|
50
|
-
blank();
|
|
51
|
-
}
|
|
52
|
-
// Construct payload
|
|
53
|
-
const payload = JSON.stringify(postData === null || postData === void 0 ? void 0 : postData.text);
|
|
54
|
-
if (payload) {
|
|
55
|
-
push(`payload <- ${payload}`);
|
|
56
|
-
blank();
|
|
57
|
-
}
|
|
58
|
-
// Define encode
|
|
59
|
-
if (postData && (postData.text || postData.jsonObj || postData.params)) {
|
|
60
|
-
switch (postData.mimeType) {
|
|
61
|
-
case 'application/x-www-form-urlencoded':
|
|
62
|
-
push('encode <- "form"');
|
|
63
|
-
blank();
|
|
64
|
-
break;
|
|
65
|
-
case 'application/json':
|
|
66
|
-
push('encode <- "json"');
|
|
67
|
-
blank();
|
|
68
|
-
break;
|
|
69
|
-
case 'multipart/form-data':
|
|
70
|
-
push('encode <- "multipart"');
|
|
71
|
-
blank();
|
|
72
|
-
break;
|
|
73
|
-
default:
|
|
74
|
-
push('encode <- "raw"');
|
|
75
|
-
blank();
|
|
76
|
-
break;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
// Construct headers
|
|
80
|
-
const cookieHeader = getHeader(allHeaders, 'cookie');
|
|
81
|
-
const acceptHeader = getHeader(allHeaders, 'accept');
|
|
82
|
-
const setCookies = cookieHeader
|
|
83
|
-
? `set_cookies(\`${String(cookieHeader).replace(/;/g, '", `').replace(/` /g, '`').replace(/[=]/g, '` = "')}")`
|
|
84
|
-
: undefined;
|
|
85
|
-
const setAccept = acceptHeader ? `accept("${escapeForDoubleQuotes(acceptHeader)}")` : undefined;
|
|
86
|
-
const setContentType = `content_type("${escapeForDoubleQuotes((_b = postData === null || postData === void 0 ? void 0 : postData.mimeType) !== null && _b !== void 0 ? _b : 'application/octet-stream')}")`;
|
|
87
|
-
const otherHeaders = Object.entries(allHeaders)
|
|
88
|
-
// These headers are all handled separately:
|
|
89
|
-
.filter(([key]) => !['cookie', 'accept', 'content-type'].includes(key.toLowerCase()))
|
|
90
|
-
.map(([key, value]) => `'${key}' = '${escapeForSingleQuotes(value)}'`)
|
|
91
|
-
.join(', ');
|
|
92
|
-
const setHeaders = otherHeaders ? `add_headers(${otherHeaders})` : undefined;
|
|
93
|
-
// Construct request
|
|
94
|
-
let request = `response <- VERB("${method}", url`;
|
|
95
|
-
if (payload) {
|
|
96
|
-
request += ', body = payload';
|
|
97
|
-
}
|
|
98
|
-
if (queryString.length) {
|
|
99
|
-
request += ', query = queryString';
|
|
100
|
-
}
|
|
101
|
-
const headerAdditions = [setHeaders, setContentType, setAccept, setCookies].filter((x) => !!x).join(', ');
|
|
102
|
-
if (headerAdditions) {
|
|
103
|
-
request += `, ${headerAdditions}`;
|
|
104
|
-
}
|
|
105
|
-
if (postData && (postData.text || postData.jsonObj || postData.params)) {
|
|
106
|
-
request += ', encode = encode';
|
|
107
|
-
}
|
|
108
|
-
request += ')';
|
|
109
|
-
push(request);
|
|
110
|
-
blank();
|
|
111
|
-
// Print response
|
|
112
|
-
push('content(response, "text")');
|
|
113
|
-
return join();
|
|
114
|
-
},
|
|
115
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../../../src/httpsnippet-lite/targets/ruby/native/client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAA;AAE/D,eAAO,MAAM,MAAM,EAAE,MAkEpB,CAAA"}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { CodeBuilder } from '../../../../httpsnippet-lite/helpers/code-builder.js';
|
|
2
|
-
import { escapeForSingleQuotes } from '../../../../httpsnippet-lite/helpers/escape.js';
|
|
3
|
-
export const native = {
|
|
4
|
-
info: {
|
|
5
|
-
key: 'native',
|
|
6
|
-
title: 'net::http',
|
|
7
|
-
link: 'http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html',
|
|
8
|
-
description: 'Ruby HTTP client',
|
|
9
|
-
},
|
|
10
|
-
convert: ({ uriObj, method: rawMethod, fullUrl, postData, allHeaders }, options = {}) => {
|
|
11
|
-
const { insecureSkipVerify = false } = options;
|
|
12
|
-
const { push, blank, join } = new CodeBuilder();
|
|
13
|
-
push("require 'uri'");
|
|
14
|
-
push("require 'net/http'");
|
|
15
|
-
blank();
|
|
16
|
-
// To support custom methods we check for the supported methods
|
|
17
|
-
// and if doesn't exist then we build a custom class for it
|
|
18
|
-
const method = rawMethod.toUpperCase();
|
|
19
|
-
const methods = [
|
|
20
|
-
'GET',
|
|
21
|
-
'POST',
|
|
22
|
-
'HEAD',
|
|
23
|
-
'DELETE',
|
|
24
|
-
'PATCH',
|
|
25
|
-
'PUT',
|
|
26
|
-
'OPTIONS',
|
|
27
|
-
'COPY',
|
|
28
|
-
'LOCK',
|
|
29
|
-
'UNLOCK',
|
|
30
|
-
'MOVE',
|
|
31
|
-
'TRACE',
|
|
32
|
-
];
|
|
33
|
-
const capMethod = method.charAt(0) + method.substring(1).toLowerCase();
|
|
34
|
-
if (!methods.includes(method)) {
|
|
35
|
-
push(`class Net::HTTP::${capMethod} < Net::HTTPRequest`);
|
|
36
|
-
push(` METHOD = '${method.toUpperCase()}'`);
|
|
37
|
-
push(` REQUEST_HAS_BODY = '${(postData === null || postData === void 0 ? void 0 : postData.text) ? 'true' : 'false'}'`);
|
|
38
|
-
push(' RESPONSE_HAS_BODY = true');
|
|
39
|
-
push('end');
|
|
40
|
-
blank();
|
|
41
|
-
}
|
|
42
|
-
push(`url = URI("${fullUrl}")`);
|
|
43
|
-
blank();
|
|
44
|
-
push('http = Net::HTTP.new(url.host, url.port)');
|
|
45
|
-
if (uriObj.protocol === 'https:') {
|
|
46
|
-
push('http.use_ssl = true');
|
|
47
|
-
if (insecureSkipVerify) {
|
|
48
|
-
push('http.verify_mode = OpenSSL::SSL::VERIFY_NONE');
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
blank();
|
|
52
|
-
push(`request = Net::HTTP::${capMethod}.new(url)`);
|
|
53
|
-
const headers = Object.keys(allHeaders);
|
|
54
|
-
if (headers.length) {
|
|
55
|
-
headers.forEach((key) => {
|
|
56
|
-
push(`request["${key}"] = '${escapeForSingleQuotes(allHeaders[key])}'`);
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
if (postData === null || postData === void 0 ? void 0 : postData.text) {
|
|
60
|
-
push(`request.body = ${JSON.stringify(postData.text)}`);
|
|
61
|
-
}
|
|
62
|
-
blank();
|
|
63
|
-
push('response = http.request(request)');
|
|
64
|
-
push('puts response.read_body');
|
|
65
|
-
return join();
|
|
66
|
-
},
|
|
67
|
-
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
interface Options {
|
|
2
|
-
indent: string;
|
|
3
|
-
pretty: boolean;
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Create a string corresponding to a valid declaration and initialization of a Swift array or dictionary literal
|
|
7
|
-
*
|
|
8
|
-
* @param name Desired name of the instance
|
|
9
|
-
* @param parameters Key-value object of parameters to translate to a Swift object literal
|
|
10
|
-
* @param opts Target options
|
|
11
|
-
* @return {string}
|
|
12
|
-
*/
|
|
13
|
-
export declare const literalDeclaration: (name: string, parameters: unknown, opts: Options) => string;
|
|
14
|
-
export {};
|
|
15
|
-
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../../src/httpsnippet-lite/targets/swift/helpers.ts"],"names":[],"mappings":"AAqBA,UAAU,OAAO;IACf,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,OAAO,CAAA;CAChB;AAED;;;;;;;GAOG;AAEH,eAAO,MAAM,kBAAkB,GAAI,MAAM,MAAM,EAAE,YAAY,OAAO,EAAE,MAAM,OAAO,WACvB,CAAA"}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Create an string of given length filled with blank spaces
|
|
3
|
-
*
|
|
4
|
-
* @param length Length of the array to return
|
|
5
|
-
* @param str String to pad out with
|
|
6
|
-
*/
|
|
7
|
-
const buildString = (length, str) => str.repeat(length);
|
|
8
|
-
/**
|
|
9
|
-
* Create a string corresponding to a Dictionary or Array literal representation with pretty option and indentation.
|
|
10
|
-
*/
|
|
11
|
-
const concatArray = (arr, pretty, indentation, indentLevel) => {
|
|
12
|
-
const currentIndent = buildString(indentLevel, indentation);
|
|
13
|
-
const closingBraceIndent = buildString(indentLevel - 1, indentation);
|
|
14
|
-
const join = pretty ? `,\n${currentIndent}` : ', ';
|
|
15
|
-
if (pretty) {
|
|
16
|
-
return `[\n${currentIndent}${arr.join(join)}\n${closingBraceIndent}]`;
|
|
17
|
-
}
|
|
18
|
-
return `[${arr.join(join)}]`;
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Create a string corresponding to a valid declaration and initialization of a Swift array or dictionary literal
|
|
22
|
-
*
|
|
23
|
-
* @param name Desired name of the instance
|
|
24
|
-
* @param parameters Key-value object of parameters to translate to a Swift object literal
|
|
25
|
-
* @param opts Target options
|
|
26
|
-
* @return {string}
|
|
27
|
-
*/
|
|
28
|
-
export const literalDeclaration = (name, parameters, opts) => `let ${name} = ${literalRepresentation(parameters, opts)}`;
|
|
29
|
-
/**
|
|
30
|
-
* Create a valid Swift string of a literal value according to its type.
|
|
31
|
-
*
|
|
32
|
-
* @param value Any JavaScript literal
|
|
33
|
-
* @param opts Target options
|
|
34
|
-
*/
|
|
35
|
-
const literalRepresentation = (value, opts, indentLevelInput) => {
|
|
36
|
-
const indentLevel = indentLevelInput === undefined ? 1 : indentLevelInput + 1;
|
|
37
|
-
switch (Object.prototype.toString.call(value)) {
|
|
38
|
-
case '[object Number]':
|
|
39
|
-
return value;
|
|
40
|
-
case '[object Array]': {
|
|
41
|
-
// Don't prettify arrays to avoid taking too much space
|
|
42
|
-
let pretty = false;
|
|
43
|
-
const valuesRepresentation = value.map((v) => {
|
|
44
|
-
// Switch to prettify if the value is a dictionary with multiple keys
|
|
45
|
-
if (Object.prototype.toString.call(v) === '[object Object]') {
|
|
46
|
-
pretty = Object.keys(v).length > 1;
|
|
47
|
-
}
|
|
48
|
-
return literalRepresentation(v, opts, indentLevel);
|
|
49
|
-
});
|
|
50
|
-
return concatArray(valuesRepresentation, pretty, opts.indent, indentLevel);
|
|
51
|
-
}
|
|
52
|
-
case '[object Object]': {
|
|
53
|
-
const keyValuePairs = [];
|
|
54
|
-
const _value = value;
|
|
55
|
-
for (const key in _value) {
|
|
56
|
-
if (Object.hasOwn(_value, key)) {
|
|
57
|
-
keyValuePairs.push(`"${key}": ${literalRepresentation(_value[key], opts, indentLevel)}`);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return concatArray(keyValuePairs, opts.pretty && keyValuePairs.length > 1, opts.indent, indentLevel);
|
|
61
|
-
}
|
|
62
|
-
case '[object Boolean]':
|
|
63
|
-
return value.toString();
|
|
64
|
-
default:
|
|
65
|
-
if (value === null || value === undefined) {
|
|
66
|
-
return '';
|
|
67
|
-
}
|
|
68
|
-
return `"${value.toString().replace(/"/g, '\\"')}"`;
|
|
69
|
-
}
|
|
70
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
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 type { Client } from '../../../../httpsnippet-lite/targets/target.js';
|
|
11
|
-
export declare const nsurlsession: Client;
|
|
12
|
-
//# sourceMappingURL=client.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../../../src/httpsnippet-lite/targets/swift/nsurlsession/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAA;AAE/D,eAAO,MAAM,YAAY,EAAE,MAuH1B,CAAA"}
|
|
@@ -1,128 +0,0 @@
|
|
|
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;
|
|
37
|
-
blank();
|
|
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
|
-
},
|
|
128
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"httr.d.ts","sourceRoot":"","sources":["../../../../src/plugins/r/httr/httr.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,KAAK,EAAE,MAQnB,CAAA"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { httr } from '../../../httpsnippet-lite/targets/r/httr/client.js';
|
|
2
|
-
import { convertWithHttpSnippetLite } from '../../../utils/convertWithHttpSnippetLite.js';
|
|
3
|
-
/**
|
|
4
|
-
* r/httr
|
|
5
|
-
*/
|
|
6
|
-
export const rHttr = {
|
|
7
|
-
target: 'r',
|
|
8
|
-
client: 'httr',
|
|
9
|
-
title: 'httr',
|
|
10
|
-
generate(request) {
|
|
11
|
-
// TODO: Write an own converter
|
|
12
|
-
return convertWithHttpSnippetLite(httr, request);
|
|
13
|
-
},
|
|
14
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/plugins/r/httr/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { rHttr } from './httr.js';
|