Effect CommunityEC
Effect Community16mo ago
11 replies
Chris

Challenges in Refactoring HttpClient Pipeline for OpenSearch Operations

Hi I'm updating a project using effect/platform/HttpClient to the latest refactor but find it hard to do using the pipeline approach, I had a service which abstracts away opensearch operations in an effectful pattern, with some simplification for the example:
const _query = (query: esb.RequestBodySearch, {index}: {index: string}) =>
    envVars.OPENSEARCH_URL.pipe(
        E.map((baseUrl) =>
            HttpClientRequest.post(`${baseUrl}/${index}/_search`),
        ),
        E.flatMap(applyAuth),
        E.flatMap(HttpClientRequest.jsonBody(query.toJSON())), // This had been renamed easy to fix
        E.flatMap(HttpClient.fetchOk), // This is now failing of course
        E.flatMap(HttpClientResponse.schemaBodyJson(SearchResult)),
        E.scoped,
        E.catchAll(E.die),
    )

export class OpensearchClient extends Context.Tag('OpensearchClient')<
    OpensearchClient,
    {
        readonly query: typeof _query
        readonly put: typeof _put
        readonly remove: typeof _remove
    }
>() {}

export const OpensearchClientLive = Layer.succeed(OpensearchClient, {
    query: _query,
    put: _put,
    remove: _remove,
})


after changing to this: it works except for the filterStatusOk part:
const _query = (query: esb.RequestBodySearch, {index}: {index: string}) =>
    envVars.OPENSEARCH_URL.pipe(
        E.map((baseUrl) =>
            HttpClientRequest.post(`${baseUrl}/${index}/_search`),
        ),
        E.flatMap(applyAuth),
        E.flatMap(HttpClientRequest.bodyJson(query.toJSON())),
        E.bindTo('request'),
        E.bind('client', () => HttpClient.HttpClient), // Missing the part for 2xx statuscode filtering
        // E.bind('client', () => HttpClient.HttpClient.pipe(HttpClient.filterStatusOk)), // This would not work
        E.flatMap(({request, client}) => client.execute(request)),
        E.flatMap(HttpClientResponse.schemaBodyJson(SearchResult)),
        E.scoped,
        E.catchAll(E.die),
Was this page helpful?