Skip to content

Commit

Permalink
feat(rest): print curl (#4396)
Browse files Browse the repository at this point in the history
* feat(rest): print curl

* fix: missing some code

* address CR

* address CR
  • Loading branch information
kobenguyent committed Jun 19, 2024
1 parent e48051c commit 0852fa9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
11 changes: 6 additions & 5 deletions docs/helpers/REST.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ Type: [object][4]
### Properties

- `endpoint` **[string][3]?** API base URL
- `prettyPrintJson` **[boolean][6]?** pretty print json for response/request on console logs
- `timeout` **[number][5]?** timeout for requests in milliseconds. 10000ms by default
- `defaultHeaders` **[object][4]?** a list of default headers
- `prettyPrintJson` **[boolean][6]?** pretty print json for response/request on console logs.
- `printCurl` **[boolean][6]?** print cURL request on console logs. False by default.
- `timeout` **[number][5]?** timeout for requests in milliseconds. 10000ms by default.
- `defaultHeaders` **[object][4]?** a list of default headers.
- `httpAgent` **[object][4]?** create an agent with SSL certificate
- `onRequest` **[function][7]?** a async function which can update request object.
- `onResponse` **[function][7]?** a async function which can update response object.
- `onRequest` **[function][7]?** an async function which can update request object.
- `onResponse` **[function][7]?** an async function which can update response object.
- `maxUploadFileSize` **[number][5]?** set the max content file size in MB when performing api calls.


Expand Down
35 changes: 30 additions & 5 deletions lib/helper/REST.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ const { beautify } = require('../utils');
* @typedef RESTConfig
* @type {object}
* @prop {string} [endpoint] - API base URL
* @prop {boolean} [prettyPrintJson=false] - pretty print json for response/request on console logs
* @prop {number} [timeout=1000] - timeout for requests in milliseconds. 10000ms by default
* @prop {object} [defaultHeaders] - a list of default headers
* @prop {boolean} [prettyPrintJson=false] - pretty print json for response/request on console logs.
* @prop {boolean} [printCurl=false] - print cURL request on console logs. False by default.
* @prop {number} [timeout=1000] - timeout for requests in milliseconds. 10000ms by default.
* @prop {object} [defaultHeaders] - a list of default headers.
* @prop {object} [httpAgent] - create an agent with SSL certificate
* @prop {function} [onRequest] - a async function which can update request object.
* @prop {function} [onResponse] - a async function which can update response object.
* @prop {function} [onRequest] - an async function which can update request object.
* @prop {function} [onResponse] - an async function which can update response object.
* @prop {number} [maxUploadFileSize] - set the max content file size in MB when performing api calls.
*/
const config = {};
Expand All @@ -42,6 +43,7 @@ const config = {};
* }
*}
* ```
*
* With httpAgent
*
* ```js
Expand Down Expand Up @@ -192,6 +194,9 @@ class REST extends Helper {
}

this.options.prettyPrintJson ? this.debugSection('Request', beautify(JSON.stringify(_debugRequest))) : this.debugSection('Request', JSON.stringify(_debugRequest));
if (this.options.printCurl) {
this.debugSection('CURL Request', curlize(request));
}

let response;
try {
Expand Down Expand Up @@ -372,3 +377,23 @@ class REST extends Helper {
}
}
module.exports = REST;

function curlize(request) {
if (request.data?.constructor.name.toLowerCase() === 'formdata') return 'cURL is not printed as the request body is not a JSON';
let curl = `curl --location --request ${request.method ? request.method.toUpperCase() : 'GET'} ${request.baseURL} `.replace("'", '');

if (request.headers) {
Object.entries(request.headers).forEach(([key, value]) => {
curl += `-H "${key}: ${value}" `;
});
}

if (!curl.toLowerCase().includes('content-type: application/json')) {
curl += '-H "Content-Type: application/json" ';
}

if (request.data) {
curl += `-d '${JSON.stringify(request.data)}'`;
}
return curl;
}

0 comments on commit 0852fa9

Please sign in to comment.