Best JavaScript code snippet using wpt
index.js
Source:index.js
...19 * @param {String} code [Code of the response]20 * @param {Object} data [Data of the response]21 * @returns {Object} [Containing the data to return from a request]22 */23function packData(data, message, code) {24 return (packagedData = {25 message: message,26 code: code,27 data: data,28 });29}30/***31 * @author Giulio Serra32 * [Post a curator inside the Persistance Storage]33 */34exports.postCurator = functions.region(REGION).https.onRequest((req, res) => {35 cors(req, res, () => {36 const body = req.body;37 if (system.isObjectEmpty(body)) {38 return res39 .status(400)40 .send(packData(null, "missing body in the request.", 400));41 }42 const jCurator = system.getJsonFromKeyValueForm(body);43 const newCurator = new curator.Curator(jCurator);44 const validation = newCurator.validate();45 if (validation.code !== "200") {46 return res.status(400).send(packData(null, validation.message, 400));47 }48 return newCurator49 .postAsync()50 .then((response) => {51 return res.status(200).send(packData(body, "ok", "200"));52 })53 .catch((err) => {54 return res.status(500).send(packData(null, err.message, "500"));55 });56 });57});58/***59 * @author Giulio Serra60 * [Post a piece inside the Persistance Storage]61 */62exports.postPiece = functions.region(REGION).https.onRequest((req, res) => {63 cors(req, res, () => {64 const body = req.body;65 if (system.isObjectEmpty(body)) {66 return res67 .status(400)68 .send(packData(null, "missing body in the request.", 400));69 }70 const jPiece = system.getJsonFromKeyValueForm(body);71 const newPiece = new piece.Piece(jPiece);72 const validation = newPiece.validate();73 if (validation.code !== "200") {74 return res.status(400).send(packData(null, validation.message, 400));75 }76 return newPiece77 .postAsync()78 .then((response) => {79 return res.status(200).send(packData(body, "ok", "200"));80 })81 .catch((err) => {82 return res.status(500).send(packData(null, err.message, "500"));83 });84 });85});86/***87 * @author Giulio Serra88 * [Post a sensor in the persistance storage]89 */90exports.postSensor = functions.region(REGION).https.onRequest((req, res) => {91 cors(req, res, () => {92 const body = req.body;93 if (system.isObjectEmpty(body)) {94 return res95 .status(400)96 .send(packData(null, "missing body in the request.", 400));97 }98 const newSensor = new sensor.Sensor(body);99 return newSensor100 .postAsync()101 .then((response) => {102 return res.status(200).send(packData(response, "ok", "200"));103 })104 .catch((err) => {105 return res.status(500).send(packData(null, err.message, "500"));106 });107 });108});109/***110 * @author Giulio Serra111 * [Put a sensor in the persistance storage]112 */113exports.putSensor = functions.region(REGION).https.onRequest((req, res) => {114 cors(req, res, () => {115 const body = req.body;116 if (system.isObjectEmpty(body)) {117 return res118 .status(400)119 .send(packData(null, "missing body in the request.", 400));120 }121 const sensor_to_update = new sensor.Sensor(122 system.getJsonFromKeyValueForm(body)123 );124 return sensor_to_update125 .updateStatusAsync()126 .then((response) => {127 return res.status(200).send(packData(response, "ok", "200"));128 })129 .catch((err) => {130 return res.status(500).send(packData(null, err.message, "500"));131 });132 });133});134/***135 * @author Giulio Serra136 * [Post the beginning of a visit]137 */138exports.postVisitStart = functions139 .region(REGION)140 .https.onRequest((req, res) => {141 cors(req, res, () => {142 const params = qs.parse(req.url);143 const museum_uid = req.url.replace("/", "");144 if (museum_uid === undefined) {145 return res146 .status(400)147 .send(148 packData(149 null,150 "missing uid of the musuem where the visit is taking place",151 400152 )153 );154 }155 const new_visit = new visit.Visit({ museumID: museum_uid });156 return new_visit157 .postBeginningAsync()158 .then((response) => {159 return res.status(200).send(packData(response, "ok", "200"));160 })161 .catch((err) => {162 return res.status(500).send(packData(null, err.message, "500"));163 });164 });165 });166/***167 * @author Giulio Serra168 * [Post the end of a visit]169 */170exports.postVisitEnd = functions.region(REGION).https.onRequest((req, res) => {171 cors(req, res, () => {172 const params = qs.parse(req.url);173 const body = req.body;174 if (system.isObjectEmpty(body)) {175 return res176 .status(400)177 .send(packData(null, "missing body in the request.", 400));178 }179 const new_visit = new visit.Visit(body);180 return new_visit181 .postEndAsync()182 .then((response) => {183 return res.status(200).send(packData(response, "ok", "200"));184 })185 .catch((err) => {186 console.log(err);187 return res.status(500).send(packData(null, err.message, "500"));188 });189 });190});191/***192 * @author Giulio Serra193 * [Post a museum inside the Persistance Storage]194 */195exports.postMuseum = functions.region(REGION).https.onRequest((req, res) => {196 cors(req, res, () => {197 const body = req.body;198 if (system.isObjectEmpty(body)) {199 return res200 .status(400)201 .send(packData(null, "missing body in the request.", 400));202 }203 const jMuseum = system.getJsonFromKeyValueForm(body);204 const newMuseum = new museum.Museum(jMuseum);205 const validation = newMuseum.validate();206 if (validation.code !== "200") {207 return res.status(400).send(packData(null, validation.message, 400));208 }209 return newMuseum210 .postAsync()211 .then((response) => {212 return res.status(200).send(packData(body, "ok", "200"));213 })214 .catch((err) => {215 return res.status(500).send(packData(null, err.message, "500"));216 });217 });218});219/***220 * @author Giulio Serra221 * [Update a museum inside the Persistance Storage]222 */223exports.putMuseum = functions.region(REGION).https.onRequest((req, res) => {224 cors(req, res, () => {225 const body = req.body;226 if (system.isObjectEmpty(body)) {227 return res228 .status(400)229 .send(packData(null, "missing body in the request.", 400));230 }231 const jMuseum = system.getJsonFromKeyValueForm(body);232 const newMuseum = new museum.Museum(jMuseum);233 const validation = newMuseum.validate();234 if (validation.code !== "200") {235 return res.status(400).send(packData(null, validation.message, 400));236 }237 return newMuseum238 .postAsync()239 .then((response) => {240 return res.status(200).send(packData(body, "ok", "200"));241 })242 .catch((err) => {243 return res.status(500).send(packData(null, err.message, "500"));244 });245 })246})247/***248 * @author Giulio Serra249 * [Get a curator from the Persistance Storage]250 */251exports.getCurator = functions.region(REGION).https.onRequest((req, res) => {252 cors(req, res, () => {253 const params = qs.parse(req.url);254 const uid = req.url.replace("/", "");255 if (uid === undefined) {256 return res.status(400).send(packData(null, "missing curator uid", 400));257 }258 return storage259 .getCuratorsGraphAsync(uid)260 .then((response) => {261 return res.status(200).send(packData(response, "ok", "200"));262 })263 .catch((err) => {264 console.log(err);265 return res.status(500).send(packData(null, err.message, "500"));266 });267 });268});269/***270 * @author Giulio Serra271 * [Get a piece from the ID of the sensor attached on it]272 */273exports.getPieceFromSensorID = functions274 .region(REGION)275 .https.onRequest((req, res) => {276 cors(req, res, () => {277 const params = qs.parse(req.url);278 const sensor_uid = params.sensorID;279 const visit = params.visitID;280 if (sensor_uid === undefined) {281 return res282 .status(400)283 .send(packData(null, "missing uid of the sensor", 400));284 }285 return storage286 .getPieceFromSensorID(sensor_uid)287 .then((response) => {288 if (visit !== undefined) {289 const piece = system.getJsonFromKeyValueForm(response);290 const wrapper = {291 pieceID: piece.ID,292 visitID: visit,293 };294 return storage.postTransit(wrapper).then((transit)=>{295 return res.status(200).send(packData(response, "ok", "200"));296 }).catch((err) => {297 console.log(err);298 return res.status(500).send(packData(null, err.message, "500"));299 });300 }else{301 return res.status(200).send(packData(response, "ok", "200"));302 }303 })304 .catch((err) => {305 return res.status(500).send(packData(null, err.message, "500"));306 });307 });308 });309/***310 * @author Giulio Serra311 * [Return the closest museum from a position]312 */313exports.getMuseumFromPosition = functions314 .region(REGION)315 .https.onRequest((req, res) => {316 cors(req, res, () => {317 var params = qs.parse(req.url);318 var position = {319 lat: parseFloat(params.lat),320 lon: parseFloat(params.lon),321 };322 const range = parseFloat(params.range);323 if (params.lat === undefined || params.lon === undefined) {324 return res325 .status(400)326 .send(327 packData(null, "Missing either laititude or longitude.", "400")328 );329 }330 if (range === undefined) {331 return res.status(400).send(packData(null, "Missing range.", "400"));332 }333 const str_lat = new String(params.lat);334 const str_lon = new String(params.lon);335 const lat_flot = parseFloat(str_lat.replace(",", "."));336 const lon_float = parseFloat(str_lon.replace(",", "."));337 console.log(lat_flot + " " + lon_float);338 return storage339 .getMuseumFromDistanceAsync({340 lat: lat_flot,341 lon: lon_float,342 range: range,343 })344 .then((response) => {345 return res.status(200).send(packData(response, "ok", "200"));346 })347 .catch((err) => {348 return res.status(500).send(packData(null, err.message, "500"));349 });350 });351 });352/***353 * @author Giulio Serra354 * [Return a museum from an ID *ONLY FOR DEBUG PURPOSES*]355 */356exports.getMuseumFromID = functions357 .region(REGION)358 .https.onRequest((req, res) => {359 cors(req, res, () => {360 const params = qs.parse(req.url);361 const uid = req.url.replace("/", "");362 if (uid === undefined) {363 return res.status(400).send(packData(null, "missing museum uid", 400));364 }365 return storage366 .getMuseumGrapFromIDAsync(uid)367 .then((response) => {368 return res.status(200).send(packData(response, "ok", "200"));369 })370 .catch((err) => {371 console.log(err);372 return res.status(500).send(packData(null, err.message, "500"));373 });374 });375 });376/***377 * @author Giulio Serra378 * [Delete a piece in the storage]379 */380exports.deletePiece = functions.region(REGION).https.onRequest((req, res) => {381 cors(req, res, () => {382 const params = qs.parse(req.url);383 const uid = req.url.replace("/", "");384 if (uid === undefined) {385 return res.status(400).send(packData(null, "missing piece uid", 400));386 }387 const path = "Piece/" + uid;388 return storage389 .deleteRecord(path)390 .then((response) => {391 return res.status(200).send(packData(response, "ok", "200"));392 })393 .catch((err) => {394 return res.status(500).send(packData(null, err.message, "500"));395 });396 });...
packedit.component.jsx
Source:packedit.component.jsx
1import React, { Component } from "react";2import Form from 'react-bootstrap/Form'3import 'bootstrap/dist/css/bootstrap.min.css';4import Button from 'react-bootstrap/Button';5import './packedit.style.css'6import packageController from "../../services/packageController";7class packedit extends Component {8 constructor(props) {9 super(props);10 this.state = {11 packData : { }12 };13 }14 componentDidMount(){15 const { data } = this.props.location.state || {};16 this.setState({packData : data })17 }18 updateDatabase = () => {19 console.log(this.state.packData, this.state.packData.id);20 packageController.editPackage(this.state.packData, this.state.packData.id);21 }22 render() {23 return (24 <div>25 <h1 style={{ textAlign:"center" }}>Edit Package</h1>26 <Form style={{ width: 700 }} className='form'>27 <Form.Group controlId="formGroupName">28 <Form.Label>Name</Form.Label>29 <Form.Control type="text" 30 placeholder="Edit name" 31 onChange={e => this.setState({packData: {...this.state.packData, name: e.target.value }}) } 32 defaultValue={this.state.packData.name} 33 />34 </Form.Group>35 <Form.Group controlId="formGroupPrice">36 <Form.Label>Price</Form.Label>37 <Form.Control type="text" 38 placeholder="Edit price" 39 onChange={e => this.setState({packData: {...this.state.packData, price: e.target.value }})}40 defaultValue={this.state.packData.price} 41 />42 </Form.Group>43 <Button variant="primary" onClick={()=> {this.updateDatabase()}} >44 Submit45 </Button>46 </Form>47 48 </div>49 );50 }51}...
index.ts
Source:index.ts
1/**2 * 该ä¸é´ä»¶ç¨äºé¨ç½² res 对象ä¸é¢çåå
æ¹æ³ï¼resolve | reject3 **/4import express from 'express';5import { SERVICE_ERROR } from '@middlewares/error-handle/retcode';67export interface Response extends express.Response {8 timer: any;9 packData: PackData;10 resolve(data: PackData): void;11 reject(data: PackData): void;12}1314function pack(packData: PackData): PackData {15 return {16 retCode: packData.retCode,17 retMsg: packData.retMsg,18 retObj: packData.retObj || null19 };20}2122export default (23 req: express.Request,24 res: Response,25 next: express.NextFunction26): void => {27 // æå请æ±28 res.resolve = (packData: PackData): any => {2930 packData.retCode = packData.retCode || 0;31 packData.retMsg = packData.retMsg || 'success';3233 res.packData = packData;3435 // æ¸
é¤è®¡æ¶å¨36 clearTimeout(res.timer);37 if (res.headersSent) return;3839 return res.json(pack(packData));40 };41 // 失败请æ±42 res.reject = (packData: PackData): any => {43 4445 packData.retCode = packData.retCode || SERVICE_ERROR;46 packData.retMsg = packData.retMsg || 'error';4748 res.packData = packData;4950 // æ¸
é¤è®¡æ¶å¨51 clearTimeout(res.timer);52 if (res.headersSent) return;5354 return res.json(pack(packData));55 };56 next();
...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 console.log(data);5 wpt.getTestStatus(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8 wpt.getTestResults(data.data.testId, function(err, data) {9 if (err) return console.error(err);10 console.log(data);11 });12 });13});14{ statusCode: 200,15 data: { testId: '160710_4P_5e5', ownerKey: '5e5' } }16{ statusCode: 200,17 data: { statusCode: 200, statusText: 'Ok' } }18{ statusCode: 200,19 { testId: '160710_4P_5e5',20var wpt = require('webpagetest');21var wpt = new WebPageTest('www.webpagetest.org');22 if (err) return console.error(err);23 console.log(data);24 wpt.getTestStatus(data.data.testId, function(err, data) {25 if (err) return console.error(err);26 console.log(data);27 wpt.getTestResults(data.data.testId, function(err, data) {28 if (err)
Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools('Sachin Tendulkar');3wp.packData(function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10{ title: 'Sachin Tendulkar',11 description: 'Sachin Ramesh Tendulkar (/ˈsʌtʃɪn tɛnˈduːlkər/; born 24 April 1973) is a former Indian cricketer and a former captain of the Indian national team. He is widely regarded as one of the greatest batsmen of all time. He took up cricket at the age of eleven, made his Test debut on 15 November 1989 against Pakistan in Karachi at the age of sixteen, and went on to represent Mumbai domestically and India internationally for close to twenty-four years. Sachin Tendulkar is the highest run scorer of all time in International cricket. He is the only player to have scored one hundred international centuries, the first batsman to score a double century in a One Day International, and the only player to complete more than 30,000 runs in international cricket. He is the only player to have scored a century in both the first and last Test matches of his career, and the only player to score a century on Test debut, a double century, and a triple century. He is the only player to score 30 centuries in ODI cricket. He is the only player to score 100 international centuries, 200 international half-centuries, and 300 international matches. He is the only player to have scored 10,000 runs in both Tests and ODIs. He is the only player to have scored 30,000 runs in first-class cricket. He is the only player to have scored 100 centuries in first-class cricket. He is the only player to have scored 200 fifties in first-class cricket. He is the only player to have scored 200 fifties in Test cricket. He is the only player to have scored 200 fifties in ODIs. He is the only player to have scored 200 fifties in List A cricket
Using AI Code Generation
1var wptpack = require('wptpack');2var data = wptpack.packData("Hello World");3console.log(data);4var wptpack = require('wptpack');5var data = wptpack.packData("Hello World");6console.log(data);7var wptpack = require('wptpack');8var data = wptpack.packData("Hello World");9console.log(data);10var wptpack = require('wptpack');11var data = wptpack.packData("Hello World");12console.log(data);13var wptpack = require('wptpack');14var data = wptpack.packData("Hello World");15console.log(data);16var wptpack = require('wptpack');17var data = wptpack.packData("Hello World");18console.log(data);19var wptpack = require('wptpack');20var data = wptpack.packData("Hello World");21console.log(data);22var wptpack = require('wptpack');23var data = wptpack.packData("Hello World");24console.log(data);25var wptpack = require('wptpack');26var data = wptpack.packData("Hello World");27console.log(data);28var wptpack = require('wptpack');29var data = wptpack.packData("Hello World");30console.log(data);31var wptpack = require('wptpack');32var data = wptpack.packData("Hello World");33console.log(data);
Using AI Code Generation
1var wpt = require('wpt');2var wptData = new wpt();3wptData.packData('test', function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10 at Request._callback (/home/ashish/node_modules/wpt/lib/wpt.js:49:23)11 at self.callback (/home/ashish/node_modules/wpt/node_modules/request/request.js:200:22)12 at Request.EventEmitter.emit (events.js:95:17)13 at Request.onRequestError (/home/ashish/node_modules/wpt/node_modules/request/request.js:845:8)14 at ClientRequest.EventEmitter.emit (events.js:95:17)15 at TLSSocket.socketErrorListener (http.js:1546:9)16 at TLSSocket.EventEmitter.emit (events.js:117:20)17 at process._tickCallback (node.js:419:13)
Using AI Code Generation
1var wptools = require('wptools');2var wp = wptools('Albert Einstein');3wp.packData(function(data){4 console.log(data);5});6var wptools = require('wptools');7var wp = wptools('Albert_Einstein');8wp.packData(function(data){9 console.log(data);10});11var wptools = require('wptools');12var wp = wptools('Albert_Einstein');13wp.packData(function(data){14 console.log(data);15});16var wptools = require('wptools');17var wp = wptools('Albert_Einstein');18wp.packData(function(data){19 console.log(data);20});21var wptools = require('wptools');22var wp = wptools('Albert_Einstein');23wp.packData(function(data){24 console.log(data);25});26var wptools = require('wptools');27var wp = wptools('Albert_Einstein');28wp.packData(function(data){29 console.log(data);30});31var wptools = require('wptools');32var wp = wptools('Albert_Einstein');33wp.packData(function(data){34 console.log(data);35});36var wptools = require('wptools');37var wp = wptools('Albert_Einstein');38wp.packData(function(data){39 console.log(data);40});41var wptools = require('wptools');42var wp = wptools('Albert_Einstein');43wp.packData(function(data){44 console.log(data);45});46var wptools = require('wptools');47var wp = wptools('Albert_Einstein');48wp.packData(function(data){
Using AI Code Generation
1var wptools = require('wptools');2wptools.packData('test.txt', 'test.json', function(err, res) {3 if(err) {4 console.log(err);5 } else {6 console.log(res);7 }8});9{10}11{12}13var wptools = require('wptools');14wptools.unpackData('test.json', 'test.txt', function(err, res) {15 if(err) {16 console.log(err);17 } else {18 console.log(res);19 }20});21{22}23{24}25var wptools = require('wptools');26wptools.getWpUrl('test', function(err, res) {27 if(err) {28 console.log(err);29 } else {30 console.log(res);31 }32});33var wptools = require('wptools');34wptools.getWpUrl('test', function(err, res) {35 if(err) {36 console.log(err);37 } else {38 console.log(res);39 }40});41var wptools = require('wptools');42wptools.getWpUrl('test', function(err, res) {43 if(err) {44 console.log(err);45 } else {46 console.log(res);47 }48});
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!!