Effect CommunityEC
Effect Community8mo ago
6 replies
Jambudipa

Streaming responses from HttpApi of my own schema types

The HttpApi streaming stuff appears to have improved greatly since I looked last a few months ago. Or maybe I got that wrong and I just understand it better!

Let's say I have this piece of code:
export const myApi = HttpApi.make('myApi').add(
  HttpApiGroup.make('chat').add(
    HttpApiEndpoint.get('getStream', '/stream')
      .setUrlParams(
        Schema.Struct({
          message: Schema.String
        })
      )
      .addSuccess(
        Schema.String.pipe(
          HttpApiSchema.withEncoding({
            kind: 'Text',
            contentType: 'application/octet-stream'
          })
        )
      )
  )
);

Now, this works great, if I convert everything using TextEncoder, even my own schema types. But I would like to know if the above code can be augmented to support my own schemas, like this:
export const AIMessage = Schema.Struct({
  content: Schema.String
});

export type AIMessageType = Schema.Schema.Type<typeof AIMessage>;

Of course it works fine like that, if I convert it, but in order to materialise it on the client side, I would have to perhaps add a discriminator, like type: 'AIMessage', or something like that.

Is there a baked-in way to have effect encode and decode automatically when streaming?
Was this page helpful?