Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
configure(config = {}) {
const { container } = this;
assert(
'Cannot call `configure` once requests have been handled.',
this._requests.length === 0
);
assert(
'Cannot call `configure` on an instance of Polly that is not running.',
this.mode !== MODES.STOPPED
);
// Disconnect from all current adapters before updating the config
this.disconnect();
this.config = mergeConfigs(DefaultConfig, this.config, config);
// Register and connect to all specified adapters
this.config.adapters.forEach(adapter => this.connectTo(adapter));
/* Handle Persister */
let { persister } = this.config;
if (persister) {
export function validateRequestConfig(config) {
assert(
`Invalid config provided. Expected object, received: "${typeof config}".`,
isObjectLike(config) && !Array.isArray(config)
);
// The following options cannot be overridden on a per request basis
[
'mode',
'adapters',
'adapterOptions',
'persister',
'persisterOptions'
].forEach(key =>
assert(
`Invalid configuration option provided. The "${key}" option cannot be overridden using the server configuration API.`,
!(key in config)
)
this.config = mergeConfigs(DefaultConfig, this.config, config);
// Register and connect to all specified adapters
this.config.adapters.forEach(adapter => this.connectTo(adapter));
/* Handle Persister */
let { persister } = this.config;
if (persister) {
if (typeof persister === 'function') {
container.register(persister);
persister = persister.name;
}
assert(
`Persister matching the name \`${persister}\` was not registered.`,
container.has(`persister:${persister}`)
);
this.persister = new (container.lookup(`persister:${persister}`))(this);
}
}
static get name() {
assert('Must override the static `name` getter.');
}
export function validateRecordingName(name) {
assert(
`Invalid recording name provided. Expected string, received: "${typeof name}".`,
typeof name === 'string'
);
assert(
`Invalid recording name provided. Received An empty or blank string.`,
name.trim().length > 0
);
}
assert(message, ...args) {
assert(
`[${this.constructor.type}:${this.constructor.name}] ${message}`,
...args
);
}
function assertListener(listener) {
assert(
`Invalid listener provided. Expected function, received: "${typeof listener}".`,
typeof listener === 'function'
);
}
emitSync(eventName, ...args) {
assertEventName(eventName, this[EVENT_NAMES]);
const event = new Event(eventName);
for (const listener of this.listeners(eventName)) {
const returnValue = listener(...args, event);
assert(
`Attempted to emit a synchronous event "${eventName}" but an asynchronous listener was called.`,
!(isObjectLike(returnValue) && typeof returnValue.then === 'function')
);
if (event.shouldStopPropagating) {
return false;
}
}
return true;
}
}
status(statusCode) {
const status = parseInt(statusCode, 10);
assert(
`[Response] Invalid status code: ${status}`,
status >= 100 && status < 600
);
this.statusCode = status;
return this;
}