Skip to content

Fix quotes #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions dist/curl-generator.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ function rawBodyToString(body) {
return body.content;
}
function rawBodyToCommand(body) {
return "-d \"" + rawBodyToString(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");
return JSON.stringify(content).replace(/([\\'])/g, "\\$1");
}
function jsonBodyToString(body) {
return jsonContentToString(body.content);
}
function jsonBodyToCommand(body) {
return "-d \"" + jsonBodyToString(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 "-d '" + body.content.toString() + "'";
}
return Object.entries(body.content)
.map(function (_a) {
Expand All @@ -61,10 +61,10 @@ function formBodyToCommand(body) {

function bodyToCommand(body) {
if (typeof body === "string") {
return "-d \"" + body + "\"";
return "-d '" + body + "'";
}
else if (body instanceof URLSearchParams) {
return "-d \"" + body.toString() + "\"";
return "-d '" + body.toString() + "'";
}
else if (isCurlFileBody(body)) {
return fileBodyToCommand(body);
Expand All @@ -79,7 +79,7 @@ function bodyToCommand(body) {
return formBodyToCommand(body);
}
else if (typeof body === "object") {
return "-d \"" + jsonContentToString(body) + "\"";
return "-d '" + jsonContentToString(body) + "'";
}
throw new Error("Invalid body type: " + body);
}
Expand Down Expand Up @@ -113,7 +113,7 @@ var getCurlHeaders = function (headers) {
var result = "";
if (headers) {
Object.keys(headers).map(function (val) {
result += "" + slash + newLine + " -H \"" + val + ": " + headers[val].replace(/(\\|")/g, "\\$1") + "\"";
result += "" + slash + newLine + " -H '" + val + ": " + headers[val].replace(/(\\|')/g, "\\$1") + "'";
});
}
return result;
Expand Down
16 changes: 8 additions & 8 deletions dist/curl-generator.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ function rawBodyToString(body) {
return body.content;
}
function rawBodyToCommand(body) {
return "-d \"" + rawBodyToString(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");
return JSON.stringify(content).replace(/([\\'])/g, "\\$1");
}
function jsonBodyToString(body) {
return jsonContentToString(body.content);
}
function jsonBodyToCommand(body) {
return "-d \"" + jsonBodyToString(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 "-d '" + body.content.toString() + "'";
}
return Object.entries(body.content)
.map(function (_a) {
Expand All @@ -57,10 +57,10 @@ function formBodyToCommand(body) {

function bodyToCommand(body) {
if (typeof body === "string") {
return "-d \"" + body + "\"";
return "-d '" + body + "'";
}
else if (body instanceof URLSearchParams) {
return "-d \"" + body.toString() + "\"";
return "-d '" + body.toString() + "'";
}
else if (isCurlFileBody(body)) {
return fileBodyToCommand(body);
Expand All @@ -75,7 +75,7 @@ function bodyToCommand(body) {
return formBodyToCommand(body);
}
else if (typeof body === "object") {
return "-d \"" + jsonContentToString(body) + "\"";
return "-d '" + jsonContentToString(body) + "'";
}
throw new Error("Invalid body type: " + body);
}
Expand Down Expand Up @@ -109,7 +109,7 @@ var getCurlHeaders = function (headers) {
var result = "";
if (headers) {
Object.keys(headers).map(function (val) {
result += "" + slash + newLine + " -H \"" + val + ": " + headers[val].replace(/(\\|")/g, "\\$1") + "\"";
result += "" + slash + newLine + " -H '" + val + ": " + headers[val].replace(/(\\|')/g, "\\$1") + "'";
});
}
return result;
Expand Down
16 changes: 8 additions & 8 deletions dist/curl-generator.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@
return body.content;
}
function rawBodyToCommand(body) {
return "-d \"" + rawBodyToString(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");
return JSON.stringify(content).replace(/([\\'])/g, "\\$1");
}
function jsonBodyToString(body) {
return jsonContentToString(body.content);
}
function jsonBodyToCommand(body) {
return "-d \"" + jsonBodyToString(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 "-d '" + body.content.toString() + "'";
}
return Object.entries(body.content)
.map(function (_a) {
Expand All @@ -63,10 +63,10 @@

function bodyToCommand(body) {
if (typeof body === "string") {
return "-d \"" + body + "\"";
return "-d '" + body + "'";
}
else if (body instanceof URLSearchParams) {
return "-d \"" + body.toString() + "\"";
return "-d '" + body.toString() + "'";
}
else if (isCurlFileBody(body)) {
return fileBodyToCommand(body);
Expand All @@ -81,7 +81,7 @@
return formBodyToCommand(body);
}
else if (typeof body === "object") {
return "-d \"" + jsonContentToString(body) + "\"";
return "-d '" + jsonContentToString(body) + "'";
}
throw new Error("Invalid body type: " + body);
}
Expand Down Expand Up @@ -115,7 +115,7 @@
var result = "";
if (headers) {
Object.keys(headers).map(function (val) {
result += "" + slash + newLine + " -H \"" + val + ": " + headers[val].replace(/(\\|")/g, "\\$1") + "\"";
result += "" + slash + newLine + " -H '" + val + ": " + headers[val].replace(/(\\|')/g, "\\$1") + "'";
});
}
return result;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "curl-generator",
"author": "albertodeagostini.dev@gmail.com",
"version": "0.4.1",
"version": "0.4.2",
"main": "dist/curl-generator.cjs.js",
"module": "dist/curl-generator.esm.js",
"browser": "dist/curl-generator.umd.js",
Expand Down Expand Up @@ -39,4 +39,4 @@
"url": "https://github.com/albertodeago/curl-generator/issues"
},
"homepage": "https://github.com/albertodeago/curl-generator"
}
}
8 changes: 4 additions & 4 deletions src/bodies/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export function bodyToString(body: CurlBody): string {

export function bodyToCommand(body: CurlBody): string {
if (typeof body === "string") {
return `-d "${body}"`;
return `-d '${body}'`;
} else if (body instanceof URLSearchParams) {
return `-d "${body.toString()}"`;
return `-d '${body.toString()}'`;
} else if (isCurlFileBody(body)) {
return fileBodyToCommand(body);
} else if (isCurlRawBody(body)) {
Expand All @@ -48,8 +48,8 @@ export function bodyToCommand(body: CurlBody): string {
} else if (isCurlFormBody(body)) {
return formBodyToCommand(body);
} else if (typeof body === "object") {
return `-d "${jsonContentToString(body)}"`;
return `-d '${jsonContentToString(body)}'`;
}

throw new Error(`Invalid body type: ${body}`);
}
}
2 changes: 1 addition & 1 deletion src/bodies/form-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function isCurlFormBody(body: unknown): body is CurlFormBody {

export function formBodyToCommand(body: CurlFormBody): string {
if (body.content instanceof URLSearchParams) {
return `-d "${body.content.toString()}"`;
return `-d '${body.content.toString()}'`;
}

return Object.entries(body.content)
Expand Down
4 changes: 2 additions & 2 deletions src/bodies/json-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function isCurlJsonBody(body: unknown): body is CurlJsonBody {

export function jsonContentToString(content: Record<string | number | symbol, unknown>): string {
return JSON.stringify(content).replace(
/([\\"])/g,
/([\\'])/g,
"\\$1"
);
}
Expand All @@ -27,5 +27,5 @@ export function jsonBodyToString(body: CurlJsonBody): string {
}

export function jsonBodyToCommand(body: CurlJsonBody): string {
return `-d "${jsonBodyToString(body)}"`;
return `-d '${jsonBodyToString(body)}'`;
}
2 changes: 1 addition & 1 deletion src/bodies/raw-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export function rawBodyToString(body: CurlRawBody): string {
}

export function rawBodyToCommand(body: CurlRawBody): string {
return `-d "${rawBodyToString(body)}"`;
return `-d '${rawBodyToString(body)}'`;
}
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ const getCurlHeaders = function (headers?: StringMap): string {
let result = "";
if (headers) {
Object.keys(headers).map((val) => {
result += `${slash}${newLine} -H "${val}: ${headers[val].replace(
/(\\|")/g,
result += `${slash}${newLine} -H '${val}: ${headers[val].replace(
/(\\|')/g,
"\\$1"
)}"`;
)}'`;
});
}
return result;
Expand Down
44 changes: 22 additions & 22 deletions test/__snapshots__/main.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ exports[`del1 1`] = `
exports[`fileBody 1`] = `
"curl https://jsonplaceholder.typicode.com/posts \\
-X POST \\
-H "Content-Type: application/json" \\
-H 'Content-Type: application/json' \\
--data-binary @data.json"
`;

exports[`formBody1 1`] = `
"curl https://jsonplaceholder.typicode.com/posts \\
-X POST \\
-H "Content-Type: application/x-www-form-urlencoded" \\
-H 'Content-Type: application/x-www-form-urlencoded' \\
-F name=foo"
`;

exports[`formBody2 1`] = `
"curl https://jsonplaceholder.typicode.com/posts \\
-X POST \\
-H "Content-Type: multipart/form-data" \\
-H 'Content-Type: multipart/form-data' \\
-F file1=@data.json"
`;

exports[`formBody3 1`] = `
"curl https://jsonplaceholder.typicode.com/posts \\
-X POST \\
-H "Content-Type: application/x-www-form-urlencoded" \\
-H 'Content-Type: application/x-www-form-urlencoded' \\
-F file1=string data"
`;

exports[`formBody4 1`] = `
"curl https://jsonplaceholder.typicode.com/posts \\
-X POST \\
-H "Content-Type: application/x-www-form-urlencoded" \\
-d "key1=value+1""
-H 'Content-Type: application/x-www-form-urlencoded' \\
-d 'key1=value+1'"
`;

exports[`get1 1`] = `"curl https://jsonplaceholder.typicode.com/posts/101 \\"`;
Expand All @@ -50,20 +50,20 @@ exports[`get2 1`] = `
exports[`getEscapeHeaders 1`] = `
"curl https://jsonplaceholder.typicode.com/todos/1 \\
-X GET \\
-H "key: a \\"strange\\" value""
-H 'key: a "strange" value'"
`;

exports[`getWithHeaders 1`] = `
"curl https://jsonplaceholder.typicode.com/todos/1 \\
-X GET \\
-H "Content-Type: application/json""
-H 'Content-Type: application/json'"
`;

exports[`jsonBody 1`] = `
"curl https://jsonplaceholder.typicode.com/posts \\
-X POST \\
-H "Content-Type: application/json" \\
-d "{\\"name\\":\\"foo\\"}""
-H 'Content-Type: application/json' \\
-d '{"name":"foo"}'"
`;

exports[`opt1 1`] = `
Expand All @@ -81,41 +81,41 @@ exports[`opt2 1`] = `
exports[`patch1 1`] = `
"curl https://jsonplaceholder.typicode.com/posts/1 \\
-X PATCH \\
-H "Content-type: application/json; charset=UTF-8" \\
-d "{\\"title\\":\\"foo patched\\"}""
-H 'Content-type: application/json; charset=UTF-8' \\
-d '{"title":"foo patched"}'"
`;

exports[`post1 1`] = `
"curl https://jsonplaceholder.typicode.com/posts \\
-X POST \\
-H "Content-type: application/json; charset=UTF-8" \\
-d "{\\"id\\":\\"123-456-789\\",\\"key1\\":\\"value 1\\",\\"key2\\":\\"a \\\\\\"complex\\\\\\" value\\"}""
-H 'Content-type: application/json; charset=UTF-8' \\
-d '{"id":"123-456-789","key1":"value 1","key2":"a \\\\"complex\\\\" value"}'"
`;

exports[`put1 1`] = `
"curl https://jsonplaceholder.typicode.com/posts/1 \\
-X PUT \\
-H "Content-type: application/json; charset=UTF-8" \\
-d "{\\"id\\":1,\\"title\\":\\"foo\\",\\"body\\":\\"barzzz\\",\\"userId\\":1}""
-H 'Content-type: application/json; charset=UTF-8' \\
-d '{"id":1,"title":"foo","body":"barzzz","userId":1}'"
`;

exports[`rawBody1 1`] = `
"curl https://jsonplaceholder.typicode.com/posts \\
-X POST \\
-H "Content-Type: text/plain" \\
-d "string data""
-H 'Content-Type: text/plain' \\
-d 'string data'"
`;

exports[`rawBody2 1`] = `
"curl https://jsonplaceholder.typicode.com/posts \\
-X POST \\
-H "Content-Type: text/plain" \\
-d "string data""
-H 'Content-Type: text/plain' \\
-d 'string data'"
`;

exports[`searchParamsBody 1`] = `
"curl https://jsonplaceholder.typicode.com/posts \\
-X POST \\
-H "Content-Type: text/plain" \\
-d "key1=value+1""
-H 'Content-Type: text/plain' \\
-d 'key1=value+1'"
`;