convert-fetch-to-curl
Version:
Example to convert fetch to curl
157 lines (148 loc) • 4.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.convertFetchToCurl = void 0;
exports.escapeBody = escapeBody;
exports.generateBody = generateBody;
exports.generateCompress = generateCompress;
exports.isInstanceOfHeaders = exports.generateMethod = exports.generateHeader = void 0;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
/**
* see https://fetch.spec.whatwg.org/#methods
*
* @export
* @param {any} options
* @returns {string}
*/
var generateMethod = function generateMethod(options) {
var method = options.method;
if (!method) return '';
var type = {
GET: ' -X GET',
POST: ' -X POST',
PUT: ' -X PUT',
PATCH: ' -X PATCH',
DELETE: ' -X DELETE',
HEAD: ' -X HEAD',
OPTIONS: ' -X OPTIONS'
};
return type[method.toUpperCase()] || '';
};
/**
* @export
* @param {any} val
* @returns true if the envirtonment supports Headers and val is of instance Headers
*/
exports.generateMethod = generateMethod;
var isInstanceOfHeaders = function isInstanceOfHeaders(val) {
if (typeof Headers !== "function") {
/**
* Environment does not support the Headers constructor
* old internet explorer?
*/
return false;
}
return val instanceof Headers;
};
/**
* @typedef {Object} HeaderParams
* @property {Boolean} isEncode - A flag which is set to true if the request should set the --compressed flag
* @property {String} params - The header params as string
*/
exports.isInstanceOfHeaders = isInstanceOfHeaders;
var getHeaderString = function getHeaderString(name, val) {
return " -H \"".concat(name, ": ").concat("".concat(val).replace(/(\\|")/g, '\\$1'), "\"");
};
/**
* @export
* @param {object={}} options
* @param {object|Headers} options.headers
* @returns {HeaderParams} An Object with the header info
*/
var generateHeader = function generateHeader() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _ref = options || {},
headers = _ref.headers;
var isEncode = false;
var headerParam = '';
if (isInstanceOfHeaders(headers)) {
headers.forEach(function (val, name) {
if (name.toLocaleLowerCase() !== 'content-length') {
headerParam += getHeaderString(name, val);
}
if (name.toLocaleLowerCase() === 'accept-encoding') {
isEncode = true;
}
});
} else if (headers) {
Object.keys(headers).map(function (name) {
if (name.toLocaleLowerCase() !== 'content-length') {
headerParam += getHeaderString(name, headers[name]);
}
if (name.toLocaleLowerCase() === 'accept-encoding') {
isEncode = true;
}
});
}
return {
params: headerParam,
isEncode: isEncode
};
};
/**
* @export
* @param {Object} body
* @returns {string}
*/
exports.generateHeader = generateHeader;
function escapeBody(body) {
if (typeof body !== 'string') return body;
return body.replace(/'/g, "'\\''");
}
/**
* @export
* @param {Object} body
* @returns {string}
*/
function generateBody(body) {
if (!body) return '';
if (_typeof(body) === "object") {
return " --data-binary '".concat(escapeBody(JSON.stringify(body)), "'");
}
return " --data-binary '".concat(escapeBody(body), "'");
}
/**
* @export
* @param {boolean} isEncode
* @return {string}
*/
function generateCompress(isEncode) {
return isEncode ? ' --compressed' : '';
}
/**
* @export
* @param {string|object} requestInfo
* @param {object={}} requestInit
*/
var convertFetchToCurl = function convertFetchToCurl(requestInfo, requestInit) {
var url, options;
/**
* initialization with an empty object is done here to
* keep everything backwards compatible to 0.4.0 and below
*/
if (typeof requestInfo === "string" || requestInfo instanceof URL) {
url = requestInfo;
options = requestInit || {};
} else {
url = (requestInfo || {}).url;
options = requestInfo || {};
}
var _options = options,
body = _options.body;
var headers = generateHeader(options);
return "curl '".concat(url, "'").concat(generateMethod(options)).concat(headers.params || '').concat(generateBody(body)).concat(generateCompress(headers.isEncode));
};
exports.convertFetchToCurl = convertFetchToCurl;
var _default = convertFetchToCurl;
exports["default"] = _default;