Best JavaScript code snippet using wpt
idb-wrapper.js
Source:idb-wrapper.js
...132 }133 count () {134 const t = this._handle.transaction([this.name], 'readonly')135 const store = t.objectStore(this.name).count()136 return requestPromise(store, { property: 'result' })137 }138 keys () {139 const t = this._handle.transaction([this.name], 'readonly')140 const store = t.objectStore(this.name).getAllKeys()141 return requestPromise(store, { property: 'result' })142 }143 find (index) {144 const t = this._handle.transaction([this.name], 'readonly')145 const store = t.objectStore(this.name).get(index)146 return requestPromise(store, { property: 'result' })147 }148 async findAll (opts) {149 const defaultOptions = {150 limit: -1,151 offset: 0,152 direction: 'ASC'153 }154 const options = Object.assign(defaultOptions, opts)155 const t = this._handle.transaction([this.name], 'readonly')156 const query = t.objectStore(this.name).getAll()157 const results = await requestPromise(query, { property: 'result' })158 const output = results.slice(options.offset, options.limit)159 return output160 }161 insert (data) {162 const t = this._handle.transaction([this.name], 'readwrite')163 const store = t.objectStore(this.name).add(data)164 this._sendEvent('DbUpdated')165 return requestPromise(store, { property: 'result'})166 }167 update (index, data) {168 $log(`Updating #${index}`, data)169 const t = this._handle.transaction([this.name], 'readwrite')170 const store = t.objectStore(this.name).put(data)171 return requestPromise(store, data)172 }173 delete (index) {174 const t = this._handle.transaction([this.name], 'readwrite')175 const store = t.objectStore(this.name).delete(index)176 return requestPromise(store, data)177 }178 flush () {179 const t = this._handle.transaction([this.name], 'readwrite')180 const query = t.objectStore(this.name).clear()181 return requestPromise(query, {msg: 'Cleared'})182 }...
coss-api.service.ts
Source:coss-api.service.ts
1import {CandleStick, Depth, ExchangeInfo, MarketPrices, MarketSummaries, TradeHistory} from "./coss-api.model";2import * as request from "request-promise-native";3import {RequestPromise} from "request-promise-native";4import {5 AccountBalanceArray,6 AccountDetails,7 CancelOrder,8 CancelOrderResponse,9 Order,10 OrderDetail,11 OrderListRequest,12 OrderListResponse,13 OrderResponse,14 TradeDetailRequest,15 TradeDetailsArray16} from "../swaggerSchema";17import CryptoJS from "crypto-js";18import querystring from "querystring";19export class CossApiService {20 private TRADE = "https://trade.coss.io/c/api/v1/";21 private ENGINE = "https://engine.coss.io/api/v1/";22 private EXCHANGE = "https://exchange.coss.io/api/";23 private WEB = "https://trade.coss.io/c/";24 private publicKey: string;25 private privateKey: string;26 private publicConfig = {27 json: true28 };29 private privateConfig = {30 json: true,31 headers: {32 'Content-Type': 'application/json',33 'Authorization': '',34 'Signature': '',35 'X-Requested-With' : 'XMLHttpRequest',36 'x-coss-bot': 'CBS2312192',37 },38 body: {}39 };40 constructor(publicKey: string, privateKey: string) {41 this.publicKey = publicKey;42 this.privateKey = privateKey;43 this.privateConfig.headers.Authorization = this.publicKey;44 }45 // Public46 publicRequest(url: string, payload: any): RequestPromise<any> {47 const query = payload ? '?' + querystring.stringify(payload) : '';48 return request.get(url + query, this.publicConfig);49 }50 // Exchange51 getMarketSummaries(): RequestPromise<MarketSummaries> {52 return this.publicRequest(this.EXCHANGE + "getmarketsummaries", null);53 }54 // Trade55 getExchangeInfo(): RequestPromise<ExchangeInfo> {56 return this.publicRequest(this.TRADE + "exchange-info", null);57 }58 getMarketPrice(pair = ""): RequestPromise<MarketPrices> {59 return this.publicRequest(this.TRADE + "market-price", {symbol: pair});60 }61 testConnection(): RequestPromise<any> {62 return this.publicRequest(this.TRADE + "ping", null);63 }64 getServerTime(): RequestPromise<any> {65 return request.get(this.TRADE + "time", {json: true});66 }67 // Engine68 getDepth(pair: string): RequestPromise<Depth> {69 return request.get(this.ENGINE + "dp?symbol=" + pair, {json: true});70 }71 getHistory(pair: string): RequestPromise<TradeHistory> {72 return this.publicRequest(this.ENGINE + "ht", {symbol: pair});73 }74 getCandlestick(pair: string): RequestPromise<CandleStick> {75 return request.get(this.ENGINE + "cs?symbol=" + pair + "&tt=15m", {json: true});76 }77 // Web78 getCoinsInfo(): RequestPromise<any> {79 return request.get(this.WEB + "coins/getinfo/all", {json: true});80 }81 getCoinsBaseList(): RequestPromise<any> {82 return request.get(this.WEB + "coins/get_base_list", {json: true});83 }84 getOrderSymbols(): RequestPromise<Array<{allow_trading: boolean, amount_limit_decimal: number, price_limit_decimal: number, symbol: string}>> {85 return this.publicRequest(this.WEB + "order/symbols", null);86 }87 // PRIVATE88 privateGet(url: string, payload: any): RequestPromise<any> {89 const config = this.privateConfig;90 config.headers.Signature = CryptoJS.HmacSHA256(querystring.stringify(payload), this.privateKey).toString(CryptoJS.enc.Hex);91 delete config.body;92 return request.get(url + '?' + querystring.stringify(payload), config);93 }94 privatePost(url: string, payload: any): RequestPromise<any> {95 const config = this.privateConfig;96 config.headers.Signature = CryptoJS.HmacSHA256(JSON.stringify(payload), this.privateKey).toString(CryptoJS.enc.Hex);97 config.body = payload;98 return request.post(url, config);99 }100 privateDelete(url: string, payload: any): RequestPromise<any> {101 const config = this.privateConfig;102 config.headers.Signature = CryptoJS.HmacSHA256(JSON.stringify(payload), this.privateKey).toString(CryptoJS.enc.Hex);103 config.body = payload;104 return request.delete(url, config);105 }106 // Balance107 getBalance(timestamp: number): RequestPromise<AccountBalanceArray> {108 return this.privateGet(this.TRADE + "account/balances", {timestamp: timestamp, recvWindow: 9999999999});109 }110 // Account details111 getAccountDetails(timestamp: number): RequestPromise<AccountDetails> {112 return this.privateGet(this.TRADE + "account/details", {timestamp: timestamp, recvWindow: 9999999999});113 }114 // Place Order115 placeOrder(order: Order): RequestPromise<OrderResponse> {116 return this.privatePost(this.TRADE + "order/add", order);117 }118 // Cancel Order119 cancelOrder(order: CancelOrder): RequestPromise<CancelOrderResponse> {120 return this.privateDelete(this.TRADE + "order/cancel", order);121 }122 // Order Detail123 getOrderDetails(order: OrderDetail): RequestPromise<OrderResponse> {124 return this.privatePost(this.TRADE + "order/details", order);125 }126 // Trade Details127 getTradeDetails(trade: TradeDetailRequest): RequestPromise<TradeDetailsArray> {128 return this.privatePost(this.TRADE + "order/trade-detail", trade);129 }130 // Open Orders131 getOpenOrders(orders: OrderListRequest): RequestPromise<OrderListResponse> {132 return request.post(this.TRADE + "order/list/open", {json: true});133 }134 // Completed Orders135 getCompletedOrders(orders: OrderListRequest): RequestPromise<OrderListResponse> {136 return this.privatePost(this.TRADE + "order/list/completed", orders);137 }138 // All Orders139 getAllOrders(orders: OrderListRequest): RequestPromise<OrderListResponse> {140 return request.post(this.TRADE + "order/list/all", {json: true});141 }...
factory.js
Source:factory.js
1module.exports.createElement = function (ElementModel) {2 return async function (req, res) {3 try {4 let element = req.body;5 if (element) {6 element = await ElementModel.create(element);7 res.status(200).json({8 element9 });10 } else {11 res.status(200).json({12 message: "kindly enter data"13 });14 }15 } catch (err) {16 console.error(err);17 res.status(500).json({18 message: "Server error"19 });20 }21 }22}23module.exports.getElements = function (ElementModel) {24 return async function (req, res) {25 try {26 let requestPromise;27 // query28 if (req.query.myQuery) {29 requestPromise = ElementModel.find(req.query.myQuery);30 } else {31 requestPromise = ElementModel.find();32 }33 // sort34 if (req.query.sort) {35 requestPromise = requestPromise.sort(req.query.sort)36 }37 // select38 if (req.query.select) {39 let params = req.query.select.split("%").join(" ");40 requestPromise = requestPromise.select(params);41 }42 // paginate 43 let page = Number(req.query.page) || 1;44 let limit = Number(req.query.limit) || 4;45 let toSkip = (page - 1) * limit;46 requestPromise = requestPromise47 .skip(toSkip)48 .limit(limit);49 let elements = await requestPromise;50 res.status(200).json({51 "message": elements52 })53 } catch (err) {54 res.status(502).json({55 message: err.message56 })57 }58 }59}60module.exports.updateElement = function (ElementModel) {61 return async function updateElement(req, res) {62 let { id } = req.params;63 try {64 // user model -> check 65 // plan -> if -> fail66 if (req.body.password || req.body.confirmPassword) {67 return res.json({68 message: "use forget password instead"69 })70 }71 let element = await ElementModel.findById(id);72 console.log("60", element)73 if (element) {74 for (let key in req.body) {75 element[key] = req.body[key];76 }77 // save -> confirm ,password78 await element.save({79 validateBeforeSave: false80 });81 res.status(200).json({82 element: element83 });84 } else {85 res.status(404).json({86 message: "element not found"87 })88 }89 } catch (err) {90 console.error(err);91 res.status(500).json({92 message: "Server error"93 });94 }95 }96}97module.exports.deleteElement = function (ElementModel) {98 return async function deletePlan(req, res) {99 let { id } = req.body;100 try {101 let element = await ElementModel.finByIdAndDelete(id, req.body);102 // let element = await ElementModel.findOne({ _id: id });103 if (!element) {104 res.status(404).json({105 message: "resource not found"106 })107 } else {108 res.status(200).json(element);109 }110 } catch (err) {111 console.log(err);112 res.status(500).json({113 message: "Server error"114 });115 }116 }117}118module.exports.getElementById = function (ElementModel) {119 return async function getElementById(req, res) {120 try {121 let id = req.params.id;122 let element = await ElementModel.getElementById(id);123 res.status(200).json({124 element: element125 });126 } catch (err) {127 console.log(err);128 res.status(500).json({129 message: "Server error"130 });131 }132 }...
Using AI Code Generation
1const wpt = require('wpt-api');2const wptApi = new wpt(process.env.WPT_API_KEY);3const options = {4};5wptApi.requestPromise(options)6 .then(data => {7 console.log(data);8 })9 .catch(err => {10 console.log(err);11 });12const wpt = require('wpt-api');13const wptApi = new wpt(process.env.WPT_API_KEY);14const options = {15};16wptApi.request(options, (err, data) => {17 if (err) {18 console.log(err);19 } else {20 console.log(data);21 }22});23const wpt = require('wpt-api');24const wptApi = new wpt(process.env.WPT_API_KEY);25const options = {26};27wptApi.request(options)28 .then(data => {29 console.log(data);30 })31 .catch(err => {32 console.log(err);33 });
Using AI Code Generation
1var requestPromise = require('request-promise');2var options = {3};4requestPromise(options).then(function (result) {5 console.log(result);6}).catch(function (err) {7 console.log(err);8});
Using AI Code Generation
1const { requestPromise } = require("wpt-api");2requestPromise(url)3 .then((res) => {4 console.log(res);5 })6 .catch((err) => {7 console.log(err);8 });9const { requestAsync } = require("wpt-api");10requestAsync(url, (err, res) => {11 if (err) {12 console.log(err);13 } else {14 console.log(res);15 }16});17const { request } = require("wpt-api");18request(url, (err, res) => {19 if (err) {20 console.log(err);21 } else {22 console.log(res);23 }24});25const { getResults } = require("wpt-api");26getResults(url, (err, res) => {27 if (err) {28 console.log(err);29 } else {30 console.log(res);31 }32});33const { getLocations } = require("wpt-api");34getLocations(url, (err, res) => {35 if (err) {36 console.log(err);37 } else {38 console.log(res);39 }40});
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!!