function promiseEnsureIdempotency(req) {
  // Enable feature only for MongoDB
  if (
    !(
      req.config.database.adapter instanceof MongoStorageAdapter ||
      req.config.database.adapter instanceof PostgresStorageAdapter
    )
  ) {
    return Promise.resolve();
  }
  // Get parameters
  const config = req.config;
  const requestId = ((req || {}).headers || {})['x-parse-request-id'];
  const { paths, ttl } = config.idempotencyOptions;
  if (!requestId || !config.idempotencyOptions) {
    return Promise.resolve();
  }
  // Request path may contain trailing slashes, depending on the original request, so remove
  // leading and trailing slashes to make it easier to specify paths in the configuration
  const reqPath = req.path.replace(/^\/|\/$/, '');
  // Determine whether idempotency is enabled for current request path
  let match = false;
  for (const path of paths) {
    // Assume one wants a path to always match from the beginning to prevent any mistakes
    const regex = new RegExp(path.charAt(0) === '^' ? path : '^' + path);
    if (reqPath.match(regex)) {
      match = true;
      break;
    }
  }
  if (!match) {
    return Promise.resolve();
  }
  // Try to store request
  const expiryDate = new Date(new Date().setSeconds(new Date().getSeconds() + ttl));
  return rest
    .create(config, auth.master(config), '_Idempotency', {
      reqId: requestId,
      expire: Parse._encode(expiryDate),
    })
    .catch(e => {
      if (e.code == Parse.Error.DUPLICATE_VALUE) {
        throw new Parse.Error(Parse.Error.DUPLICATE_REQUEST, 'Duplicate request');
      }
      throw e;
    });
}
Deduplicates a request to ensure idempotency. Duplicates are determined by the request ID
in the request header. If a request has no request ID, it is executed anyway.
@param {*} req The request to evaluate.
@returns Promise<{}>