'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.getRequestOptions = getRequestOptions; exports.dataIsAndCanBePrimitive = dataIsAndCanBePrimitive; exports.getApidocTypeForPrimitive = getApidocTypeForPrimitive; exports.getTypesForUnion = getTypesForUnion; exports.isObject = isObject; exports.getQueryString = getQueryString; exports.doRequest = doRequest; var _request = require('request'); var _request2 = _interopRequireDefault(_request); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var showLogMessages = process.env.NODE_DEBUG ? process.env.NODE_DEBUG.indexOf('apidoc') !== -1 : false; var enums = [{ "name": "original_type", "plural": "original_types", "values": [{ "name": "api_json", "attributes": [], "description": "The original is in the api.json format" }, { "name": "avro_idl", "attributes": [], "description": "The original is in Avro Idl format" }, { "name": "service_json", "attributes": [], "description": "This is the canonical service spec for apibuilder itself. See https://www.apibuilder.io/apicollective/apibuilder-spec/latest#model-service" }, { "name": "swagger", "attributes": [], "description": "The original in the Swagger JSON or YAML format" }], "attributes": [] }, { "name": "publication", "plural": "publications", "values": [{ "name": "membership_requests.create", "attributes": [], "description": "For organizations for which I am an administrator, email me whenever a user applies to join the org." }, { "name": "memberships.create", "attributes": [], "description": "For organizations for which I am a member, email me whenever a user joins the org." }, { "name": "applications.create", "attributes": [], "description": "For organizations for which I am a member, email me whenever an application is created." }, { "name": "versions.create", "attributes": [], "description": "For applications that I watch, email me whenever a version is created." }], "attributes": [], "description": "A publication represents something that a user can subscribe to. An example would be subscribing to an email alert whenever a new version of an application is created." }, { "name": "visibility", "plural": "visibilities", "values": [{ "name": "user", "attributes": [], "description": "Only the creator can view this application" }, { "name": "organization", "attributes": [], "description": "Any member of the organization can view this application" }, { "name": "public", "attributes": [], "description": "Anybody, including non logged in users, can view this application" }], "attributes": [], "description": "Controls who is able to view this version" }]; var unions = [{ "name": "diff", "plural": "diffs", "types": [{ "type": "diff_breaking", "attributes": [] }, { "type": "diff_non_breaking", "attributes": [] }], "attributes": [], "interfaces": [], "discriminator": "type", "description": "Represents a single diff in an application" }, { "name": "item_detail", "plural": "item_details", "types": [{ "type": "application_summary", "attributes": [], "description": "Represents that the item indexed was an application" }], "attributes": [], "interfaces": [], "discriminator": "type", "description": "Identifies the specific type of item that was indexed by search" }]; function getRequestOptions(settings, userOptions) { return new Promise(function (resolve, reject) { if (!isObject(userOptions || {})) { reject(new Error('options parameter is required and must be a plain object')); return; } // Don't mutate the options the user provided var options = Object.assign({}, userOptions); // Handle the body of the request (options.data as provided by the user) if (settings.requiresBody) { if (!options.data) { reject(new Error('options.data property is required.')); return; } if (settings.discriminator) { if (dataIsAndCanBePrimitive(settings.bodyType, options.data)) { var _dataUpdate; var dataUpdate = (_dataUpdate = {}, _defineProperty(_dataUpdate, settings.discriminator, getApidocTypeForPrimitive(settings.bodyType, options.data)), _defineProperty(_dataUpdate, 'value', options.data), _dataUpdate); options.data = Object.assign({}, dataUpdate); } if (!options.data[settings.discriminator]) { reject(new Error('options.data.' + settings.discriminator + ' must be defined. Available options for [' + settings.bodyType + '] are: ' + getTypesForUnion(settings.bodyType).join(', '))); return; } } } var qs = {}; settings.queryParams.forEach(function (param) { if (options[param.nameCamelCase]) { qs[param.name] = options[param.nameCamelCase]; } }); var json = settings.isApplicationJson ? options.data : undefined; var form = settings.isFormUrlEncoded ? options.data : undefined; var method = settings.method; resolve(Object.assign(options, { json: json, form: form, method: method, qs: qs })); }); } function dataIsAndCanBePrimitive(bodyType, data) { var typesForUnion = getTypesForUnion(bodyType); var dataType = typeof data === 'undefined' ? 'undefined' : _typeof(data); if (dataType === 'string') { return typesForUnion.indexOf('string') != -1; } else if (dataType === 'number') { return typesForUnion.indexOf('decimal') != -1 || typesForUnion.indexOf('double') != -1 || typesForUnion.indexOf('integer') != -1 || typesForUnion.indexOf('long') != -1; } else { return false; } } // Within the context of a union type function getApidocTypeForPrimitive(bodyType, data) { var typesForUnion = getTypesForUnion(bodyType); var dataType = typeof data === 'undefined' ? 'undefined' : _typeof(data); if (dataType === 'string') { // Check if data matches an enum. Use the enum name if it does. var enumForData = enums.find(function (e) { return !!e.values.find(function (v) { return v.name === data; }); }); if (enumForData) { return enumForData.name; } // default to 'string' return dataType; } else if (dataType === 'number') { var containsDecimalPoint = data.toString().indexOf('.') !== -1; var possibleApidocTypes = containsDecimalPoint ? ['decimal', 'double'] : ['integer', 'long']; // Return the first one found. Buggy in that this code should know the // difference between each of the apidoc types. They are all 'number' in // JavaScript though. return typesForUnion.find(function (t) { return possibleApidocTypes.indexOf(t) !== -1; }); } } function getTypesForUnion(unionType) { var ut = unions.find(function (u) { return u.name === unionType; }); if (ut) { return ut.types.map(function (t) { return t.type; }); } return []; } /** * Simple plain object check. This is actually really involved to be 100% * accurate */ function isObject(obj) { return (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && !Array.isArray(obj); } function log(message) { if (showLogMessages) { console.log('[apidoc] ' + message); } } function getQueryString(options) { var qs = options.qs || {}; var params = Object.keys(qs).reduce(function (kvPairs, key) { return kvPairs.concat(key + '=' + qs[key]); }, []); return params.join('&'); } function doRequest(options) { var startMs = Date.now(); options = options || {}; options.qsStringifyOptions = { arrayFormat: 'repeat' }; var queryString = getQueryString(options); var url = options.uri + (queryString ? '?' + queryString : ''); log(options.method + ' ' + url); if (options.method === 'POST' || options.method === 'PUT' || options.method === 'PATCH') { log('|--> With body:\n\t\t' + JSON.stringify(options.json)); } return new Promise(function (resolve, reject) { (0, _request2.default)(options, function (err, response) { if (err) { reject(err); return; } var duration = Date.now() - startMs; var requestHref = response.request ? response.request.href : ''; log('Completed ' + options.method + ' ' + requestHref + ' ' + response.statusCode + ' ' + duration + 'ms'); resolve(response); }); }); }