UNPKG

wpt-api-client

Version:

Unofficial Node.js client for the WebPageTest REST API

66 lines 3.33 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.makeCancelTestById = exports.cannotCancelTestIdErrorMessage = exports.genericErrorMessage = void 0; const phin_1 = __importDefault(require("phin")); const utils_1 = require("./utils"); const WPT_CANNOT_CANCEL_TEST_ERROR_MESSAGE = '<h3>Sorry, the test could not be cancelled. It might have already started or been cancelled</h3>'; const re = new RegExp(WPT_CANNOT_CANCEL_TEST_ERROR_MESSAGE); const genericErrorMessage = (id) => { return `Cannot cancel test id ${id}`; }; exports.genericErrorMessage = genericErrorMessage; const cannotCancelTestIdErrorMessage = (id) => { return `[test id=${id}]: ${WPT_CANNOT_CANCEL_TEST_ERROR_MESSAGE}` .replace('<h3>', '') .replace('</h3>', ''); }; exports.cannotCancelTestIdErrorMessage = cannotCancelTestIdErrorMessage; /** * Create function to call the /cancelTest.php endpoint. * * https://webpagetest.org/cancelTest.php */ const makeCancelTestById = (apiKey, wptServer) => { utils_1.checkApiKey(apiKey, wptServer); const endpoint = `${utils_1.baseUrl(wptServer)}/cancelTest.php`; return function cancelTestById(id) { return __awaiter(this, void 0, void 0, function* () { let url = `${endpoint}?&test=${id}`; if (apiKey) { url = `${url}&k=${apiKey}`; } const response = yield phin_1.default({ url }); const str = response.body.toString(); // The WebPageTest API returns an empty string and a HTTP 200 even for // a non-existing test ID / invalid API key. I don't think I can distinguish // between the 2, but at least I can throw, because this is clearly not a // success condition. if (str === '') { throw new Error(exports.genericErrorMessage(id)); } // The WebPageTest API returns a HTTP 200 even when it could not cancel the // test. I think that the only case this string matches the regex is when // both the API key and the test ID were valid, but the test was already // canceled or already started. const matches = re.exec(str); if (matches && matches.length > 0) { throw new Error(exports.cannotCancelTestIdErrorMessage(id)); } return str; }); }; }; exports.makeCancelTestById = makeCancelTestById; //# sourceMappingURL=cancel-test.js.map