razshare / sveltekit-sse

Server Sent Events with SvelteKit
https://www.npmjs.com/package/sveltekit-sse
MIT License
301 stars 9 forks source link

Feature request: sourceJSON() to receive JSON objects on client side #21

Closed bluepuma77 closed 9 months ago

bluepuma77 commented 9 months ago

The default library seems very easy to use with strings on the client side:

<script>
  import { source } from 'sveltekit-sse'
  const value = source('/custom-event')
</script>
{$value}

I would be great to have an additional simple sourceJSON() to get JSON objects on the client side, that's probably a very standard use case for developers:

<script>
  import { sourceJSON } from 'sveltekit-sse'
  const value = sourceJSON('/custom-event-json')
</script>
{JSON.stringify($value)}

The whole Transform looks a bit too complicated when just using JSON.

razshare commented 9 months ago

Yeah I agree the transform thing is too much just to convert stuff to json.

It should be easy to achieve, I'll take a look at it tomorrow, Idk if I'll manage to implement it then.

I'll try to get it done by Sunday though.

Thanks for the suggestion!

bluepuma77 commented 9 months ago

Got basics to work, maybe a little info in README.md is enough to help devs with JSON:

<script>
  import { source } from 'sveltekit-sse'
  const json = source('/custom-event')
  let obj = {}
  $: if ($json) { obj = JSON.parse($json || {}) }
</script>
{JSON.stringify(obj}

Thanks. I will check for sourceJSON() but feel free to just close this issue if you like.

razshare commented 9 months ago

Hi @bluepuma77 , I just released v0.6.0 with a live json parsing feature.

You can read more about it in the Json section on the home page.

I also added this section which is relevant to the json parsing feature.

Let me know if you encounter any other issues.