Best JavaScript code snippet using wpt
actions.ts
Source:actions.ts
...12} from './types';13// add article14export const addArticle =15 (body: IArticleCreateProps) => async (dispatch: Dispatch<ArticleAction>) => {16 const { addArticleOptions } = createFetchOptions();17 const { getGlobalArticlesOptions } = createFetchOptions();18 await fetchAction(URLS.ADD_ARTICLE_URL, addArticleOptions(body));19 const { data, error } = await fetchAction(20 URLS.GET_GLOBAL_ARTICLES_URL,21 getGlobalArticlesOptions()22 );23 if (error) {24 throw new Error("Can't add article: " + error);25 }26 dispatch({27 type: ArticleActionTypes.ADD_ARTICLE,28 payload: { articles: data, formFetchMode: FETCH_MODE.FETCHED },29 });30 };31// update article32export const updateArticle =33 (body: IArticleCreateProps, slug: string) => async (dispatch: Dispatch<ArticleAction>) => {34 const { updateArticleOptions } = createFetchOptions();35 const { getGlobalArticlesOptions } = createFetchOptions();36 const url = URLS.UPDATE_ARTICLE + slug;37 await fetchAction(url, updateArticleOptions(body));38 const { data, error } = await fetchAction(39 URLS.GET_GLOBAL_ARTICLES_URL,40 getGlobalArticlesOptions()41 );42 if (error) {43 throw new Error("Can't update article: " + error);44 }45 dispatch({46 type: ArticleActionTypes.UPDATE_ARTICLE,47 payload: { articles: data, formFetchMode: FETCH_MODE.FETCHED },48 });49 };50// remove article51export const removeArticle = (slug: string) => async (dispatch: Dispatch<ArticleAction>) => {52 const { removeArticleOptions } = createFetchOptions();53 const { getGlobalArticlesOptions } = createFetchOptions();54 const url = URLS.REMOVE_ARTICLE_URL + slug;55 await fetchAction(url, removeArticleOptions());56 const { data, error } = await fetchAction(57 URLS.GET_GLOBAL_ARTICLES_URL,58 getGlobalArticlesOptions()59 );60 if (error) {61 throw new Error("Can't update article: " + error);62 }63 {64 dispatch({65 type: ArticleActionTypes.REMOVE_ARTICLE,66 payload: {67 articles: data,68 buttonFetchMode: FETCH_MODE.FETCHED,69 formFetchMode: FETCH_MODE.FETCHED,70 },71 });72 }73};74// set article for edition75export const setEditArticle = (article: IArticle) => (dispatch: Dispatch<ArticleAction>) => {76 dispatch({77 type: ArticleActionTypes.SET_EDIT_ARTICLE,78 payload: { editArticle: article },79 });80};81// set mode of editor82export const setEditorMode = (editorMode: EDITOR_MODE) => (dispatch: Dispatch<ArticleAction>) => {83 dispatch({84 type: ArticleActionTypes.SET_EDITOR_MODE,85 payload: { editorMode },86 });87};88// get user articles89export const getUserArticles = (author: string) => async (dispatch: Dispatch<ArticleAction>) => {90 const { getUserArticlesOptions } = createFetchOptions();91 const url = URLS.GET_USER_ARTICLES_URL + author;92 const { data, error } = await fetchAction(url, getUserArticlesOptions());93 if (error) {94 throw new Error("Can't get user articles: " + error);95 }96 dispatch({97 type: ArticleActionTypes.GET_USER_ARTICLES,98 payload: { articles: data, articleFetchMode: FETCH_MODE.FETCHED },99 });100};101// get feed articles102export const getFeedArticles = () => async (dispatch: Dispatch<ArticleAction>) => {103 const { getFeedArticlesOptions } = createFetchOptions();104 const url = URLS.GET_FEED_ARTICLES_URL;105 const { data, error } = await fetchAction(url, getFeedArticlesOptions());106 if (error) {107 throw new Error("Can't get feed articles: " + error);108 }109 dispatch({110 type: ArticleActionTypes.GET_FEED_ARTICLES,111 payload: { feedArticles: data, articleFetchMode: FETCH_MODE.FETCHED },112 });113};114// get global articles115export const getGlobalArticles = () => async (dispatch: Dispatch<ArticleAction>) => {116 const { getGlobalArticlesOptions } = createFetchOptions();117 const { data, error } = await fetchAction(118 URLS.GET_GLOBAL_ARTICLES_URL,119 getGlobalArticlesOptions()120 );121 if (error) {122 throw new Error("Can't get articles: " + error);123 }124 dispatch({125 type: ArticleActionTypes.GET_GLOBAL_ARTICLES,126 payload: { articles: data, articleFetchMode: FETCH_MODE.FETCHED },127 });128};129// get tags130export const getTags = () => async (dispatch: Dispatch<ArticleAction>) => {131 const { getTagsOptions } = createFetchOptions();132 const { data, error } = await fetchAction(URLS.GET_TAGS_URL, getTagsOptions());133 if (error) {134 throw new Error("Can't get articles: " + error);135 }136 dispatch({ type: ArticleActionTypes.GET_TAGS, payload: { tags: data } });137};138// set fetch mode for forms139export const setFormFetchMode = (mode: FETCH_MODE) => (dispatch: Dispatch<ArticleAction>) => {140 dispatch({141 type: ArticleActionTypes.SET_FORM_FETCH_MODE,142 payload: { formFetchMode: mode },143 });144};145// set fetch mode for buttons146export const setButtonFetchMode = (mode: FETCH_MODE) => (dispatch: Dispatch<ArticleAction>) => {147 dispatch({148 type: ArticleActionTypes.SET_BUTTON_FETCH_MODE,149 payload: { buttonFetchMode: mode },150 });151};152// set fetch mode for list of articles153export const setArticleFetchMode = (mode: FETCH_MODE) => (dispatch: Dispatch<ArticleAction>) => {154 dispatch({155 type: ArticleActionTypes.SET_ARTICLE_FETCH_MODE,156 payload: { articleFetchMode: mode },157 });158};159// favorite article160export const favoriteArticle = (slug: string) => async (dispatch: Dispatch<ArticleAction>) => {161 const { getGlobalArticlesOptions, favoriteArticleOptions } = createFetchOptions();162 const url = URLS.FAVORITE_ARTICLE_URL + slug + '/favorite/';163 await fetchAction(url, favoriteArticleOptions());164 const { data, error } = await fetchAction(165 URLS.GET_GLOBAL_ARTICLES_URL,166 getGlobalArticlesOptions()167 );168 if (error) {169 throw new Error("Can't unfavorite article: " + error);170 }171 dispatch({172 type: ArticleActionTypes.FAVORITE_ARTICLE,173 payload: { articles: data, buttonFetchMode: FETCH_MODE.FETCHED },174 });175};176// unfavorite article177export const unfavoriteArticle = (slug: string) => async (dispatch: Dispatch<ArticleAction>) => {178 const { getGlobalArticlesOptions, unfavoriteArticleOptions } = createFetchOptions();179 const url = URLS.UNFAVORITE_ARTICLE_URL + slug + '/favorite/';180 await fetchAction(url, unfavoriteArticleOptions());181 const { data, error } = await fetchAction(182 URLS.GET_GLOBAL_ARTICLES_URL,183 getGlobalArticlesOptions()184 );185 if (error) {186 throw new Error("Can't favorite article: " + error);187 }188 dispatch({189 type: ArticleActionTypes.UNFAVORITE_ARTICLE,190 payload: { articles: data, buttonFetchMode: FETCH_MODE.FETCHED },191 });192};193// add comment to an article194export const addComment =195 (slug: string, body: string) => async (dispatch: Dispatch<ArticleAction>) => {196 const { addCommentOptions, getCommentsOptions } = createFetchOptions();197 const url = URLS.ADD_COMMENT_URL + slug + '/comments';198 await fetchAction(url, addCommentOptions(body));199 const { data, error } = await fetchAction(url, getCommentsOptions());200 if (error) {201 throw new Error("Can't add comment: " + error);202 }203 dispatch({204 type: ArticleActionTypes.ADD_COMMENT,205 payload: { comments: data, formFetchMode: FETCH_MODE.FETCHED },206 });207 };208// delete comment209export const deleteComment =210 (slug: string, id: string) => async (dispatch: Dispatch<ArticleAction>) => {211 const { deleteCommentOptions, getCommentsOptions } = createFetchOptions();212 const url = URLS.DELETE_COMMENT_URL + slug + '/comments/' + id;213 await fetchAction(url, deleteCommentOptions());214 const getUrl = URLS.GET_COMMENTS_URL + slug + '/comments';215 const { data, error } = await fetchAction(getUrl, getCommentsOptions());216 if (error) {217 throw new Error("Can't delete comment: " + error);218 }219 dispatch({220 type: ArticleActionTypes.DELETE_COMMENT,221 payload: { comments: data, buttonFetchMode: FETCH_MODE.FETCHED },222 });223 };224// get comments from an article225export const getComments = (slug: string) => async (dispatch: Dispatch<ArticleAction>) => {226 const { getCommentsOptions } = createFetchOptions();227 const url = URLS.GET_COMMENTS_URL + slug + '/comments';228 const { data, error } = await fetchAction(url, getCommentsOptions());229 if (error) {230 throw new Error("Can't get comments: " + error);231 }232 dispatch({233 type: ArticleActionTypes.GET_COMMENTS,234 payload: { comments: data, articleFetchMode: FETCH_MODE.FETCHED },235 });236};237// set new article238export const setNewArticle = (title: string) => (dispatch: Dispatch<ArticleAction>) => {239 dispatch({ type: ArticleActionTypes.SET_NEW_ARTICLE, payload: { newArticle: { title } } });240};
Client.js
Source:Client.js
...24* @param data HTTP body25* @returns An object containing any custom settings that you want to apply to the fetch() request.26*27**/28function createFetchOptions(method, data){29 let requestOptions;30 if (method == "GET"){31 requestOptions = {32 method: method,33 mode: 'cors',34 headers: myHeaders,35 }36 }37 else{38 let body = JSON.stringify(data)39 requestOptions = {40 method: method,41 mode: 'cors',42 headers: myHeaders,43 body: body,44 }45 }46 console.log(requestOptions)47 return requestOptions48}49export async function connectToServer(){50 console.log("Connecting to Server");51 const requestOptions = createFetchOptions('GET');52 const NEW_URL = URL + SERVER_PATHS.CONNECTING;53 const response = fetch(NEW_URL, requestOptions).catch(error => error);54 return response; 55}56/** 57* @param path The desired state a user wants. 58**/59export async function getState(data){60 console.log("Gettting State");61 const requestOptions = createFetchOptions('GET');62 const NEW_URL = URL + SERVER_PATHS.STATE + "/" + data.state;63 const response = await fetch(NEW_URL, requestOptions).catch(error => error);64 return await response.json()65}66export async function getJobs(){67 console.log("Gettting Jobs");68 const requestOptions = createFetchOptions('GET');69 const NEW_URL = URL + SERVER_PATHS.JOBS70 const response = await fetch(NEW_URL, requestOptions).catch(error => error);71 return await response.json()72}73export async function getStatePrecincts(){ 74 console.log("Gettting State Precincts"); 75 const requestOptions = createFetchOptions('GET'); 76 const NEW_URL = URL + SERVER_PATHS.PRECINCTS; 77 const response = await fetch(NEW_URL, requestOptions).catch(error => error); 78 return await response.json() 79} 80export async function getPrecinctDemographic(precinctObj){81 console.log("Gettting Precinct's Demographic"); 82 const requestOptions = createFetchOptions('GET'); 83 const NEW_URL = URL + SERVER_PATHS.PRECINCT_DEMOGRAPHIC + "/" + precinctObj.fips; 84 const response = await fetch(NEW_URL, requestOptions).catch(error => error); 85 return await response.json() 86}87export async function getPlans(data){88 console.log("Getting District Plans");89 const requestOptions = createFetchOptions('GET');90 const NEW_URL = URL + SERVER_PATHS.JOB + "/" + data.job.jobID;91 const response = await fetch(NEW_URL, requestOptions).catch(error => error);92 return response.json(); 93}94export async function getPlanGraph(data){95 console.log("Getting Box n Whisker For Plan");96 const requestOptions = createFetchOptions('GET');97 const NEW_URL = URL + SERVER_PATHS.BOXWHISKER + "/" + data.job.jobID98 const response = await fetch(NEW_URL, requestOptions).catch(error => error);99 return response.json(); 100}101export async function cancelJob(data){102 console.log("Cancel");103 const requestOptions = createFetchOptions('PUT');104 const NEW_URL = URL + SERVER_PATHS.BOXWHISKER + "/" + data.job.id105 const response = fetch(NEW_URL, requestOptions).catch(error => error);106 return response; 107}108export async function deleteJob(data){109 console.log("Delete");110 const requestOptions = createFetchOptions('DELETE');111 const NEW_URL = URL + SERVER_PATHS.DELETE + "/" + data.job.jobID112 const response = fetch(NEW_URL, requestOptions).catch(error => error);113 return response;114}115export async function generateJob(data){116 console.log("Generating Job");117 const requestOptions = createFetchOptions('POST', data);118 const NEW_URL = URL + SERVER_PATHS.GENERATE_JOB;119 const response = await fetch(NEW_URL, requestOptions).catch(error => error);120 return await response.json()121}122export async function generateHeatMap(data) {123 console.log("Getting heat map");124 const requestOptions = createFetchOptions('GET');125 const NEW_URL = URL + SERVER_PATHS.GENERATE_HEATMAP + "/" + data.name;126 const response = await fetch(NEW_URL, requestOptions).catch(error => error);127 return await response.json()128}129export async function getJobSummary(data) {130 console.log("Getting job summary");131 const requestOptions = createFetchOptions('GET');132 const NEW_URL = URL + SERVER_PATHS.SUMMARY + "/" + data.job.jobID133 const response = await fetch(NEW_URL, requestOptions).catch(error => error);134 return await response.json()...
connector.js
Source:connector.js
1// @flow2/* eslint-disable no-unused-vars */3import type { DocumentNode } from 'graphql';4import type { Middleware, $Request } from 'express';5import {6 merge,7 map,8 union,9 without,10 castArray,11 concat,12 reduce,13 pluck,14 assignIn,15} from 'lodash';16import log from '../helpers/log';17const combine = (features, extractor): any =>18 without(union(...map(features, res => castArray(extractor(res)))), undefined);19type FeatureParams = {20 schema: DocumentNode | DocumentNode[],21 createResolversFunc?: Function | Function[],22 createContextFunc?: Function | Function[],23 beforeware?: Middleware | Middleware[],24 middleware?: Middleware | Middleware[],25 createFetchOptions?: Function | Function[],26 creatModelFunc?: Function | Function[],27};28class Feature {29 schema: DocumentNode[];30 createResolversFunc: Function[];31 createContextFunc: Function[];32 createFetchOptions: Function[];33 beforeware: Function[];34 middleware: Function[];35 constructor(feature?: FeatureParams, ...features: Feature[]) {36 const allFeatures = concat(castArray(feature), features);37 // console.log(feature.schema[0] instanceof DocumentNode);38 this.schema = combine(allFeatures, arg => arg.schema);39 this.createResolversFunc = combine(40 allFeatures,41 arg => arg.createResolversFunc,42 );43 this.createContextFunc = combine(allFeatures, arg => arg.createContextFunc);44 this.beforeware = combine(allFeatures, arg => arg.beforeware);45 this.middleware = combine(allFeatures, arg => arg.middleware);46 this.createFetchOptions = combine(47 allFeatures,48 arg => arg.createFetchOptions,49 );50 }51 get schemas(): DocumentNode[] {52 return this.schema;53 }54 async createContext(req: $Request, connectionParams: any, webSocket: any) {55 const results = await Promise.all(56 this.createContextFunc.map(createContext =>57 createContext(req, connectionParams, webSocket),58 ),59 );60 return merge({}, ...results);61 }62 createResolvers(pubsub: any) {63 return merge(64 {},65 ...this.createResolversFunc.map(createResolvers =>66 createResolvers(pubsub),67 ),68 );69 }70 get beforewares(): Middleware[] {71 return this.beforeware;72 }73 get middlewares(): Middleware[] {74 return this.middleware;75 }76 get constructFetchOptions(): any {77 return this.createFetchOptions.length78 ? (...args) => {79 try {80 let result = {};81 for (const func of this.createFetchOptions) {82 result = { ...result, ...func(...args) };83 }84 return result;85 } catch (e) {86 log.error(e.stack);87 }88 }89 : null;90 }91}...
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2console.log(options);3var wptoolkit = require('wptoolkit');4console.log(options);5var wptoolkit = require('wptoolkit');6console.log(options);7var wptoolkit = require('wptoolkit');8console.log(options);9var wptoolkit = require('wptoolkit');10console.log(options);11var wptoolkit = require('wptoolkit');12console.log(options);13var wptoolkit = require('wptoolkit');14console.log(options);15var wptoolkit = require('wptoolkit');16console.log(options);17var wptoolkit = require('wptoolkit');18console.log(options);19var wptoolkit = require('wptoolkit');20console.log(options);21var wptoolkit = require('wptoolkit');
Using AI Code Generation
1const { createFetchOptions } = require('wpt-api');2const fetch = require('node-fetch');3const options = {4}5const fetchOptions = createFetchOptions(options);6 .then(response => response.json())7 .then(data => console.log(data))8 .catch(error => console.log(error));
Using AI Code Generation
1var wptools = require('wptools');2var options = wptools.createFetchOptions('en', 'wikipedia');3console.log(options);4{ host: 'en.wikipedia.org',5 headers: { 'User-Agent': 'wptools' } }6var wptools = require('wptools');7var options = wptools.createFetchOptions('en', 'wiktionary');8console.log(options);9{ host: 'en.wiktionary.org',10 headers: { 'User-Agent': 'wptools' } }11var wptools = require('wptools');12var options = wptools.createFetchOptions('en', 'wikisource');13console.log(options);14{ host: 'en.wikisource.org',15 headers: { 'User-Agent': 'wptools' } }16var wptools = require('wptools');17var options = wptools.createFetchOptions('en', 'wikibooks');18console.log(options);19{ host: 'en.wikibooks.org',20 headers: { 'User-Agent': 'wptools' } }21var wptools = require('wptools');22var options = wptools.createFetchOptions('en', 'wikinews');23console.log(options);24{ host: 'en.wikinews.org',
Using AI Code Generation
1const { createFetchOptions } = require("wptoolkit");2const fetch = require("node-fetch");3const { URL } = require("url");4const fetchOptions = createFetchOptions(url, {5 headers: {6 },7});8fetch(url, fetchOptions)9 .then((res) => res.json())10 .then((json) => console.log(json));11const { createFetchOptions } = require("wptoolkit");12const fetch = require("node-fetch");13const { URL } = require("url");14const fetchOptions = createFetchOptions(url, {15 headers: {16 },17});18fetch(url, fetchOptions)19 .then((res) => res.json())20 .then((json) => console.log(json));21const { createFetchOptions } = require("wptoolkit");22const fetch = require("node-fetch");23const { URL } = require("url");24const fetchOptions = createFetchOptions(url, {25 headers: {26 },27});28fetch(url, fetchOptions)29 .then((res) => res.json())30 .then((json) => console.log(json));31const { createFetchOptions } = require("wptoolkit");32const fetch = require("node-fetch");33const { URL } = require("url");34const fetchOptions = createFetchOptions(url, {35 headers: {36 },37});38fetch(url, fetchOptions)39 .then((res) => res.json())40 .then((json) => console.log(json));
Using AI Code Generation
1var wpt = require('wpt-api');2console.log(options);3var wpt = require('wpt-api');4console.log(options);5var wpt = require('wpt-api');6console.log(options);7var wpt = require('wpt-api');8console.log(options);9var wpt = require('wpt-api');10console.log(options);11var wpt = require('wpt-api');12console.log(options);13var wpt = require('wpt-api');14console.log(options);15var wpt = require('wpt-api');
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!!