diff --git a/.gitignore b/.gitignore index 9daa824..a44058c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store node_modules +dist \ No newline at end of file diff --git a/dist/bodies/body.d.ts b/dist/bodies/body.d.ts deleted file mode 100644 index da79f39..0000000 --- a/dist/bodies/body.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { CurlFileBody } from "./file-body"; -import { CurlRawBody } from "./raw-body"; -import { CurlJsonBody } from "./json-body"; -import { CurlFormBody } from "./form-body"; -export declare type CurlBody = string | Record | URLSearchParams | CurlFileBody | CurlRawBody | CurlJsonBody | CurlFormBody; -export declare function bodyToString(body: CurlBody): string; -export declare function bodyToCommand(body: CurlBody): string; diff --git a/dist/bodies/file-body.d.ts b/dist/bodies/file-body.d.ts deleted file mode 100644 index e774231..0000000 --- a/dist/bodies/file-body.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -export interface CurlFileBody { - /** - * File body type for using a file as the body data. - */ - type: "file"; - /** - * The file name without the @. - */ - fileName: string; -} -export declare function isCurlFileBody(body: unknown): body is CurlFileBody; -export declare function fileBodyToString(body: CurlFileBody): string; -export declare function fileBodyToCommand(body: CurlFileBody): string; diff --git a/dist/bodies/form-body.d.ts b/dist/bodies/form-body.d.ts deleted file mode 100644 index e5cc244..0000000 --- a/dist/bodies/form-body.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { CurlFileBody } from "./file-body"; -import { CurlRawBody } from "./raw-body"; -export interface CurlFormBody { - /** - * Form body type for URL-encoded form data. - */ - type: "form"; - /** - * The content of the body. - */ - content: Record | URLSearchParams; -} -export declare function formBodyToString(body: CurlFormBody): string; -export declare function isCurlFormBody(body: unknown): body is CurlFormBody; -export declare function formBodyToCommand(body: CurlFormBody): string; diff --git a/dist/bodies/json-body.d.ts b/dist/bodies/json-body.d.ts deleted file mode 100644 index 7f6d362..0000000 --- a/dist/bodies/json-body.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -export interface CurlJsonBody { - /** - * JSON body type for JSON objects. - */ - type: "json"; - /** - * The content of the body. - */ - content: Record; -} -export declare function isCurlJsonBody(body: unknown): body is CurlJsonBody; -export declare function jsonContentToString(content: Record): string; -export declare function jsonBodyToString(body: CurlJsonBody): string; -export declare function jsonBodyToCommand(body: CurlJsonBody): string; diff --git a/dist/bodies/raw-body.d.ts b/dist/bodies/raw-body.d.ts deleted file mode 100644 index 099e426..0000000 --- a/dist/bodies/raw-body.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -export interface CurlRawBody { - /** - * Raw body type for strings/text. - */ - type: "raw"; - /** - * The content of the body. - */ - content: string; -} -export declare function isCurlRawBody(body: unknown): body is CurlRawBody; -export declare function rawBodyToString(body: CurlRawBody): string; -export declare function rawBodyToCommand(body: CurlRawBody): string; diff --git a/dist/curl-generator.cjs.js b/dist/curl-generator.cjs.js deleted file mode 100644 index 004eeb8..0000000 --- a/dist/curl-generator.cjs.js +++ /dev/null @@ -1,172 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, '__esModule', { value: true }); - -function isCurlFileBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "file" && "fileName" in body; -} -function fileBodyToString(body) { - return "@" + body.fileName; -} -function fileBodyToCommand(body) { - return "--data-binary " + fileBodyToString(body); -} - -function isCurlRawBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "raw" && "content" in body; -} -function rawBodyToString(body) { - return body.content; -} -function rawBodyToCommand(body) { - return "-d \"" + rawBodyToString(body) + "\""; -} - -function isCurlJsonBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "json" && "content" in body; -} -function jsonContentToString(content) { - return JSON.stringify(content).replace(/([\\"])/g, "\\$1"); -} -function jsonBodyToString(body) { - return jsonContentToString(body.content); -} -function jsonBodyToCommand(body) { - return "-d \"" + jsonBodyToString(body) + "\""; -} - -function isCurlFormBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "form" && "content" in body; -} -function formBodyToCommand(body) { - if (body.content instanceof URLSearchParams) { - return "-d \"" + body.content.toString() + "\""; - } - return Object.entries(body.content) - .map(function (_a) { - var key = _a[0], value = _a[1]; - if (typeof value === "string") { - return "-F " + key + "=" + value; - } - if (value.type === "file") { - return "-F " + key + "=@" + value.fileName; - } - if (value.type === "raw") { - return "-F " + key + "=" + value.content; - } - throw new Error("Invalid form body value type: " + value); - }) - .join(" \\\n "); -} - -function bodyToCommand(body) { - if (typeof body === "string") { - return "-d \"" + body + "\""; - } - else if (body instanceof URLSearchParams) { - return "-d \"" + body.toString() + "\""; - } - else if (isCurlFileBody(body)) { - return fileBodyToCommand(body); - } - else if (isCurlRawBody(body)) { - return rawBodyToCommand(body); - } - else if (isCurlJsonBody(body)) { - return jsonBodyToCommand(body); - } - else if (isCurlFormBody(body)) { - return formBodyToCommand(body); - } - else if (typeof body === "object") { - return "-d \"" + jsonContentToString(body) + "\""; - } - throw new Error("Invalid body type: " + body); -} - -// slash for connecting previous breakup line to current line for running cURL directly in Command Prompt -var slash = " \\"; -var newLine = "\n"; -/** - * @param {string} [method] - * @returns {string} - */ -var getCurlMethod = function (method) { - var result = ""; - if (method) { - var types = { - GET: "-X GET", - POST: "-X POST", - PUT: "-X PUT", - PATCH: "-X PATCH", - DELETE: "-X DELETE", - }; - result = " " + types[method.toUpperCase()]; - } - return slash + newLine + result; -}; -/** - * @param {StringMap} headers - * @returns {string} - */ -var getCurlHeaders = function (headers) { - var result = ""; - if (headers) { - Object.keys(headers).map(function (val) { - result += "" + slash + newLine + " -H \"" + val + ": " + headers[val].replace(/(\\|")/g, "\\$1") + "\""; - }); - } - return result; -}; -/** - * @param {CurlBody} body - * @returns {string} - */ -var getCurlBody = function (body) { - var result = ""; - if (body) { - result += "" + slash + newLine + " " + bodyToCommand(body); - } - return result; -}; -/** - * Given the curl additional options, turn them into curl syntax - * @param {CurlAdditionalOptions} [options] - * @returns {string} - */ -var getCurlOptions = function (options) { - var result = ""; - if (options) { - Object.keys(options).forEach(function (key) { - var kebabKey = key.replace(/[A-Z]/g, function (letter) { return "-" + letter.toLowerCase(); }); - if (!options[key]) { - throw new Error("Invalid Curl option " + key); - } - else if (typeof options[key] === "boolean" && options[key]) { - // boolean option, we just add --opt - result += " --" + kebabKey; - } - else if (typeof options[key] === "string") { - // string option, we have to add --opt=value - result += " --" + kebabKey + " " + options[key]; - } - }); - } - return result ? "" + slash + newLine + result : result; -}; -/** - * @param {CurlRequest} params - * @param {CurlAdditionalOptions} [options] - * @returns {string} - */ -var CurlGenerator = function (params, options) { - var curlSnippet = "curl "; - curlSnippet += params.url; - curlSnippet += getCurlMethod(params.method); - curlSnippet += getCurlHeaders(params.headers); - curlSnippet += getCurlBody(params.body); - curlSnippet += getCurlOptions(options); - return curlSnippet.trim(); -}; - -exports.CurlGenerator = CurlGenerator; diff --git a/dist/curl-generator.esm.js b/dist/curl-generator.esm.js deleted file mode 100644 index db0449d..0000000 --- a/dist/curl-generator.esm.js +++ /dev/null @@ -1,168 +0,0 @@ -function isCurlFileBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "file" && "fileName" in body; -} -function fileBodyToString(body) { - return "@" + body.fileName; -} -function fileBodyToCommand(body) { - return "--data-binary " + fileBodyToString(body); -} - -function isCurlRawBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "raw" && "content" in body; -} -function rawBodyToString(body) { - return body.content; -} -function rawBodyToCommand(body) { - return "-d \"" + rawBodyToString(body) + "\""; -} - -function isCurlJsonBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "json" && "content" in body; -} -function jsonContentToString(content) { - return JSON.stringify(content).replace(/([\\"])/g, "\\$1"); -} -function jsonBodyToString(body) { - return jsonContentToString(body.content); -} -function jsonBodyToCommand(body) { - return "-d \"" + jsonBodyToString(body) + "\""; -} - -function isCurlFormBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "form" && "content" in body; -} -function formBodyToCommand(body) { - if (body.content instanceof URLSearchParams) { - return "-d \"" + body.content.toString() + "\""; - } - return Object.entries(body.content) - .map(function (_a) { - var key = _a[0], value = _a[1]; - if (typeof value === "string") { - return "-F " + key + "=" + value; - } - if (value.type === "file") { - return "-F " + key + "=@" + value.fileName; - } - if (value.type === "raw") { - return "-F " + key + "=" + value.content; - } - throw new Error("Invalid form body value type: " + value); - }) - .join(" \\\n "); -} - -function bodyToCommand(body) { - if (typeof body === "string") { - return "-d \"" + body + "\""; - } - else if (body instanceof URLSearchParams) { - return "-d \"" + body.toString() + "\""; - } - else if (isCurlFileBody(body)) { - return fileBodyToCommand(body); - } - else if (isCurlRawBody(body)) { - return rawBodyToCommand(body); - } - else if (isCurlJsonBody(body)) { - return jsonBodyToCommand(body); - } - else if (isCurlFormBody(body)) { - return formBodyToCommand(body); - } - else if (typeof body === "object") { - return "-d \"" + jsonContentToString(body) + "\""; - } - throw new Error("Invalid body type: " + body); -} - -// slash for connecting previous breakup line to current line for running cURL directly in Command Prompt -var slash = " \\"; -var newLine = "\n"; -/** - * @param {string} [method] - * @returns {string} - */ -var getCurlMethod = function (method) { - var result = ""; - if (method) { - var types = { - GET: "-X GET", - POST: "-X POST", - PUT: "-X PUT", - PATCH: "-X PATCH", - DELETE: "-X DELETE", - }; - result = " " + types[method.toUpperCase()]; - } - return slash + newLine + result; -}; -/** - * @param {StringMap} headers - * @returns {string} - */ -var getCurlHeaders = function (headers) { - var result = ""; - if (headers) { - Object.keys(headers).map(function (val) { - result += "" + slash + newLine + " -H \"" + val + ": " + headers[val].replace(/(\\|")/g, "\\$1") + "\""; - }); - } - return result; -}; -/** - * @param {CurlBody} body - * @returns {string} - */ -var getCurlBody = function (body) { - var result = ""; - if (body) { - result += "" + slash + newLine + " " + bodyToCommand(body); - } - return result; -}; -/** - * Given the curl additional options, turn them into curl syntax - * @param {CurlAdditionalOptions} [options] - * @returns {string} - */ -var getCurlOptions = function (options) { - var result = ""; - if (options) { - Object.keys(options).forEach(function (key) { - var kebabKey = key.replace(/[A-Z]/g, function (letter) { return "-" + letter.toLowerCase(); }); - if (!options[key]) { - throw new Error("Invalid Curl option " + key); - } - else if (typeof options[key] === "boolean" && options[key]) { - // boolean option, we just add --opt - result += " --" + kebabKey; - } - else if (typeof options[key] === "string") { - // string option, we have to add --opt=value - result += " --" + kebabKey + " " + options[key]; - } - }); - } - return result ? "" + slash + newLine + result : result; -}; -/** - * @param {CurlRequest} params - * @param {CurlAdditionalOptions} [options] - * @returns {string} - */ -var CurlGenerator = function (params, options) { - var curlSnippet = "curl "; - curlSnippet += params.url; - curlSnippet += getCurlMethod(params.method); - curlSnippet += getCurlHeaders(params.headers); - curlSnippet += getCurlBody(params.body); - curlSnippet += getCurlOptions(options); - return curlSnippet.trim(); -}; - -export { CurlGenerator }; diff --git a/dist/curl-generator.umd.js b/dist/curl-generator.umd.js deleted file mode 100644 index de3f66f..0000000 --- a/dist/curl-generator.umd.js +++ /dev/null @@ -1,178 +0,0 @@ -(function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : - typeof define === 'function' && define.amd ? define(['exports'], factory) : - (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["curl-generator"] = {})); -})(this, (function (exports) { 'use strict'; - - function isCurlFileBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "file" && "fileName" in body; - } - function fileBodyToString(body) { - return "@" + body.fileName; - } - function fileBodyToCommand(body) { - return "--data-binary " + fileBodyToString(body); - } - - function isCurlRawBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "raw" && "content" in body; - } - function rawBodyToString(body) { - return body.content; - } - function rawBodyToCommand(body) { - return "-d \"" + rawBodyToString(body) + "\""; - } - - function isCurlJsonBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "json" && "content" in body; - } - function jsonContentToString(content) { - return JSON.stringify(content).replace(/([\\"])/g, "\\$1"); - } - function jsonBodyToString(body) { - return jsonContentToString(body.content); - } - function jsonBodyToCommand(body) { - return "-d \"" + jsonBodyToString(body) + "\""; - } - - function isCurlFormBody(body) { - return typeof body === "object" && body !== null && "type" in body && body.type === "form" && "content" in body; - } - function formBodyToCommand(body) { - if (body.content instanceof URLSearchParams) { - return "-d \"" + body.content.toString() + "\""; - } - return Object.entries(body.content) - .map(function (_a) { - var key = _a[0], value = _a[1]; - if (typeof value === "string") { - return "-F " + key + "=" + value; - } - if (value.type === "file") { - return "-F " + key + "=@" + value.fileName; - } - if (value.type === "raw") { - return "-F " + key + "=" + value.content; - } - throw new Error("Invalid form body value type: " + value); - }) - .join(" \\\n "); - } - - function bodyToCommand(body) { - if (typeof body === "string") { - return "-d \"" + body + "\""; - } - else if (body instanceof URLSearchParams) { - return "-d \"" + body.toString() + "\""; - } - else if (isCurlFileBody(body)) { - return fileBodyToCommand(body); - } - else if (isCurlRawBody(body)) { - return rawBodyToCommand(body); - } - else if (isCurlJsonBody(body)) { - return jsonBodyToCommand(body); - } - else if (isCurlFormBody(body)) { - return formBodyToCommand(body); - } - else if (typeof body === "object") { - return "-d \"" + jsonContentToString(body) + "\""; - } - throw new Error("Invalid body type: " + body); - } - - // slash for connecting previous breakup line to current line for running cURL directly in Command Prompt - var slash = " \\"; - var newLine = "\n"; - /** - * @param {string} [method] - * @returns {string} - */ - var getCurlMethod = function (method) { - var result = ""; - if (method) { - var types = { - GET: "-X GET", - POST: "-X POST", - PUT: "-X PUT", - PATCH: "-X PATCH", - DELETE: "-X DELETE", - }; - result = " " + types[method.toUpperCase()]; - } - return slash + newLine + result; - }; - /** - * @param {StringMap} headers - * @returns {string} - */ - var getCurlHeaders = function (headers) { - var result = ""; - if (headers) { - Object.keys(headers).map(function (val) { - result += "" + slash + newLine + " -H \"" + val + ": " + headers[val].replace(/(\\|")/g, "\\$1") + "\""; - }); - } - return result; - }; - /** - * @param {CurlBody} body - * @returns {string} - */ - var getCurlBody = function (body) { - var result = ""; - if (body) { - result += "" + slash + newLine + " " + bodyToCommand(body); - } - return result; - }; - /** - * Given the curl additional options, turn them into curl syntax - * @param {CurlAdditionalOptions} [options] - * @returns {string} - */ - var getCurlOptions = function (options) { - var result = ""; - if (options) { - Object.keys(options).forEach(function (key) { - var kebabKey = key.replace(/[A-Z]/g, function (letter) { return "-" + letter.toLowerCase(); }); - if (!options[key]) { - throw new Error("Invalid Curl option " + key); - } - else if (typeof options[key] === "boolean" && options[key]) { - // boolean option, we just add --opt - result += " --" + kebabKey; - } - else if (typeof options[key] === "string") { - // string option, we have to add --opt=value - result += " --" + kebabKey + " " + options[key]; - } - }); - } - return result ? "" + slash + newLine + result : result; - }; - /** - * @param {CurlRequest} params - * @param {CurlAdditionalOptions} [options] - * @returns {string} - */ - var CurlGenerator = function (params, options) { - var curlSnippet = "curl "; - curlSnippet += params.url; - curlSnippet += getCurlMethod(params.method); - curlSnippet += getCurlHeaders(params.headers); - curlSnippet += getCurlBody(params.body); - curlSnippet += getCurlOptions(options); - return curlSnippet.trim(); - }; - - exports.CurlGenerator = CurlGenerator; - - Object.defineProperty(exports, '__esModule', { value: true }); - -})); diff --git a/dist/main.d.ts b/dist/main.d.ts deleted file mode 100644 index 2c59a0d..0000000 --- a/dist/main.d.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { CurlBody } from "./bodies/body"; -declare type StringMap = { - [key: string]: string; -}; -/** - * Additional options for curl command. - * - * --compressed -> Request compressed response - * --compressed-ssh -> Enable SSH compression - * --fail -> Fail silently (no output at all) on HTTP errors - * --fail-early -> Fail on first transfer error, do not continue - * --head -> Show document info only - * --include -> Include protocol response headers in the output - * --insecure -> Allow insecure server connections when using SSL - * --ipv4 -> Resolve names to IPv4 addresses - * --ipv6 -> Resolve names to IPv6 addresses - * --list-only -> List only mode - * --location -> Follow redirects - * --location-trusted -> Like --location, and send auth to other hosts - * --no-keepalive -> Disable TCP keepalive on the connection - * --show-error -> Show error even when -s is used - * --silent -> Silent mode - * --ssl -> Try SSL/TLS - * --sslv2 -> Use SSLv2 - * --sslv3 -> Use SSLv3 - * --verbose -> Make the operation more talkative - */ -declare type CurlAdditionalOptions = { - compressed: boolean; - compressedSsh: boolean; - fail: boolean; - failEarly: boolean; - head: boolean; - include: boolean; - insecure: boolean; - ipv4: boolean; - ipv6: boolean; - listOnly: boolean; - location: boolean; - locationTrusted: boolean; - noKeepalive: boolean; - output: string; - showError: boolean; - silent: boolean; - ssl: boolean; - sslv2: boolean; - sslv3: boolean; - verbose: boolean; -}; -declare type CurlRequest = { - method?: "GET" | "get" | "POST" | "post" | "PUT" | "put" | "PATCH" | "patch" | "DELETE" | "delete"; - headers?: StringMap; - body?: CurlBody; - url: string; -}; -/** - * @param {CurlRequest} params - * @param {CurlAdditionalOptions} [options] - * @returns {string} - */ -declare const CurlGenerator: (params: CurlRequest, options?: CurlAdditionalOptions | undefined) => string; -export { CurlGenerator }; diff --git a/example/example-parameters.js b/example/example-parameters.js index cde38f7..5727a14 100644 --- a/example/example-parameters.js +++ b/example/example-parameters.js @@ -102,6 +102,23 @@ const fileBody = { fileName: "data.json" } } +const queries1 = { + url: "https://example.com/posts?", + method: "GET", + queries: { + key: "val", + key2: ["val1", "val2"] + } +} + +const queries2 = { + url: "https://example.com/posts?a=b", + method: "GET", + queries: { + key: "val", + key2: ["val1", "val2"] + } +} const params = { @@ -114,7 +131,9 @@ const params = { opt2, rawBody1, rawBody2, - fileBody + fileBody, + queries1, + queries2 } const results = { get1: `curl https://jsonplaceholder.typicode.com/posts/101 \\`, @@ -130,6 +149,8 @@ const results = { rawBody1: `${'curl https://jsonplaceholder.typicode.com/posts \\\n' + ' -X POST \\\n' + ' -H "Content-Type: text/plain" \\\n' + ' -d "string data"'}`, rawBody2: `${'curl https://jsonplaceholder.typicode.com/posts \\\n' + ' -X POST \\\n' + ' -H "Content-Type: text/plain" \\\n' + ' -d "string data"'}`, fileBody: `${'curl https://jsonplaceholder.typicode.com/posts \\\n' + ' -X POST \\\n' + ' -H "Content-Type: application/json" \\\n' + ' --data-binary @data.json'}`, + queries1: 'curl https://example.com/posts?key=val&key2=val1,val2 \\\n -X GET', + queries2: 'curl https://example.com/posts?a=b&key=val&key2=val1,val2 \\\n -X GET', } module.exports = { params, results } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index ff80c74..a968d9e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "curl-generator", - "version": "0.3.1", + "version": "0.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/main.ts b/src/main.ts index 7f4a853..be177a9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,6 +2,10 @@ import { bodyToCommand, CurlBody } from "./bodies/body"; type StringMap = { [key: string]: string }; +type CurlQueries = { + [key: string]: string | string[]; +}; + /** * Additional options for curl command. * @@ -26,33 +30,44 @@ type StringMap = { [key: string]: string }; * --verbose -> Make the operation more talkative */ type CurlAdditionalOptions = { - compressed: boolean, - compressedSsh: boolean, - fail: boolean, - failEarly: boolean, - head: boolean, - include: boolean, - insecure: boolean, - ipv4: boolean, - ipv6: boolean, - listOnly: boolean, - location: boolean, - locationTrusted: boolean, - noKeepalive: boolean, - output: string, - showError: boolean, - silent: boolean, - ssl: boolean, - sslv2: boolean, - sslv3: boolean, - verbose: boolean, + compressed: boolean; + compressedSsh: boolean; + fail: boolean; + failEarly: boolean; + head: boolean; + include: boolean; + insecure: boolean; + ipv4: boolean; + ipv6: boolean; + listOnly: boolean; + location: boolean; + locationTrusted: boolean; + noKeepalive: boolean; + output: string; + showError: boolean; + silent: boolean; + ssl: boolean; + sslv2: boolean; + sslv3: boolean; + verbose: boolean; }; type CurlRequest = { - method?: "GET" | "get" | "POST" | "post" | "PUT" | "put" | "PATCH" | "patch" | "DELETE" | "delete", - headers?: StringMap, - body?: CurlBody, - url: string, + method?: + | "GET" + | "get" + | "POST" + | "post" + | "PUT" + | "put" + | "PATCH" + | "patch" + | "DELETE" + | "delete"; + headers?: StringMap; + body?: CurlBody; + queries?: CurlQueries; + url: string; }; // slash for connecting previous breakup line to current line for running cURL directly in Command Prompt @@ -78,6 +93,44 @@ const getCurlMethod = function (method?: string): string { return slash + newLine + result; }; +/** + * @param {StringMap} headers + * @returns {string} + */ +const getURLWithQueries = function ( + url: string, + queries?: CurlQueries +): string { + let result = url; + + if (queries) { + if (result.indexOf("?") === -1) { + result += "?"; + } + + Object.entries(queries).forEach(([key, val]) => { + let q = ""; + if (typeof val === "string") { + q = val; + } else if (Array.isArray(val)) { + q = val.join(","); // can add custom separator support if needed + } + + if (q) { + if ( + result[result.length - 1] !== "&" && + result[result.length - 1] !== "?" + ) { + result += "&"; + } + result += key + "=" + q; + } + }); + } + + return result; +}; + /** * @param {StringMap} headers * @returns {string} @@ -148,7 +201,7 @@ const CurlGenerator = function ( options?: CurlAdditionalOptions ): string { let curlSnippet = "curl "; - curlSnippet += params.url; + curlSnippet += getURLWithQueries(params.url, params.queries); curlSnippet += getCurlMethod(params.method); curlSnippet += getCurlHeaders(params.headers); curlSnippet += getCurlBody(params.body); diff --git a/test/test.ts b/test/test.ts index 120af10..52ab465 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,6 +1,6 @@ -const assert = require('assert'); -var CurlGenerator = require('../dist/curl-generator.cjs').CurlGenerator; -const data = require('../example/example-parameters'); +const assert = require("assert"); +var CurlGenerator = require("../dist/curl-generator.cjs").CurlGenerator; +const data = require("../example/example-parameters"); const params = data.params; const results = data.results; @@ -14,11 +14,18 @@ Object.keys(params).forEach((key) => { test(params[key], params[key].options, results[key]); }); -console.log(CurlGenerator({ - url: "https://example.com", - method: "GET", - body: { - type: "file", - fileName: "test.txt" - } -})); \ No newline at end of file +console.log( + CurlGenerator({ + url: "https://example.com", + method: "GET", + body: { + type: "file", + fileName: "test.txt", + }, + queries: { + key: "val", + key2: ["val1", "val2"], + key3: "", + }, + }) +);