Skip to content

Commit

Permalink
Merge branch 'main' of github.com:agrostar/zzapi
Browse files Browse the repository at this point in the history
  • Loading branch information
vasan-agrostar committed Jan 21, 2024
2 parents 3e324d4 + 70d8bd6 commit 4d135d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
35 changes: 19 additions & 16 deletions src/constructCurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@ import { getParamsForUrl, getURL } from "./executeRequest";
import { RequestSpec } from "./models";

export function getCurlRequest(request: RequestSpec): string {
const methodFlag = ` -X ${request.httpRequest.method.toUpperCase()}`;
let curl: string = "curl";

let headersFlag = "";
// method
curl += ` -X ${request.httpRequest.method.toUpperCase()}`;

// headers
if (request.httpRequest.headers !== undefined) {
for (const header in request.httpRequest.headers) {
headersFlag += ` -H '${header}: ${request.httpRequest.headers[header]}'`;
}
for (const header in request.httpRequest.headers)
curl += ` -H '${header}: ${request.httpRequest.headers[header]}'`;
}

let bodyFlag = "";
// body
if (request.httpRequest.body !== undefined)
bodyFlag += ` -d '${getStringValueIfDefined(request.httpRequest.body)}'`;
curl += ` -d '${getStringValueIfDefined(request.httpRequest.body)}'`;

// options.follow
if (request.options.follow) curl += " -L";

let followRedirectFlag = "";
if (request.options.follow) followRedirectFlag = " -L";
// options.verifySSL
if (!request.options.verifySSL) curl += " -k";

let verifySSLFlag = "";
if (!request.options.verifySSL) verifySSLFlag = " -k";
// options.showHeaders
if (request.options.showHeaders) curl += " -i";

const url = ` '${getURL(
// url w/ params
curl += ` '${getURL(
request.httpRequest.baseUrl,
request.httpRequest.url,
getParamsForUrl(request.httpRequest.params, request.options.rawParams)
)}'`;

const finalCurl =
"curl" + methodFlag + headersFlag + bodyFlag + followRedirectFlag + verifySSLFlag + url;

return finalCurl;
return curl;
}
2 changes: 1 addition & 1 deletion src/executeRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function executeGotRequest(httpRequest: GotRequest): Promise<{
}

export function getParamsForUrl(params: Param[] | undefined, rawParams: boolean): string {
if (!params) return "";
if (!params || params.length < 1) return "";

let paramArray: string[] = [];
params.forEach((param) => {
Expand Down

0 comments on commit 4d135d8

Please sign in to comment.