Best JavaScript code snippet using stryker-parent
users.js
Source:users.js
...5const NotFoundError = require('../errors/NotFoundError'); // 4046const UnauthorizedError = require('../errors/UnauthorizedError'); // 4017const ValidationError = require('../errors/ValidationError'); // 4008const SECRET_KEY = 'some-secret-key';9function describeErrors(err, res, next) {10 if (err.name === 'ValidationError' || err.name === 'CastError') {11 next(new ValidationError('ÐеÑÐµÐ´Ð°Ð½Ñ Ð½ÐµÐºÐ¾ÑÑекÑнÑе даннÑе'));12 } else {13 next(err);14 }15}16module.exports.login = (req, res, next) => {17 const { email, password } = req.body;18 User.findOne({ email }).select('+password') // вÑзваÑÑ Ð¼ÐµÑод select, Ñак полÑÑаем Ñ
ÑÑ Ð¿Ð°ÑолÑ19 .then((user) => {20 if (!user) { // полÑзоваÑÐµÐ»Ñ Ð½Ðµ найден - оÑклонÑем пÑомиÑ21 throw new UnauthorizedError('ÐепÑавилÑнÑе поÑÑа или паÑолÑ');22 }23 return bcrypt.compare(password, user.password)24 .then((matched) => {25 if (!matched) { // Ñ
ÑÑи не Ñовпали - оÑклонÑем пÑомиÑ26 throw new UnauthorizedError('ÐепÑавилÑнÑе поÑÑа или паÑолÑ');27 }28 return user;29 });30 })31 .then((user) => {32 const token = jwt.sign({ _id: user._id }, SECRET_KEY, { expiresIn: '7d' });33 res.cookie('jwt', token, { httpOnly: true, sameSite: true }).send({ message: 'ÐвÑоÑизаÑÐ¸Ñ Ð¿ÑоÑла ÑÑпеÑно!' });34 })35 .catch((err) => next(err));36};37module.exports.createUser = (req, res, next) => { // ÑоздаÑÑ Ð¿Ð¾Ð»ÑзоваÑелÑ38 const {39 name, about, avatar, email, password,40 } = req.body;41 bcrypt.hash(password, 10) // Ñ
еÑиÑÑем паÑолÑ42 .then((hash) => User.create({43 name, about, avatar, email, password: hash,44 })) // запиÑÑваем даннÑе в базÑ45 .then((user) => res.status(201).send({46 name: user.name,47 about: user.about,48 avatar: user.avatar,49 email: user.email,50 })) // возвÑаÑаем запиÑаннÑе даннÑе в Ð±Ð°Ð·Ñ Ð¿Ð¾Ð»ÑзоваÑелÑ51 .catch((err) => {52 if (err.code === 11000) { // еÑли полÑзоваÑÐµÐ»Ñ ÑегиÑÑÑиÑÑеÑÑÑ Ð¿Ð¾ ÑÑÑеÑÑвÑÑÑÐµÐ¼Ñ Ð² базе email53 next(new ConflictError('ÐаннÑй email Ñже ÑÑÑеÑÑвÑеÑ'));54 } else {55 describeErrors(err, res, next);56 }57 });58};59module.exports.getUsers = (req, res, next) => {60 User.find({}) // поиÑк вÑеÑ
докÑменÑов по паÑамеÑÑам61 .then((users) => res.status(200).send(users))62 .catch((err) => next(err));63};64module.exports.getInfoAboutMe = (req, res, next) => {65 User.findById(req.user._id) // наÑ
Ð¾Ð´Ð¸Ñ ÑекÑÑего полÑзоваÑÐµÐ»Ñ Ð¿Ð¾ _id66 .then((user) => {67 if (!user) {68 throw new NotFoundError('ÐолÑзоваÑÐµÐ»Ñ Ð¿Ð¾ ÑÐºÐ°Ð·Ð°Ð½Ð½Ð¾Ð¼Ñ Id не найден');69 } else {70 res.status(200).send(user);71 }72 })73 .catch((err) => describeErrors(err, res, next));74};75module.exports.getUserById = (req, res, next) => {76 User.findById(req.params.userId) // возвÑаÑÐ°ÐµÑ Ð¿ÐµÑвого полÑзоваÑÐµÐ»Ñ Ð¸Ð· Ð±Ð°Ð·Ñ Ð¿Ð¾ _id77 .then((user) => {78 if (!user) {79 throw new NotFoundError('ÐолÑзоваÑÐµÐ»Ñ Ð¿Ð¾ ÑÐºÐ°Ð·Ð°Ð½Ð½Ð¾Ð¼Ñ Id не найден');80 } else {81 res.status(200).send(user);82 }83 })84 .catch((err) => describeErrors(err, res, next));85};86module.exports.updateProfile = (req, res, next) => {87 const { name, about } = req.body;88 User.findByIdAndUpdate(89 req.user._id,90 { name, about },91 {92 new: true,93 runValidators: true,94 },95 )96 .then((user) => res.status(200).send(user))97 .catch((err) => describeErrors(err, res, next));98};99module.exports.updateAvatar = (req, res, next) => {100 const { avatar } = req.body;101 User.findByIdAndUpdate(102 req.user._id,103 { avatar },104 {105 new: true,106 runValidators: true,107 },108 )109 .then((user) => res.status(200).send(user))110 .catch((err) => describeErrors(err, res, next));...
cards.js
Source:cards.js
1const Cards = require('../models/card');2const NotFoundError = require('../errors/NotFoundError');3const ForbiddenError = require('../errors/ForbiddenError');4const ValidationError = require('../errors/ValidationError');5function describeErrors(err, res, next) {6 if (err.name === 'ValidationError' || err.name === 'CastError') {7 next(new ValidationError('ÐеÑÐµÐ´Ð°Ð½Ñ Ð½ÐµÐºÐ¾ÑÑекÑнÑе даннÑе'));8 } else {9 next(err);10 }11}12function addError(req, res, card) {13 if (!card) {14 throw new NotFoundError('ÐаÑÑоÑка Ñ ÑказаннÑм _id не найдена');15 } else {16 res.status(200).send(card);17 }18}19module.exports.createCard = (req, res, next) => {20 const { name, link } = req.body;21 const id = req.user._id; // id полÑзоваÑÐµÐ»Ñ Ð²Ð·Ñли из мидлвÑÑÑ Ð² Ñайле app.js22 Cards.create({ name, link, owner: id })23 .then((card) => res.status(201).send({ card }))24 .catch((err) => describeErrors(err, res, next));25};26module.exports.getCards = (req, res, next) => {27 Cards.find({}) // поиÑк вÑеÑ
докÑменÑов по паÑамеÑÑам28 .then((cards) => res.status(200).send(cards))29 .catch((err) => describeErrors(err, res, next));30};31module.exports.deleteCard = (req, res, next) => {32 Cards.findById(req.params.cardId) // Ñдаление каÑÑоÑки по Id33 .then((card) => {34 if (!card) {35 throw new NotFoundError('ÐаÑÑоÑка Ñ ÑказаннÑм _id не найдена');36 }37 if (req.user._id !== card.owner.toString()) { // Ð½ÐµÑ Ð¿Ñав ÑдалÑÑÑ ÐºÑÑоÑÐºÑ Ð´ÑÑгого полÑзоваÑелÑ38 throw new ForbiddenError('ÐÑ Ð½Ðµ можеÑе ÑдалиÑÑ ÑÑÑ ÐºÐ°ÑÑоÑкÑ');39 }40 Cards.findByIdAndRemove(req.params.cardId)41 .then(() => res.status(200).send({ message: 'ÐаÑÑоÑка Ñдалена' }))42 .catch(next);43 })44 .catch((err) => describeErrors(err, res, next));45};46module.exports.likeCard = (req, res, next) => {47 Cards.findByIdAndUpdate(48 (req.params.cardId),49 { $addToSet: { likes: req.user._id } }, // добавиÑÑ _id в маÑÑив, еÑли его Ñам неÑ50 {51 new: true,52 },53 )54 .then((card) => {55 addError(req, res, card);56 })57 .catch((err) => describeErrors(err, res, next));58};59module.exports.dislikeCard = (req, res, next) => {60 Cards.findByIdAndUpdate( // ÑÐ´Ð°Ð»Ð¸Ñ Ð¿ÐµÑвое Ñовпадение по id61 (req.params.cardId),62 { $pull: { likes: req.user._id } }, // ÑбÑаÑÑ _id из маÑÑива63 {64 new: true,65 },66 )67 .then((card) => {68 addError(req, res, card);69 })70 .catch((err) => describeErrors(err, res, next));...
fancy_forms.js
Source:fancy_forms.js
...35 });36 it('describes 0 errors', function () {37 $scope.val = 'someting';38 $scope.$apply();39 expect(ngForm.describeErrors()).to.be('0 Errors');40 });41 it('describes 0 error when the model is invalid but untouched', function () {42 $scope.$apply();43 expect(ngForm.describeErrors()).to.be('0 Errors');44 });45 it('describes 1 error when the model is touched', function () {46 $el.find('input').blur();47 $scope.$apply();48 expect(ngForm.describeErrors()).to.be('1 Error');49 });50 });...
Using AI Code Generation
1const { describeErrors } = require('stryker-parent');2describeErrors(() => {3 console.log('describeErrors');4});5const { describeErrors } = require('stryker-parent');6describeErrors(() => {7 console.log('describeErrors');8});9const { describeErrors } = require('stryker-parent');10describeErrors(() => {11 console.log('describeErrors');12});13const { describeErrors } = require('stryker-parent');14describeErrors(() => {15 console.log('describeErrors');16});17const { describeErrors } = require('stryker-parent');18describeErrors(() => {19 console.log('describeErrors');20});21const { describeErrors } = require('stryker-parent');22describeErrors(() => {23 console.log('describeErrors');24});25const { describeErrors } = require('stryker-parent');26describeErrors(() => {27 console.log('describeErrors');28});29const { describeErrors } = require('stryker-parent');30describeErrors(() => {31 console.log('describeErrors');32});33const { describeErrors } = require('stryker-parent');34describeErrors(() => {35 console.log('describeErrors');36});37const { describeErrors } = require('stryker-parent');38describeErrors(() => {39 console.log('describeErrors');40});41const { describeErrors } = require('stryker-parent');42describeErrors(() => {43 console.log('describeErrors');44});45const { describeErrors
Using AI Code Generation
1const { describeErrors } = require('stryker-parent');2describeErrors();3module.exports = require('stryker-parent').describeErrors;4import { describeErrors } from 'stryker-parent';5export { describeErrors };6import { describeErrors } from 'stryker-parent';7export { describeErrors };8const { describeErrors } = require('stryker-parent');9module.exports = { describeErrors };10const { describeErrors } = require('stryker-parent');11module.exports = { describeErrors };12const { describeErrors } = require('stryker-parent');13module.exports = { describeErrors };14const { describeErrors } = require('stryker-parent');15module.exports = { describeErrors };16const { describeErrors } = require('stryker-parent');17module.exports = { describeErrors };18const { describeErrors } = require('stryker-parent');19module.exports = { describeErrors };20const { describeErrors } = require('stryker-parent');21module.exports = { describeErrors };22const { describeErrors } = require('stryker-parent');23module.exports = { describeErrors };24const { describeErrors } = require('stryker-parent');25module.exports = { describeErrors };26const { describeErrors } = require('stry
Using AI Code Generation
1var strykerParent = require('stryker-parent');2var errors = strykerParent.describeErrors(['SOME_ERROR']);3console.log(errors.SOME_ERROR);4var strykerApi = require('stryker-api');5var errors = strykerApi.describeErrors(['SOME_ERROR']);6console.log(errors.SOME_ERROR);
Using AI Code Generation
1var describeErrors = require('stryker-parent').describeErrors;2describeErrors('my error', function() {3});4var describeErrors = require('stryker-parent').describeErrors;5describeErrors('my error', function() {6});7var describeErrors = require('stryker-parent').describeErrors;8describeErrors('my error', function() {9});10var describeErrors = require('stryker-parent').describeErrors;11describeErrors('my error', function() {12});13var describeErrors = require('stryker-parent').describeErrors;14describeErrors('my error', function() {15});16var describeErrors = require('stryker-parent').describeErrors;17describeErrors('my error', function() {18});19var describeErrors = require('stryker-parent').describeErrors;20describeErrors('my error', function() {21});22var describeErrors = require('stryker-parent').describeErrors;23describeErrors('my error', function() {24});25var describeErrors = require('stryker-parent').describeErrors;26describeErrors('my error', function() {27});28var describeErrors = require('stryker-parent').describeErrors;29describeErrors('my error', function() {30});
Using AI Code Generation
1describeErrors(1, 2, 3)2function describeErrors(...errors) {3 console.log(errors)4}5cloneArray([1, 2, 3])6function cloneArray(array) {7 console.log(clone)8}9combineArrays([1, 2, 3], [4, 5, 6], [7, 8, 9])10function combineArrays(...arrays) {11 console.log(combined)12}13combineObjects({ a: 1, b: 2, c: 3 }, { d: 4, e: 5, f: 6 }, { g: 7, h: 8, i: 9 })14function combineObjects(...objects) {15 const combined = { ...objects }16 console.log(combined)17}18combineObjects({ a: 1, b: 2, c: 3 }, { d: 4, e: 5, f: 6 }, { g: 7, h: 8, i: 9 })
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!!