@laravel/stream-react 0.3.4 → 0.3.6
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/dist/index.d.ts +6 -6
- package/dist/index.es.js +2 -2
- package/dist/index.umd.js +1 -1
- package/package.json +66 -66
package/dist/index.d.ts
CHANGED
|
@@ -15,9 +15,9 @@ declare type EventStreamResult = {
|
|
|
15
15
|
clearMessage: () => void;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
declare type StreamOptions = {
|
|
18
|
+
declare type StreamOptions<TSendBody extends Record<string, any> = {}> = {
|
|
19
19
|
id?: string;
|
|
20
|
-
initialInput?:
|
|
20
|
+
initialInput?: TSendBody;
|
|
21
21
|
headers?: Record<string, string>;
|
|
22
22
|
csrfToken?: string;
|
|
23
23
|
json?: boolean;
|
|
@@ -42,24 +42,24 @@ declare type StreamOptions = {
|
|
|
42
42
|
*/
|
|
43
43
|
export declare const useEventStream: (url: string, { eventName, endSignal, glue, replace, onMessage, onComplete, onError, }?: EventStreamOptions) => EventStreamResult;
|
|
44
44
|
|
|
45
|
-
export declare const useJsonStream: <TJsonData = null>(url: string, options?: Omit<StreamOptions
|
|
45
|
+
export declare const useJsonStream: <TJsonData = null, TSendBody extends Record<string, any> = {}>(url: string, options?: Omit<StreamOptions<TSendBody>, "json">) => {
|
|
46
46
|
isFetching: boolean;
|
|
47
47
|
isStreaming: boolean;
|
|
48
48
|
id: string;
|
|
49
|
-
send: (body:
|
|
49
|
+
send: (body: TSendBody) => void;
|
|
50
50
|
cancel: () => void;
|
|
51
51
|
clearData: () => void;
|
|
52
52
|
data: TJsonData | null;
|
|
53
53
|
strData: string;
|
|
54
54
|
};
|
|
55
55
|
|
|
56
|
-
export declare const useStream: <TJsonData = null>(url: string, options?: StreamOptions) => {
|
|
56
|
+
export declare const useStream: <TSendBody extends Record<string, any> = {}, TJsonData = null>(url: string, options?: StreamOptions<TSendBody>) => {
|
|
57
57
|
data: string;
|
|
58
58
|
jsonData: TJsonData | null;
|
|
59
59
|
isFetching: boolean;
|
|
60
60
|
isStreaming: boolean;
|
|
61
61
|
id: string;
|
|
62
|
-
send: (body:
|
|
62
|
+
send: (body: TSendBody) => void;
|
|
63
63
|
cancel: () => void;
|
|
64
64
|
clearData: () => void;
|
|
65
65
|
};
|
package/dist/index.es.js
CHANGED
|
@@ -155,7 +155,7 @@ const A = /* @__PURE__ */ new Map(), $ = (r, e) => {
|
|
|
155
155
|
jsonData: null
|
|
156
156
|
});
|
|
157
157
|
}, []), D = u(
|
|
158
|
-
(s
|
|
158
|
+
(s) => {
|
|
159
159
|
const a = new AbortController(), c = {
|
|
160
160
|
method: "POST",
|
|
161
161
|
signal: a.signal,
|
|
@@ -163,7 +163,7 @@ const A = /* @__PURE__ */ new Map(), $ = (r, e) => {
|
|
|
163
163
|
...l.current,
|
|
164
164
|
...e.headers ?? {}
|
|
165
165
|
},
|
|
166
|
-
body: JSON.stringify(s),
|
|
166
|
+
body: JSON.stringify(s ?? {}),
|
|
167
167
|
credentials: e.credentials ?? "same-origin"
|
|
168
168
|
}, b = Y(n.current, c);
|
|
169
169
|
b !== !1 && (f({
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(l,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],s):(l=typeof globalThis<"u"?globalThis:l||self,s(l.LaravelStreamReact={},l.React))})(this,function(l,s){"use strict";const A="data: ",x=(t,{eventName:e="update",endSignal:n="</stream>",glue:r=" ",replace:i=!1,onMessage:B=()=>null,onComplete:T=()=>null,onError:P=()=>null}={})=>{const f=s.useRef(null),m=s.useRef([]),C=s.useMemo(()=>Array.isArray(e)?e:[e],Array.isArray(e)?e:[e]),[w,M]=s.useState(""),[d,h]=s.useState([]),g=s.useCallback(()=>{m.current=[],M(""),h([])},[]),R=s.useCallback(a=>{if([n,`${A}${n}`].includes(a.data)){b(),T();return}i&&g(),m.current.push(a.data.startsWith(A)?a.data.substring(A.length):a.data),M(m.current.join(r)),h(m.current),B(a)},[C,r]),y=s.useCallback(a=>{P(a),b()},[]),b=s.useCallback((a=!1)=>{var o,u;C.forEach(D=>{var c;(c=f.current)==null||c.removeEventListener(D,R)}),(o=f.current)==null||o.removeEventListener("error",y),(u=f.current)==null||u.close(),f.current=null,a&&g()},[]);return s.useEffect(()=>(g(),f.current=new EventSource(t),C.forEach(a=>{var o;(o=f.current)==null||o.addEventListener(a,R)}),f.current.addEventListener("error",y),b),[t,C,R,y,g]),{message:w,messageParts:d,close:b,clearMessage:g}},X="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let K=(t=21)=>{let e="",n=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=X[n[t]&63];return e};const k=new Map,V=(t,e)=>{k.has(t)||k.set(t,{onData:[],onError:[],onFinish:[],onResponse:[],onCancel:[],onBeforeSend:[]});const n=k.get(t);return e.onData&&n.onData.push(e.onData),e.onError&&n.onError.push(e.onError),e.onFinish&&n.onFinish.push(e.onFinish),e.onResponse&&n.onResponse.push(e.onResponse),e.onCancel&&n.onCancel.push(e.onCancel),e.onBeforeSend&&n.onBeforeSend.push(e.onBeforeSend),()=>{Q(t,e)}},E=(t,e,...n)=>{const r=k.get(t);return r?r[e].map(i=>i(...n)):[]},p=t=>{E(t,"onFinish")},q=(t,e)=>{E(t,"onError",e)},W=(t,e)=>{E(t,"onResponse",e)},$=t=>{E(t,"onCancel")},G=(t,e)=>{E(t,"onData",e)},H=(t,e)=>{const n=E(t,"onBeforeSend",e);for(const r of n){if(r===!1)return!1;if(r!==null&&typeof r=="object")return r}return null},Q=(t,e)=>{const n=k.get(t);n&&(e.onData&&(n.onData=n.onData.filter(r=>r!==e.onData)),e.onError&&(n.onError=n.onError.filter(r=>r!==e.onError)),e.onFinish&&(n.onFinish=n.onFinish.filter(r=>r!==e.onFinish)),e.onResponse&&(n.onResponse=n.onResponse.filter(r=>r!==e.onResponse)),e.onCancel&&(n.onCancel=n.onCancel.filter(r=>r!==e.onCancel)),e.onBeforeSend&&(n.onBeforeSend=n.onBeforeSend.filter(r=>r!==e.onBeforeSend)))},j=new Map,S=new Map,L=t=>{const e=j.get(t);if(e)return e;const n={controller:new AbortController,data:"",isFetching:!1,isStreaming:!1,jsonData:null};return j.set(t,n),n},I=t=>(S.has(t)||S.set(t,[]),S.get(t)),J=t=>{var e;return S.has(t)&&((e=S.get(t))==null?void 0:e.length)},Y=(t,e)=>(I(t).push(e),()=>{S.set(t,I(t).filter(n=>n!==e)),J(t)||(j.delete(t),S.delete(t))}),Z=(t,e)=>{var r;j.set(t,{...L(t),...e});const n=L(t);(r=S.get(t))==null||r.forEach(i=>i(n))},O=(t,e={})=>{const n=s.useRef(e.id??K()),r=s.useRef(L(n.current)),i=s.useRef((()=>{var u;const a={"Content-Type":"application/json","X-STREAM-ID":n.current},o=e.csrfToken??((u=document.querySelector('meta[name="csrf-token"]'))==null?void 0:u.getAttribute("content"));return o&&(a["X-CSRF-TOKEN"]=o),a})()),[B,T]=s.useState(r.current.data),[P,f]=s.useState(r.current.jsonData),[m,C]=s.useState(r.current.isFetching),[w,M]=s.useState(r.current.isStreaming),d=s.useCallback(a=>{Z(n.current,a)},[]),h=s.useCallback(()=>{r.current.controller.abort(),(m||w)&&$(n.current),d({isFetching:!1,isStreaming:!1})},[m,w]),g=s.useCallback(()=>{d({data:"",jsonData:null})},[]),R=s.useCallback(
|
|
1
|
+
(function(l,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],s):(l=typeof globalThis<"u"?globalThis:l||self,s(l.LaravelStreamReact={},l.React))})(this,function(l,s){"use strict";const A="data: ",x=(t,{eventName:e="update",endSignal:n="</stream>",glue:r=" ",replace:i=!1,onMessage:B=()=>null,onComplete:T=()=>null,onError:P=()=>null}={})=>{const f=s.useRef(null),m=s.useRef([]),C=s.useMemo(()=>Array.isArray(e)?e:[e],Array.isArray(e)?e:[e]),[w,M]=s.useState(""),[d,h]=s.useState([]),g=s.useCallback(()=>{m.current=[],M(""),h([])},[]),R=s.useCallback(a=>{if([n,`${A}${n}`].includes(a.data)){b(),T();return}i&&g(),m.current.push(a.data.startsWith(A)?a.data.substring(A.length):a.data),M(m.current.join(r)),h(m.current),B(a)},[C,r]),y=s.useCallback(a=>{P(a),b()},[]),b=s.useCallback((a=!1)=>{var o,u;C.forEach(D=>{var c;(c=f.current)==null||c.removeEventListener(D,R)}),(o=f.current)==null||o.removeEventListener("error",y),(u=f.current)==null||u.close(),f.current=null,a&&g()},[]);return s.useEffect(()=>(g(),f.current=new EventSource(t),C.forEach(a=>{var o;(o=f.current)==null||o.addEventListener(a,R)}),f.current.addEventListener("error",y),b),[t,C,R,y,g]),{message:w,messageParts:d,close:b,clearMessage:g}},X="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let K=(t=21)=>{let e="",n=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=X[n[t]&63];return e};const k=new Map,V=(t,e)=>{k.has(t)||k.set(t,{onData:[],onError:[],onFinish:[],onResponse:[],onCancel:[],onBeforeSend:[]});const n=k.get(t);return e.onData&&n.onData.push(e.onData),e.onError&&n.onError.push(e.onError),e.onFinish&&n.onFinish.push(e.onFinish),e.onResponse&&n.onResponse.push(e.onResponse),e.onCancel&&n.onCancel.push(e.onCancel),e.onBeforeSend&&n.onBeforeSend.push(e.onBeforeSend),()=>{Q(t,e)}},E=(t,e,...n)=>{const r=k.get(t);return r?r[e].map(i=>i(...n)):[]},p=t=>{E(t,"onFinish")},q=(t,e)=>{E(t,"onError",e)},W=(t,e)=>{E(t,"onResponse",e)},$=t=>{E(t,"onCancel")},G=(t,e)=>{E(t,"onData",e)},H=(t,e)=>{const n=E(t,"onBeforeSend",e);for(const r of n){if(r===!1)return!1;if(r!==null&&typeof r=="object")return r}return null},Q=(t,e)=>{const n=k.get(t);n&&(e.onData&&(n.onData=n.onData.filter(r=>r!==e.onData)),e.onError&&(n.onError=n.onError.filter(r=>r!==e.onError)),e.onFinish&&(n.onFinish=n.onFinish.filter(r=>r!==e.onFinish)),e.onResponse&&(n.onResponse=n.onResponse.filter(r=>r!==e.onResponse)),e.onCancel&&(n.onCancel=n.onCancel.filter(r=>r!==e.onCancel)),e.onBeforeSend&&(n.onBeforeSend=n.onBeforeSend.filter(r=>r!==e.onBeforeSend)))},j=new Map,S=new Map,L=t=>{const e=j.get(t);if(e)return e;const n={controller:new AbortController,data:"",isFetching:!1,isStreaming:!1,jsonData:null};return j.set(t,n),n},I=t=>(S.has(t)||S.set(t,[]),S.get(t)),J=t=>{var e;return S.has(t)&&((e=S.get(t))==null?void 0:e.length)},Y=(t,e)=>(I(t).push(e),()=>{S.set(t,I(t).filter(n=>n!==e)),J(t)||(j.delete(t),S.delete(t))}),Z=(t,e)=>{var r;j.set(t,{...L(t),...e});const n=L(t);(r=S.get(t))==null||r.forEach(i=>i(n))},O=(t,e={})=>{const n=s.useRef(e.id??K()),r=s.useRef(L(n.current)),i=s.useRef((()=>{var u;const a={"Content-Type":"application/json","X-STREAM-ID":n.current},o=e.csrfToken??((u=document.querySelector('meta[name="csrf-token"]'))==null?void 0:u.getAttribute("content"));return o&&(a["X-CSRF-TOKEN"]=o),a})()),[B,T]=s.useState(r.current.data),[P,f]=s.useState(r.current.jsonData),[m,C]=s.useState(r.current.isFetching),[w,M]=s.useState(r.current.isStreaming),d=s.useCallback(a=>{Z(n.current,a)},[]),h=s.useCallback(()=>{r.current.controller.abort(),(m||w)&&$(n.current),d({isFetching:!1,isStreaming:!1})},[m,w]),g=s.useCallback(()=>{d({data:"",jsonData:null})},[]),R=s.useCallback(a=>{const o=new AbortController,u={method:"POST",signal:o.signal,headers:{...i.current,...e.headers??{}},body:JSON.stringify(a??{}),credentials:e.credentials??"same-origin"},D=H(n.current,u);D!==!1&&(d({isFetching:!0,controller:o}),fetch(t,D??u).then(async c=>{if(!c.ok){const F=await c.text();throw new Error(F)}if(!c.body)throw new Error("ReadableStream not yet supported in this browser.");return W(n.current,c),d({isFetching:!1,isStreaming:!0}),b(c.body.getReader())}).catch(c=>{d({isFetching:!1,isStreaming:!1}),q(n.current,c),p(n.current)}))},[t]),y=s.useCallback(a=>{h(),R(a),g()},[]),b=s.useCallback((a,o="")=>a.read().then(({done:u,value:D})=>{const c=new TextDecoder("utf-8").decode(D),F=o+c;G(n.current,c);const v={data:F};if(!u)return d(v),b(a,F);if(v.isStreaming=!1,e.json)try{v.jsonData=JSON.parse(F)}catch(N){q(n.current,N)}return d(v),p(n.current),""}),[]);return s.useEffect(()=>{const a=Y(n.current,o=>{r.current=L(n.current),C(o.isFetching),M(o.isStreaming),T(o.data),f(o.jsonData)});return()=>{a(),J(n.current)||h()}},[]),s.useEffect(()=>{const a=V(n.current,e);return()=>{a()}},[e]),s.useEffect(()=>(window.addEventListener("beforeunload",h),()=>{window.removeEventListener("beforeunload",h)}),[h]),s.useEffect(()=>{e.initialInput&&R(e.initialInput)},[]),{data:B,jsonData:P,isFetching:m,isStreaming:w,id:n.current,send:y,cancel:h,clearData:g}},_=(t,e={})=>{const{jsonData:n,data:r,...i}=O(t,{...e,json:!0});return{data:n,strData:r,...i}};l.useEventStream=x,l.useJsonStream=_,l.useStream=O,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
2
|
+
"name": "@laravel/stream-react",
|
|
3
|
+
"version": "0.3.6",
|
|
4
|
+
"description": "Laravel streaming hooks for React",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"laravel",
|
|
7
|
+
"stream",
|
|
8
|
+
"use-stream",
|
|
9
|
+
"server-sent-events",
|
|
10
|
+
"sse",
|
|
11
|
+
"react",
|
|
12
|
+
"hooks"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/laravel/stream/tree/main/packages/react#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
|
+
"scripts": {
|
|
41
|
+
"build": "vite build",
|
|
42
|
+
"lint": "eslint --config eslint.config.mjs \"src/**/*.ts\"",
|
|
43
|
+
"prepublish": "pnpm run build",
|
|
44
|
+
"release": "vitest --run && npm publish",
|
|
45
|
+
"test": "vitest",
|
|
46
|
+
"format": "prettier --write ."
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@testing-library/dom": "^10.4.0",
|
|
50
|
+
"@testing-library/react": "^16.3.0",
|
|
51
|
+
"@types/node": "^22.14.0",
|
|
52
|
+
"@types/react": "^19.1.0",
|
|
53
|
+
"@typescript-eslint/eslint-plugin": "^8.21.0",
|
|
54
|
+
"@typescript-eslint/parser": "^8.21.0",
|
|
55
|
+
"@vitejs/plugin-vue": "^5.0.0",
|
|
56
|
+
"eslint": "^9.0.0",
|
|
57
|
+
"jsdom": "^26.0.0",
|
|
58
|
+
"msw": "^2.8.2",
|
|
59
|
+
"prettier": "^3.5.3",
|
|
60
|
+
"typescript": "^5.3.0",
|
|
61
|
+
"vite": "^5.1.0",
|
|
62
|
+
"vite-plugin-dts": "^4.5.3",
|
|
63
|
+
"vitest": "^3.1.1"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
35
67
|
}
|
|
36
|
-
|
|
37
|
-
"files": [
|
|
38
|
-
"dist"
|
|
39
|
-
],
|
|
40
|
-
"devDependencies": {
|
|
41
|
-
"@testing-library/dom": "^10.4.0",
|
|
42
|
-
"@testing-library/react": "^16.3.0",
|
|
43
|
-
"@types/node": "^22.14.0",
|
|
44
|
-
"@types/react": "^19.1.0",
|
|
45
|
-
"@typescript-eslint/eslint-plugin": "^8.21.0",
|
|
46
|
-
"@typescript-eslint/parser": "^8.21.0",
|
|
47
|
-
"@vitejs/plugin-vue": "^5.0.0",
|
|
48
|
-
"eslint": "^9.0.0",
|
|
49
|
-
"jsdom": "^26.0.0",
|
|
50
|
-
"msw": "^2.8.2",
|
|
51
|
-
"prettier": "^3.5.3",
|
|
52
|
-
"typescript": "^5.3.0",
|
|
53
|
-
"vite": "^5.1.0",
|
|
54
|
-
"vite-plugin-dts": "^4.5.3",
|
|
55
|
-
"vitest": "^3.1.1"
|
|
56
|
-
},
|
|
57
|
-
"peerDependencies": {
|
|
58
|
-
"react": "^18.0.0 || ^19.0.0"
|
|
59
|
-
},
|
|
60
|
-
"scripts": {
|
|
61
|
-
"build": "vite build",
|
|
62
|
-
"lint": "eslint --config eslint.config.mjs \"src/**/*.ts\"",
|
|
63
|
-
"prepublish": "pnpm run build",
|
|
64
|
-
"release": "vitest --run && git push --follow-tags && pnpm publish",
|
|
65
|
-
"test": "vitest",
|
|
66
|
-
"format": "prettier --write ."
|
|
67
|
-
}
|
|
68
|
-
}
|
|
68
|
+
}
|