Best JavaScript code snippet using apickli
swagger-helpers.js
Source:swagger-helpers.js
1'use strict';2// Dependencies.3const RecursiveIterator = require('recursive-iterator');4/**5 * Checks if tag is already contained withing target.6 * The tag is an object of type http://swagger.io/specification/#tagObject7 * The target, is the part of the swagger specification that holds all tags.8 * @function9 * @param {object} target - Swagger object place to include the tags data.10 * @param {object} tag - Swagger tag object to be included.11 * @returns {boolean} Does tag is already present in target12 */13function _tagDuplicated(target, tag) {14 // Check input is workable.15 if (target && target.length && tag) {16 for (let i = 0; i < target.length; i = i + 1) {17 let targetTag = target[i];18 // The name of the tag to include already exists in the taget.19 // Therefore, it's not necessary to be added again.20 if (targetTag.name === tag.name) {21 return true;22 }23 }24 }25 // This will indicate that `tag` is not present in `target`.26 return false;27}28/**29 * Adds the tags property to a swagger object.30 * @function31 * @param {object} conf - Flexible configuration.32 */33function _attachTags(conf) {34 let tag = conf.tag;35 let swaggerObject = conf.swaggerObject;36 let propertyName = conf.propertyName;37 // Correct deprecated property.38 if (propertyName === 'tag') {39 propertyName = 'tags';40 }41 if (Array.isArray(tag)) {42 for (let i = 0; i < tag.length; i = i + 1) {43 if (!_tagDuplicated(swaggerObject[propertyName], tag[i])) {44 swaggerObject[propertyName].push(tag[i]);45 }46 }47 } else {48 if (!_tagDuplicated(swaggerObject[propertyName], tag)) {49 swaggerObject[propertyName].push(tag);50 }51 }52}53/**54 * Merges two objects55 * @function56 * @param {object} obj1 - Object 157 * @param {object} obj2 - Object 258 * @returns {object} Merged Object59 */60function _objectMerge(obj1, obj2) {61 let obj3 = {};62 for (let attr in obj1) {63 if (obj1.hasOwnProperty(attr)) {64 obj3[attr] = obj1[attr];65 }66 }67 for (let name in obj2) {68 if (obj2.hasOwnProperty(name)) {69 obj3[name] = obj2[name];70 }71 }72 return obj3;73}74/**75 * Adds necessary swagger schema object properties.76 * @see https://goo.gl/Eoagtl77 * @function78 * @param {object} swaggerObject - The object to receive properties.79 * @returns {object} swaggerObject - The updated object.80 */81function swaggerizeObj(swaggerObject) {82 swaggerObject.swagger = '2.0';83 swaggerObject.paths = swaggerObject.paths || {};84 swaggerObject.definitions = swaggerObject.definitions || {};85 swaggerObject.responses = swaggerObject.responses || {};86 swaggerObject.parameters = swaggerObject.parameters || {};87 swaggerObject.securityDefinitions = swaggerObject.securityDefinitions || {};88 swaggerObject.tags = swaggerObject.tags || [];89 return swaggerObject;90}91/**92 * List of deprecated or wrong swagger schema properties in singular.93 * @function94 * @returns {array} The list of deprecated property names.95 */96function _getSwaggerSchemaWrongProperties() {97 return [98 'consume',99 'produce',100 'path',101 'tag',102 'definition',103 'securityDefinition',104 'scheme',105 'response',106 'parameter',107 'deprecated'108 ];109}110/**111 * Makes a deprecated property plural if necessary.112 * @function113 * @param {string} propertyName - The swagger property name to check.114 * @returns {string} The updated propertyName if neccessary.115 */116function _correctSwaggerKey(propertyName) {117 let wrong = _getSwaggerSchemaWrongProperties();118 if (wrong.indexOf(propertyName) > 0) {119 // Returns the corrected property name.120 return propertyName + 's';121 }122 return propertyName;123}124/**125 * Handles swagger propertyName in pathObject context for swaggerObject.126 * @function127 * @param {object} swaggerObject - The swagger object to update.128 * @param {object} pathObject - The input context of an item for swaggerObject.129 * @param {string} propertyName - The property to handle.130 */131function _organizeSwaggerProperties(swaggerObject, pathObject, propertyName) {132 let simpleProperties = [133 'consume',134 'consumes',135 'produce',136 'produces',137 // 'path',138 // 'paths',139 'schema',140 'schemas',141 'securityDefinition',142 'securityDefinitions',143 'response',144 'responses',145 'parameter',146 'parameters',147 'definition',148 'definitions',149 ];150 // Common properties.151 if (simpleProperties.indexOf(propertyName) !== -1) {152 let keyName = _correctSwaggerKey(propertyName);153 let definitionNames = Object154 .getOwnPropertyNames(pathObject[propertyName]);155 for (let k = 0; k < definitionNames.length; k = k + 1) {156 let definitionName = definitionNames[k];157 swaggerObject[keyName][definitionName] =158 pathObject[propertyName][definitionName];159 }160 // Tags.161 } else if (propertyName === 'tag' || propertyName === 'tags') {162 let tag = pathObject[propertyName];163 _attachTags({164 tag: tag,165 swaggerObject: swaggerObject,166 propertyName: propertyName,167 });168 // Paths.169 } else {170 let routes = Object171 .getOwnPropertyNames(pathObject[propertyName]);172 for (let k = 0; k < routes.length; k = k + 1) {173 let route = routes[k];174 if(!swaggerObject.paths){175 swaggerObject.paths = {};176 }177 swaggerObject.paths[route] = _objectMerge(178 swaggerObject.paths[route], pathObject[propertyName][route]179 );180 }181 }182}183/**184 * Adds the data in to the swagger object.185 * @function186 * @param {object} swaggerObject - Swagger object which will be written to187 * @param {object[]} data - objects of parsed swagger data from yml or jsDoc188 * comments189 */190function addDataToSwaggerObject(swaggerObject, data) {191 if (!swaggerObject || !data) {192 throw new Error('swaggerObject and data are required!');193 }194 for (let i = 0; i < data.length; i = i + 1) {195 let pathObject = data[i];196 let propertyNames = Object.getOwnPropertyNames(pathObject);197 // Iterating the properties of the a given pathObject.198 for (let j = 0; j < propertyNames.length; j = j + 1) {199 let propertyName = propertyNames[j];200 // Do what's necessary to organize the end specification.201 _organizeSwaggerProperties(swaggerObject, pathObject, propertyName);202 }203 }204}205/**206 * Aggregates a list of wrong properties in problems.207 * Searches in object based on a list of wrongSet.208 * @param {Array|object} list - a list to iterate209 * @param {Array} wrongSet - a list of wrong properties210 * @param {Array} problems - aggregate list of found problems211 */212function seekWrong(list, wrongSet, problems) {213 let iterator = new RecursiveIterator(list, 0, false);214 for (let item = iterator.next(); !item.done; item = iterator.next()) {215 let isDirectChildOfProperties =216 item.value.path[item.value.path.length - 2] === 'properties';217 if (wrongSet.indexOf(item.value.key) > 0 && !isDirectChildOfProperties) {218 problems.push(item.value.key);219 }220 }221}222/**223 * Returns a list of problematic tags if any.224 * @function225 * @param {Array} sources - a list of objects to iterate and check226 * @returns {Array} problems - a list of problems encountered227 */228function findDeprecated(sources) {229 let wrong = _getSwaggerSchemaWrongProperties();230 // accumulate problems encountered231 let problems = [];232 sources.forEach(function(source) {233 // Iterate through `source`, search for `wrong`, accumulate in `problems`.234 seekWrong(source, wrong, problems);235 });236 return problems;237}238module.exports = {239 addDataToSwaggerObject: addDataToSwaggerObject,240 swaggerizeObj: swaggerizeObj,241 findDeprecated: findDeprecated,...
docs.ts
Source:docs.ts
1import fs from 'fs'2const expressJSDocSwagger = require('express-jsdoc-swagger');3class SwaggerDoc {4 async generateDocFile (app: any) {5 const options = {6 info: {7 version: '1.0.0',8 title: 'Example Api',9 license: {10 name: 'MIT',11 },12 },13 security: {14 BasicAuth: {15 type: 'http',16 scheme: 'basic',17 },18 },19 filesPattern: './**/*.ts', // Glob pattern to find your jsdoc files20 swaggerUIPath: 'htttp://localhost:8080', // SwaggerUI will be render in this url. Default: '/v1/api-docs'21 baseDir: __dirname,22 };23 const listener = expressJSDocSwagger(app)(options);24 // Event emitter API25 listener.on('error', (error: any) => {26 console.error(`Error: ${error}`);27 });28 listener.on('process', (entity: any, swaggerObject: any) => {29 console.log(`entity: ${entity}`);30 console.log('swaggerObject');31 console.log(swaggerObject);32 });33 listener.on('finish', (swaggerObject: any) => {34 console.log('Finish');35 console.log(swaggerObject);36 console.log(swaggerObject.paths);37 fs.writeFileSync('swagger.json', JSON.stringify(swaggerObject))38 });39 return40 }41}...
Using AI Code Generation
1const Apickli = require('apickli');2const { Before, Given, When, Then } = require('cucumber');3Before(function () {4 this.apickli = new Apickli.Apickli('http', 'localhost:8000');5});6Given('I have a request body', function (callback) {7 this.apickli.setRequestBody('{"name": "John", "age": 30}');8 callback();9});10When('I send a POST request to /test', function (callback) {11 this.apickli.post('/test', callback);12});13Then('I should receive a 200 response', function (callback) {14 this.apickli.assertResponseCode(200);15 callback();16});17const Apickli = require('apickli');18const { Before, Given, When, Then } = require('cucumber');19Before(function () {20 this.apickli = new Apickli.Apickli('http', 'localhost:8000');21});22Given('I have a request body', function (callback) {23 this.apickli.setRequestBody('{"name": "John", "age": 30}');24 callback();25});26When('I send a POST request to /test', function (callback) {27 this.apickli.swaggerObject('/test', 'post', callback);28});29Then('I should receive a 200 response', function (callback) {30 this.apickli.assertResponseCode(200);31 callback();32});
Using AI Code Generation
1var apickli = require('apickli-gherkin');2var {defineSupportCode} = require('cucumber');3defineSupportCode(function ({Given, When, Then}) {4 var apickli = new apickli.Apickli('http', 'localhost:8080');5 apickli.addRequestHeader('Content-Type', 'application/json');6 apickli.addRequestHeader('Accept', 'application/json');7 Given('I have a request body', function (callback) {8 var requestBody = {9 };10 apickli.storeValueInScenarioScope('requestBody', requestBody);11 callback();12 });13 When('I send a POST request', function (callback) {14 var requestBody = apickli.getScenarioVariable('requestBody');15 apickli.post('/post', requestBody, function (error, response) {16 if (error) {17 callback(error);18 } else {19 callback();20 }21 });22 });23 Then('I should get a 200 response', function (callback) {24 apickli.assertResponseCode(200);25 callback();26 });27});
Using AI Code Generation
1var apickli = require('apickli');2var apickliSwagger = require('apickli-swagger');3var apickliApigee = require('apickli-apigee');4var apickliGherkin = require('apickli-gherkin');5var fs = require('fs');6var path = require('path');7var swaggerObject = require('./swagger.json');8var apickliObject = new apickli.Apickli('http', 'localhost:8080');9apickliSwagger(apickliObject);10apickliApigee(apickliObject);11apickliGherkin(apickliObject);12apickliObject.swaggerObject(swaggerObject);13module.exports = function () {14 this.Before(function (scenario, callback) {15 this.apickli.resetRequest();16 callback();17 });18 this.Given(/^I set the path parameter (.*) to (.*)$/, function (parameter, value, callback) {19 this.apickli.setPathParameter(parameter, value);20 callback();21 });22 this.Given(/^I set the request body to (.*)$/, function (body, callback) {23 this.apickli.setRequestBody(body);24 callback();25 });26 this.Given(/^I set the request header (.*) to (.*)$/, function (header, value, callback) {27 this.apickli.setRequestHeader(header, value);28 callback();29 });30 this.When(/^I (GET|PUT|POST|DELETE) the resource (.*)$/, function (method, resource, callback) {31 this.apickli.invokeMethod(method, resource, callback);32 });33 this.Then(/^the response code should be (\d+)$/, function (statusCode, callback) {34 this.apickli.assertResponseCode(statusCode);35 callback();36 });37 this.Then(/^the response body should match (.*)$/, function (expectedBody, callback) {38 this.apickli.assertResponseBodyContains(expectedBody);39 callback();40 });41 this.Then(/^the response header (.*) should be (.*)$/, function (header, value, callback) {42 this.apickli.assertResponseHeader(header, value);43 callback();44 });45};46var apickli = require('apickli');47var apickliSwagger = require('
Using AI Code Generation
1var Apickli = require('apickli');2var apickli = new Apickli.Apickli('https', 'httpbin.org');3var {defineSupportCode} = require('cucumber');4defineSupportCode(function({Given, When, Then}) {5 Given('I set header {string} to {string}', function (header, value, callback) {6 apickli.addRequestHeader(header, value);7 callback(null, 'pending');8 });9 When('I GET {string}', function (path, callback) {10 apickli.get(path, callback);11 });12 Then('I should get HTTP response code {int}', function (statusCode, callback) {13 apickli.assertResponseCode(statusCode);14 callback(null, 'pending');15 });16});
Using AI Code Generation
1var apickli = require('apickli-gherkin');2var apickliObject = new apickli.Apickli('http', 'localhost:8080');3var apickli = require('apickli');4var apickliObject = new apickli.Apickli('http', 'localhost:8080');5var apickli = require('apickli-gherkin');6var apickliObject = new apickli.Apickli('http', 'localhost:8080');7apickliObject.swaggerObject('swagger.json', function(err, swaggerObject) {8 if (err) {9 } else {10 }11});12var apickli = require('apickli');13var apickliObject = new apickli.Apickli('http', 'localhost:8080');14apickliObject.swaggerObject('swagger.json', function(err, swaggerObject) {15 if (err) {16 } else {17 }18});19var apickli = require('apickli');20var apickliObject = new apickli.Apickli('http', 'localhost:8080');21 if (err) {22 } else {23 }24});25var apickli = require('apickli');26var apickliObject = new apickli.Apickli('http', 'localhost:8080');27 if (err) {28 } else {29 }
Using AI Code Generation
1const apickli = require('apickli');2module.exports = function() {3 this.Given(/^I have a test$/, function (callback) {4 callback(null, 'pending');5 });6 this.When(/^I run a test$/, function (callback) {7 callback(null, 'pending');8 });9 this.Then(/^I should get a result$/, function (callback) {10 callback(null, 'pending');11 });12};
Using AI Code Generation
1var swaggerObject = apickli.getSwaggerObject();2swaggerObject.host = "api.example.com";3apickli.setSwaggerObject(swaggerObject);4var swaggerObject = apickli.getSwaggerObject();5swaggerObject.host = "api.example.com";6apickli.setSwaggerObject(swaggerObject);7var swaggerObject = apickli.getSwaggerObject();8swaggerObject.host = "api.example.com";9apickli.setSwaggerObject(swaggerObject);10var swaggerObject = apickli.getSwaggerObject();11swaggerObject.host = "api.example.com";12apickli.setSwaggerObject(swaggerObject);13var swaggerObject = apickli.getSwaggerObject();14swaggerObject.host = "api.example.com";15apickli.setSwaggerObject(swaggerObject);16var swaggerObject = apickli.getSwaggerObject();17swaggerObject.host = "api.example.com";18apickli.setSwaggerObject(swaggerObject);19var swaggerObject = apickli.getSwaggerObject();20swaggerObject.host = "api.example.com";21apickli.setSwaggerObject(swaggerObject);22var swaggerObject = apickli.getSwaggerObject();23swaggerObject.host = "api.example.com";24apickli.setSwaggerObject(swaggerObject);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!