mirror of
https://github.com/curlconverter/curlconverter.git
synced 2022-05-22 02:35:29 +03:00
add more type definitions (#374)
* rename github actions file * type request * type generators
This commit is contained in:
committed by
GitHub
parent
1864b07750
commit
31ca0dfb08
@@ -59,7 +59,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"compile": "tsc",
|
"compile": "tsc",
|
||||||
"watch": "tsc -watch -p ./",
|
"watch": "tsc -watch -p ./",
|
||||||
"pretest": "npm run compile",
|
"pretest": "TSC_COMPILE_ON_ERROR=true npm run compile || exit 0",
|
||||||
"test": "NODE_OPTIONS=--enable-source-maps npx tape dist/test/test.js",
|
"test": "NODE_OPTIONS=--enable-source-maps npx tape dist/test/test.js",
|
||||||
"prelint": "npm run compile",
|
"prelint": "npm run compile",
|
||||||
"lint": "eslint src --ext ts",
|
"lint": "eslint src --ext ts",
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ import { ansibleTemplate } from "../templates/ansible.js";
|
|||||||
import nunjucks from "nunjucks";
|
import nunjucks from "nunjucks";
|
||||||
import querystring from "query-string";
|
import querystring from "query-string";
|
||||||
|
|
||||||
function getDataString(request: Request): any {
|
function getDataString(request: Request): string | object {
|
||||||
|
if (!request.data) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
const parsedQueryString = querystring.parse(request.data, { sort: false });
|
const parsedQueryString = querystring.parse(request.data, { sort: false });
|
||||||
const keyCount = Object.keys(parsedQueryString).length;
|
const keyCount = Object.keys(parsedQueryString).length;
|
||||||
const singleKeyOnly =
|
const singleKeyOnly =
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ export const _toDart = (r: Request): string => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const hasData = r.data;
|
const hasData = r.data;
|
||||||
if (hasData) {
|
if (r.data) {
|
||||||
// escape single quotes if there're not already escaped
|
// escape single quotes if there're not already escaped
|
||||||
if (r.data.indexOf("'") !== -1 && r.data.indexOf("\\'") === -1)
|
if (r.data.indexOf("'") !== -1 && r.data.indexOf("\\'") === -1)
|
||||||
r.data = jsesc(r.data);
|
r.data = jsesc(r.data);
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ function getBody(request: Request): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getFormDataString(request: Request): string {
|
function getFormDataString(request: Request): string {
|
||||||
if (request.data && typeof request.data === "string") {
|
if (request.data) {
|
||||||
return getDataString(request);
|
return getDataString(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,6 +137,10 @@ ${content}
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getDataString(request: Request): string {
|
function getDataString(request: Request): string {
|
||||||
|
if (!request.data) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
if (!request.isDataRaw && request.data.startsWith("@")) {
|
if (!request.isDataRaw && request.data.startsWith("@")) {
|
||||||
const filePath = request.data.slice(1);
|
const filePath = request.data.slice(1);
|
||||||
if (request.isDataBinary) {
|
if (request.isDataBinary) {
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ export const _toJavaScript = (request: Request): string => {
|
|||||||
request.method.toUpperCase() !== "GET" ||
|
request.method.toUpperCase() !== "GET" ||
|
||||||
request.headers ||
|
request.headers ||
|
||||||
request.auth ||
|
request.auth ||
|
||||||
request.body
|
request.data
|
||||||
) {
|
) {
|
||||||
jsFetchCode += ", {\n";
|
jsFetchCode += ", {\n";
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,12 @@ type JSONOutput = {
|
|||||||
auth?: { user: string; password: string };
|
auth?: { user: string; password: string };
|
||||||
};
|
};
|
||||||
|
|
||||||
function getDataString(request: Request) {
|
function getDataString(request: Request): {
|
||||||
|
data?: { [key: string]: string | string[] };
|
||||||
|
} {
|
||||||
|
if (!request.data) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
if ( !request.isDataRaw && request.data.startsWith('@') ) {
|
if ( !request.isDataRaw && request.data.startsWith('@') ) {
|
||||||
var filePath = request.data.slice(1);
|
var filePath = request.data.slice(1);
|
||||||
|
|||||||
@@ -115,7 +115,10 @@ const addCellArray = (
|
|||||||
return response;
|
return response;
|
||||||
};
|
};
|
||||||
|
|
||||||
const structify = (obj: any, indentLevel?: number) => {
|
const structify = (
|
||||||
|
obj: number[] | string[] | { [key: string]: string } | string | number | null,
|
||||||
|
indentLevel?: number
|
||||||
|
) => {
|
||||||
let response = "";
|
let response = "";
|
||||||
indentLevel = !indentLevel ? 1 : ++indentLevel;
|
indentLevel = !indentLevel ? 1 : ++indentLevel;
|
||||||
const indent = " ".repeat(4 * indentLevel);
|
const indent = " ".repeat(4 * indentLevel);
|
||||||
|
|||||||
@@ -205,7 +205,7 @@ const prepareData = (request: Request) => {
|
|||||||
response = callFunction("body", "FormProvider", data);
|
response = callFunction("body", "FormProvider", data);
|
||||||
} else if (Object.prototype.hasOwnProperty.call(request, "data")) {
|
} else if (Object.prototype.hasOwnProperty.call(request, "data")) {
|
||||||
response = prepareDataProvider(
|
response = prepareDataProvider(
|
||||||
request.data,
|
request.data as string,
|
||||||
"body",
|
"body",
|
||||||
";",
|
";",
|
||||||
0,
|
0,
|
||||||
@@ -221,7 +221,7 @@ const prepareData = (request: Request) => {
|
|||||||
|
|
||||||
const prepareRequestMessage = (request: Request): string => {
|
const prepareRequestMessage = (request: Request): string => {
|
||||||
let reqMessage: string[] | string = [repr(request.method.toLowerCase())];
|
let reqMessage: string[] | string = [repr(request.method.toLowerCase())];
|
||||||
if (request.cookie || request.headers) {
|
if (request.cookies || request.headers) {
|
||||||
reqMessage.push("header");
|
reqMessage.push("header");
|
||||||
} else if (request.method.toLowerCase() === "get") {
|
} else if (request.method.toLowerCase() === "get") {
|
||||||
reqMessage = "";
|
reqMessage = "";
|
||||||
|
|||||||
@@ -152,9 +152,13 @@ const prepareBasicData = (request: Request): string | string[] => {
|
|||||||
if (Object.prototype.hasOwnProperty.call(request, "data")) {
|
if (Object.prototype.hasOwnProperty.call(request, "data")) {
|
||||||
if (request.data === "") {
|
if (request.data === "") {
|
||||||
response = setVariableValue("body", repr());
|
response = setVariableValue("body", repr());
|
||||||
} else if (request.data[0] === "@") {
|
} else if ((request.data as string)[0] === "@") {
|
||||||
response.push(
|
response.push(
|
||||||
callFunction("body", "fileread", repr(request.data.slice(1)))
|
callFunction(
|
||||||
|
"body",
|
||||||
|
"fileread",
|
||||||
|
repr((request.data as string).slice(1))
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!request.isDataBinary) {
|
if (!request.isDataBinary) {
|
||||||
@@ -164,7 +168,7 @@ const prepareBasicData = (request: Request): string | string[] => {
|
|||||||
// if the data is in JSON, store it as struct in MATLAB
|
// if the data is in JSON, store it as struct in MATLAB
|
||||||
// otherwise just keep it as a char vector
|
// otherwise just keep it as a char vector
|
||||||
try {
|
try {
|
||||||
const jsonData = JSON.parse(request.data);
|
const jsonData = JSON.parse(request.data as string);
|
||||||
if (typeof jsonData === "object") {
|
if (typeof jsonData === "object") {
|
||||||
let jsonText = structify(jsonData);
|
let jsonText = structify(jsonData);
|
||||||
if (!jsonText.startsWith("struct")) jsonText = repr(jsonText);
|
if (!jsonText.startsWith("struct")) jsonText = repr(jsonText);
|
||||||
|
|||||||
@@ -81,11 +81,16 @@ export const _toPhp = (request: Request): string => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
requestDataCode += "]";
|
requestDataCode += "]";
|
||||||
} else if (request.isDataBinary && request.data.charAt(0) === "@") {
|
} else if (
|
||||||
|
request.isDataBinary &&
|
||||||
|
(request.data as string).charAt(0) === "@"
|
||||||
|
) {
|
||||||
requestDataCode =
|
requestDataCode =
|
||||||
"file_get_contents('" + quote(request.data.substring(1)) + "')";
|
"file_get_contents('" +
|
||||||
|
quote((request.data as string).substring(1)) +
|
||||||
|
"')";
|
||||||
} else {
|
} else {
|
||||||
requestDataCode = "'" + quote(request.data) + "'";
|
requestDataCode = "'" + quote(request.data as string) + "'";
|
||||||
}
|
}
|
||||||
phpCode +=
|
phpCode +=
|
||||||
"curl_setopt($ch, CURLOPT_POSTFIELDS, " + requestDataCode + ");\n";
|
"curl_setopt($ch, CURLOPT_POSTFIELDS, " + requestDataCode + ");\n";
|
||||||
|
|||||||
@@ -20,7 +20,10 @@ function repr(value: string): string {
|
|||||||
return reprWithVariable(value, false);
|
return reprWithVariable(value, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
function objToPython(obj: any, indent = 0): string {
|
function objToPython(
|
||||||
|
obj: string | number | boolean | object | null,
|
||||||
|
indent = 0
|
||||||
|
): string {
|
||||||
let s = "";
|
let s = "";
|
||||||
switch (typeof obj) {
|
switch (typeof obj) {
|
||||||
case "string":
|
case "string":
|
||||||
@@ -87,7 +90,12 @@ function objToDictOrListOfTuples(obj: Query | QueryDict): string {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDataString(request: Request) {
|
function getDataString(
|
||||||
|
request: Request
|
||||||
|
): [string | null, string | null, boolean | null] {
|
||||||
|
if (!request.data) {
|
||||||
|
return [null, null, null];
|
||||||
|
}
|
||||||
if (!request.isDataRaw && request.data.startsWith("@")) {
|
if (!request.isDataRaw && request.data.startsWith("@")) {
|
||||||
let filePath = request.data.slice(1);
|
let filePath = request.data.slice(1);
|
||||||
if (filePath === "-") {
|
if (filePath === "-") {
|
||||||
@@ -505,7 +513,8 @@ export const _toPython = (request: Request): string => {
|
|||||||
if (request.insecure) {
|
if (request.insecure) {
|
||||||
requestLineBody += ", verify=False";
|
requestLineBody += ", verify=False";
|
||||||
} else if (request.cacert || request.capath) {
|
} else if (request.cacert || request.capath) {
|
||||||
requestLineBody += ", verify=" + repr(request.cacert || request.capath);
|
requestLineBody +=
|
||||||
|
", verify=" + repr((request.cacert || request.capath) as string);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.auth) {
|
if (request.auth) {
|
||||||
|
|||||||
@@ -46,7 +46,10 @@ function getQueryDict(request: Request): string | undefined {
|
|||||||
return queryDict;
|
return queryDict;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDataString(request: Request) {
|
function getDataString(request: Request): string {
|
||||||
|
if (!request.data) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
if (!request.isDataRaw && request.data.startsWith("@")) {
|
if (!request.isDataRaw && request.data.startsWith("@")) {
|
||||||
const filePath = request.data.slice(1);
|
const filePath = request.data.slice(1);
|
||||||
return "data = upload_file('" + filePath + "')";
|
return "data = upload_file('" + filePath + "')";
|
||||||
@@ -167,7 +170,7 @@ export const _toR = (request: Request) => {
|
|||||||
|
|
||||||
let dataString;
|
let dataString;
|
||||||
let filesString;
|
let filesString;
|
||||||
if (request.data && typeof request.data === "string") {
|
if (request.data) {
|
||||||
dataString = getDataString(request);
|
dataString = getDataString(request);
|
||||||
} else if (request.multipartUploads) {
|
} else if (request.multipartUploads) {
|
||||||
filesString = getFilesString(request);
|
filesString = getFilesString(request);
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ import yaml from "yamljs";
|
|||||||
import jsesc from "jsesc";
|
import jsesc from "jsesc";
|
||||||
import querystring from "query-string";
|
import querystring from "query-string";
|
||||||
|
|
||||||
function getDataString(request: Request) {
|
function getDataString(request: Request): PostData | null {
|
||||||
|
if (!request.data) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
let mimeType = "application/json";
|
let mimeType = "application/json";
|
||||||
if (request.data.indexOf("'") > -1) {
|
if (request.data.indexOf("'") > -1) {
|
||||||
request.data = jsesc(request.data);
|
request.data = jsesc(request.data);
|
||||||
@@ -36,8 +39,40 @@ function getDataString(request: Request) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const _toStrest = (request: Request) => {
|
type PostData = {
|
||||||
const response: { [key: string]: any } = { version: 2 };
|
mimeType: string;
|
||||||
|
text: object | string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type BasicAuth = {
|
||||||
|
username?: string;
|
||||||
|
password: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type StrestOutput = {
|
||||||
|
version: number;
|
||||||
|
allowInsecure?: boolean;
|
||||||
|
requests?: {
|
||||||
|
curl_converter: {
|
||||||
|
request: {
|
||||||
|
url: string;
|
||||||
|
method: string;
|
||||||
|
postData?: PostData | null;
|
||||||
|
headers?: {
|
||||||
|
name: string;
|
||||||
|
value: string | null;
|
||||||
|
}[];
|
||||||
|
queryString?: { name: string; value: string }[];
|
||||||
|
};
|
||||||
|
auth?: {
|
||||||
|
basic: BasicAuth;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const _toStrest = (request: Request): string => {
|
||||||
|
const response: StrestOutput = { version: 2 };
|
||||||
if (request.insecure) {
|
if (request.insecure) {
|
||||||
response.allowInsecure = true;
|
response.allowInsecure = true;
|
||||||
}
|
}
|
||||||
@@ -52,7 +87,7 @@ export const _toStrest = (request: Request) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
if (request.data && typeof request.data === "string") {
|
if (request.data) {
|
||||||
response.requests.curl_converter.request.postData = getDataString(request);
|
response.requests.curl_converter.request.postData = getDataString(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,12 +102,12 @@ export const _toStrest = (request: Request) => {
|
|||||||
}
|
}
|
||||||
if (request.auth) {
|
if (request.auth) {
|
||||||
const [username, password] = request.auth;
|
const [username, password] = request.auth;
|
||||||
const basic: { [key: string]: string } = {};
|
const basic: { username?: string; password?: string } = {};
|
||||||
if (username) {
|
if (username) {
|
||||||
basic.username = username;
|
basic.username = username;
|
||||||
}
|
}
|
||||||
basic.password = password;
|
basic.password = password;
|
||||||
response.requests.curl_converter.auth = { basic };
|
response.requests.curl_converter.auth = { basic: basic as BasicAuth };
|
||||||
}
|
}
|
||||||
|
|
||||||
let queryList;
|
let queryList;
|
||||||
@@ -85,7 +120,7 @@ export const _toStrest = (request: Request) => {
|
|||||||
const yamlString = yaml.stringify(response, 100, 2);
|
const yamlString = yaml.stringify(response, 100, 2);
|
||||||
return yamlString;
|
return yamlString;
|
||||||
};
|
};
|
||||||
export const toStrest = (curlCommand: string | string[]) => {
|
export const toStrest = (curlCommand: string | string[]): string => {
|
||||||
const request = util.parseCurlCommand(curlCommand);
|
const request = util.parseCurlCommand(curlCommand);
|
||||||
return _toStrest(request);
|
return _toStrest(request);
|
||||||
};
|
};
|
||||||
|
|||||||
48
src/util.ts
48
src/util.ts
@@ -86,14 +86,33 @@ interface ParsedArguments {
|
|||||||
|
|
||||||
interface Request {
|
interface Request {
|
||||||
url: string;
|
url: string;
|
||||||
|
urlWithoutQuery: string;
|
||||||
query?: Query;
|
query?: Query;
|
||||||
queryDict?: QueryDict;
|
queryDict?: QueryDict;
|
||||||
|
method: string;
|
||||||
headers?: Headers;
|
headers?: Headers;
|
||||||
stdin?: string;
|
stdin?: string;
|
||||||
input?: string;
|
input?: string;
|
||||||
multipartUploads?: [string, string][];
|
multipartUploads?: [string, string][];
|
||||||
auth?: [string, string];
|
auth?: [string, string];
|
||||||
[key: string]: any;
|
cookies?: Cookies;
|
||||||
|
compressed?: boolean;
|
||||||
|
isDataBinary?: boolean;
|
||||||
|
isDataRaw?: boolean;
|
||||||
|
digest?: boolean;
|
||||||
|
dataArray?: string[];
|
||||||
|
data?: string;
|
||||||
|
insecure?: boolean;
|
||||||
|
cert?: string | [string, string];
|
||||||
|
cacert?: string;
|
||||||
|
capath?: string;
|
||||||
|
proxy?: string;
|
||||||
|
proxyAuth?: string;
|
||||||
|
timeout?: string;
|
||||||
|
followRedirects?: boolean;
|
||||||
|
output?: string;
|
||||||
|
http2?: boolean;
|
||||||
|
http3?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
// BEGIN GENERATED CURL OPTIONS
|
// BEGIN GENERATED CURL OPTIONS
|
||||||
@@ -1365,20 +1384,25 @@ function buildRequest(parsedArguments: ParsedArguments): Request {
|
|||||||
urlObject.query = urlObject.query.slice(0, -1);
|
urlObject.query = urlObject.query.slice(0, -1);
|
||||||
}
|
}
|
||||||
const [queryAsList, queryAsDict] = parseQueryString(urlObject.query);
|
const [queryAsList, queryAsDict] = parseQueryString(urlObject.query);
|
||||||
|
const useParsedQuery =
|
||||||
|
queryAsList &&
|
||||||
|
queryAsList.length &&
|
||||||
|
queryAsList.every((p) => p[1] !== null);
|
||||||
// Most software libraries don't let you distinguish between a=&b= and a&b,
|
// Most software libraries don't let you distinguish between a=&b= and a&b,
|
||||||
// so if we get an `a&b`-type query string, don't bother.
|
// so if we get an `a&b`-type query string, don't bother.
|
||||||
const request: Request = { url };
|
let urlWithoutQuery;
|
||||||
if (!queryAsList || queryAsList.some((p) => p[1] === null)) {
|
if (useParsedQuery) {
|
||||||
request.urlWithoutQuery = url; // TODO: rename?
|
|
||||||
} else {
|
|
||||||
urlObject.search = null; // Clean out the search/query portion.
|
urlObject.search = null; // Clean out the search/query portion.
|
||||||
request.urlWithoutQuery = URL.format(urlObject);
|
urlWithoutQuery = URL.format(urlObject);
|
||||||
|
} else {
|
||||||
|
urlWithoutQuery = url; // TODO: rename?
|
||||||
|
}
|
||||||
|
|
||||||
if (queryAsList.length > 0) {
|
const request: Request = { url, method, urlWithoutQuery };
|
||||||
request.query = queryAsList;
|
if (useParsedQuery) {
|
||||||
if (queryAsDict) {
|
request.query = queryAsList;
|
||||||
request.queryDict = queryAsDict;
|
if (queryAsDict) {
|
||||||
}
|
request.queryDict = queryAsDict;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1392,8 +1416,6 @@ function buildRequest(parsedArguments: ParsedArguments): Request {
|
|||||||
request.compressed = true;
|
request.compressed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
request.method = method;
|
|
||||||
|
|
||||||
// TODO: all of these could be specified in the same command.
|
// TODO: all of these could be specified in the same command.
|
||||||
// They also need to maintain order.
|
// They also need to maintain order.
|
||||||
// TODO: do all of these allow @file?
|
// TODO: do all of these allow @file?
|
||||||
|
|||||||
Reference in New Issue
Block a user