Skip to content

Commit bf1925e

Browse files
authored
Merge pull request #11 from Fabb111/master
Add support for more body types (file, raw string, form)
2 parents 603b063 + 2536edd commit bf1925e

19 files changed

+590
-736
lines changed

dist/bodies/body.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { CurlFileBody } from "./file-body";
2+
import { CurlRawBody } from "./raw-body";
3+
import { CurlJsonBody } from "./json-body";
4+
import { CurlFormBody } from "./form-body";
5+
export declare type CurlBody = string | Record<string | number | symbol, unknown> | URLSearchParams | CurlFileBody | CurlRawBody | CurlJsonBody | CurlFormBody;
6+
export declare function bodyToString(body: CurlBody): string;
7+
export declare function bodyToCommand(body: CurlBody): string;

dist/bodies/file-body.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface CurlFileBody {
2+
/**
3+
* File body type for using a file as the body data.
4+
*/
5+
type: "file";
6+
/**
7+
* The file name without the @.
8+
*/
9+
fileName: string;
10+
}
11+
export declare function isCurlFileBody(body: unknown): body is CurlFileBody;
12+
export declare function fileBodyToString(body: CurlFileBody): string;
13+
export declare function fileBodyToCommand(body: CurlFileBody): string;

dist/bodies/form-body.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { CurlFileBody } from "./file-body";
2+
import { CurlRawBody } from "./raw-body";
3+
export interface CurlFormBody {
4+
/**
5+
* Form body type for URL-encoded form data.
6+
*/
7+
type: "form";
8+
/**
9+
* The content of the body.
10+
*/
11+
content: Record<string, string | CurlFileBody | CurlRawBody> | URLSearchParams;
12+
}
13+
export declare function formBodyToString(body: CurlFormBody): string;
14+
export declare function isCurlFormBody(body: unknown): body is CurlFormBody;
15+
export declare function formBodyToCommand(body: CurlFormBody): string;

dist/bodies/json-body.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export interface CurlJsonBody {
2+
/**
3+
* JSON body type for JSON objects.
4+
*/
5+
type: "json";
6+
/**
7+
* The content of the body.
8+
*/
9+
content: Record<string | number | symbol, unknown>;
10+
}
11+
export declare function isCurlJsonBody(body: unknown): body is CurlJsonBody;
12+
export declare function jsonContentToString(content: Record<string | number | symbol, unknown>): string;
13+
export declare function jsonBodyToString(body: CurlJsonBody): string;
14+
export declare function jsonBodyToCommand(body: CurlJsonBody): string;

dist/bodies/raw-body.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface CurlRawBody {
2+
/**
3+
* Raw body type for strings/text.
4+
*/
5+
type: "raw";
6+
/**
7+
* The content of the body.
8+
*/
9+
content: string;
10+
}
11+
export declare function isCurlRawBody(body: unknown): body is CurlRawBody;
12+
export declare function rawBodyToString(body: CurlRawBody): string;
13+
export declare function rawBodyToCommand(body: CurlRawBody): string;

dist/curl-generator.cjs.js

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,88 @@
22

33
Object.defineProperty(exports, '__esModule', { value: true });
44

5+
function isCurlFileBody(body) {
6+
return typeof body === "object" && body !== null && "type" in body && body.type === "file" && "fileName" in body;
7+
}
8+
function fileBodyToString(body) {
9+
return "@" + body.fileName;
10+
}
11+
function fileBodyToCommand(body) {
12+
return "--data-binary " + fileBodyToString(body);
13+
}
14+
15+
function isCurlRawBody(body) {
16+
return typeof body === "object" && body !== null && "type" in body && body.type === "raw" && "content" in body;
17+
}
18+
function rawBodyToString(body) {
19+
return body.content;
20+
}
21+
function rawBodyToCommand(body) {
22+
return "-d \"" + rawBodyToString(body) + "\"";
23+
}
24+
25+
function isCurlJsonBody(body) {
26+
return typeof body === "object" && body !== null && "type" in body && body.type === "json" && "content" in body;
27+
}
28+
function jsonContentToString(content) {
29+
return JSON.stringify(content).replace(/([\\"])/g, "\\$1");
30+
}
31+
function jsonBodyToString(body) {
32+
return jsonContentToString(body.content);
33+
}
34+
function jsonBodyToCommand(body) {
35+
return "-d \"" + jsonBodyToString(body) + "\"";
36+
}
37+
38+
function isCurlFormBody(body) {
39+
return typeof body === "object" && body !== null && "type" in body && body.type === "form" && "content" in body;
40+
}
41+
function formBodyToCommand(body) {
42+
if (body.content instanceof URLSearchParams) {
43+
return "-d \"" + body.content.toString() + "\"";
44+
}
45+
return Object.entries(body.content)
46+
.map(function (_a) {
47+
var key = _a[0], value = _a[1];
48+
if (typeof value === "string") {
49+
return "-F " + key + "=" + value;
50+
}
51+
if (value.type === "file") {
52+
return "-F " + key + "=@" + value.fileName;
53+
}
54+
if (value.type === "raw") {
55+
return "-F " + key + "=" + value.content;
56+
}
57+
throw new Error("Invalid form body value type: " + value);
58+
})
59+
.join(" \\\n ");
60+
}
61+
62+
function bodyToCommand(body) {
63+
if (typeof body === "string") {
64+
return "-d \"" + body + "\"";
65+
}
66+
else if (body instanceof URLSearchParams) {
67+
return "-d \"" + body.toString() + "\"";
68+
}
69+
else if (isCurlFileBody(body)) {
70+
return fileBodyToCommand(body);
71+
}
72+
else if (isCurlRawBody(body)) {
73+
return rawBodyToCommand(body);
74+
}
75+
else if (isCurlJsonBody(body)) {
76+
return jsonBodyToCommand(body);
77+
}
78+
else if (isCurlFormBody(body)) {
79+
return formBodyToCommand(body);
80+
}
81+
else if (typeof body === "object") {
82+
return "-d \"" + jsonContentToString(body) + "\"";
83+
}
84+
throw new Error("Invalid body type: " + body);
85+
}
86+
587
// slash for connecting previous breakup line to current line for running cURL directly in Command Prompt
688
var slash = " \\";
789
var newLine = "\n";
@@ -31,19 +113,19 @@ var getCurlHeaders = function (headers) {
31113
var result = "";
32114
if (headers) {
33115
Object.keys(headers).map(function (val) {
34-
result += "" + slash + newLine + "-H \"" + val + ": " + headers[val].replace(/(\\|")/g, "\\$1") + "\"";
116+
result += "" + slash + newLine + " -H \"" + val + ": " + headers[val].replace(/(\\|")/g, "\\$1") + "\"";
35117
});
36118
}
37119
return result;
38120
};
39121
/**
40-
* @param {Object} body
122+
* @param {CurlBody} body
41123
* @returns {string}
42124
*/
43125
var getCurlBody = function (body) {
44126
var result = "";
45127
if (body) {
46-
result += "" + slash + newLine + "-d \"" + JSON.stringify(body).replace(/(\\|")/g, "\\$1") + "\"";
128+
result += "" + slash + newLine + " " + bodyToCommand(body);
47129
}
48130
return result;
49131
};
@@ -62,11 +144,11 @@ var getCurlOptions = function (options) {
62144
}
63145
else if (typeof options[key] === "boolean" && options[key]) {
64146
// boolean option, we just add --opt
65-
result += "--" + kebabKey + " ";
147+
result += " --" + kebabKey;
66148
}
67149
else if (typeof options[key] === "string") {
68150
// string option, we have to add --opt=value
69-
result += "--" + kebabKey + " " + options[key] + " ";
151+
result += " --" + kebabKey + " " + options[key];
70152
}
71153
});
72154
}

dist/curl-generator.esm.js

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,85 @@
1+
function isCurlFileBody(body) {
2+
return typeof body === "object" && body !== null && "type" in body && body.type === "file" && "fileName" in body;
3+
}
4+
function fileBodyToString(body) {
5+
return "@" + body.fileName;
6+
}
7+
function fileBodyToCommand(body) {
8+
return "--data-binary " + fileBodyToString(body);
9+
}
10+
11+
function isCurlRawBody(body) {
12+
return typeof body === "object" && body !== null && "type" in body && body.type === "raw" && "content" in body;
13+
}
14+
function rawBodyToString(body) {
15+
return body.content;
16+
}
17+
function rawBodyToCommand(body) {
18+
return "-d \"" + rawBodyToString(body) + "\"";
19+
}
20+
21+
function isCurlJsonBody(body) {
22+
return typeof body === "object" && body !== null && "type" in body && body.type === "json" && "content" in body;
23+
}
24+
function jsonContentToString(content) {
25+
return JSON.stringify(content).replace(/([\\"])/g, "\\$1");
26+
}
27+
function jsonBodyToString(body) {
28+
return jsonContentToString(body.content);
29+
}
30+
function jsonBodyToCommand(body) {
31+
return "-d \"" + jsonBodyToString(body) + "\"";
32+
}
33+
34+
function isCurlFormBody(body) {
35+
return typeof body === "object" && body !== null && "type" in body && body.type === "form" && "content" in body;
36+
}
37+
function formBodyToCommand(body) {
38+
if (body.content instanceof URLSearchParams) {
39+
return "-d \"" + body.content.toString() + "\"";
40+
}
41+
return Object.entries(body.content)
42+
.map(function (_a) {
43+
var key = _a[0], value = _a[1];
44+
if (typeof value === "string") {
45+
return "-F " + key + "=" + value;
46+
}
47+
if (value.type === "file") {
48+
return "-F " + key + "=@" + value.fileName;
49+
}
50+
if (value.type === "raw") {
51+
return "-F " + key + "=" + value.content;
52+
}
53+
throw new Error("Invalid form body value type: " + value);
54+
})
55+
.join(" \\\n ");
56+
}
57+
58+
function bodyToCommand(body) {
59+
if (typeof body === "string") {
60+
return "-d \"" + body + "\"";
61+
}
62+
else if (body instanceof URLSearchParams) {
63+
return "-d \"" + body.toString() + "\"";
64+
}
65+
else if (isCurlFileBody(body)) {
66+
return fileBodyToCommand(body);
67+
}
68+
else if (isCurlRawBody(body)) {
69+
return rawBodyToCommand(body);
70+
}
71+
else if (isCurlJsonBody(body)) {
72+
return jsonBodyToCommand(body);
73+
}
74+
else if (isCurlFormBody(body)) {
75+
return formBodyToCommand(body);
76+
}
77+
else if (typeof body === "object") {
78+
return "-d \"" + jsonContentToString(body) + "\"";
79+
}
80+
throw new Error("Invalid body type: " + body);
81+
}
82+
183
// slash for connecting previous breakup line to current line for running cURL directly in Command Prompt
284
var slash = " \\";
385
var newLine = "\n";
@@ -27,19 +109,19 @@ var getCurlHeaders = function (headers) {
27109
var result = "";
28110
if (headers) {
29111
Object.keys(headers).map(function (val) {
30-
result += "" + slash + newLine + "-H \"" + val + ": " + headers[val].replace(/(\\|")/g, "\\$1") + "\"";
112+
result += "" + slash + newLine + " -H \"" + val + ": " + headers[val].replace(/(\\|")/g, "\\$1") + "\"";
31113
});
32114
}
33115
return result;
34116
};
35117
/**
36-
* @param {Object} body
118+
* @param {CurlBody} body
37119
* @returns {string}
38120
*/
39121
var getCurlBody = function (body) {
40122
var result = "";
41123
if (body) {
42-
result += "" + slash + newLine + "-d \"" + JSON.stringify(body).replace(/(\\|")/g, "\\$1") + "\"";
124+
result += "" + slash + newLine + " " + bodyToCommand(body);
43125
}
44126
return result;
45127
};
@@ -58,11 +140,11 @@ var getCurlOptions = function (options) {
58140
}
59141
else if (typeof options[key] === "boolean" && options[key]) {
60142
// boolean option, we just add --opt
61-
result += "--" + kebabKey + " ";
143+
result += " --" + kebabKey;
62144
}
63145
else if (typeof options[key] === "string") {
64146
// string option, we have to add --opt=value
65-
result += "--" + kebabKey + " " + options[key] + " ";
147+
result += " --" + kebabKey + " " + options[key];
66148
}
67149
});
68150
}

0 commit comments

Comments
 (0)