Best JavaScript code snippet using wpt
app.js
Source:app.js
1"use strict";2const Homey = require('homey');3let nodemailer = require('nodemailer');4let mail_user, mail_pass, mail_host, mail_port, mail_from, mail_secure, use_credentials;5class EmailApp extends Homey.App {6 7 onInit() {8 9 //mail settings (if any)10 use_credentials = Homey.ManagerSettings.get('use_credentials');11 12 //backwards compatibility13 if (typeof use_credentials == undefined || typeof use_credentials == 'undefined') {14 use_credentials = true;15 Homey.manager('settings').set( 'use_credentials', true);16 }17 18 mail_user = Homey.ManagerSettings.get('mail_user');19 mail_pass = Homey.ManagerSettings.get('mail_password');20 mail_host = Homey.ManagerSettings.get('mail_host');21 mail_port = Homey.ManagerSettings.get('mail_port');22 mail_from = Homey.ManagerSettings.get('mail_from');23 mail_secure = Homey.ManagerSettings.get('mail_secure');24 25 this.log('Backend settings updated');26 27 let sendMessage = new Homey.FlowCardAction('sendmail');28 sendMessage29 .register()30 .registerRunListener(( args, state ) => {31 32 if ( typeof mail_user !== 'undefined' && typeof mail_pass !== 'undefined' && typeof mail_host !== 'undefined' && typeof mail_port !== 'undefined' && typeof mail_from !== 'undefined') {33 if (typeof use_credentials == undefined) use_credentials = true;34 35 if (use_credentials) {36 var transporter = nodemailer.createTransport(37 {38 host: mail_host,39 port: mail_port,40 secure: mail_secure,41 auth: {42 user: mail_user,43 pass: mail_pass44 },45 tls: {rejectUnauthorized: false}46 });47 } else {48 // Don't use authentication. Not supported by all providers49 var transporter = nodemailer.createTransport(50 {51 host: mail_host,52 port: mail_port,53 secure: mail_secure,54 tls: {rejectUnauthorized: false}55 });56 }57 58 var mailOptions = {59 60 from: 'Homey <' + mail_from + '>',61 to: args.mailto,62 subject: args.subject,63 text: args.body,64 html: args.body65 }66 67 transporter.sendMail(mailOptions, function(error, info){68 if(error){69 //return this.error(error);70 console.log ("Error: " + error);71 return Promise.resolve (false);72 73 }74 console.log('Message sent: ' + info.response);75 return Promise.resolve (true);76 77 });78 79 } else {80 81 this.log('Not all required variables for mailing have been set');82 83 return callback ('Not all required variables for mailing have been set', false);84 85 }86 87 })88 89 90 let sendascii = new Homey.FlowCardAction('sendascii');91 sendascii92 .register()93 .registerRunListener(( args, state ) => {94 95 if ( typeof mail_user !== 'undefined' && typeof mail_pass !== 'undefined' && typeof mail_host !== 'undefined' && typeof mail_port !== 'undefined' && typeof mail_from !== 'undefined') {96 if (typeof use_credentials == undefined) use_credentials = true;97 98 if (use_credentials) {99 var transporter = nodemailer.createTransport(100 {101 host: mail_host,102 port: mail_port,103 secure: mail_secure,104 auth: {105 user: mail_user,106 pass: mail_pass107 },108 tls: {rejectUnauthorized: false}109 });110 } else {111 // Don't use authentication. Not supported by all providers112 var transporter = nodemailer.createTransport(113 {114 host: mail_host,115 port: mail_port,116 secure: mail_secure,117 tls: {rejectUnauthorized: false}118 });119 }120 121 var mailOptions = {122 123 from: 'Homey <' + mail_from + '>',124 to: args.mailto,125 subject: args.subject,126 text: args.body127 }128 129 transporter.sendMail(mailOptions, function(error, info){130 if(error){131 132 console.log(error);133 134 return Promise.resolve (false);135 }136 console.log('Message sent: ' + info.response);137 return Promise.resolve (true);138 139 });140 141 } else {142 143 this.log('Not all required variables for mailing have been set');144 145 callback ('Not all required variables for mailing have been set', false);146 147 }148 149 })150 151 let sendimage = new Homey.FlowCardAction('sendimage');152 sendimage153 .register()154 .registerRunListener(( args, state ) => {155 156 if ( typeof mail_user !== 'undefined' && typeof mail_pass !== 'undefined' && typeof mail_host !== 'undefined' && typeof mail_port !== 'undefined' && typeof mail_from !== 'undefined') {157 if (typeof use_credentials == undefined) use_credentials = true;158 159 if (use_credentials) {160 var transporter = nodemailer.createTransport(161 {162 host: mail_host,163 port: mail_port,164 secure: mail_secure,165 auth: {166 user: mail_user,167 pass: mail_pass168 },169 tls: {rejectUnauthorized: false}170 });171 } else {172 // Don't use authentication. Not supported by all providers173 var transporter = nodemailer.createTransport(174 {175 host: mail_host,176 port: mail_port,177 secure: mail_secure,178 tls: {rejectUnauthorized: false}179 });180 }181 182 let image = args.droptoken;183 184 if (image.getStream) {185 186 //image.getStream()187 //.then( buf => {188 189 190 //const stream = await image.getStream();191 192 if (image.contentType == "jpg") {193 194 var filename = "x.jpg";195 196 } else if (image.contentType == "gif") {197 198 var filename = "x.gif";199 200 } else if (image.contentType == "png") {201 202 var filename = "x.png";203 204 }205 206 var mailOptions = {207 208 from: 'Homey <' + mail_from + '>',209 to: args.mailto,210 subject: args.subject,211 //text: body,212 //html: body213 attachments:[{214 filename: image.filename,215 content: image.contentType216 }]217 }218 219 transporter.sendMail(mailOptions, function(error, info){220 if(error){221 222 console.log(error);223 224 return Promise.resolve (false);225 226 }227 console.log('Message sent: ' + info.response);228 return Promise.resolve (true);229 230 });231 232 /*233 })234 .catch ( err => {235 console.error (err); 236 });237 */238 239 } else {240 //Pre Homey 2.2.0241 242 image.getBuffer()243 .then( buf => {244 245 if (image.getFormat() == "jpg") {246 247 var filename = "x.jpg";248 249 } else if (image.getFormat() == "gif") {250 251 var filename = "x.gif";252 253 } else if (image.getFormat() == "png") {254 255 var filename = "x.png";256 257 }258 259 var mailOptions = {260 261 from: 'Homey <' + mail_from + '>',262 to: args.mailto,263 subject: args.subject,264 //text: body,265 //html: body266 attachments:[{267 filename: filename,268 content: buf269 }]270 }271 272 transporter.sendMail(mailOptions, function(error, info){273 if(error){274 275 console.log(error);276 277 return Promise.resolve (false);278 279 }280 console.log('Message sent: ' + info.response);281 return Promise.resolve (true);282 283 });284 285 })286 .catch ( err => {287 console.error (err); 288 });289 290 }291 292 } else {293 294 this.log('Not all required variables for mailing have been set');295 296 callback ('Not all required variables for mailing have been set', false);297 298 }299 300 })301 302 }303}...
validation-schema.js
Source:validation-schema.js
1// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14import * as Yup from 'yup'15import sharedMessages from '@ttn-lw/lib/shared-messages'16import {17 id as idRegexp,18 address as addressRegexp,19 mqttUrl as mqttUrlRegexp,20 mqttPassword as mqttPasswordRegexp,21 noSpaces as noSpacesRegexp,22} from '@console/lib/regexp'23import { qosLevels } from './qos-options'24export default Yup.object().shape({25 pub_sub_id: Yup.string()26 .matches(idRegexp, sharedMessages.validateIdFormat)27 .min(2, Yup.passValues(sharedMessages.validateTooShort))28 .max(25, Yup.passValues(sharedMessages.validateTooLong))29 .required(sharedMessages.validateRequired),30 format: Yup.string().required(sharedMessages.validateRequired),31 base_topic: Yup.string().required(sharedMessages.validateRequired),32 nats: Yup.object().when('_provider', {33 is: 'nats',34 then: Yup.object().shape({35 use_credentials: Yup.boolean(),36 username: Yup.string().when('use_credentials', {37 is: true,38 then: Yup.string()39 .matches(idRegexp, sharedMessages.validateIdFormat)40 .min(2, Yup.passValues(sharedMessages.validateTooShort))41 .max(100, Yup.passValues(sharedMessages.validateTooLong))42 .required(sharedMessages.validateRequired),43 otherwise: Yup.string().strip(),44 }),45 password: Yup.string().when('use_credentials', {46 is: true,47 then: Yup.string()48 .min(2, Yup.passValues(sharedMessages.validateTooShort))49 .max(100, Yup.passValues(sharedMessages.validateTooLong))50 .required(sharedMessages.validateRequired),51 otherwise: Yup.string().strip(),52 }),53 address: Yup.string()54 .matches(addressRegexp, sharedMessages.validateAddressFormat)55 .required(sharedMessages.validateRequired),56 port: Yup.number()57 .integer(sharedMessages.validateInt32)58 .positive(sharedMessages.validateInt32)59 .required(sharedMessages.validateRequired),60 secure: Yup.boolean(),61 }),62 otherwise: Yup.object().strip(),63 }),64 mqtt: Yup.object().when('_provider', {65 is: 'mqtt',66 then: Yup.object().shape({67 use_credentials: Yup.boolean(),68 server_url: Yup.string()69 .matches(mqttUrlRegexp, sharedMessages.validateUrl)70 .required(sharedMessages.validateRequired),71 client_id: Yup.string()72 .matches(noSpacesRegexp, sharedMessages.validateNoSpaces)73 .min(2, Yup.passValues(sharedMessages.validateTooShort))74 .max(23, Yup.passValues(sharedMessages.validateTooLong))75 .required(sharedMessages.validateRequired),76 username: Yup.string().when('use_credentials', {77 is: true,78 then: Yup.string()79 .matches(noSpacesRegexp, sharedMessages.validateNoSpaces)80 .min(2, Yup.passValues(sharedMessages.validateTooShort))81 .max(100, Yup.passValues(sharedMessages.validateTooLong))82 .required(sharedMessages.validateRequired),83 otherwise: Yup.string().strip(),84 }),85 password: Yup.string().when('use_credentials', {86 is: true,87 then: Yup.string().matches(mqttPasswordRegexp, sharedMessages.validateMqttPassword),88 otherwise: Yup.string().strip(),89 }),90 subscribe_qos: Yup.string()91 .oneOf(qosLevels, sharedMessages.validateRequired)92 .required(sharedMessages.validateRequired),93 publish_qos: Yup.string()94 .oneOf(qosLevels, sharedMessages.validateRequired)95 .required(sharedMessages.validateRequired),96 use_tls: Yup.boolean(),97 tls_ca: Yup.string().when('use_tls', {98 is: true,99 then: Yup.string().required(sharedMessages.validateRequired),100 otherwise: Yup.string().strip(),101 }),102 tls_client_cert: Yup.string().when('use_tls', {103 is: true,104 then: Yup.string().required(sharedMessages.validateRequired),105 otherwise: Yup.string().strip(),106 }),107 tls_client_key: Yup.string().when('use_tls', {108 is: true,109 then: Yup.string().required(sharedMessages.validateRequired),110 otherwise: Yup.string().strip(),111 }),112 }),113 otherwise: Yup.object().strip(),114 }),...
api.js
Source:api.js
1const Homey = require('homey');2module.exports = [3 {4 description: 'Test email',5 method: 'PUT',6 path: '/testmail',7 fn: function(args, callback){8 9 var nodemailer = require('nodemailer');10 11 //console.log('POST = ' + JSON.stringify(args));12 13 var use_credentials = args.body.use_credentials;14 if (typeof use_credentials == undefined) use_credentials = true;15 16 //console.log ('use_credentials=' + use_credentials);17 if (use_credentials) {18 var transporter = nodemailer.createTransport(19 {20 host: args.body.mail_host,21 port: args.body.mail_port,22 secure: args.body.mail_secure,23 auth: {24 user: args.body.mail_user,25 pass: args.body.mail_password26 },27 tls: {rejectUnauthorized: false} 28 });29 } else {30 // Don't use authentication. Not supported by all providers31 var transporter = nodemailer.createTransport(32 {33 host: args.body.mail_host,34 port: args.body.mail_port,35 secure: args.body.mail_secure,36 tls: {rejectUnauthorized: false} 37 });38 }39 40 var mailOptions = {41 42 from: 'Homey <' + args.body.mail_from + '>',43 to: args.body.mail_from,44 subject: 'Testmail',45 text: 'This is a testmail',46 html: 'This is a testmail'47 }48 49 transporter.sendMail(mailOptions, function(error, info){50 if(error){51 52 console.error('error = ' + error);53 54 return callback (false, 'Error: ' + error);55 }56 57 console.log('Message sent: ' + info.response);58 return callback (false, 'Message sent: ' + info.response);59 });60 61 }62 },63 /* Required GET input: mailto (e-mail address), subject, body (message) */64 {65 66 description: 'API Send Email',67 method: 'GET',68 path: '/sendmail',69 fn: function(args, callback){70 71 var nodemailer = require('nodemailer');72 73 mail_user = Homey.ManagerSettings.get('mail_user');74 mail_pass = Homey.ManagerSettings.get('mail_password');75 mail_host = Homey.ManagerSettings.get('mail_host');76 mail_port = Homey.ManagerSettings.get('mail_port');77 mail_from = Homey.ManagerSettings.get('mail_from');78 mail_secure = Homey.ManagerSettings.get('mail_secure');79 80 if ( typeof mail_user !== 'undefined' && typeof mail_pass !== 'undefined' && typeof mail_host !== 'undefined' && typeof mail_port !== 'undefined' && typeof mail_from !== 'undefined') {81 var use_credentials = args.query.use_credentials;82 if (typeof use_credentials == undefined) use_credentials = true;83 84 if (use_credentials) {85 var transporter = nodemailer.createTransport(86 {87 host: mail_host,88 port: mail_port,89 secure: mail_secure,90 auth: {91 user: mail_user,92 pass: mail_pass93 },94 tls: {rejectUnauthorized: false}95 });96 } else {97 // Don't use authentication. Not supported by all providers98 var transporter = nodemailer.createTransport(99 {100 host: mail_host,101 port: mail_port,102 secure: mail_secure,103 tls: {rejectUnauthorized: false}104 });105 }106 107 var mailOptions = {108 109 from: 'Homey <' + mail_from + '>',110 to: args.query.mailto,111 subject: args.query.subject,112 text: args.query.body,113 html: args.query.body114 }115 116 transporter.sendMail(mailOptions, function(error, info){117 if(error){118 return Promise.resolve(false);119 }120 console.log('Message sent: ' + info.response);121 return Promise.resolve (true);122 123 });124 125 } else {126 127 this.log('Not all required variables for mailing have been set');128 129 return callback ('Not all required variables for mailing have been set', false);130 131 }132 133 }134 135 }...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.use_credentials('username', 'password');4var wpt = require('webpagetest');5var wpt = new WebPageTest('www.webpagetest.org');6 console.log(data);7});8var wpt = require('webpagetest');9var wpt = new WebPageTest('www.webpagetest.org');10 console.log(data);11});12var wpt = require('webpagetest');13var wpt = new WebPageTest('www.webpagetest.org');14 console.log(data);15});16var wpt = require('webpagetest');17var wpt = new WebPageTest('www.webpagetest.org');18 console.log(data);19});20var wpt = require('webpagetest');21var wpt = new WebPageTest('www.webpagetest.org');22 console.log(data);23});24var wpt = require('webpagetest');25var wpt = new WebPageTest('www.webpagetest.org');
Using AI Code Generation
1var wpt = require('./wpt.js');2var wptConfig = require('./wptConfig.js');3var credentials = {4};5var wptTest = new wpt(credentials);6wptTest.use_credentials(credentials);7wptTest.runTest(wptConfig.testUrl, wptConfig, function (err, data) {8 if (err) {9 console.log(err);10 } else {11 console.log(data);12 }13});
Using AI Code Generation
1var wptools = require("../wptools.js");2var wiki = new wptools("Albert Einstein");3wiki.use_credentials("user", "password", "en.wikipedia.org");4wiki.get_page(function (err, resp) {5 if (err) {6 console.log("Error: " + err);7 } else {8 console.log("Page: " + resp);9 }10});11var wptools = require("../wptools.js");12var wiki = new wptools("Albert Einstein");13wiki.get_page(function (err, resp) {14 if (err) {15 console.log("Error: " + err);16 } else {17 console.log("Page: " + resp);18 }19});20var wptools = require("../wptools.js");21var wiki = new wptools("Albert Einstein");22wiki.get_wikitext(function (err, resp) {23 if (err) {24 console.log("Error: " + err);25 } else {26 console.log("Wikitext: " + resp);27 }28});29var wptools = require("../wptools.js");30var wiki = new wptools("Albert Einstein");31wiki.get_wikidata(function (err, resp) {32 if (err) {33 console.log("Error: " + err);34 } else {35 console.log("Wikidata: " + resp);36 }37});38var wptools = require("../wptools.js");39var wiki = new wptools("Albert Einstein");
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!!