public getPotentialOperations()

in lib/liveValidation/operationSearcher.ts [228:319]


  public getPotentialOperations(requestInfo: ValidationRequest): PotentialOperationsResult {
    if (this.cache.size === 0) {
      const msgStr =
        `Please call "liveValidator.initialize()" before calling this method, ` +
        `so that cache is populated.`;
      throw new Error(msgStr);
    }

    const ret: Writable<PotentialOperationsResult> = {
      matches: [],
      resourceProvider: requestInfo.providerNamespace,
      apiVersion: requestInfo.apiVersion,
    };

    if (requestInfo.pathStr === "") {
      ret.reason = new LiveValidationError(
        ErrorCodes.PathNotFoundInRequestUrl.name,
        `Could not find path from requestUrl: "${requestInfo.requestUrl}".`
      );
      return ret;
    }

    // Search using provider
    const allApiVersions = this.cache.get(requestInfo.providerNamespace);
    let meta;
    if (allApiVersions === undefined) {
      // provider does not exist in cache
      meta = getOavErrorMeta(ErrorCodes.OperationNotFoundInCacheWithProvider.name as any, {
        providerNamespace: requestInfo.providerNamespace,
      });
      ret.reason = new LiveValidationError(
        ErrorCodes.OperationNotFoundInCacheWithProvider.name,
        meta.message
      );
      return ret;
    }

    // Search using api-version found in the requestUrl
    if (!requestInfo.apiVersion) {
      ret.reason = new LiveValidationError(
        ErrorCodes.OperationNotFoundInCacheWithApi.name,
        `Could not find api-version in requestUrl "${requestInfo.requestUrl}".`
      );
      return ret;
    }

    const allMethods = allApiVersions.get(requestInfo.apiVersion);
    if (allMethods === undefined) {
      meta = getOavErrorMeta(ErrorCodes.OperationNotFoundInCacheWithApi.name as any, {
        apiVersion: requestInfo.apiVersion,
        providerNamespace: requestInfo.providerNamespace,
      });
      ret.reason = new LiveValidationError(
        ErrorCodes.OperationNotFoundInCacheWithApi.name,
        meta.message
      );
      return ret;
    }

    const operationsForHttpMethod = allMethods?.get(requestInfo.requestMethod!);
    // Search using requestMethod provided by user
    if (operationsForHttpMethod === undefined) {
      meta = getOavErrorMeta(ErrorCodes.OperationNotFoundInCacheWithVerb.name as any, {
        requestMethod: requestInfo.requestMethod,
        apiVersion: requestInfo.apiVersion,
        providerNamespace: requestInfo.providerNamespace,
      });
      ret.reason = new LiveValidationError(
        ErrorCodes.OperationNotFoundInCacheWithVerb.name,
        meta.message
      );
      return ret;
    }

    // Find the best match using regex on path
    ret.matches = getMatchedOperations(
      requestInfo.host!,
      requestInfo.pathStr!,
      operationsForHttpMethod,
      requestInfo.query
    );
    if (ret.matches.length === 0 && ret.reason === undefined) {
      meta = getOavErrorMeta(ErrorCodes.OperationNotFoundInCache.name as any, {
        requestMethod: requestInfo.requestMethod,
        apiVersion: requestInfo.apiVersion,
        providerNamespace: requestInfo.providerNamespace,
      });
      ret.reason = new LiveValidationError(ErrorCodes.OperationNotFoundInCache.name, meta.message);
    }

    return ret;
  }