UNPKG

@serenity-js/rest

Version:

Serenity/JS Screenplay Pattern library for interacting with REST and other HTTP-based services, supporting comprehensive API testing and blended testing scenarios

68 lines 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HTTPRequest = void 0; const core_1 = require("@serenity-js/core"); const io_1 = require("@serenity-js/core/lib/io"); /** * HTTP Request sent by the [`Actor`](https://serenity-js.org/api/core/class/Actor/) * using the [interaction](https://serenity-js.org/api/core/class/Interaction/) to [`Send`](https://serenity-js.org/api/rest/class/Send/) * * @group Models */ class HTTPRequest extends core_1.Question { resourceUri; data; config; /** * @param [resourceUri] * URL to which the request should be sent * * @param [data] * Request body to be sent as part of the Put, Post or Patch request * * @param {Answerable<WithAnswerableProperties<AxiosRequestConfig>>} [config] * Axios request configuration, which can be used to override the defaults * provided when the [ability](https://serenity-js.org/api/core/class/Ability/) to [`CallAnApi`](https://serenity-js.org/api/rest/class/CallAnApi/) is instantiated */ constructor(resourceUri, data, config) { super(`${HTTPRequest.requestDescription(new.target.name)} to ${(0, io_1.d) `${resourceUri}`}`); this.resourceUri = resourceUri; this.data = data; this.config = config; } /** * @inheritDoc */ answeredBy(actor) { return Promise.all([ this.resourceUri ? actor.answer(this.resourceUri) : Promise.resolve(void 0), this.config ? actor.answer(this.config) : Promise.resolve({}), this.data ? actor.answer(this.data) : Promise.resolve(void 0), ]). then(([url, config, data]) => Object.assign({}, { url, data }, config, { method: HTTPRequest.httpMethodName(this.constructor.name) })). then(config => // eslint-disable-next-line unicorn/prefer-object-from-entries Object.keys(config).reduce((acc, key) => { if (config[key] !== null && config[key] !== undefined) { acc[key] = config[key]; } return acc; }, {})); } /** * Determines the request method based on the name of the request class. * For example: GetRequest => GET, PostRequest => POST, etc. */ static httpMethodName(className) { return className.replace(/Request/, '').toUpperCase(); } /** * A human-readable description of the request, such as "a GET request", "an OPTIONS request", etc. */ static requestDescription(className) { const vowels = ['A', 'E', 'I', 'O', 'U'], method = HTTPRequest.httpMethodName(className); return `${~vowels.indexOf(method[0]) ? 'an' : 'a'} ${method} request`; } } exports.HTTPRequest = HTTPRequest; //# sourceMappingURL=HTTPRequest.js.map