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).
76 lines (67 loc) • 2.22 kB
JavaScript
import Endpoint from '../Endpoint.js';
import Test from '../Tests/Test.js';
import Exception from '../Exception.js';
import Client from '../Client.js';
import Url from './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]
*/
export default class Tests extends Endpoint {
/**
* @param {LoaderIOTestCreateData} data
* @return {Promise<Test>}
*/
async create(data) {
const responseData = await this.client.request('tests', Client.METHOD.POST, {
body: {
test_type: Test.TYPE.CLIENTS_PER_SECOND,
initial: 1,
total: 25,
timeout: 10000,
error_threshold: 50,
...data,
urls: data.urls.map((url) => url instanceof Url ? url.toJSON() : url)
}
});
if (responseData?.message !== 'success') {
throw new Exception(`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.METHOD.GET);
if (data === undefined) {
throw new Exception(`Loader.io test ${id} can not be found.`);
}
return new Test(this.client, data);
}
/**
*
* @return {Promise<Test[]>}
*/
async list() {
const data = await this.client.request('tests', Client.METHOD.GET);
if (data === undefined) {
return [];
}
return data.map((entry) => new Test(this.client, entry));
}
}