UNPKG

codingame-connector

Version:
66 lines (57 loc) 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _promise = require("babel-runtime/core-js/promise"); var _promise2 = _interopRequireDefault(_promise); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * @file Module 'codingame/parsers/success' * @author woshilapin <woshilapin@tuziwo.info> */ /** * Parse successful response from Codingame when test pass. * * The typical response in this case if of the following form. * ```json * { * success: { * "output": output, * "comparison": { * "success": true * } * } * } * ``` * @module codingame/parsers/success */ var name = "success"; /** * Attempt to parse the body of a successful request to Codingame test API. * This function will try to map a response for a successful test. * * @function parse * @param {Object} body Body of the response * @returns {Promise<string>} Resolve with the successful result value * @throws {Error} Throw is parsing failed * @instance */ var parse = function parse(body) { try { var output = body["output"], success = body["comparison"]["success"]; if (success) { return _promise2.default.resolve(output); } else { throw "Success property must be true"; } } catch (error) { var message = "The body is not of response type '" + name + "'\n"; message += error.message; throw new Error(message); } }; exports.default = { "name": name, "parse": parse };