diff --git a/docs/helpers/REST.md b/docs/helpers/REST.md index cd96d5e0f..953208444 100644 --- a/docs/helpers/REST.md +++ b/docs/helpers/REST.md @@ -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. diff --git a/lib/helper/REST.js b/lib/helper/REST.js index 0af333464..7e4e3b3c2 100644 --- a/lib/helper/REST.js +++ b/lib/helper/REST.js @@ -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 = {}; @@ -42,6 +43,7 @@ const config = {}; * } *} * ``` + * * With httpAgent * * ```js @@ -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 { @@ -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; +}