@scalar/snippetz 0.9.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.
Files changed (36) hide show
  1. package/dist/clients/index.d.ts.map +1 -1
  2. package/dist/clients/index.js +2 -1
  3. package/dist/libs/http.d.ts +29 -0
  4. package/dist/libs/http.d.ts.map +1 -1
  5. package/dist/libs/http.js +53 -0
  6. package/dist/plugins/c/libcurl/libcurl.d.ts.map +1 -1
  7. package/dist/plugins/c/libcurl/libcurl.js +145 -5
  8. package/dist/plugins/go/native/native.d.ts.map +1 -1
  9. package/dist/plugins/go/native/native.js +1 -42
  10. package/dist/plugins/python/aiohttp/aiohttp.d.ts +6 -0
  11. package/dist/plugins/python/aiohttp/aiohttp.d.ts.map +1 -0
  12. package/dist/plugins/python/aiohttp/aiohttp.js +107 -0
  13. package/dist/plugins/python/aiohttp/index.d.ts +2 -0
  14. package/dist/plugins/python/aiohttp/index.d.ts.map +1 -0
  15. package/dist/plugins/python/aiohttp/index.js +1 -0
  16. package/dist/plugins/python/python3/python3.d.ts.map +1 -1
  17. package/dist/plugins/python/python3/python3.js +192 -5
  18. package/dist/plugins/python/requestsLike.d.ts +2 -0
  19. package/dist/plugins/python/requestsLike.d.ts.map +1 -1
  20. package/dist/plugins/python/requestsLike.js +9 -9
  21. package/dist/plugins/swift/nsurlsession/nsurlsession.d.ts.map +1 -1
  22. package/dist/plugins/swift/nsurlsession/nsurlsession.js +118 -5
  23. package/dist/snippetz.d.ts +2 -2
  24. package/package.json +7 -2
  25. package/dist/httpsnippet-lite/targets/c/libcurl/client.d.ts +0 -3
  26. package/dist/httpsnippet-lite/targets/c/libcurl/client.d.ts.map +0 -1
  27. package/dist/httpsnippet-lite/targets/c/libcurl/client.js +0 -74
  28. package/dist/httpsnippet-lite/targets/python/python3/client.d.ts +0 -12
  29. package/dist/httpsnippet-lite/targets/python/python3/client.d.ts.map +0 -1
  30. package/dist/httpsnippet-lite/targets/python/python3/client.js +0 -88
  31. package/dist/httpsnippet-lite/targets/swift/helpers.d.ts +0 -15
  32. package/dist/httpsnippet-lite/targets/swift/helpers.d.ts.map +0 -1
  33. package/dist/httpsnippet-lite/targets/swift/helpers.js +0 -70
  34. package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.d.ts +0 -12
  35. package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.d.ts.map +0 -1
  36. package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js +0 -128
@@ -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
- };