Best JavaScript code snippet using wpt
OSCService.js
Source:OSCService.js
...31 isSelf(id) {32 return GLOBAL_USER.id === id;33 }34 searchProjects(query, page) {35 return this.fetchPromise(PROJECTS + "search/" + encodeURI(query) + "?page=" + page);36 }37 /**38 * å建ä¸ä¸ªissue39 */40 pubCreateIssue(projectId, title, description, assignee_id, milestone_id) {41 return this.fetchPromise(PROJECTS + projectId + "/issues", "POST", {description: description, title: title, assignee_id:assignee_id,milestone_id:milestone_id});42 }43 getProjectCodeTree(pId, path, refName) {44 var param = {};45 if(path && refName) {46 param = {path: path, ref_name: refName};47 }48 return this.fetchPromise(PROJECTS + pId + "/repository/tree", "GET", param);49 }50 /**51 * è·åè¯è¨å表52 * {53 * "created_at": "2013-08-01T22:39:56+08:00",54 "detail": null,55 "id": 5,56 "ident": "Java",57 "name": "Java",58 "order": 4,59 "parent_id": 1,60 "projects_count": 43479,61 "updated_at": "2013-08-01T22:39:56+08:00"62 * }63 */64 getLanguageList() {65 return this.fetchPromise(PROJECTS + "languages");66 }67 /**68 * æ ¹æ®è¯è¨çIDè·å¾é¡¹ç®çå表69 */70 getLanguageProjectList(languageId, page) {71 let url = PROJECTS + "languages/" + languageId + "?page=" + page;72 return this.fetchPromise(url);73 }74 getRandomProject() {75 return this.fetchPromise(PROJECTS + "random", "GET", {luck: 1})76 }77 getPersonalProjects(uId, page) {78 return this.fetchPromise(USER + uId + "/" + "projects?page=" + page);79 }80 getPersonalEvents(uId, page) {81 return this.fetchPromise(EVENT + "user" + "/" + uId + "?page=" + page);82 }83 getMyEvents(page) {84 return this.fetchPromise(EVENT + "?page=" + page);85 }86 getPersonalStarProjects(uId, page) {87 return this.fetchPromise(USER + uId + "/stared_projects?page=" + page);88 }89 getPersonalWatchProjects(uId, page) {90 return this.fetchPromise(USER + uId + "/watched_projects?page=" + page);91 }92 starProject(projectId){93 return this.fetchPromise(PROJECTS + projectId + "/star", "POST");94 }95 unStarProject(projectId){96 return this.fetchPromise(PROJECTS + projectId + "/unstar", "POST");97 }98 watchProject(projectId){99 return this.fetchPromise(PROJECTS + projectId + "/watch", "POST");100 }101 unWatchProject(projectId){102 return this.fetchPromise(PROJECTS + projectId + "/unwatch", "POST");103 }104 getExploreLatestProject(page = 1) {105 return this.fetchPromise(PROJECTS + "latest?page=" + page);106 }107 getExploreFeaturedProject(page = 1) {108 return this.fetchPromise(PROJECTS + "featured?page=" + page);109 }110 getExplorePopularProject(page = 1) {111 return this.fetchPromise(PROJECTS + "popular?page=" + page);112 }113 getProject(id) {114 return this.fetchPromise(PROJECTS + id);115 }116 /**117 * å®é
æ¯å¾rplees/react-native-gitoscå建ä¸ä¸ªissue118 * @param title119 * @param message120 * @returns {*}121 */122 feedback(title, message) {123 return this.pubCreateIssue(834878,title, message, 95171, "");124 }125 login(name, pwd) {126 let param = {email: name, password: pwd};127 return this.fetchPromise(BASE_URL + "session", "POST", param)128 .then(json => {129 Object.assign(GLOBAL_USER, json);130 this.__saveUser2Disk();131 return GLOBAL_USER;132 });133 }134 packagePathWithToken(path) {135 if(this.isLogined()) {136 let split = path.indexOf("?") > -1 ? "&": "?";137 path += split + "private_token=" + GLOBAL_USER.private_token;138 }139 return path;140 }141 fetchPromise(path, method="GET", param) {142 if(param) {143 path += (path.indexOf("?") > -1 ? "&" : "?");144 path += Utils.JsonUtils.encode(param);145 }146 let url = this.packagePathWithToken(path);147 L.debug("åå¤è¯·æ±å°å:{}", url);148 return fetch(url, {149 method: method,150 timeout:5000,//FIXME:è¿ä¸ªåæ°æ²¡çæ151 headers: {152 'User-Agent': config.userAgent,153 'Accept': 'application/json; charset=utf-8'154 },155 }).then(response => {...
api.js
Source:api.js
1export default class {2 isDev = false;3 BASE_URL = 'https://imbilly.site/api/v1';4 TEST_BASE_URL = 'http://localhost:8080/api/v1';5 constructor() {}6 getImage = async (query, page = 1, size = 15) => {7 let fetchPromise = await this.myfetch(`/image?size=${size}`);8 return await fetchPromise.json();9 };10 getImagePopular = async (size = 10) => {11 let fetchPromise = await this.myfetch(`/image/popular?size=${size}`);12 return await fetchPromise.json();13 };14 getImageNew = async (size = 10) => {15 let fetchPromise = await this.myfetch(`/image/new?size=${size}`);16 return await fetchPromise.json();17 };18 getImageRandom = async (size = 50) => {19 let fetchPromise = await this.myfetch(`/image/random?size=${size}`);20 return await fetchPromise.json();21 };22 getImageDetail = async (id) => {23 let fetchPromise = await this.myfetch(`/image/${id}`);24 return await fetchPromise.json();25 };26 getImageRelated = async (tags, page = 1, size = 15) => {27 let fetchPromise = await this.myfetch(`/image/related?size=${size}`);28 return await fetchPromise.json();29 };30 postImage = async (size = 10) => {31 let formdata = new FormData();32 formdata.append('image', fileInput.files[0]);33 formdata.append('tags', '["íí","ì
íë¬","ì£ì¡íë°ì
íë¬ì¸ì?"]');34 // imageë blob, tagsë jsonì¼ë¡35 var requestOptions = {36 method: 'POST',37 body: formdata,38 redirect: 'follow',39 };40 fetch('https://imbilly.site/api/v1/image', requestOptions)41 .then((response) => response.text())42 .then((result) => console.log(result))43 .catch((error) => console.log('error', error));44 };45 patchImageViewCount = async (size = 10) => {46 let fetchPromise = await this.myfetch(`/image/popular?size=${size}`);47 return await fetchPromise.json();48 };49 getImageKeyword = async (size = 10) => {50 let fetchPromise = await this.myfetch(`/image/popular?size=${size}`);51 return await fetchPromise.json();52 };53 myfetch = (url) => {54 if (this.isDev) {55 return fetch(this.TEST_BASE_URL + url);56 } else {57 return fetch(this.BASE_URL + url);58 }59 };...
fetch.test.js
Source:fetch.test.js
...12 })13})14describe('fetchPromise', () => {15 test('fetchPromise then catch test', () => {16 return fetchPromise().then(({ data }) => {17 expect(data.code).toBe(0)18 }).catch(error => {19 expect(error.toString().indexOf('failed') >= 0).toBe(true)20 })21 })22 test('fetchPromise resolves return test', () => {23 return expect(fetchPromise()).resolves.toMatchObject({24 data: {25 code: 026 }27 })28 })29 test('fetchPromise resolves await test', async () => {30 await expect(fetchPromise()).resolves.toMatchObject({31 data: {32 code: 033 }34 })35 })36 // test('fetchPromise rejects return test', () => {37 // return expect(fetchPromise()).rejects.toThrow()38 // })39 // test('fetchPromise rejects return test', async () => {40 // await expect(fetchPromise()).rejects.toThrow()41 // })42 test('fetchPromise async await test', async () => {43 try {44 const { data } = await fetchPromise()45 expect(data.code).toBe(0)46 } catch (error) {47 expect(error.toString().indexOf('failed') >= 0).toBe(true)48 }49 })...
Using AI Code Generation
1var wpt = require('wpt-api');2.then(function(data) {3 console.log(data);4})5.catch(function(err) {6 console.log(err);7});8var wpt = require('wpt-api');9 if(err) {10 console.log(err);11 }12 else {13 console.log(data);14 }15});16var wpt = require('wpt-api');17 if(err) {18 console.log(err);19 }20 else {21 console.log(data);22 }23});24var wpt = require('wpt-api');25.then(function(data) {26 console.log(data);27})28.catch(function(err) {29 console.log(err);30});31var wpt = require('wpt-api');32 if(err) {33 console.log(err);34 }35 else {36 console.log(data);37 }38});39var wpt = require('wpt-api');
Using AI Code Generation
1const wpt = require('./wpt-api.js');2wpt.fetchPromise(url)3 .then(function (result) {4 console.log(result);5 })6 .catch(function (error) {7 console.log(error);8 });
Using AI Code Generation
1var testId = "150121_1C_4E6";2var filePath = "testResults.json";3var humanReadableFilePath = "testResultsHumanReadable.txt";4var humanReadableFilePath = "testResultsHumanReadable.txt";5var humanReadableFilePath = "testResultsHumanReadable.txt";6var humanReadableFilePath = "testResultsHumanReadable.txt";7var humanReadableFilePath = "testResultsHumanReadable.txt";8var humanReadableFilePath = "testResultsHumanReadable.txt";9var humanReadableFilePath = "testResultsHumanReadable.txt";10var humanReadableFilePath = "testResultsHumanReadable.txt";11var humanReadableFilePath = "testResultsHumanReadable.txt";12var humanReadableFilePath = "testResultsHumanReadable.txt";
Using AI Code Generation
1const wpt = require('webpagetest');2const api = wpt('A.8f0e05e6a2b6a1c6e7a8f5b5c2c2d9d9');3const options = {4};5api.runTest(url, options, function (err, data) {6 if (err) {7 console.log('Error: ' + err);8 } else {9 console.log('Test Status: ' + data.statusText);10 console.log('Test ID: ' + data.data.testId);11 console.log('Test URL: ' + data.data.summary);12 }13});14const wpt = require('webpagetest');15const api = wpt('A.8f0e05e6a2b6a1c6e7a8f5b5c2c2d9d9');16const options = {17};18api.runTest(url, options)19 .then(data => {20 console.log('Test Status: ' + data.statusText);21 console.log('Test ID: ' + data.data.testId);22 console.log('Test URL: ' + data.data.summary);23 })24 .catch(err => console.log('Error: ' + err));
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!!