ecyrbe / zodios

typescript http client and server with zod validation
https://www.zodios.org/
MIT License
1.71k stars 46 forks source link

How to handle stream responses? #604

Closed jung-han closed 6 months ago

jung-han commented 6 months ago

Hello @ecyrbe! Thank you for creating a good library. I am receiving great help during development.

I think it's related to this issue(https://github.com/ecyrbe/zodios/issues/390), I have a question. Currently, we are in a situation where we need to receive openai responses via streaming.

responseType: 'stream', I am receiving a response with the Responsetype set to stream, but I am looking for a way to control it.

I thought onDownloadProgress could be a solution, but the parameters do not contain the data being passed. What method should I use to handle data in real time? I think there was some talk about middleware. Is there anything that can be of reference?

Thanks for reading! I'll wait for your reply.

jung-han commented 6 months ago

related issue: https://github.com/axios/axios/issues/479

jung-han commented 6 months ago

with RQ.

import { useMutation } from '@tanstack/react-query';
import { StatusCodes } from 'http-status-codes';
import { z } from 'zod';

export const useExecutePromptMutation = ({ onStream, onError }) => {
  return useMutation({
    mutationFn: async ({ data }: { data: SendMessageDto }) => {
      await api.sendMessage(data, {
        responseType: 'stream',
        onDownloadProgress: (progress) => {
          const request = progress.event.target;

          if (request.status === StatusCodes.OK || request.status === StatusCodes.CREATED) {
            onStream(request.response); // response
          } else {
            onError(request.status);
          }
        },
      });
    },
  });
};