Skip to content

Commit

Permalink
Add more fields to the JSON generator and a method to return the non-…
Browse files Browse the repository at this point in the history
…serialized JSON object (curlconverter#704)
  • Loading branch information
netroy authored Oct 30, 2024
1 parent eb4fe44 commit aa8c66c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
8 changes: 7 additions & 1 deletion src/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export interface Request {
compressed?: boolean;
transferEncoding?: boolean;

include?: boolean;

multipartUploads?: FormParam[];
// When multipartUploads comes from parsing a string in --data
// this can be set to true to say that sending the original data
Expand Down Expand Up @@ -924,7 +926,7 @@ function buildRequest(
}
if ((config.output || []).length > config.url.length) {
warnf(global_, [
"too-many-ouptut-files",
"too-many-output-files",
"Got more --output/-o options than URLs: " +
config.output?.map((f) => JSON.stringify(f.toString())).join(", "),
]);
Expand Down Expand Up @@ -983,6 +985,10 @@ function buildRequest(
request.transferEncoding = config["tr-encoding"];
}

if (config.include) {
request.include = true;
}

if (config.json) {
headers.setIfMissing("Content-Type", "application/json");
headers.setIfMissing("Accept", "application/json");
Expand Down
71 changes: 58 additions & 13 deletions src/generators/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ export const supportedArgs = new Set([
"no-location",
"location-trusted",
"no-location-trusted",
"max-redirs",

"output",
"include",

"proxy",
"max-time",
"connect-timeout",

Expand All @@ -37,8 +42,7 @@ export const supportedArgs = new Set([
"no-ntlm-wb",
]);

// TODO: export this or Request
type JSONOutput = {
export type JSONOutput = {
url: string;
raw_url: string;
method: string;
Expand All @@ -53,15 +57,21 @@ type JSONOutput = {
insecure?: boolean;
compressed?: boolean;

include?: boolean;
auth?: { user: string; password: string };
auth_type?: AuthType;
aws_sigv4?: string;
delegation?: string;

follow_redirects?: boolean; // --location
max_redirects?: number;

proxy?: string;

timeout?: number; // --max-time
connect_timeout?: number;

output?: string;
};

function getDataString(
Expand Down Expand Up @@ -133,23 +143,24 @@ function getFilesString(
};
}

export function _toJsonString(
export function _toJsonObject(
requests: Request[],
warnings: Warnings = [],
): string {
): JSONOutput {
const request = getFirst(requests, warnings);
const requestUrl = request.urls[0];

const requestJson: JSONOutput = {
url: (request.urls[0].queryDict
? request.urls[0].urlWithoutQueryList
: request.urls[0].url
url: (requestUrl.queryDict
? requestUrl.urlWithoutQueryList
: requestUrl.url
)
.toString()
.replace(/\/$/, ""),
// url: request.queryDict ? request.urlWithoutQueryList : request.url,
raw_url: request.urls[0].url.toString(),
raw_url: requestUrl.url.toString(),
// TODO: move this after .query?
method: request.urls[0].method.toLowerCase().toString(), // lowercase for backwards compatibility
method: requestUrl.method.toLowerCase().toString(), // lowercase for backwards compatibility
};
// if (request.queryDict) {
// requestJson.query = request.queryDict
Expand All @@ -174,10 +185,10 @@ export function _toJsonString(
requestJson.headers = Object.fromEntries(headers);
}

if (request.urls[0].queryDict) {
if (requestUrl.queryDict) {
// TODO: rename
requestJson.queries = Object.fromEntries(
request.urls[0].queryDict.map((q) => [
requestUrl.queryDict.map((q) => [
q[0].toString(),
Array.isArray(q[1]) ? q[1].map((qq) => qq.toString()) : q[1].toString(),
]),
Expand All @@ -199,8 +210,12 @@ export function _toJsonString(
requestJson.insecure = false;
}

if (request.urls[0].auth) {
const [user, password] = request.urls[0].auth;
if (request.include) {
requestJson.include = true;
}

if (requestUrl.auth) {
const [user, password] = requestUrl.auth;
requestJson.auth = {
user: user.toString(),
password: password.toString(),
Expand All @@ -218,6 +233,13 @@ export function _toJsonString(

if (Object.prototype.hasOwnProperty.call(request, "followRedirects")) {
requestJson.follow_redirects = request.followRedirects;
if (request.maxRedirects) {
requestJson.max_redirects = parseInt(request.maxRedirects.toString(), 10);
}
}

if (request.proxy) {
requestJson.proxy = request.proxy.toString();
}

if (request.timeout) {
Expand All @@ -227,6 +249,29 @@ export function _toJsonString(
requestJson.connect_timeout = parseFloat(request.connectTimeout.toString());
}

if (requestUrl.output) {
requestJson.output = requestUrl.output.toString();
}

return requestJson;
}
export function toJsonObjectWarn(
curlCommand: string | string[],
warnings: Warnings = [],
): [JSONOutput, Warnings] {
const requests = parse(curlCommand, supportedArgs, warnings);
const json = _toJsonObject(requests, warnings);
return [json, warnings];
}
export function toJsonObject(curlCommand: string | string[]): JSONOutput {
return toJsonObjectWarn(curlCommand)[0];
}

export function _toJsonString(
requests: Request[],
warnings: Warnings = [],
): string {
const requestJson = _toJsonObject(requests, warnings);
return (
JSON.stringify(
Object.keys(requestJson).length ? requestJson : "{}",
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ export {
toJavaScriptXHR,
toJavaScriptXHRWarn,
} from "./generators/javascript/xhr.js";
export { toJsonString, toJsonStringWarn } from "./generators/json.js";
export {
toJsonObject,
toJsonObjectWarn,
toJsonString,
toJsonStringWarn,
type JSONOutput,
} from "./generators/json.js";
export { toJulia, toJuliaWarn } from "./generators/julia.js";
export { toKotlin, toKotlinWarn } from "./generators/kotlin.js";
export { toLua, toLuaWarn } from "./generators/lua.js";
Expand Down

0 comments on commit aa8c66c

Please sign in to comment.