UNPKG

wpt-api-client

Version:

Unofficial Node.js client for the WebPageTest REST API

90 lines 3.96 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.makeTestStatus = exports.Status = void 0; const phin_1 = __importDefault(require("phin")); const utils_1 = require("./utils"); const error_1 = require("./error"); var Status; (function (Status) { Status["COMPLETED"] = "COMPLETED"; Status["QUEUED"] = "QUEUED"; Status["STARTED"] = "STARTED"; })(Status = exports.Status || (exports.Status = {})); /** * Create function to call the /testStatus.php endpoint. * * https://docs.webpagetest.org/api/reference/#check-test-status */ const makeTestStatus = (wptServer) => { const endpoint = `${utils_1.baseUrl(wptServer)}/testStatus.php`; const testStatus = (id) => __awaiter(void 0, void 0, void 0, function* () { const url = `${endpoint}?f=json&test=${id}`; const { body } = yield phin_1.default({ url, parse: 'json' }); error_1.throwIfNotSuccess(body); // The WebPageTest API returns some duplicate properties, and some // fields are not in the most appropriate data type. So we cleanup a bit. // https://docs.webpagetest.org/api/reference/#check-test-status switch (body.statusCode) { case 100: { return { startTime: body.data.startTime, status: Status.STARTED }; } case 101: { return { behindCount: body.data.behindCount, status: Status.QUEUED }; } case 200: { const d = body.data; return { status: Status.COMPLETED, browser: d.testInfo.browser, bwDownloadKbps: d.testInfo.bwIn, bwUploadKbps: d.testInfo.bwOut, completeTime: d.completeTime, connectivity: d.testInfo.connectivity, firstViewOnly: d.fvonly ? true : false, hasDevToolsTimeline: d.timeline ? true : false, hasVideo: d.testInfo.video ? true : false, ignoreSSL: d.testInfo.ignoreSSL ? true : false, label: d.testInfo.label, latencyMs: d.testInfo.latency, location: d.location, mobile: d.testInfo.mobile ? true : false, netlog: d.testInfo.netlog ? true : false, noscript: d.testInfo.noscript ? true : false, packetLossRate: d.testInfo.plr, runs: d.runs, tcpdump: d.testInfo.tcpdump ? true : false, trace: d.trace ? true : false, url: d.testInfo.url }; } case 400: throw new Error(`Test ${id} not found`); default: throw new Error(`statusCode ${body.statusCode} not handled`); } }); return testStatus; }; exports.makeTestStatus = makeTestStatus; //# sourceMappingURL=test-status.js.map