Skip to content

Commit

Permalink
Merge pull request #10 from zvakanaka/esm
Browse files Browse the repository at this point in the history
3.0.0 ESM
  • Loading branch information
zvakanaka authored Feb 8, 2023
2 parents ba16e28 + 43ee237 commit 89a048d
Show file tree
Hide file tree
Showing 7 changed files with 635 additions and 3,155 deletions.
24 changes: 24 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export type Options = {
method?: string,
headers?: object,
body?: object | string
};

export type CurlStringOptions = {
colorJson: boolean,
jsonIndentWidth: number,
}

/**
* Builds a human readable cURL command and returns the string.
* @param {string} url - JSON object to highlighter.
* @param {Options} [options] - A map with the ANSI characters for each supported color.
* @param {CurlStringOptions} [curlStringOptions] - An object to configure the coloring.
* @returns {string} cURL command
*/
export default function curlString(
url: string,
options?: Options,
curlStringOptions?: CurlStringOptions,
): string

19 changes: 8 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
const cj = require('color-json');
import cj from 'color-json';
const hasOwnProperty = (object, property) => Object.prototype.hasOwnProperty.call(object, property);

module.exports = curlString;

/**
* Builds a curl command and returns the string.
* @param {String} url Endpoint
* @param {Object} options Object with headers, etc. (fetch format)
* @param {Object} [curlStringOptions={colorJson:true,jsonIndentWidth:2}] Formatting options
* @return {String} cURL command
*/
function curlString(
export default function curlString(
url,
options,
curlStringOptions = { colorJson: true, jsonIndentWidth: 2 }
Expand All @@ -24,15 +22,13 @@ function curlString(
options && options.headers && typeof options.headers === 'object';
const hasBody = options && options.body;

let curl = `curl --request ${method} \\\n--url ${url}${
hasHeaders || hasBody ? ' \\' : ''
}`;
let curl = `curl --request ${method} \\\n--url ${url}${hasHeaders || hasBody ? ' \\' : ''
}`;

if (hasHeaders) {
Object.entries(options.headers).forEach(([key, value], i) => {
curl += `\n--header '${key}: ${value}'${
hasBody || i < Object.keys(options.headers).length - 1 ? ' \\' : ''
}`;
curl += `\n--header '${key}: ${value}'${hasBody || i < Object.keys(options.headers).length - 1 ? ' \\' : ''
}`;
});
}

Expand Down Expand Up @@ -71,7 +67,7 @@ function bodyToDataString(options, curlStringOptions) {

// return an ampersand delimited string
if (hasHeader(options, 'content-type') &&
getHeader(options, 'content-type').toLowerCase() === 'application/x-www-form-urlencoded') {
getHeader(options, 'content-type').toLowerCase() === 'application/x-www-form-urlencoded') {
if (typeof parsedData === 'string') {
return parsedData;
} else {
Expand All @@ -89,3 +85,4 @@ function bodyToDataString(options, curlStringOptions) {
: JSON.stringify(parsedData, null, jsonIndentWidth);
}
}

2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
testPathIgnorePatterns: ["/node_modules/"],
collectCoverage: true,
coverageReporters: ["lcov", "text"],
Expand Down
Loading

0 comments on commit 89a048d

Please sign in to comment.