@robert.tools/curl 1.0.0
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/LICENSE +21 -0
- package/README.md +37 -0
- package/dist/index.config.d.ts +9 -0
- package/dist/index.config.js +16 -0
- package/dist/index.config.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +58 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/utils.d.ts +52 -0
- package/dist/utils/utils.js +168 -0
- package/dist/utils/utils.js.map +1 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Robert Willemelis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# 🗂️ @robert.tools/curl
|
|
2
|
+
|
|
3
|
+
Provide an easy to use curl function
|
|
4
|
+
|
|
5
|
+
## 📜 Usage
|
|
6
|
+
|
|
7
|
+
### 🟢 Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @robert.tools/curl
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### 📝 Sample usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { curl } from '@robert.tools/curl';
|
|
17
|
+
|
|
18
|
+
curl('www.robert.tools', { silent: true }); // 'curl: robert.tools'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 🗃️ commands
|
|
22
|
+
|
|
23
|
+
After an npm install with `npm i` the following commands are available:
|
|
24
|
+
|
|
25
|
+
- execute curl command: `curl(url, options)`
|
|
26
|
+
- get curl data option: `getCurlData(request)`
|
|
27
|
+
|
|
28
|
+
## ⚖️ Notes
|
|
29
|
+
|
|
30
|
+
This software is hand-crafted, test-driven and assisted by AI tools. I know each line of my code. ✌️
|
|
31
|
+
|
|
32
|
+
| Tool | Comment |
|
|
33
|
+
| ------------- | ------------- |
|
|
34
|
+
| <img src="https://img.shields.io/badge/Jest-TDD-008800?logo=jest" alt="assisted by Jest" /> | Test-driven development with Jest |
|
|
35
|
+
| <img src="https://img.shields.io/badge/robert.tools-ecosystem-008800" alt="assisted by robert.tools" /> | Part of the robert.tools ecosystem |
|
|
36
|
+
| <img src="https://img.shields.io/badge/GitHub_Copilot-assisted-8A2BE2?logo=githubcopilot" alt="assisted by GitHub Copilot" /> | Code completion |
|
|
37
|
+
| <img src="https://img.shields.io/badge/OpenAI-assisted-8A2BE2?logo=openaigym" alt="assisted by OpenAI" /> | chatGPT research |
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { CURL_PARAM } from './index.d';
|
|
2
|
+
export declare const CURL_OPTIONS: {
|
|
3
|
+
[key: string]: CURL_PARAM;
|
|
4
|
+
};
|
|
5
|
+
export declare const HEADER_OPTIONS: {
|
|
6
|
+
[key: string]: CURL_PARAM;
|
|
7
|
+
};
|
|
8
|
+
export declare const PARAM_KEYS: string[];
|
|
9
|
+
export declare const DEFAULT_UA = "nodejs";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_UA = exports.PARAM_KEYS = exports.HEADER_OPTIONS = exports.CURL_OPTIONS = void 0;
|
|
4
|
+
exports.CURL_OPTIONS = {
|
|
5
|
+
method: { key: 'X' },
|
|
6
|
+
data: { key: 'd' },
|
|
7
|
+
silent: { key: 's', noValue: true },
|
|
8
|
+
timeout: { key: 'm', defaultValue: '0' },
|
|
9
|
+
};
|
|
10
|
+
exports.HEADER_OPTIONS = {
|
|
11
|
+
ua: { key: 'User-Agent', defaultValue: 'nodejs', noColon: false },
|
|
12
|
+
token: { key: '', defaultValue: '', noColon: true },
|
|
13
|
+
};
|
|
14
|
+
exports.PARAM_KEYS = ['forwarding'];
|
|
15
|
+
exports.DEFAULT_UA = 'nodejs';
|
|
16
|
+
//# sourceMappingURL=index.config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.config.js","sourceRoot":"","sources":["../src/index.config.ts"],"names":[],"mappings":";;;AAEa,QAAA,YAAY,GAAkC;IACvD,MAAM,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE;IACpB,IAAI,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE;IAClB,MAAM,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE;IACnC,OAAO,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,YAAY,EAAE,GAAG,EAAE;CAC3C,CAAC;AACW,QAAA,cAAc,GAAkC;IACzD,EAAE,EAAE,EAAE,GAAG,EAAE,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE;IACjE,KAAK,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,YAAY,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;CACtD,CAAC;AAEW,QAAA,UAAU,GAAG,CAAC,YAAY,CAAC,CAAC;AAC5B,QAAA,UAAU,GAAG,QAAQ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { URI } from '@robert.tools/typings';
|
|
2
|
+
import type { CURL_OPTS } from './index.d';
|
|
3
|
+
/**
|
|
4
|
+
* 🎯 Parses the curl request string to extract the curl parameters.
|
|
5
|
+
* @param {URI} request ➡️ The curl request string.
|
|
6
|
+
* @returns {CURL_ITEM} 📤 The parsed curl parameters object.
|
|
7
|
+
*/
|
|
8
|
+
export declare const getCurlData: (request: URI) => any;
|
|
9
|
+
/**
|
|
10
|
+
* 🎯 Executes a curl command with the specified URL and options.
|
|
11
|
+
* @param {URI} url ➡️ The URL to send the curl request to.
|
|
12
|
+
* @param {CURL_OPTS} options ➡️ The curl options to include in the request.
|
|
13
|
+
* @returns {string} 📤 The raw response from the curl command.
|
|
14
|
+
*/
|
|
15
|
+
export declare const curl: (url: URI, options: CURL_OPTS) => string;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.curl = exports.getCurlData = void 0;
|
|
4
|
+
// external dependencies
|
|
5
|
+
const log_1 = require("@robert.tools/log");
|
|
6
|
+
const cmd_1 = require("@robert.tools/cmd");
|
|
7
|
+
// internal dependencies
|
|
8
|
+
const utils_1 = require("./utils/utils");
|
|
9
|
+
const index_config_1 = require("./index.config");
|
|
10
|
+
/**
|
|
11
|
+
* 🎯 Parses the curl request string to extract the curl parameters.
|
|
12
|
+
* @param {URI} request ➡️ The curl request string.
|
|
13
|
+
* @returns {CURL_ITEM} 📤 The parsed curl parameters object.
|
|
14
|
+
*/
|
|
15
|
+
const getCurlData = (request) => {
|
|
16
|
+
const result = {};
|
|
17
|
+
for (const method in index_config_1.CURL_OPTIONS) {
|
|
18
|
+
const key = index_config_1.CURL_OPTIONS[method].key;
|
|
19
|
+
const value = (0, utils_1._getParamValue)(request, key);
|
|
20
|
+
if (value) {
|
|
21
|
+
if (method === 'data') {
|
|
22
|
+
try {
|
|
23
|
+
const isJson = value.match(/^\s*[\{\[]/);
|
|
24
|
+
result[method] = isJson ? JSON.parse(value) : value;
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
log_1.LOG.WARN(`Failed to parse JSON data: ${e.message}`);
|
|
28
|
+
result[method] = '';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
result[method] = value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
exports.getCurlData = getCurlData;
|
|
39
|
+
/**
|
|
40
|
+
* 🎯 Executes a curl command with the specified URL and options.
|
|
41
|
+
* @param {URI} url ➡️ The URL to send the curl request to.
|
|
42
|
+
* @param {CURL_OPTS} options ➡️ The curl options to include in the request.
|
|
43
|
+
* @returns {string} 📤 The raw response from the curl command.
|
|
44
|
+
*/
|
|
45
|
+
const curl = (url, options) => {
|
|
46
|
+
const defaultOpts = {
|
|
47
|
+
silent: true,
|
|
48
|
+
timeout: 0,
|
|
49
|
+
};
|
|
50
|
+
const allOpts = { ...defaultOpts, ...options };
|
|
51
|
+
const params = (0, utils_1._setParams)(url, allOpts);
|
|
52
|
+
const uri = allOpts.noEncode === true ? url : encodeURI(url);
|
|
53
|
+
const finalCommand = `curl ${params} -i "${uri}"`;
|
|
54
|
+
const rawData = (0, cmd_1.command)(finalCommand);
|
|
55
|
+
return rawData;
|
|
56
|
+
};
|
|
57
|
+
exports.curl = curl;
|
|
58
|
+
//# sourceM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c;QAC3B,MAAM,EAAE,IAAI;QACZ,OAAO,EAAE,CAAC;KACb,CAAC;IACF,MAAM,OAAO,GAAG,EAAE,GAAG,WAAW,EAAE,GAAG,OAAO,EAAE,CAAC;IAC/C,MAAM,MAAM,GAAG,IAAA,kBAAU,EAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IACxC,MAAM,GAAG,GAAG,OAAO,CAAC,QAAQ,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAC7D,MAAM,YAAY,GAAG,QAAQ,MAAM,QAAQ,GAAG,GAAG,CAAC;IAClD,MAAM,OAAO,GAAW,IAAA,aAAO,EAAC,YAAY,CAAC,CAAC;IAC9C,OAAO,OAAO,CAAC;AACnB,CAAC,CAAC;AAXW,QAAA,IAAI,QAWf"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { $string, URI } from '@robert.tools/typings';
|
|
2
|
+
import type { CURL, CURL_OPTS, CURL_PARAMS } from '../index.d';
|
|
3
|
+
/**
|
|
4
|
+
* 🎯 Generates a curl header string.
|
|
5
|
+
* @param {$string} key ➡️ The header key.
|
|
6
|
+
* @param {string} value ➡️ The header value.
|
|
7
|
+
* @returns {$string} 📤 The curl header string.
|
|
8
|
+
*/
|
|
9
|
+
export declare const _setHeader: (key: $string, value: string, col?: boolean) => CURL;
|
|
10
|
+
/**
|
|
11
|
+
* 🎯 Iterates over the provided curl options and generates curl command string.
|
|
12
|
+
* @param {any} options ➡️ values for the curl parameters.
|
|
13
|
+
* @param {CURL_PARAMS} OPTS ➡️ corresponding curl parameter configurations.
|
|
14
|
+
* @returns {string} 📤 The generated curl command string.
|
|
15
|
+
*/
|
|
16
|
+
export declare const _iterateOptions: (options: any, OPTS: CURL_PARAMS) => string;
|
|
17
|
+
/**
|
|
18
|
+
* 🎯 Set all curl options based on the provided parameters.
|
|
19
|
+
* @param {URI} url ➡️ The URL for the curl request.
|
|
20
|
+
* @param {HTTP_OPTS} options ➡️ The HTTP options object.
|
|
21
|
+
* @returns {CURL} 📤 The generated curl options string.
|
|
22
|
+
*/
|
|
23
|
+
export declare const _setParams: (url: URI, options: CURL_OPTS) => CURL;
|
|
24
|
+
/**
|
|
25
|
+
* 🎯 Extracts the value of a specific curl parameter from the request string.
|
|
26
|
+
* @param {URI} request ➡️ The curl request string.
|
|
27
|
+
* @param {string} key ➡️ The curl parameter key to extract.
|
|
28
|
+
* @returns {string} 📤 The value of the specified curl parameter.
|
|
29
|
+
*/
|
|
30
|
+
export declare const _getParamValue: (request: URI, key: string) => string;
|
|
31
|
+
/**
|
|
32
|
+
* 🎯 Formats the input value with the specified quote and optional filter keys.
|
|
33
|
+
* @param {any} input ➡️ The input value to format.
|
|
34
|
+
* @param {string} quote ➡️ The quote character to use for formatting.
|
|
35
|
+
* @param {string[]} KEYS ➡️ Optional array of keys to filter the input object.
|
|
36
|
+
* @returns {string} 📤 The formatted value.
|
|
37
|
+
*/
|
|
38
|
+
export declare const _setValue: (input: any, quote: string, KEYS?: string[]) => string;
|
|
39
|
+
/**
|
|
40
|
+
* 🎯 Retrieves the allowed curl option for the given key.
|
|
41
|
+
* @param {string} key ➡️ The key to check for allowed curl option.
|
|
42
|
+
* @returns {string | undefined} 📤 The allowed curl option key or undefined if not allowed.
|
|
43
|
+
*/
|
|
44
|
+
export declare const _getAllowedOption: (key: string) => $string;
|
|
45
|
+
/**
|
|
46
|
+
* 🎯 Generates the curl parameter string for the specified key and value.
|
|
47
|
+
* @param {string} key ➡️ The curl parameter key to set.
|
|
48
|
+
* @param {any} input ➡️ The value to set for the specified curl parameter.
|
|
49
|
+
* @param {object} opts ➡️ Optional configuration object.
|
|
50
|
+
* @returns {CURL} 📤 The generated curl parameter string.
|
|
51
|
+
*/
|
|
52
|
+
export declare const _setParam: (key: string, input: any, opts?: {}) => CURL;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports._setParam = exports._getAllowedOption = exports._setValue = exports._getParamValue = exports._setParams = exports._iterateOptions = exports._setHeader = void 0;
|
|
4
|
+
// external dependencies
|
|
5
|
+
const uri_1 = require("@robert.tools/uri");
|
|
6
|
+
const utils_1 = require("@robert.tools/utils");
|
|
7
|
+
// internal dependencies
|
|
8
|
+
const index_config_1 = require("../index.config");
|
|
9
|
+
/**
|
|
10
|
+
* 🎯 Generates a curl header string.
|
|
11
|
+
* @param {$string} key ➡️ The header key.
|
|
12
|
+
* @param {string} value ➡️ The header value.
|
|
13
|
+
* @returns {$string} 📤 The curl header string.
|
|
14
|
+
*/
|
|
15
|
+
const _setHeader = (key, value, col = true) => {
|
|
16
|
+
const hasKey = key && key !== '' ? true : false;
|
|
17
|
+
const hasValue = value && value !== '' ? true : false;
|
|
18
|
+
return hasKey && hasValue ? `-H "${key}${col ? ':' : ''} ${value}"` : '';
|
|
19
|
+
};
|
|
20
|
+
exports._setHeader = _setHeader;
|
|
21
|
+
/**
|
|
22
|
+
* 🎯 Iterates over the provided curl options and generates curl command string.
|
|
23
|
+
* @param {any} options ➡️ values for the curl parameters.
|
|
24
|
+
* @param {CURL_PARAMS} OPTS ➡️ corresponding curl parameter configurations.
|
|
25
|
+
* @returns {string} 📤 The generated curl command string.
|
|
26
|
+
*/
|
|
27
|
+
const _iterateOptions = (options, OPTS) => {
|
|
28
|
+
let result = '';
|
|
29
|
+
for (const key of Object.keys(OPTS)) {
|
|
30
|
+
const item = OPTS[key];
|
|
31
|
+
const col = item.noColon ? '' : ':';
|
|
32
|
+
const isHeader = item.hasOwnProperty('noColon');
|
|
33
|
+
const val = (0, utils_1.getProp)(options, key);
|
|
34
|
+
if (val && (val.toString().trim() !== '' || item.noValue === true)) {
|
|
35
|
+
const defaultValue = item.noValue ? '' : val;
|
|
36
|
+
const v = isHeader
|
|
37
|
+
? `${item.key}${col} ${val}`.trim()
|
|
38
|
+
: defaultValue;
|
|
39
|
+
const opts = isHeader ? { quote: '"' } : {};
|
|
40
|
+
const tmp = (0, exports._setParam)(key, v, opts);
|
|
41
|
+
result += ` ${tmp.trim()}`;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return ` ${result.trim()} `;
|
|
45
|
+
};
|
|
46
|
+
exports._iterateOptions = _iterateOptions;
|
|
47
|
+
/**
|
|
48
|
+
* 🎯 Set all curl options based on the provided parameters.
|
|
49
|
+
* @param {URI} url ➡️ The URL for the curl request.
|
|
50
|
+
* @param {HTTP_OPTS} options ➡️ The HTTP options object.
|
|
51
|
+
* @returns {CURL} 📤 The generated curl options string.
|
|
52
|
+
*/
|
|
53
|
+
const _setParams = (url, options) => {
|
|
54
|
+
let result = '';
|
|
55
|
+
// make a copy of options
|
|
56
|
+
const headerOpts = { ...(0, utils_1.getProp)(options, 'header', {}) };
|
|
57
|
+
const opts = { ...(0, utils_1.filterObject)(options, ['header'], 'exclude') };
|
|
58
|
+
// flags
|
|
59
|
+
const urlKey = (0, uri_1.getUrlKey)(url);
|
|
60
|
+
// set ua prop
|
|
61
|
+
const isGithub = urlKey === 'github';
|
|
62
|
+
// no user agent for github or if noUA= true
|
|
63
|
+
if (isGithub || (0, utils_1.getProp)(options, 'noUA')) {
|
|
64
|
+
headerOpts['ua'] = '';
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
headerOpts['ua'] = (0, utils_1.getProp)(headerOpts, 'ua', index_config_1.DEFAULT_UA);
|
|
68
|
+
}
|
|
69
|
+
// custom forwarding as alternative to -L
|
|
70
|
+
if ((0, utils_1.getProp)(options, 'forwarding') === true) {
|
|
71
|
+
opts['data'] = { forwarding: true };
|
|
72
|
+
}
|
|
73
|
+
const token = (0, utils_1.getProp)(headerOpts, 'token');
|
|
74
|
+
// setup auth header if token is provided
|
|
75
|
+
const TOKEN_KEYS = {
|
|
76
|
+
github: 'Authorization: token',
|
|
77
|
+
gitlab: 'PRIVATE-TOKEN:',
|
|
78
|
+
};
|
|
79
|
+
const tokenKey = (0, utils_1.getProp)(TOKEN_KEYS, urlKey);
|
|
80
|
+
headerOpts['token'] = tokenKey && token ? `${tokenKey} ${token}` : '';
|
|
81
|
+
result += (0, exports._iterateOptions)(headerOpts, index_config_1.HEADER_OPTIONS);
|
|
82
|
+
result += (0, exports._iterateOptions)(opts, index_config_1.CURL_OPTIONS);
|
|
83
|
+
return result.replace(/\s{2,}/g, ' ').trim();
|
|
84
|
+
};
|
|
85
|
+
exports._setParams = _setParams;
|
|
86
|
+
/**
|
|
87
|
+
* 🎯 Extracts the value of a specific curl parameter from the request string.
|
|
88
|
+
* @param {URI} request ➡️ The curl request string.
|
|
89
|
+
* @param {string} key ➡️ The curl parameter key to extract.
|
|
90
|
+
* @returns {string} 📤 The value of the specified curl parameter.
|
|
91
|
+
*/
|
|
92
|
+
const _getParamValue = (request, key) => {
|
|
93
|
+
const regexSQ = new RegExp(`-${key}\\s+[']([^']+)[']`, 'i');
|
|
94
|
+
const regexDQ = new RegExp(`-${key}\\s+["]([^"]+)["]`, 'i');
|
|
95
|
+
const regexNQ = new RegExp(`-${key}\\s+([^\\s]+)`, 'i');
|
|
96
|
+
const match = request.match(regexSQ) ||
|
|
97
|
+
request.match(regexDQ) ||
|
|
98
|
+
request.match(regexNQ);
|
|
99
|
+
return match ? match[1].replace(/^['"]|['"]$/g, '') : '';
|
|
100
|
+
};
|
|
101
|
+
exports._getParamValue = _getParamValue;
|
|
102
|
+
/**
|
|
103
|
+
* 🎯 Formats the input value with the specified quote and optional filter keys.
|
|
104
|
+
* @param {any} input ➡️ The input value to format.
|
|
105
|
+
* @param {string} quote ➡️ The quote character to use for formatting.
|
|
106
|
+
* @param {string[]} KEYS ➡️ Optional array of keys to filter the input object.
|
|
107
|
+
* @returns {string} 📤 The formatted value.
|
|
108
|
+
*/
|
|
109
|
+
const _setValue = (input, quote, KEYS = []) => {
|
|
110
|
+
const isJSON = typeof input === 'object';
|
|
111
|
+
const doFilter = isJSON && KEYS.length > 0;
|
|
112
|
+
const value = doFilter ? (0, utils_1.filterObject)(input, KEYS) : input;
|
|
113
|
+
const str = isJSON ? JSON.stringify(value) : value;
|
|
114
|
+
const hasSpace = /\s+/.test(input);
|
|
115
|
+
const isJSONStr = typeof input === 'string' && /^\s*[\{\[]/.test(input);
|
|
116
|
+
const finalValue = hasSpace || isJSONStr ? `${quote}${str}${quote}` : input;
|
|
117
|
+
return finalValue;
|
|
118
|
+
};
|
|
119
|
+
exports._setValue = _setValue;
|
|
120
|
+
/**
|
|
121
|
+
* 🎯 Retrieves the allowed curl option for the given key.
|
|
122
|
+
* @param {string} key ➡️ The key to check for allowed curl option.
|
|
123
|
+
* @returns {string | undefined} 📤 The allowed curl option key or undefined if not allowed.
|
|
124
|
+
*/
|
|
125
|
+
const _getAllowedOption = (key) => {
|
|
126
|
+
const k = index_config_1.CURL_OPTIONS[key]?.key;
|
|
127
|
+
if (k)
|
|
128
|
+
return k;
|
|
129
|
+
if (index_config_1.HEADER_OPTIONS[key])
|
|
130
|
+
return 'H';
|
|
131
|
+
return undefined;
|
|
132
|
+
};
|
|
133
|
+
exports._getAllowedOption = _getAllowedOption;
|
|
134
|
+
/**
|
|
135
|
+
* 🎯 Generates the curl parameter string for the specified key and value.
|
|
136
|
+
* @param {string} key ➡️ The curl parameter key to set.
|
|
137
|
+
* @param {any} input ➡️ The value to set for the specified curl parameter.
|
|
138
|
+
* @param {object} opts ➡️ Optional configuration object.
|
|
139
|
+
* @returns {CURL} 📤 The generated curl parameter string.
|
|
140
|
+
*/
|
|
141
|
+
const _setParam = (key, input, opts = {}) => {
|
|
142
|
+
if (input === undefined || input === null)
|
|
143
|
+
return '';
|
|
144
|
+
let forward = '';
|
|
145
|
+
const k = (0, exports._getAllowedOption)(key);
|
|
146
|
+
if (k === undefined)
|
|
147
|
+
return '';
|
|
148
|
+
const isJSON = typeof input === 'object';
|
|
149
|
+
const value = isJSON ? (0, utils_1.filterObject)(input, index_config_1.PARAM_KEYS) : input;
|
|
150
|
+
let hasOutput = true;
|
|
151
|
+
switch (key) {
|
|
152
|
+
case 'data': {
|
|
153
|
+
if (typeof value === 'object') {
|
|
154
|
+
hasOutput = Object.keys(value).length > 0; // dont add empty data
|
|
155
|
+
}
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const quote = (0, utils_1.getProp)(opts, 'quote', "'");
|
|
160
|
+
// if (input === undefined || input === null || input === '') return '';
|
|
161
|
+
if (hasOutput) {
|
|
162
|
+
const finalValue = (0, exports._setValue)(input, quote, index_config_1.PARAM_KEYS);
|
|
163
|
+
forward = ` -${k} ${finalValue} `;
|
|
164
|
+
}
|
|
165
|
+
return forward;
|
|
166
|
+
};
|
|
167
|
+
exports._setParam = _setParam;
|
|
168
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":";;;AAAA,wBAAwB;AACxB,2CAA8C;AAC9C,+CAA4D;AAG5D,wBAAwB;AACxB,kDAKyB;AAKzB;;;;;GAKG;AACI,MAAM,UAAU,GAAG,CAAC,GAAY,EAAE,KAAa,EAAE,GAAG,GAAG,IAAI,EAAQ,EAAE;IACxE,MAAM,MAAM,GAAG,GAAG,IAAI,GAAG,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IAChD,MAAM,QAAQ,GAAG,KAAK,IAAI,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IACtD,OAAO,MAAM,IAAI,QAAQ,CAAC,CAAC,CAAC,OAAO,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;AAC7E,CAAC,CAAC;AAJW,QAAA,UAAU,cAIrB;AAEF;;;;;GAKG;AACI,MAAM,eAAe,GAAG,CAA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,CAAC;IAC/B,MAAM,MAAM,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC;IAEzC,MAAM,KAAK,GAAQ,MAAM,CAAC,CAAC,CAAC,IAAA,oBAAY,EAAC,KAAK,EAAE,yBAAU,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IACpE,IAAI,SAAS,GAAG,IAAI,CAAC;IACrB,QAAQ,GAAG,EAAE,CAAC;QACV,KAAK,MAAM,CAAC,CAAC,CAAC;YACV,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;gBAC5B,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,sBAAsB;YACrE,CAAC;YACD,MAAM;QACV,CAAC;IACL,CAAC;IACD,MAAM,KAAK,GAAG,IAAA,eAAO,EAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IAC1C,wEAAwE;IACxE,IAAI,SAAS,EAAE,CAAC;QACZ,MAAM,UAAU,GAAG,IAAA,iBAAS,EAAC,KAAK,EAAE,KAAK,EAAE,yBAAU,CAAC,CAAC;QACvD,OAAO,GAAG,KAAK,CAAC,IAAI,UAAU,GAAG,CAAC;IACtC,CAAC;IACD,OAAO,OAAO,CAAC;AACnB,CAAC,CAAC;AAxBW,QAAA,SAAS,aAwBpB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@robert.tools/curl",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Provide an easy to use curl function",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"init": "robi init",
|
|
15
|
+
"release": "robi release",
|
|
16
|
+
"status": "robi status",
|
|
17
|
+
"version": "robi version",
|
|
18
|
+
"verbose": "robi verbose",
|
|
19
|
+
"pub": "npm whoami && npm publish --access public",
|
|
20
|
+
"build": "tsc -p tsconfig.build.json",
|
|
21
|
+
"test": "jest --coverage --runInBand --verbose",
|
|
22
|
+
"lint": "eslint .",
|
|
23
|
+
"check": "npm run lint && npm test && npm run build",
|
|
24
|
+
"prepublishOnly": "npm run check"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/robert-tools/curl.git"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/robert-tools/curl/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/robert-tools/curl#readme",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"typescript",
|
|
36
|
+
"node",
|
|
37
|
+
"curl",
|
|
38
|
+
"robert.tools"
|
|
39
|
+
],
|
|
40
|
+
"author": "Robert Willemelis",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public",
|
|
47
|
+
"registry": "https://registry.npmjs.org/"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@robert.tools/setup": "^1.3.0",
|
|
51
|
+
"@types/jest": "^29.5.14",
|
|
52
|
+
"@types/node": "^24.0.0",
|
|
53
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
54
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
55
|
+
"eslint": "^9.0.0",
|
|
56
|
+
"eslint-config-prettier": "^10.1.8",
|
|
57
|
+
"eslint-plugin-prettier": "^5.5.6",
|
|
58
|
+
"jest": "^29.7.0",
|
|
59
|
+
"jest-environment-jsdom": "^29.6.2",
|
|
60
|
+
"ts-jest": "^29.4.0",
|
|
61
|
+
"typescript": "^5.9.0"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"@robert.tools/cmd": "^1.0.0",
|
|
65
|
+
"@robert.tools/log": "^1.0.0",
|
|
66
|
+
"@robert.tools/typings": "^1.0.4",
|
|
67
|
+
"@robert.tools/uri": "^1.0.0",
|
|
68
|
+
"@robert.tools/utils": "^1.1.0"
|
|
69
|
+
}
|
|
70
|
+
}
|