UNPKG

wpt-api-client

Version:

Unofficial Node.js client for the WebPageTest REST API

85 lines 4.04 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeRunTest = void 0; const phin_1 = __importDefault(require("phin")); const defaults_1 = require("./defaults"); const utils_1 = require("./utils"); const error_1 = require("./error"); // const httpClient = new HttpClient('run-test-client') /** * Create function to call the /runtest.php endpoint. * * https://docs.webpagetest.org/api/reference/#running-a-test * https://github.com/marcelduran/webpagetest-api/blob/1b6c221708ade50282799feacce5345de674150d/lib/webpagetest.js#L318 */ const makeRunTest = (apiKey, wptServer) => { utils_1.checkApiKey(apiKey, wptServer); const endpoint = `${utils_1.baseUrl(wptServer)}/runtest.php`; return function runTest(testUrl, options) { return __awaiter(this, void 0, void 0, function* () { const { blockUrls, captureTcpPackets, disableJavaScript, emulateMobile, firstViewOnly, isPrivate, label, pingback, runs, video } = options || {}; const browser = ((options === null || options === void 0 ? void 0 : options.browser) && options.browser) || defaults_1.BROWSER; const connectivity = ((options === null || options === void 0 ? void 0 : options.connectivity) && options.connectivity) || defaults_1.CONNECTIVITY; const locationId = ((options === null || options === void 0 ? void 0 : options.locationId) && options.locationId) || defaults_1.LOCATION_ID; // TODO: maybe check location before making a request to WPT? const wptLocation = `${locationId}:${browser}.${connectivity}`; let url = `${endpoint}?f=json&url=${testUrl}&location=${wptLocation}`; if (apiKey) { url = `${url}&k=${apiKey}`; } if (blockUrls) { // WPT wants a space-delimited list of urls const block = blockUrls.join('%20'); url = `${url}&block=${block}`; } if (captureTcpPackets) { url = `${url}&tcpdump=${1}`; } if (disableJavaScript) { url = `${url}&noscript=${1}`; } if (emulateMobile) { url = `${url}&mobile=${1}`; } if (firstViewOnly) { url = `${url}&fvonly=${1}`; } if (isPrivate || isPrivate === undefined) { url = `${url}&private=${1}`; } if (label) { url = `${url}&label=${label}`; } if (pingback) { url = `${url}&pingback=${pingback}`; } if (runs && runs > 0) { url = `${url}&runs=${runs}`; } if (video) { url = `${url}&video=1`; } const response = yield phin_1.default({ url, parse: 'json' }); error_1.throwIfNotSuccess(response.body); return Object.assign(Object.assign({}, response.body.data), { browser, connectivity, location: locationId }); }); }; }; exports.makeRunTest = makeRunTest; //# sourceMappingURL=run-test.js.map