@laravel/stream-vue 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Laravel Stream for Vue
2
+
3
+ <p align="left">
4
+ <a href="https://github.com/laravel/stream/actions/workflows/tests.yml"><img src="https://github.com/laravel/stream/actions/workflows/tests.yml/badge.svg" alt="Build Status"></a>
5
+ <a href="https://www.npmjs.com/package/@laravel/stream-vue"><img src="https://img.shields.io/npm/dt/@laravel/stream-vue" alt="Total Downloads"></a>
6
+ <a href="https://www.npmjs.com/package/@laravel/stream-vue"><img src="https://img.shields.io/npm/v/@laravel/stream-vue" alt="Latest Stable Version"></a>
7
+ <a href="https://www.npmjs.com/package/@laravel/stream-vue"><img src="https://img.shields.io/npm/l/@laravel/stream-vue" alt="License"></a>
8
+ </p>
9
+
10
+ Easily consume [Server-Sent Events (SSE)](https://laravel.com/docs/responses#event-streams) in your Vue application.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install @laravel/stream-vue
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ Provide your stream URL and the hook will automatically update the `message` with the concatenated response as messages are returned from your server:
21
+
22
+ ```vue
23
+ <script setup lang="ts">
24
+ import { useStream } from "@laravel/stream-vue";
25
+
26
+ const { message } = useStream("/stream");
27
+ </script>
28
+
29
+ <template>
30
+ <div>{{ message }}</div>
31
+ </template>
32
+ ```
33
+
34
+ You also have access to the array of message parts:
35
+
36
+ ```vue
37
+ <script setup lang="ts">
38
+ import { useStream } from "@laravel/stream-vue";
39
+
40
+ const { messageParts } = useStream("/stream");
41
+ </script>
42
+
43
+ <template>
44
+ <ul>
45
+ <li v-for="message in messageParts">
46
+ {{ message }}
47
+ </li>
48
+ </ul>
49
+ </template>
50
+ ```
51
+
52
+ The second parameter is options object, all properties are optional (defaults are shown here):
53
+
54
+ ```vue
55
+ <script setup lang="ts">
56
+ import { useStream } from "@laravel/stream-vue";
57
+
58
+ const { message } = useStream("/stream", {
59
+ event: "update",
60
+ endSignal: "</stream>",
61
+ glue: " ",
62
+ onMessage: (message) => {
63
+ //
64
+ },
65
+ onError: (error) => {
66
+ //
67
+ },
68
+ onComplete: () => {
69
+ //
70
+ },
71
+ });
72
+ </script>
73
+ ```
74
+
75
+ ## License
76
+
77
+ Laravel Stream is open-sourced software licensed under the [MIT license](LICENSE.md).
@@ -0,0 +1,31 @@
1
+ import { Ref } from 'vue';
2
+
3
+ declare type Options = {
4
+ eventName?: string;
5
+ endSignal?: string;
6
+ glue?: string;
7
+ onMessage?: (event: MessageEvent) => void;
8
+ onComplete?: () => void;
9
+ onError?: (error: Event) => void;
10
+ };
11
+
12
+ declare type StreamResult = {
13
+ message: Readonly<Ref<string>>;
14
+ messageParts: Readonly<Ref<readonly string[]>>;
15
+ close: (resetMessage?: boolean) => void;
16
+ clearMessage: () => void;
17
+ };
18
+
19
+ /**
20
+ * Composable for handling server-sent events (SSE) streams
21
+ *
22
+ * @param url - The URL to connect to for the EventSource
23
+ * @param options - Options for the stream
24
+ *
25
+ * @link https://laravel.com/docs/responses#event-streams
26
+ *
27
+ * @returns StreamResult object containing the accumulated response, close, and reset functions
28
+ */
29
+ export declare const useStream: (url: string, { eventName, endSignal, glue, onMessage, onComplete, onError, }?: Options) => StreamResult;
30
+
31
+ export { }
@@ -0,0 +1,47 @@
1
+ import { ref as g, onMounted as M, onUnmounted as P, watch as w, readonly as f } from "vue";
2
+ const o = "data: ", C = (l, {
3
+ eventName: d = "update",
4
+ endSignal: u = "</stream>",
5
+ glue: v = " ",
6
+ onMessage: h = () => null,
7
+ onComplete: E = () => null,
8
+ onError: p = () => null
9
+ } = {}) => {
10
+ const n = g(""), a = g([]);
11
+ let e = null;
12
+ const r = () => {
13
+ n.value = "", a.value = [];
14
+ }, s = (t = !1) => {
15
+ e == null || e.removeEventListener(d, i), e == null || e.removeEventListener("error", c), e == null || e.close(), e = null, t && r();
16
+ }, i = (t) => {
17
+ if ([u, `${o}${u}`].includes(t.data)) {
18
+ s(), E();
19
+ return;
20
+ }
21
+ a.value.push(
22
+ t.data.startsWith(o) ? t.data.substring(o.length) : t.data
23
+ ), n.value = a.value.join(v), h(t);
24
+ }, c = (t) => {
25
+ p(t), s();
26
+ }, m = () => {
27
+ r(), e = new EventSource(l), e.addEventListener(d, i), e.addEventListener("error", c);
28
+ };
29
+ return M(() => {
30
+ m();
31
+ }), P(() => {
32
+ s();
33
+ }), w(
34
+ () => l,
35
+ (t, L) => {
36
+ t !== L && (s(), m());
37
+ }
38
+ ), {
39
+ message: f(n),
40
+ messageParts: f(a),
41
+ close: s,
42
+ clearMessage: r
43
+ };
44
+ };
45
+ export {
46
+ C as useStream
47
+ };
@@ -0,0 +1 @@
1
+ (function(s,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],t):(s=typeof globalThis<"u"?globalThis:s||self,t(s.LaravelStreamVue={},s.Vue))})(this,function(s,t){"use strict";const r="data: ",g=(i,{eventName:u="update",endSignal:f="</stream>",glue:h=" ",onMessage:y=()=>null,onComplete:E=()=>null,onError:S=()=>null}={})=>{const l=t.ref(""),o=t.ref([]);let e=null;const d=()=>{l.value="",o.value=[]},a=(n=!1)=>{e==null||e.removeEventListener(u,c),e==null||e.removeEventListener("error",m),e==null||e.close(),e=null,n&&d()},c=n=>{if([f,`${r}${f}`].includes(n.data)){a(),E();return}o.value.push(n.data.startsWith(r)?n.data.substring(r.length):n.data),l.value=o.value.join(h),y(n)},m=n=>{S(n),a()},p=()=>{d(),e=new EventSource(i),e.addEventListener(u,c),e.addEventListener("error",m)};return t.onMounted(()=>{p()}),t.onUnmounted(()=>{a()}),t.watch(()=>i,(n,v)=>{n!==v&&(a(),p())}),{message:t.readonly(l),messageParts:t.readonly(o),close:a,clearMessage:d}};s.useStream=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@laravel/stream-vue",
3
+ "version": "0.1.0",
4
+ "description": "The Laravel useStream hook for Vue",
5
+ "keywords": [
6
+ "laravel",
7
+ "stream",
8
+ "use-stream",
9
+ "server-sent-events",
10
+ "sse",
11
+ "vue",
12
+ "composables"
13
+ ],
14
+ "homepage": "https://github.com/laravel/stream/tree/main/packages/vue#readme",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/laravel/stream.git"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/laravel/stream/issues"
21
+ },
22
+ "license": "MIT",
23
+ "author": {
24
+ "name": "Taylor Otwell"
25
+ },
26
+ "type": "module",
27
+ "main": "dist/index.umd.js",
28
+ "module": "dist/index.es.js",
29
+ "types": "dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.es.js",
34
+ "require": "./dist/index.umd.js"
35
+ }
36
+ },
37
+ "files": [
38
+ "dist"
39
+ ],
40
+ "devDependencies": {
41
+ "@types/node": "^22.14.0",
42
+ "@typescript-eslint/eslint-plugin": "^8.21.0",
43
+ "@typescript-eslint/parser": "^8.21.0",
44
+ "@vitejs/plugin-vue": "^5.0.0",
45
+ "eslint": "^9.0.0",
46
+ "jsdom": "^26.0.0",
47
+ "prettier": "^3.5.3",
48
+ "typescript": "^5.3.0",
49
+ "vite": "^5.4.19",
50
+ "vite-plugin-dts": "^4.5.3",
51
+ "vitest": "^3.1.1"
52
+ },
53
+ "peerDependencies": {
54
+ "vue": "^3.0.0"
55
+ },
56
+ "scripts": {
57
+ "build": "vite build",
58
+ "lint": "eslint --config eslint.config.mjs \"src/**/*.ts\"",
59
+ "prepublish": "pnpm run build",
60
+ "release": "vitest --run && git push --follow-tags && pnpm publish",
61
+ "test": "vitest",
62
+ "format": "prettier --write ."
63
+ }
64
+ }