| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- "use strict";
- var _ = require('../../lodash'), parseBody = require('./util/parseBody'), sanitize = require('./util/sanitize').sanitize, sanitizeOptions = require('./util/sanitize').sanitizeOptions, addFormParam = require('./util/sanitize').addFormParam, self;
- /**
- * Used to parse the request headers
- *
- * @param {Object} request - postman SDK-request object
- * @param {String} indentation - used for indenting snippet's structure
- * @returns {String} - request headers in the desired format
- */
- function getHeaders(request, indentation) {
- var headerArray = request.toJSON().header, headerMap;
- if (!_.isEmpty(headerArray)) {
- headerArray = _.reject(headerArray, 'disabled');
- headerMap = _.map(headerArray, function (header) {
- return `${indentation.repeat(2)}'${sanitize(header.key, 'header', true)}: ` +
- `${sanitize(header.value, 'header')}'`;
- });
- return `${indentation}CURLOPT_HTTPHEADER => [\n${headerMap.join(',\n')}\n${indentation}],\n`;
- }
- return '';
- }
- self = module.exports = {
- /**
- * Used to return options which are specific to a particular plugin
- *
- * @returns {Array}
- */
- getOptions: function () {
- return [{
- name: 'Set indentation count',
- id: 'indentCount',
- type: 'positiveInteger',
- default: 2,
- description: 'Set the number of indentation characters to add per code level'
- },
- {
- name: 'Set indentation type',
- id: 'indentType',
- type: 'enum',
- availableOptions: ['Tab', 'Space'],
- default: 'Space',
- description: 'Select the character used to indent lines of code'
- },
- {
- name: 'Set request timeout',
- id: 'requestTimeout',
- type: 'positiveInteger',
- default: 0,
- description: 'Set number of milliseconds the request should wait for a response' +
- ' before timing out (use 0 for infinity)'
- },
- {
- name: 'Follow redirects',
- id: 'followRedirect',
- type: 'boolean',
- default: true,
- description: 'Automatically follow HTTP redirects'
- },
- {
- name: 'Trim request body fields',
- id: 'trimRequestBody',
- type: 'boolean',
- default: false,
- description: 'Remove white space and additional lines that may affect the server\'s response'
- }];
- },
- /**
- * Used to convert the postman sdk-request object in php-curl request snippet
- *
- * @param {Object} request - postman SDK-request object
- * @param {Object} options
- * @param {String} options.indentType - type of indentation eg: Space / Tab (default: Space)
- * @param {Number} options.indentCount - frequency of indent (default: 4 for indentType: Space,
- default: 1 for indentType: Tab)
- * @param {Number} options.requestTimeout : time in milli-seconds after which request will bail out
- (default: 0 -> never bail out)
- * @param {Boolean} options.trimRequestBody : whether to trim request body fields (default: false)
- * @param {Boolean} options.followRedirect : whether to allow redirects of a request
- * @param {Function} callback - function with parameters (error, snippet)
- */
- convert: function (request, options, callback) {
- var snippet = '', indentation = '', identity = '', finalUrl;
- if (_.isFunction(options)) {
- callback = options;
- options = null;
- }
- else if (!_.isFunction(callback)) {
- throw new Error('Php-Curl~convert: Callback is not a function');
- }
- options = sanitizeOptions(options, self.getOptions());
- identity = options.indentType === 'Tab' ? '\t' : ' ';
- indentation = identity.repeat(options.indentCount);
- // concatenation and making up the final string
- finalUrl = request.url.toString();
- if (finalUrl !== encodeURI(finalUrl)) {
- // needs to be encoded
- finalUrl = encodeURI(finalUrl);
- }
- snippet = '<?php\n\n$curl = curl_init();\n\n';
- snippet += 'curl_setopt_array($curl, [\n';
- snippet += `${indentation}CURLOPT_URL => '${sanitize(finalUrl, 'url')}',\n`;
- snippet += `${indentation}CURLOPT_RETURNTRANSFER => true,\n`;
- // snippet += `${indentation}CURLOPT_ENCODING => '',\n`;
- // snippet += `${indentation}CURLOPT_MAXREDIRS => 10,\n`;
- // snippet += `${indentation}CURLOPT_TIMEOUT => ${options.requestTimeout},\n`;
- // snippet += `${indentation}CURLOPT_FOLLOWLOCATION => ${options.followRedirect},\n`;
- // snippet += `${indentation}CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,\n`;
- snippet += `${indentation}CURLOPT_CUSTOMREQUEST => '${request.method}',\n`;
- // The following code handles multiple files in the same formdata param.
- // It removes the form data params where the src property is an array of filepath strings
- // Splits that array into different form data params with src set as a single filepath string
- if (request.body && request.body.mode === 'formdata') {
- let formdata = request.body.formdata, formdataArray = [];
- formdata.members.forEach((param) => {
- let key = param.key, type = param.type, disabled = param.disabled, contentType = param.contentType;
- // check if type is file or text
- if (type === 'file') {
- // if src is not of type string we check for array(multiple files)
- if (typeof param.src !== 'string') {
- // if src is an array(not empty), iterate over it and add files as separate form fields
- if (Array.isArray(param.src) && param.src.length) {
- param.src.forEach((filePath) => {
- addFormParam(formdataArray, key, param.type, filePath, disabled, contentType);
- });
- }
- // if src is not an array or string, or is an empty array, add a placeholder for file path(no files case)
- else {
- addFormParam(formdataArray, key, param.type, '/path/to/file', disabled, contentType);
- }
- }
- // if src is string, directly add the param with src as filepath
- else {
- addFormParam(formdataArray, key, param.type, param.src, disabled, contentType);
- }
- }
- // if type is text, directly add it to formdata array
- else {
- addFormParam(formdataArray, key, param.type, param.value, disabled, contentType);
- }
- });
- request.body.update({
- mode: 'formdata',
- formdata: formdataArray
- });
- }
- snippet += `${parseBody(request.toJSON(), options.trimRequestBody, indentation)}`;
- if (request.body && !request.headers.has('Content-Type')) {
- if (request.body.mode === 'file') {
- request.addHeader({
- key: 'Content-Type',
- value: 'text/plain'
- });
- }
- else if (request.body.mode === 'graphql') {
- request.addHeader({
- key: 'Content-Type',
- value: 'application/json'
- });
- }
- }
- snippet += `${getHeaders(request, indentation)}`;
- snippet += ']);\n\n';
- snippet += '$response = curl_exec($curl);\n\n';
- snippet += 'curl_close($curl);\n';
- snippet += 'echo $response;\n';
- return callback(null, snippet);
- }
- };
|