public static async Task <Stream> QueryContinuousWithFilterAsync <TSource>(this IFlurlRequest request, CouchOptions options, ChangesFeedFilter filter, CancellationToken cancellationToken)
            where TSource : CouchDocument
        {
            if (filter is DocumentIdsChangesFeedFilter documentIdsFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_doc_ids")
                       .PostJsonStreamAsync(new ChangesFeedFilterDocuments(documentIdsFilter.Value), cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }

            if (filter is SelectorChangesFeedFilter <TSource> selectorFilter)
            {
                MethodCallExpression whereExpression = Expression.Call(typeof(Queryable), nameof(Queryable.Where),
                                                                       new[] { typeof(TSource) }, Expression.Constant(Array.Empty <TSource>().AsQueryable()), selectorFilter.Value);

                var        optimizer      = new QueryOptimizer();
                Expression optimizedQuery = optimizer.Optimize(whereExpression);
                var        jsonSelector   = new QueryTranslator(options).Translate(optimizedQuery);
                return(await request
                       .WithHeader("Content-Type", "application/json")
                       .SetQueryParam("filter", "_selector")
                       .PostStringStreamAsync(jsonSelector, cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }

            if (filter is DesignChangesFeedFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_design")
                       .GetStreamAsync(cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }

            if (filter is ViewChangesFeedFilter viewFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_view")
                       .SetQueryParam("view", viewFilter.Value)
                       .GetStreamAsync(cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }
            throw new InvalidOperationException($"Filter of type {filter.GetType().Name} not supported.");
        }
        public static async Task <Stream> QueryContinuousWithFilterAsync <TSource>(this IFlurlRequest request, IAsyncQueryProvider queryProvider, ChangesFeedFilter filter, CancellationToken cancellationToken)
            where TSource : CouchDocument
        {
            if (filter is DocumentIdsChangesFeedFilter documentIdsFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_doc_ids")
                       .PostJsonStreamAsync(new ChangesFeedFilterDocuments(documentIdsFilter.Value), cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }

            if (filter is SelectorChangesFeedFilter <TSource> selectorFilter)
            {
                MethodCallExpression whereExpression = selectorFilter.Value.WrapInWhereExpression();
                var jsonSelector = queryProvider.ToString(whereExpression);

                return(await request
                       .WithHeader("Content-Type", "application/json")
                       .SetQueryParam("filter", "_selector")
                       .PostStringStreamAsync(jsonSelector, cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }

            if (filter is DesignChangesFeedFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_design")
                       .GetStreamAsync(cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }

            if (filter is ViewChangesFeedFilter viewFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_view")
                       .SetQueryParam("view", viewFilter.Value)
                       .GetStreamAsync(cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }
            throw new InvalidOperationException($"Filter of type {filter.GetType().Name} not supported.");
        }