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

29 lines 1.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createUrl = createUrl; const tiny_types_1 = require("tiny-types"); function createUrl(options) { const hostname = (0, tiny_types_1.ensure)('hostname', options?.hostname, (0, tiny_types_1.isString)(), (0, tiny_types_1.isNotBlank)()).trim(); const port = options?.port ? ':' + options?.port : (options?.protocol ? undefined : ':80'); return new URL([ options?.protocol ? protocolFrom(options?.protocol) : 'http://', (options?.username || options?.password) && credentialsFrom(options.username, options.password), hostname, port, ].filter(Boolean).join('')); } function protocolFrom(protocol) { const protocolName = protocol.match(/([A-Za-z]+)[/:]*/)[1]; (0, tiny_types_1.ensure)('hostname', protocolName, (0, tiny_types_1.isDefined)()); return protocolName + '://'; } function credentialsFrom(username, password) { return [ username && encodeURIComponent(username), password && ':' + encodeURIComponent(password), '@' ].filter(Boolean).join(''); } //# sourceMappingURL=createUrl.js.map