UNPKG

loader.io.api

Version:

loader.io api wrapper for nodejs. If you interested in this npm package, take a look at the npm package [perst](https://dasred.github.io/perst).

84 lines (71 loc) 2.46 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var Endpoint = require('../Endpoint.js'); var Test = require('./Test.js'); var Exception = require('../Exception.js'); var Client = require('../Client.js'); var Url = require('./Url.js'); /** * @typedef {Object} LoaderIOTestCreateData * @property {string} name * @property {string} [test_type = Test.TYPE.CYCLING] * @property {Url[]} urls * @property {number} duration * @property {number} [initial = 1] will be ignored for non-cycling tests * @property {number} [total = 25] * @property {number} [timeout = 10000] * @property {number} [error_threshold = 50] * @property {string} [callback] * @property {string} [callback_email] * @property {Date} [scheduled_at] * @property {string} [notes] * @property {string[]} [tage_names] */ class Tests extends Endpoint['default'] { /** * @param {LoaderIOTestCreateData} data * @return {Promise<Test>} */ async create(data) { const responseData = await this.client.request('tests', Client['default'].METHOD.POST, { body: { test_type: Test['default'].TYPE.CLIENTS_PER_SECOND, initial: 1, total: 25, timeout: 10000, error_threshold: 50, ...data, urls: data.urls.map((url) => url instanceof Url['default'] ? url.toJSON() : url) } }); if (responseData?.message !== 'success') { throw new Exception['default'](`Loader.io test ${data.name} can not be created.`); } return this.get(responseData.test_id); } /** * * @param {string} id * @return {Promise<Test>} */ async get(id) { const data = await this.client.request(`tests/${id}`, Client['default'].METHOD.GET); if (data === undefined) { throw new Exception['default'](`Loader.io test ${id} can not be found.`); } return new Test['default'](this.client, data); } /** * * @return {Promise<Test[]>} */ async list() { const data = await this.client.request('tests', Client['default'].METHOD.GET); if (data === undefined) { return []; } return data.map((entry) => new Test['default'](this.client, entry)); } } module.exports = exports = Tests; exports['default'] = Tests;