Effect CommunityEC
Effect Community7mo ago
3 replies
Ammar

How do I get a streamed response in HttpApiClient?

i have this code from the effect github repo that sets up an HttpApiEndpoint that streams the response
// Define the API with a single streaming endpoint
export const myApi = HttpApi.make("myApi").add(
  HttpApiGroup.make("group")
    .add(
      HttpApiEndpoint.get("getStream", "/stream").addSuccess(
        Schema.String.pipe(
          HttpApiSchema.withEncoding({
            kind: "Text",
            contentType: "application/octet-stream",
          }),
        ),
      ),
    )
    .prefix("/api"),
);

// Simulate a stream of data
const stream = Stream.make("a", "b", "c").pipe(
  Stream.schedule(Schedule.spaced("500 millis")),
  Stream.map((s) => new TextEncoder().encode(s)),
);

const groupLive = HttpApiBuilder.group(myApi, "group", (handlers) =>
  handlers.handleRaw("getStream", () => HttpServerResponse.stream(stream)),
);

const MyApiLive = HttpApiBuilder.api(myApi).pipe(Layer.provide(groupLive));


however, when I call this endpoint on the client, I get a string instead of the stream
export const program = Effect.gen(function* () {
  const client = yield* HttpApiClient.make(myApi, {
    baseUrl: "http://localhost:3000",
  });

  // const stream: string
  const stream = yield* client.group.getStream();

  console.log(stream);

  return stream;
});


how do I get the streamed response on the client?
Was this page helpful?