How to use @pollyjs/utils - 10 common examples

To help you get started, we’ve selected a few @pollyjs/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Netflix / pollyjs / packages / @pollyjs / adapter / src / index.js View on Github external
const recordingEntry = await this.persister.findEntry(pollyRequest);

    if (recordingEntry) {
      await pollyRequest._emit('beforeReplay', recordingEntry);

      if (isExpired(recordingEntry.startedDateTime, config.expiresIn)) {
        const message =
          'Recording for the following request has expired.\n' +
          `${stringifyRequest(pollyRequest, null, 2)}`;

        switch (config.expiryStrategy) {
          // exit into the record flow if expiryStrategy is "record".
          case EXPIRY_STRATEGIES.RECORD:
            return this.record(pollyRequest);
          // throw an error and exit if expiryStrategy is "error".
          case EXPIRY_STRATEGIES.ERROR:
            this.assert(message);
            break;
          // log a warning and continue if expiryStrategy is "warn".
          case EXPIRY_STRATEGIES.WARN:
            console.warn(`[Polly] ${message}`);
            break;
          // throw an error if we encounter an unsupported expiryStrategy.
          default:
            this.assert(
              `Invalid config option passed for "expiryStrategy": "${config.expiryStrategy}"`
            );
            break;
        }
      }

      await this.timeout(pollyRequest, recordingEntry);
github Netflix / pollyjs / packages / @pollyjs / adapter / src / index.js View on Github external
async replay(pollyRequest) {
    const { config } = pollyRequest;
    const recordingEntry = await this.persister.findEntry(pollyRequest);

    if (recordingEntry) {
      await pollyRequest._emit('beforeReplay', recordingEntry);

      if (isExpired(recordingEntry.startedDateTime, config.expiresIn)) {
        const message =
          'Recording for the following request has expired.\n' +
          `${stringifyRequest(pollyRequest, null, 2)}`;

        switch (config.expiryStrategy) {
          // exit into the record flow if expiryStrategy is "record".
          case EXPIRY_STRATEGIES.RECORD:
            return this.record(pollyRequest);
          // throw an error and exit if expiryStrategy is "error".
          case EXPIRY_STRATEGIES.ERROR:
            this.assert(message);
            break;
          // log a warning and continue if expiryStrategy is "warn".
          case EXPIRY_STRATEGIES.WARN:
            console.warn(`[Polly] ${message}`);
            break;
          // throw an error if we encounter an unsupported expiryStrategy.
          default:
            this.assert(
              `Invalid config option passed for "expiryStrategy": "${config.expiryStrategy}"`
            );
            break;
        }
github Netflix / pollyjs / packages / @pollyjs / adapter-node-http / src / -private / transport-wrapper.js View on Github external
}

        // No need to carry callback around. This is what happens in original `end`.
        if (typeof callback === 'function') {
          req.once('finish', callback);
        }

        const headers =
          typeof req.getHeaders === 'function'
            ? req.getHeaders()
            : req.headers || req._headers || {};
        const host = headers.host;
        const [hostname, port = '80'] = host.split(':');
        const { method, path } = req;

        const parsedUrl = new URL('');

        parsedUrl.set('protocol', req.agent.protocol);
        parsedUrl.set('pathname', path);
        parsedUrl.set('hostname', hostname);
        parsedUrl.set('port', port !== '80' ? port : '');

        adapter
          .handleRequest({
            method,
            headers,
            url: parsedUrl.href,
            body: chunks,
            requestArguments: [wrapper, req, args]
          })
          .catch(e => {
            // This allows the consumer to handle the error gracefully
github Netflix / pollyjs / packages / @pollyjs / adapter / src / index.js View on Github external
break;
          // log a warning and continue if expiryStrategy is "warn".
          case EXPIRY_STRATEGIES.WARN:
            console.warn(`[Polly] ${message}`);
            break;
          // throw an error if we encounter an unsupported expiryStrategy.
          default:
            this.assert(
              `Invalid config option passed for "expiryStrategy": "${config.expiryStrategy}"`
            );
            break;
        }
      }

      await this.timeout(pollyRequest, recordingEntry);
      pollyRequest.action = ACTIONS.REPLAY;

      return this.onReplay(
        pollyRequest,
        normalizeRecordedResponse(recordingEntry.response),
        recordingEntry
      );
    }

    if (config.recordIfMissing) {
      return this.record(pollyRequest);
    }

    this.assert(
      'Recording for the following request is not found and `recordIfMissing` is `false`.\n' +
        stringifyRequest(pollyRequest, null, 2)
    );
github Netflix / pollyjs / packages / @pollyjs / core / src / utils / merge-configs.js View on Github external
function deprecateRecordIfExpired(mergedConfig) {
  if (mergedConfig.hasOwnProperty('recordIfExpired')) {
    console.warn(
      '[Polly] config option "recordIfExpired" is deprecated. Please use "expiryStrategy".'
    );

    if (mergedConfig.recordIfExpired) {
      // replace recordIfExpired: true with expiryStrategy: record
      mergedConfig.expiryStrategy = EXPIRY_STRATEGIES.RECORD;
    } else {
      // replace recordIfExpired: false with expiryStrategy: warn
      mergedConfig.expiryStrategy = EXPIRY_STRATEGIES.WARN;
    }

    delete mergedConfig.recordIfExpired;
  }

  return mergedConfig;
}
github Netflix / pollyjs / packages / @pollyjs / adapter / src / index.js View on Github external
if (isExpired(recordingEntry.startedDateTime, config.expiresIn)) {
        const message =
          'Recording for the following request has expired.\n' +
          `${stringifyRequest(pollyRequest, null, 2)}`;

        switch (config.expiryStrategy) {
          // exit into the record flow if expiryStrategy is "record".
          case EXPIRY_STRATEGIES.RECORD:
            return this.record(pollyRequest);
          // throw an error and exit if expiryStrategy is "error".
          case EXPIRY_STRATEGIES.ERROR:
            this.assert(message);
            break;
          // log a warning and continue if expiryStrategy is "warn".
          case EXPIRY_STRATEGIES.WARN:
            console.warn(`[Polly] ${message}`);
            break;
          // throw an error if we encounter an unsupported expiryStrategy.
          default:
            this.assert(
              `Invalid config option passed for "expiryStrategy": "${config.expiryStrategy}"`
            );
            break;
        }
      }

      await this.timeout(pollyRequest, recordingEntry);
      pollyRequest.action = ACTIONS.REPLAY;

      return this.onReplay(
        pollyRequest,
github Netflix / pollyjs / packages / @pollyjs / adapter / src / index.js View on Github external
async onRequestFailed(pollyRequest, error) {
    error = error || new PollyError('Request failed due to an unknown error.');

    try {
      await pollyRequest._emit('error', error);
      await this.respondToRequest(pollyRequest, error);
    } catch (e) {
      // Rethrow any error not handled by `respondToRequest`.
      throw e;
    } finally {
      pollyRequest.promise.reject(error);
    }
  }
}
github Netflix / pollyjs / packages / @pollyjs / core / src / utils / merge-configs.js View on Github external
function deprecateRecordIfExpired(mergedConfig) {
  if (mergedConfig.hasOwnProperty('recordIfExpired')) {
    console.warn(
      '[Polly] config option "recordIfExpired" is deprecated. Please use "expiryStrategy".'
    );

    if (mergedConfig.recordIfExpired) {
      // replace recordIfExpired: true with expiryStrategy: record
      mergedConfig.expiryStrategy = EXPIRY_STRATEGIES.RECORD;
    } else {
      // replace recordIfExpired: false with expiryStrategy: warn
      mergedConfig.expiryStrategy = EXPIRY_STRATEGIES.WARN;
    }

    delete mergedConfig.recordIfExpired;
  }

  return mergedConfig;
}
github Netflix / pollyjs / packages / @pollyjs / core / src / -private / request.js View on Github external
async respond(response) {
    const { statusCode, headers, body } = response || {};

    assert(
      'Cannot respond to a request that already has a response.',
      !this.didRespond
    );

    // Timestamp the response
    this.response.timestamp = timestamp();

    // Set the status code
    this.response.status(statusCode);

    // Se the headers
    this.response.setHeaders(headers);

    // Set the body without modifying any headers (instead of using .send())
    this.response.body = body;

    // Trigger the `beforeResponse` event
    await this._emit('beforeResponse', this.response);

    // End the response so it can no longer be modified
    this.response.end();
github Netflix / pollyjs / packages / @pollyjs / core / src / -private / request.js View on Github external
async setup() {
    // Trigger the `request` event
    await this._emit('request');

    // Setup the response
    this.response = new PollyResponse();
    this.didRespond = false;

    // Setup this request's identifiers, id, and order
    await this._identify();

    // Timestamp the request
    this.timestamp = timestamp();
  }