Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse Server Response #20

Open
ttessarolo opened this issue Dec 16, 2023 · 1 comment
Open

Parse Server Response #20

ttessarolo opened this issue Dec 16, 2023 · 1 comment

Comments

@ttessarolo
Copy link

Hi –
the decode process works fine only if the server respond with just strings.

But the Server-Sent Events (SSE) standard implies a more complex response:

interface EventMessage {
  /**
   * Message payload
   */
  data?: string;

  /**
   * Message identifier, if set, client will send `Last-Event-ID: <id>` header on reconnect
   */
  id?: string;

  /**
   * Message type
   */
  event?: string;

  /**
   * Update client reconnect interval (how long will client wait before trying to reconnect).
   */
  retry?: number;

  /**
   * Message comment
   */
  comment?: string;
}

Fort that reason I suggest to add an option to let the code not just parse simple text answers but also a SSE compliant response.

Inside the decodeStreamToJson function you could parse the complex response like that:

const decoded = decoder.decode(value);
        const splitted = decoded
          .split("\n")
          .filter((s) => {
            if (s?.startsWith("data:")) return true;
            return false;
          })
          .map((s) => s.replace("data: ", ""));

        if (splitted.length > 0 && splitted[0] !== "Stream closed") {
          yield splitted[0];
        }

Just a suggestion. Thanks for this Hook ;)

@niels-bosman
Copy link
Member

Hi @ttessarolo, thanks for the suggestion! Feel free to create a PR with this solution and I will test it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants