@laravel/stream-vue 0.3.4 → 0.3.5
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 +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -17,9 +17,9 @@ declare type EventStreamResult = {
|
|
|
17
17
|
clearMessage: () => void;
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
-
declare type StreamOptions = {
|
|
20
|
+
declare type StreamOptions<TSendBody extends Record<string, any> = {}> = {
|
|
21
21
|
id?: string;
|
|
22
|
-
initialInput?:
|
|
22
|
+
initialInput?: TSendBody;
|
|
23
23
|
headers?: Record<string, string>;
|
|
24
24
|
csrfToken?: string;
|
|
25
25
|
json?: boolean;
|
|
@@ -44,24 +44,24 @@ declare type StreamOptions = {
|
|
|
44
44
|
*/
|
|
45
45
|
export declare const useEventStream: (url: string, { eventName, endSignal, glue, replace, onMessage, onComplete, onError, }?: EventStreamOptions) => EventStreamResult;
|
|
46
46
|
|
|
47
|
-
export declare const useJsonStream: <TJsonData = null>(url: string, options?: Omit<StreamOptions
|
|
47
|
+
export declare const useJsonStream: <TJsonData = null, TSendBody extends Record<string, any> = {}>(url: string, options?: Omit<StreamOptions<TSendBody>, "json">) => {
|
|
48
48
|
isFetching: Readonly<Ref<boolean>>;
|
|
49
49
|
isStreaming: Readonly<Ref<boolean>>;
|
|
50
50
|
id: string;
|
|
51
|
-
send: (body:
|
|
51
|
+
send: (body: TSendBody) => void;
|
|
52
52
|
cancel: () => void;
|
|
53
53
|
clearData: () => void;
|
|
54
54
|
data: Readonly<TJsonData | null>;
|
|
55
55
|
strData: Readonly<Ref<string, string>>;
|
|
56
56
|
};
|
|
57
57
|
|
|
58
|
-
export declare const useStream: <TJsonData = null>(url: string, options?: StreamOptions) => {
|
|
58
|
+
export declare const useStream: <TSendBody extends Record<string, any> = {}, TJsonData = null>(url: string, options?: StreamOptions<TSendBody>) => {
|
|
59
59
|
data: Readonly<Ref<string>>;
|
|
60
60
|
jsonData: Readonly<TJsonData | null>;
|
|
61
61
|
isFetching: Readonly<Ref<boolean>>;
|
|
62
62
|
isStreaming: Readonly<Ref<boolean>>;
|
|
63
63
|
id: string;
|
|
64
|
-
send: (body:
|
|
64
|
+
send: (body: TSendBody) => void;
|
|
65
65
|
cancel: () => void;
|
|
66
66
|
clearData: () => void;
|
|
67
67
|
};
|
package/dist/index.es.js
CHANGED
|
@@ -148,7 +148,7 @@ const w = /* @__PURE__ */ new Map(), K = (t, e) => {
|
|
|
148
148
|
isFetching: !1,
|
|
149
149
|
isStreaming: !1
|
|
150
150
|
});
|
|
151
|
-
}, u = (r
|
|
151
|
+
}, u = (r) => {
|
|
152
152
|
const o = new AbortController(), g = {
|
|
153
153
|
method: "POST",
|
|
154
154
|
signal: o.signal,
|
|
@@ -156,7 +156,7 @@ const w = /* @__PURE__ */ new Map(), K = (t, e) => {
|
|
|
156
156
|
...i,
|
|
157
157
|
...e.headers ?? {}
|
|
158
158
|
},
|
|
159
|
-
body: JSON.stringify(r),
|
|
159
|
+
body: JSON.stringify(r ?? {}),
|
|
160
160
|
credentials: e.credentials ?? "same-origin"
|
|
161
161
|
}, k = G(n, g);
|
|
162
162
|
k !== !1 && (s({
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(f,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],s):(f=typeof globalThis<"u"?globalThis:f||self,s(f.LaravelStreamVue={},f.Vue))})(this,function(f,s){"use strict";const T="data: ",J=(t,{eventName:e="update",endSignal:n="</stream>",glue:a=" ",replace:u=!1,onMessage:j=()=>null,onComplete:k=()=>null,onError:y=()=>null}={})=>{const m=s.ref(""),g=s.ref([]),C=Array.isArray(e)?e:[e];let o=null;const c=()=>{m.value="",g.value=[]},h=(r=!1)=>{C.forEach(l=>{o==null||o.removeEventListener(l,v)}),o==null||o.close(),o=null,r&&c()},v=r=>{if([n,`${T}${n}`].includes(r.data)){h(),k();return}u&&c(),g.value.push(r.data.startsWith(T)?r.data.substring(T.length):r.data),m.value=g.value.join(a),j(r)},B=r=>{y(r),h()},D=()=>{c(),o=new EventSource(t),C.forEach(r=>{o.addEventListener(r,v)}),o.addEventListener("error",B)};return s.onMounted(()=>{D()}),s.onUnmounted(()=>{h()}),s.watch(()=>t,(r,l)=>{r!==l&&(h(),D())}),{message:s.readonly(m),messageParts:s.readonly(g),close:h,clearMessage:c}},x="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let I=(t=21)=>{let e="",n=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=x[n[t]&63];return e};const b=new Map,V=(t,e)=>{b.has(t)||b.set(t,{onData:[],onError:[],onFinish:[],onResponse:[],onCancel:[],onBeforeSend:[]});const n=b.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),()=>{$(t,e)}},E=(t,e,...n)=>{const a=b.get(t);return a?a[e].map(u=>u(...n)):[]},p=t=>{E(t,"onFinish")},A=(t,e)=>{E(t,"onError",e)},N=(t,e)=>{E(t,"onResponse",e)},X=t=>{E(t,"onCancel")},K=(t,e)=>{E(t,"onData",e)},W=(t,e)=>{const n=E(t,"onBeforeSend",e);for(const a of n){if(a===!1)return!1;if(a!==null&&typeof a=="object")return a}return null},$=(t,e)=>{const n=b.get(t);n&&(e.onData&&(n.onData=n.onData.filter(a=>a!==e.onData)),e.onError&&(n.onError=n.onError.filter(a=>a!==e.onError)),e.onFinish&&(n.onFinish=n.onFinish.filter(a=>a!==e.onFinish)),e.onResponse&&(n.onResponse=n.onResponse.filter(a=>a!==e.onResponse)),e.onCancel&&(n.onCancel=n.onCancel.filter(a=>a!==e.onCancel)),e.onBeforeSend&&(n.onBeforeSend=n.onBeforeSend.filter(a=>a!==e.onBeforeSend)))},F=new Map,d=new Map,R=t=>{const e=F.get(t);if(e)return e;const n={controller:new AbortController,data:"",isFetching:!1,isStreaming:!1,jsonData:null};return F.set(t,n),n},P=t=>(d.has(t)||d.set(t,[]),d.get(t)),q=t=>{var e;return d.has(t)&&((e=d.get(t))==null?void 0:e.length)},G=(t,e)=>(P(t).push(e),()=>{d.set(t,P(t).filter(n=>n!==e)),q(t)||(F.delete(t),d.delete(t))}),H=(t,e)=>{var a;F.set(t,{...R(t),...e});const n=R(t);(a=d.get(t))==null||a.forEach(u=>u(n))},O=(t,e={})=>{const n=e.id??I(),a=s.ref(R(n)),u=(()=>{var S;const r={"Content-Type":"application/json","X-STREAM-ID":n},l=e.csrfToken??((S=document.querySelector('meta[name="csrf-token"]'))==null?void 0:S.getAttribute("content"));return l&&(r["X-CSRF-TOKEN"]=l),r})(),j=s.ref(a.value.data),k=s.ref(a.value.jsonData),y=s.ref(a.value.isFetching),m=s.ref(a.value.isStreaming);let g,C;const o=r=>{H(n,r)},c=()=>{a.value.controller.abort(),(y||m)&&X(n),o({isFetching:!1,isStreaming:!1})},h=
|
|
1
|
+
(function(f,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],s):(f=typeof globalThis<"u"?globalThis:f||self,s(f.LaravelStreamVue={},f.Vue))})(this,function(f,s){"use strict";const T="data: ",J=(t,{eventName:e="update",endSignal:n="</stream>",glue:a=" ",replace:u=!1,onMessage:j=()=>null,onComplete:k=()=>null,onError:y=()=>null}={})=>{const m=s.ref(""),g=s.ref([]),C=Array.isArray(e)?e:[e];let o=null;const c=()=>{m.value="",g.value=[]},h=(r=!1)=>{C.forEach(l=>{o==null||o.removeEventListener(l,v)}),o==null||o.close(),o=null,r&&c()},v=r=>{if([n,`${T}${n}`].includes(r.data)){h(),k();return}u&&c(),g.value.push(r.data.startsWith(T)?r.data.substring(T.length):r.data),m.value=g.value.join(a),j(r)},B=r=>{y(r),h()},D=()=>{c(),o=new EventSource(t),C.forEach(r=>{o.addEventListener(r,v)}),o.addEventListener("error",B)};return s.onMounted(()=>{D()}),s.onUnmounted(()=>{h()}),s.watch(()=>t,(r,l)=>{r!==l&&(h(),D())}),{message:s.readonly(m),messageParts:s.readonly(g),close:h,clearMessage:c}},x="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let I=(t=21)=>{let e="",n=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=x[n[t]&63];return e};const b=new Map,V=(t,e)=>{b.has(t)||b.set(t,{onData:[],onError:[],onFinish:[],onResponse:[],onCancel:[],onBeforeSend:[]});const n=b.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),()=>{$(t,e)}},E=(t,e,...n)=>{const a=b.get(t);return a?a[e].map(u=>u(...n)):[]},p=t=>{E(t,"onFinish")},A=(t,e)=>{E(t,"onError",e)},N=(t,e)=>{E(t,"onResponse",e)},X=t=>{E(t,"onCancel")},K=(t,e)=>{E(t,"onData",e)},W=(t,e)=>{const n=E(t,"onBeforeSend",e);for(const a of n){if(a===!1)return!1;if(a!==null&&typeof a=="object")return a}return null},$=(t,e)=>{const n=b.get(t);n&&(e.onData&&(n.onData=n.onData.filter(a=>a!==e.onData)),e.onError&&(n.onError=n.onError.filter(a=>a!==e.onError)),e.onFinish&&(n.onFinish=n.onFinish.filter(a=>a!==e.onFinish)),e.onResponse&&(n.onResponse=n.onResponse.filter(a=>a!==e.onResponse)),e.onCancel&&(n.onCancel=n.onCancel.filter(a=>a!==e.onCancel)),e.onBeforeSend&&(n.onBeforeSend=n.onBeforeSend.filter(a=>a!==e.onBeforeSend)))},F=new Map,d=new Map,R=t=>{const e=F.get(t);if(e)return e;const n={controller:new AbortController,data:"",isFetching:!1,isStreaming:!1,jsonData:null};return F.set(t,n),n},P=t=>(d.has(t)||d.set(t,[]),d.get(t)),q=t=>{var e;return d.has(t)&&((e=d.get(t))==null?void 0:e.length)},G=(t,e)=>(P(t).push(e),()=>{d.set(t,P(t).filter(n=>n!==e)),q(t)||(F.delete(t),d.delete(t))}),H=(t,e)=>{var a;F.set(t,{...R(t),...e});const n=R(t);(a=d.get(t))==null||a.forEach(u=>u(n))},O=(t,e={})=>{const n=e.id??I(),a=s.ref(R(n)),u=(()=>{var S;const r={"Content-Type":"application/json","X-STREAM-ID":n},l=e.csrfToken??((S=document.querySelector('meta[name="csrf-token"]'))==null?void 0:S.getAttribute("content"));return l&&(r["X-CSRF-TOKEN"]=l),r})(),j=s.ref(a.value.data),k=s.ref(a.value.jsonData),y=s.ref(a.value.isFetching),m=s.ref(a.value.isStreaming);let g,C;const o=r=>{H(n,r)},c=()=>{a.value.controller.abort(),(y||m)&&X(n),o({isFetching:!1,isStreaming:!1})},h=r=>{const l=new AbortController,S={method:"POST",signal:l.signal,headers:{...u,...e.headers??{}},body:JSON.stringify(r??{}),credentials:e.credentials??"same-origin"},L=W(n,S);L!==!1&&(o({isFetching:!0,controller:l}),fetch(t,L??S).then(async i=>{if(!i.ok){const w=await i.text();throw new Error(w)}if(!i.body)throw new Error("ReadableStream not yet supported in this browser.");return N(n,i),o({isFetching:!1,isStreaming:!0}),D(i.body.getReader())}).catch(i=>{o({isFetching:!1,isStreaming:!1}),A(n,i),p(n)}))},v=r=>{c(),h(r),B()},B=()=>{o({data:"",jsonData:null})},D=(r,l="")=>r.read().then(({done:S,value:L})=>{const i=new TextDecoder("utf-8").decode(L),w=l+i;K(n,i);const M={data:w};if(!S)return o(M),D(r,w);if(M.isStreaming=!1,e.json)try{M.jsonData=JSON.parse(w)}catch(U){A(n,U)}return o(M),p(n),""});return s.onMounted(()=>{g=G(n,r=>{a.value=R(n),y.value=r.isFetching,m.value=r.isStreaming,j.value=r.data,k.value=r.jsonData}),C=V(n,e),window.addEventListener("beforeunload",c),e.initialInput&&h(e.initialInput)}),s.onUnmounted(()=>{g(),C(),window.removeEventListener("beforeunload",c),q(n)||c()}),{data:s.readonly(j),jsonData:s.readonly(k),isFetching:s.readonly(y),isStreaming:s.readonly(m),id:n,send:v,cancel:c,clearData:B}},Q=(t,e={})=>{const{jsonData:n,data:a,...u}=O(t,{...e,json:!0});return{data:n,strData:a,...u}};f.useEventStream=J,f.useJsonStream=Q,f.useStream=O,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
|