|
1 | 1 | (function (global, factory) { |
2 | 2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : |
3 | 3 | typeof define === 'function' && define.amd ? define(['exports'], factory) : |
4 | | - (global = global || self, factory(global['curl-generator'] = {})); |
5 | | -}(this, function (exports) { 'use strict'; |
| 4 | + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['curl-generator'] = {})); |
| 5 | +}(this, (function (exports) { 'use strict'; |
6 | 6 |
|
7 | | - /** |
8 | | - * @param {string} [method] |
9 | | - * @returns {string} |
10 | | - */ |
11 | | - var getCurlMethod = function (method) { |
12 | | - var result = ""; |
13 | | - if (method) { |
14 | | - var types = { |
15 | | - GET: '-X GET', |
16 | | - POST: '-X POST', |
17 | | - PUT: '-X PUT', |
18 | | - PATCH: '-X PATCH', |
19 | | - DELETE: '-X DELETE', |
20 | | - }; |
21 | | - result = types[method.toUpperCase()]; |
22 | | - } |
23 | | - return result; |
24 | | - }; |
25 | | - /** |
26 | | - * @param {StringMap} headers |
27 | | - * @returns {string} |
28 | | - */ |
29 | | - var getCurlHeaders = function (headers) { |
30 | | - var result = ""; |
31 | | - if (headers) { |
32 | | - Object.keys(headers).map(function (val) { |
33 | | - result += "-H \"" + val + ": " + headers[val].replace(/(\\|")/g, '\\$1') + "\" "; |
34 | | - }); |
35 | | - } |
36 | | - return result.trim(); |
37 | | - }; |
38 | | - /** |
39 | | - * @param {Object} body |
40 | | - * @returns {string} |
41 | | - */ |
42 | | - var getCurlBody = function (body) { |
43 | | - var result = ""; |
44 | | - if (body) { |
45 | | - result += "-d \"" + (JSON.stringify(body)).replace(/(\\|")/g, '\\$1') + "\""; |
46 | | - } |
47 | | - return result; |
48 | | - }; |
49 | | - /** |
50 | | - * @param {CurlRequest} options |
51 | | - * @returns {string} |
52 | | - */ |
53 | | - var CurlGenerator = function (options) { |
54 | | - var curlSnippet = "curl "; |
55 | | - curlSnippet += "\"" + options.url + "\" "; |
56 | | - curlSnippet += getCurlMethod(options.method) + " "; |
57 | | - curlSnippet += getCurlHeaders(options.headers) + " "; |
58 | | - curlSnippet += getCurlBody(options.body); |
59 | | - return curlSnippet.trim(); |
| 7 | + /** |
| 8 | + * @param {string} [method] |
| 9 | + * @returns {string} |
| 10 | + */ |
| 11 | + var getCurlMethod = function (method) { |
| 12 | + var result = ""; |
| 13 | + if (method) { |
| 14 | + var types = { |
| 15 | + GET: '-X GET', |
| 16 | + POST: '-X POST', |
| 17 | + PUT: '-X PUT', |
| 18 | + PATCH: '-X PATCH', |
| 19 | + DELETE: '-X DELETE', |
| 20 | + }; |
| 21 | + result = types[method.toUpperCase()]; |
| 22 | + } |
| 23 | + return result; |
| 24 | + }; |
| 25 | + /** |
| 26 | + * @param {StringMap} headers |
| 27 | + * @returns {string} |
| 28 | + */ |
| 29 | + var getCurlHeaders = function (headers) { |
| 30 | + var result = ""; |
| 31 | + if (headers) { |
| 32 | + Object.keys(headers).map(function (val) { |
| 33 | + result += "-H \"" + val + ": " + headers[val].replace(/(\\|")/g, '\\$1') + "\" "; |
| 34 | + }); |
| 35 | + } |
| 36 | + return result.trim(); |
| 37 | + }; |
| 38 | + /** |
| 39 | + * @param {Object} body |
| 40 | + * @returns {string} |
| 41 | + */ |
| 42 | + var getCurlBody = function (body) { |
| 43 | + var result = ""; |
| 44 | + if (body) { |
| 45 | + result += "-d \"" + (JSON.stringify(body)).replace(/(\\|")/g, '\\$1') + "\""; |
| 46 | + } |
| 47 | + return result; |
| 48 | + }; |
| 49 | + /** |
| 50 | + * @param {CurlRequest} options |
| 51 | + * @returns {string} |
| 52 | + */ |
| 53 | + var CurlGenerator = function (options) { |
| 54 | + var curlSnippet = "curl "; |
| 55 | + curlSnippet += "\"" + options.url + "\" "; |
| 56 | + curlSnippet += getCurlMethod(options.method) + " "; |
| 57 | + curlSnippet += getCurlHeaders(options.headers) + " "; |
| 58 | + curlSnippet += getCurlBody(options.body); |
| 59 | + return curlSnippet.trim(); |
60 | 60 | }; |
61 | 61 |
|
62 | 62 | exports.CurlGenerator = CurlGenerator; |
63 | 63 |
|
64 | 64 | Object.defineProperty(exports, '__esModule', { value: true }); |
65 | 65 |
|
66 | | -})); |
| 66 | +}))); |
0 commit comments