tina4js 1.1.0 → 1.1.1
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/api/fetch.d.ts +28 -0
- package/dist/api/fetch.d.ts.map +1 -1
- package/dist/api.cjs.js +1 -1
- package/dist/api.es.js +77 -52
- package/dist/tina4js.min.js +48 -0
- package/package.json +1 -1
package/dist/api/fetch.d.ts
CHANGED
|
@@ -84,6 +84,34 @@ export declare const api: {
|
|
|
84
84
|
patch<T = unknown>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
85
85
|
/** HTTP DELETE. */
|
|
86
86
|
delete<T = unknown>(path: string, options?: RequestOptions): Promise<T>;
|
|
87
|
+
/**
|
|
88
|
+
* Execute a GraphQL query or mutation.
|
|
89
|
+
*
|
|
90
|
+
* Sends a POST request with `{ query, variables }` body to the given path.
|
|
91
|
+
* Returns `{ data, errors }` — throws if the HTTP request itself fails.
|
|
92
|
+
*
|
|
93
|
+
* @param path - GraphQL endpoint path (e.g. `/api/graphql`).
|
|
94
|
+
* @param query - GraphQL query or mutation string.
|
|
95
|
+
* @param variables - Optional variables object.
|
|
96
|
+
* @param options - `{ params, headers }`.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* const { data, errors } = await api.graphql('/api/graphql',
|
|
100
|
+
* '{ products(limit: 10) { id name price } }'
|
|
101
|
+
* );
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* const { data } = await api.graphql('/api/graphql',
|
|
105
|
+
* 'query ($term: String!) { search_products(term: $term) { id name } }',
|
|
106
|
+
* { term: "widget" }
|
|
107
|
+
* );
|
|
108
|
+
*/
|
|
109
|
+
graphql<T = unknown>(path: string, query: string, variables?: Record<string, unknown>, options?: RequestOptions): Promise<{
|
|
110
|
+
data: T | null;
|
|
111
|
+
errors?: Array<{
|
|
112
|
+
message: string;
|
|
113
|
+
}>;
|
|
114
|
+
}>;
|
|
87
115
|
/**
|
|
88
116
|
* Upload files via FormData (multipart/form-data).
|
|
89
117
|
*
|
package/dist/api/fetch.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../src/api/fetch.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjC;AAED,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,OAAO;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,CAAC,CAAC;IACR,EAAE,EAAE,OAAO,CAAC;IACZ,OAAO,EAAE,OAAO,CAAC;IACjB,wEAAwE;IACxE,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,MAAM,kBAAkB,GAAG,CAAC,MAAM,EAAE,WAAW,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,KAAK,CAAC,WAAW,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,CAAC,GAAG,IAAI,CAAC;AAC3J,MAAM,MAAM,mBAAmB,GAAG,CAAC,QAAQ,EAAE,WAAW,KAAK,WAAW,GAAG,IAAI,CAAC;AA+BhF,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;CACpD;AA+FD;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,GAAG;IACd;;;;;;;;;;OAUG;iBACU,OAAO,CAAC,SAAS,CAAC,GAAG,IAA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,CAAC;QAAE,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;QAAC,MAAM,CAAC,EAAE,KAAK,CAAC;YAAE,OAAO,EAAE,MAAM,CAAA;SAAE,CAAC,CAAA;KAAE,CAAC;IAIxL;;;;;;;;;;;;;;;;OAgBG;WACU,CAAC,kBAAkB,MAAM,YAAY,QAAQ,YAAY,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC;IAiFjG;;;;;;;;;;;;;OAaG;oBACa,SAAS,GAAG,UAAU,MAAM,kBAAkB,GAAG,mBAAmB,GAAG,IAAI;IAQ3F,yCAAyC;cAC/B,IAAI;CAQf,CAAC"}
|
package/dist/api.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a={baseUrl:"",auth:!1,tokenKey:"tina4_token",headers:{}},k=[],T=[];let j=0;function y(){try{return localStorage.getItem(a.tokenKey)}catch{return null}}function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a={baseUrl:"",auth:!1,tokenKey:"tina4_token",headers:{}},k=[],T=[];let j=0;function y(){try{return localStorage.getItem(a.tokenKey)}catch{return null}}function q(e){try{localStorage.setItem(a.tokenKey,e)}catch{}}async function i(e,s,t,r){let c={method:e,headers:{"Content-Type":"application/json",...a.headers}};if(a.auth){const n=y();n&&(c.headers.Authorization=`Bearer ${n}`)}if(t!==void 0&&e!=="GET"){let n=typeof t=="object"&&t!==null?{...t}:t;if(a.auth&&typeof n=="object"&&n!==null){const u=y();u&&(n.formToken=u)}c.body=JSON.stringify(n)}if(r!=null&&r.headers&&Object.assign(c.headers,r.headers),r!=null&&r.params){const n=Object.entries(r.params).map(([u,I])=>`${encodeURIComponent(u)}=${encodeURIComponent(String(I))}`).join("&");s+=(s.includes("?")?"&":"?")+n}const d=a.baseUrl+s;c._url=d,c._requestId=++j;for(const n of k){const u=n(c);u&&(c=u)}const l=await fetch(d,c),g=l.headers.get("FreshToken");g&&q(g);const h=l.headers.get("Content-Type")??"";let f;h.includes("json")?f=await l.json():f=await l.text();let o={status:l.status,data:f,ok:l.ok,headers:l.headers,_requestId:c._requestId};for(const n of T){const u=n(o);u&&(o=u)}if(!l.ok)throw o;return o.data}const m={configure(e){Object.assign(a,e)},get(e,s){return i("GET",e,void 0,s)},post(e,s,t){return i("POST",e,s,t)},put(e,s,t){return i("PUT",e,s,t)},patch(e,s,t){return i("PATCH",e,s,t)},delete(e,s){return i("DELETE",e,void 0,s)},async graphql(e,s,t,r){return i("POST",e,{query:s,variables:t||{}},r)},async upload(e,s,t){let r={method:"POST",headers:{...a.headers},body:s};if(delete r.headers["Content-Type"],delete r.headers["content-type"],a.auth){const o=y();o&&(r.headers.Authorization=`Bearer ${o}`)}if(t!=null&&t.headers&&Object.assign(r.headers,t.headers),t!=null&&t.params){const o=Object.entries(t.params).map(([n,u])=>`${encodeURIComponent(n)}=${encodeURIComponent(String(u))}`).join("&");e+=(e.includes("?")?"&":"?")+o}const c=a.baseUrl+e;r._url=c,r._requestId=++j;for(const o of k){const n=o(r);n&&(r=n)}const d=await fetch(c,r),l=d.headers.get("FreshToken");l&&q(l);const g=d.headers.get("Content-Type")??"";let h;g.includes("json")?h=await d.json():h=await d.text();let f={status:d.status,data:h,ok:d.ok,headers:d.headers,_requestId:r._requestId};for(const o of T){const n=o(f);n&&(f=n)}if(!d.ok)throw f;return f.data},intercept(e,s){e==="request"?k.push(s):T.push(s)},_reset(){a.baseUrl="",a.auth=!1,a.tokenKey="tina4_token",a.headers={},k.length=0,T.length=0}};exports.api=m;
|
package/dist/api.es.js
CHANGED
|
@@ -3,8 +3,8 @@ const a = {
|
|
|
3
3
|
auth: !1,
|
|
4
4
|
tokenKey: "tina4_token",
|
|
5
5
|
headers: {}
|
|
6
|
-
},
|
|
7
|
-
let
|
|
6
|
+
}, k = [], T = [];
|
|
7
|
+
let q = 0;
|
|
8
8
|
function y() {
|
|
9
9
|
try {
|
|
10
10
|
return localStorage.getItem(a.tokenKey);
|
|
@@ -12,13 +12,13 @@ function y() {
|
|
|
12
12
|
return null;
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
-
function
|
|
15
|
+
function I(e) {
|
|
16
16
|
try {
|
|
17
17
|
localStorage.setItem(a.tokenKey, e);
|
|
18
18
|
} catch {
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
async function
|
|
21
|
+
async function i(e, s, t, r) {
|
|
22
22
|
let c = {
|
|
23
23
|
method: e,
|
|
24
24
|
headers: {
|
|
@@ -27,32 +27,32 @@ async function h(e, s, r, n) {
|
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
29
|
if (a.auth) {
|
|
30
|
-
const
|
|
31
|
-
|
|
30
|
+
const n = y();
|
|
31
|
+
n && (c.headers.Authorization = `Bearer ${n}`);
|
|
32
32
|
}
|
|
33
|
-
if (
|
|
34
|
-
let
|
|
35
|
-
if (a.auth && typeof
|
|
33
|
+
if (t !== void 0 && e !== "GET") {
|
|
34
|
+
let n = typeof t == "object" && t !== null ? { ...t } : t;
|
|
35
|
+
if (a.auth && typeof n == "object" && n !== null) {
|
|
36
36
|
const u = y();
|
|
37
|
-
u && (
|
|
37
|
+
u && (n.formToken = u);
|
|
38
38
|
}
|
|
39
|
-
c.body = JSON.stringify(
|
|
39
|
+
c.body = JSON.stringify(n);
|
|
40
40
|
}
|
|
41
|
-
if (
|
|
42
|
-
const
|
|
43
|
-
s += (s.includes("?") ? "&" : "?") +
|
|
41
|
+
if (r != null && r.headers && Object.assign(c.headers, r.headers), r != null && r.params) {
|
|
42
|
+
const n = Object.entries(r.params).map(([u, j]) => `${encodeURIComponent(u)}=${encodeURIComponent(String(j))}`).join("&");
|
|
43
|
+
s += (s.includes("?") ? "&" : "?") + n;
|
|
44
44
|
}
|
|
45
45
|
const d = a.baseUrl + s;
|
|
46
|
-
c._url = d, c._requestId = ++
|
|
47
|
-
for (const
|
|
48
|
-
const u =
|
|
46
|
+
c._url = d, c._requestId = ++q;
|
|
47
|
+
for (const n of k) {
|
|
48
|
+
const u = n(c);
|
|
49
49
|
u && (c = u);
|
|
50
50
|
}
|
|
51
|
-
const f = await fetch(d, c),
|
|
52
|
-
|
|
53
|
-
const
|
|
51
|
+
const f = await fetch(d, c), g = f.headers.get("FreshToken");
|
|
52
|
+
g && I(g);
|
|
53
|
+
const h = f.headers.get("Content-Type") ?? "";
|
|
54
54
|
let l;
|
|
55
|
-
|
|
55
|
+
h.includes("json") ? l = await f.json() : l = await f.text();
|
|
56
56
|
let o = {
|
|
57
57
|
status: f.status,
|
|
58
58
|
data: l,
|
|
@@ -60,8 +60,8 @@ async function h(e, s, r, n) {
|
|
|
60
60
|
headers: f.headers,
|
|
61
61
|
_requestId: c._requestId
|
|
62
62
|
};
|
|
63
|
-
for (const
|
|
64
|
-
const u =
|
|
63
|
+
for (const n of T) {
|
|
64
|
+
const u = n(o);
|
|
65
65
|
u && (o = u);
|
|
66
66
|
}
|
|
67
67
|
if (!f.ok)
|
|
@@ -91,7 +91,7 @@ const m = {
|
|
|
91
91
|
* const data = await api.get('/products', { params: { page: 2, limit: 20 } });
|
|
92
92
|
*/
|
|
93
93
|
get(e, s) {
|
|
94
|
-
return
|
|
94
|
+
return i("GET", e, void 0, s);
|
|
95
95
|
},
|
|
96
96
|
/**
|
|
97
97
|
* HTTP POST request.
|
|
@@ -101,20 +101,45 @@ const m = {
|
|
|
101
101
|
* @example
|
|
102
102
|
* await api.post('/users', { name: 'Alice', role: 'admin' });
|
|
103
103
|
*/
|
|
104
|
-
post(e, s,
|
|
105
|
-
return
|
|
104
|
+
post(e, s, t) {
|
|
105
|
+
return i("POST", e, s, t);
|
|
106
106
|
},
|
|
107
107
|
/** HTTP PUT — full replace. */
|
|
108
|
-
put(e, s,
|
|
109
|
-
return
|
|
108
|
+
put(e, s, t) {
|
|
109
|
+
return i("PUT", e, s, t);
|
|
110
110
|
},
|
|
111
111
|
/** HTTP PATCH — partial update. */
|
|
112
|
-
patch(e, s,
|
|
113
|
-
return
|
|
112
|
+
patch(e, s, t) {
|
|
113
|
+
return i("PATCH", e, s, t);
|
|
114
114
|
},
|
|
115
115
|
/** HTTP DELETE. */
|
|
116
116
|
delete(e, s) {
|
|
117
|
-
return
|
|
117
|
+
return i("DELETE", e, void 0, s);
|
|
118
|
+
},
|
|
119
|
+
/**
|
|
120
|
+
* Execute a GraphQL query or mutation.
|
|
121
|
+
*
|
|
122
|
+
* Sends a POST request with `{ query, variables }` body to the given path.
|
|
123
|
+
* Returns `{ data, errors }` — throws if the HTTP request itself fails.
|
|
124
|
+
*
|
|
125
|
+
* @param path - GraphQL endpoint path (e.g. `/api/graphql`).
|
|
126
|
+
* @param query - GraphQL query or mutation string.
|
|
127
|
+
* @param variables - Optional variables object.
|
|
128
|
+
* @param options - `{ params, headers }`.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* const { data, errors } = await api.graphql('/api/graphql',
|
|
132
|
+
* '{ products(limit: 10) { id name price } }'
|
|
133
|
+
* );
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* const { data } = await api.graphql('/api/graphql',
|
|
137
|
+
* 'query ($term: String!) { search_products(term: $term) { id name } }',
|
|
138
|
+
* { term: "widget" }
|
|
139
|
+
* );
|
|
140
|
+
*/
|
|
141
|
+
async graphql(e, s, t, r) {
|
|
142
|
+
return i("POST", e, { query: s, variables: t || {} }, r);
|
|
118
143
|
},
|
|
119
144
|
/**
|
|
120
145
|
* Upload files via FormData (multipart/form-data).
|
|
@@ -133,8 +158,8 @@ const m = {
|
|
|
133
158
|
* form.append('name', 'Alice');
|
|
134
159
|
* const result = await api.upload('/users/avatar', form);
|
|
135
160
|
*/
|
|
136
|
-
async upload(e, s,
|
|
137
|
-
let
|
|
161
|
+
async upload(e, s, t) {
|
|
162
|
+
let r = {
|
|
138
163
|
method: "POST",
|
|
139
164
|
headers: {
|
|
140
165
|
...a.headers
|
|
@@ -142,35 +167,35 @@ const m = {
|
|
|
142
167
|
},
|
|
143
168
|
body: s
|
|
144
169
|
};
|
|
145
|
-
if (delete
|
|
170
|
+
if (delete r.headers["Content-Type"], delete r.headers["content-type"], a.auth) {
|
|
146
171
|
const o = y();
|
|
147
|
-
o && (
|
|
172
|
+
o && (r.headers.Authorization = `Bearer ${o}`);
|
|
148
173
|
}
|
|
149
|
-
if (
|
|
150
|
-
const o = Object.entries(
|
|
174
|
+
if (t != null && t.headers && Object.assign(r.headers, t.headers), t != null && t.params) {
|
|
175
|
+
const o = Object.entries(t.params).map(([n, u]) => `${encodeURIComponent(n)}=${encodeURIComponent(String(u))}`).join("&");
|
|
151
176
|
e += (e.includes("?") ? "&" : "?") + o;
|
|
152
177
|
}
|
|
153
178
|
const c = a.baseUrl + e;
|
|
154
|
-
|
|
155
|
-
for (const o of
|
|
156
|
-
const
|
|
157
|
-
|
|
179
|
+
r._url = c, r._requestId = ++q;
|
|
180
|
+
for (const o of k) {
|
|
181
|
+
const n = o(r);
|
|
182
|
+
n && (r = n);
|
|
158
183
|
}
|
|
159
|
-
const d = await fetch(c,
|
|
160
|
-
f &&
|
|
161
|
-
const
|
|
162
|
-
let
|
|
163
|
-
|
|
184
|
+
const d = await fetch(c, r), f = d.headers.get("FreshToken");
|
|
185
|
+
f && I(f);
|
|
186
|
+
const g = d.headers.get("Content-Type") ?? "";
|
|
187
|
+
let h;
|
|
188
|
+
g.includes("json") ? h = await d.json() : h = await d.text();
|
|
164
189
|
let l = {
|
|
165
190
|
status: d.status,
|
|
166
|
-
data:
|
|
191
|
+
data: h,
|
|
167
192
|
ok: d.ok,
|
|
168
193
|
headers: d.headers,
|
|
169
|
-
_requestId:
|
|
194
|
+
_requestId: r._requestId
|
|
170
195
|
};
|
|
171
196
|
for (const o of T) {
|
|
172
|
-
const
|
|
173
|
-
|
|
197
|
+
const n = o(l);
|
|
198
|
+
n && (l = n);
|
|
174
199
|
}
|
|
175
200
|
if (!d.ok)
|
|
176
201
|
throw l;
|
|
@@ -191,11 +216,11 @@ const m = {
|
|
|
191
216
|
* });
|
|
192
217
|
*/
|
|
193
218
|
intercept(e, s) {
|
|
194
|
-
e === "request" ?
|
|
219
|
+
e === "request" ? k.push(s) : T.push(s);
|
|
195
220
|
},
|
|
196
221
|
/** @internal Reset state (for tests). */
|
|
197
222
|
_reset() {
|
|
198
|
-
a.baseUrl = "", a.auth = !1, a.tokenKey = "tina4_token", a.headers = {},
|
|
223
|
+
a.baseUrl = "", a.auth = !1, a.tokenKey = "tina4_token", a.headers = {}, k.length = 0, T.length = 0;
|
|
199
224
|
}
|
|
200
225
|
};
|
|
201
226
|
export {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";var Tina4=(()=>{var J=Object.defineProperty;var qe=Object.getOwnPropertyDescriptor;var Oe=Object.getOwnPropertyNames;var Ie=Object.prototype.hasOwnProperty;var Ne=(t,n)=>{for(var e in n)J(t,e,{get:n[e],enumerable:!0})},He=(t,n,e,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of Oe(n))!Ie.call(t,r)&&r!==e&&J(t,r,{get:()=>n[r],enumerable:!(o=qe(n,r))||o.enumerable});return t};var De=t=>He(J({},"__esModule",{value:!0}),t);var Je={};Ne(Je,{Tina4Element:()=>R,api:()=>ke,batch:()=>U,computed:()=>ce,effect:()=>k,html:()=>ue,isSignal:()=>x,navigate:()=>$,pwa:()=>Te,route:()=>he,router:()=>ve,signal:()=>v,sse:()=>Ce,ws:()=>Ee});var O=null,H=null,F=null;function _(t){F=t}function ie(){return F}var se=null,ae=null;var j=0,V=new Set;function v(t,n){let e=t,o=new Set,r={_t4:!0,get value(){if(O&&(o.add(O),H)){let s=O;H.push(()=>o.delete(s))}return e},set value(s){if(Object.is(s,e))return;let i=e;if(e=s,r._debugInfo&&r._debugInfo.updateCount++,ae&&ae(r,i,s),j>0)for(let a of o)V.add(a);else{let a;for(let l of[...o])try{l()}catch(c){a===void 0&&(a=c)}if(a!==void 0)throw a}},_subscribe(s){return o.add(s),()=>{o.delete(s)}},peek(){return e}};return se&&(r._debugInfo={label:n,createdAt:Date.now(),updateCount:0,subs:o},se(r,n)),r}function ce(t){let n=v(void 0);return k(()=>{n.value=t()}),{_t4:!0,get value(){return n.value},set value(e){throw new Error("[tina4] computed signals are read-only")},_subscribe(e){return n._subscribe(e)},peek(){return n.peek()}}}function k(t){let n=!1,e=[],o=()=>{if(n)return;for(let a of e)a();e=[];let s=O,i=H;O=o,H=e;try{t()}finally{O=s,H=i}};o();let r=()=>{n=!0;for(let s of e)s();e=[]};return F&&F.push(r),r}function U(t){j++;try{t()}finally{if(j--,j===0){let n=[...V];V.clear();let e;for(let o of n)try{o()}catch(r){e===void 0&&(e=r)}if(e!==void 0)throw e}}}function x(t){return t!==null&&typeof t=="object"&&t._t4===!0}var le=new WeakMap,z="t4:";function ue(t,...n){let e=le.get(t);if(!e){e=document.createElement("template");let i="";for(let a=0;a<t.length;a++)i+=t[a],a<n.length&&(Fe(i)?i+=`__t4_${a}__`:i+=`<!--${z}${a}-->`);e.innerHTML=i,le.set(t,e)}let o=e.content.cloneNode(!0),r=Le(o);for(let{marker:i,index:a}of r)Ke(i,n[a]);let s=Pe(o);for(let i of s)je(i,n);return o}function Le(t){let n=[];return X(t,e=>{if(e.nodeType===8){let o=e.data;if(o&&o.startsWith(z)){let r=parseInt(o.slice(z.length),10);n.push({marker:e,index:r})}}}),n}function Pe(t){let n=[];return X(t,e=>{e.nodeType===1&&n.push(e)}),n}function X(t,n){let e=t.childNodes;for(let o=0;o<e.length;o++){let r=e[o];n(r),X(r,n)}}function Ke(t,n){let e=t.parentNode;if(e)if(x(n)){let o=document.createTextNode("");e.replaceChild(o,t),k(()=>{o.data=String(n.value??"")})}else if(typeof n=="function"){let o=document.createComment("");e.replaceChild(o,t);let r=[],s=[];k(()=>{for(let u of s)u();s=[];let i=[],a=ie();_(i);let l=n();_(a),s=i;for(let u of r)u.parentNode?.removeChild(u);r=[];let c=Q(l),d=o.parentNode;if(d)for(let u of c)d.insertBefore(u,o),r.push(u)})}else if(de(n))e.replaceChild(n,t);else if(n instanceof Node)e.replaceChild(n,t);else if(Array.isArray(n)){let o=document.createDocumentFragment();for(let r of n){let s=Q(r);for(let i of s)o.appendChild(i)}e.replaceChild(o,t)}else{let o=document.createTextNode(String(n??""));e.replaceChild(o,t)}}function je(t,n){let e=[];for(let o of Array.from(t.attributes)){let r=o.name,s=o.value;if(r.startsWith("@")){let a=r.slice(1),l=s.match(/__t4_(\d+)__/);if(l){let c=n[parseInt(l[1],10)];typeof c=="function"&&t.addEventListener(a,d=>U(()=>c(d)))}e.push(r);continue}if(r.startsWith("?")){let a=r.slice(1),l=s.match(/__t4_(\d+)__/);if(l){let c=n[parseInt(l[1],10)];if(x(c)){let d=c;k(()=>{d.value?t.setAttribute(a,""):t.removeAttribute(a)})}else typeof c=="function"?k(()=>{c()?t.setAttribute(a,""):t.removeAttribute(a)}):c&&t.setAttribute(a,"")}e.push(r);continue}if(r.startsWith(".")){let a=r.slice(1),l=s.match(/__t4_(\d+)__/);if(l){let c=n[parseInt(l[1],10)];x(c)?k(()=>{t[a]=c.value}):t[a]=c}e.push(r);continue}let i=s.match(/__t4_(\d+)__/);if(i){let a=n[parseInt(i[1],10)];if(x(a)){let l=a;k(()=>{t.setAttribute(r,String(l.value??""))})}else typeof a=="function"?k(()=>{t.setAttribute(r,String(a()??""))}):t.setAttribute(r,String(a??""))}}for(let o of e)t.removeAttribute(o)}function Q(t){if(t==null||t===!1)return[];if(de(t))return Array.from(t.childNodes);if(t instanceof Node)return[t];if(Array.isArray(t)){let n=[];for(let e of t)n.push(...Q(e));return n}return[document.createTextNode(String(t))]}function de(t){return t!=null&&typeof t=="object"&&t.nodeType===11}function Fe(t){let n=!1,e=!1,o=!1;for(let r=0;r<t.length;r++){let s=t[r];s==="<"&&!n&&!e&&(o=!0),s===">"&&!n&&!e&&(o=!1),o&&(s==='"'&&!n&&(e=!e),s==="'"&&!e&&(n=!n))}return o}var fe=null,pe=null;var R=class extends HTMLElement{constructor(){super();this._props={};this._rendered=!1;let e=this.constructor;this._root=e.shadow?this.attachShadow({mode:"open"}):this;for(let[o,r]of Object.entries(e.props))this._props[o]=v(this._coerce(this.getAttribute(o),r))}static get observedAttributes(){return Object.keys(this.props)}connectedCallback(){if(this._rendered)return;this._rendered=!0;let e=this.constructor;if(e.styles&&e.shadow&&this._root instanceof ShadowRoot){let r=document.createElement("style");r.textContent=e.styles,this._root.appendChild(r)}let o=this.render();o&&this._root.appendChild(o),this.onMount(),fe&&fe(this)}disconnectedCallback(){this.onUnmount(),pe&&pe(this)}attributeChangedCallback(e,o,r){let i=this.constructor.props[e];i&&this._props[e]&&(this._props[e].value=this._coerce(r,i))}prop(e){if(!this._props[e])throw new Error(`[tina4] Prop '${e}' not declared in static props of <${this.tagName.toLowerCase()}>`);return this._props[e]}emit(e,o){this.dispatchEvent(new CustomEvent(e,{bubbles:!0,composed:!0,...o}))}onMount(){}onUnmount(){}_coerce(e,o){return o===Boolean?e!==null:o===Number?e!==null?Number(e):0:e??""}};R.props={},R.styles="",R.shadow=!0;var Y=[],I=null,D="history",Ue=!1,L=[],W=[],ge=0;function he(t,n){let e=[],o;t==="*"?o=".*":o=t.replace(/\{(\w+)\}/g,(s,i)=>(e.push(i),"([^/]+)"));let r=new RegExp(`^${o}$`);typeof n=="function"?Y.push({pattern:t,regex:r,paramNames:e,handler:n}):Y.push({pattern:t,regex:r,paramNames:e,handler:n.handler,guard:n.guard})}function $(t,n){if(D==="hash")if(n?.replace){let e=new URL(location.href);e.hash="#"+t,history.replaceState(null,"",e.toString()),P()}else location.hash="#"+t;else n?.replace?history.replaceState(null,"",t):history.pushState(null,"",t),P()}function P(){if(!I)return;let t=performance.now(),n=++ge,e=D==="hash"?location.hash.slice(1)||"/":location.pathname;for(let o of Y){let r=e.match(o.regex);if(!r)continue;let s={};if(o.paramNames.forEach((l,c)=>{s[l]=decodeURIComponent(r[c+1])}),o.guard){let l=o.guard();if(l===!1)return;if(typeof l=="string"){$(l,{replace:!0});return}}for(let l of W)l();W=[],I.innerHTML="";let i=[];_(i);let a=o.handler(s);if(a instanceof Promise)a.then(l=>{if(_(null),n!==ge){for(let d of i)d();return}me(I,l),W=i;let c=performance.now()-t;for(let d of L)d({path:e,params:s,pattern:o.pattern,durationMs:c})});else{_(null),me(I,a),W=i;let l=performance.now()-t;for(let c of L)c({path:e,params:s,pattern:o.pattern,durationMs:l})}return}}function me(t,n){n instanceof DocumentFragment||n instanceof Node?t.replaceChildren(n):typeof n=="string"?t.innerHTML=n:n!=null&&t.replaceChildren(document.createTextNode(String(n)))}var ve={start(t){if(I=document.querySelector(t.target),!I)throw new Error(`[tina4] Router target '${t.target}' not found in DOM`);D=t.mode??"history",Ue=!0,window.addEventListener("popstate",P),D==="hash"&&window.addEventListener("hashchange",P),document.addEventListener("click",n=>{if(n.metaKey||n.ctrlKey||n.shiftKey||n.altKey)return;let e=n.target.closest("a[href]");if(!e||e.origin!==location.origin||e.hasAttribute("target")||e.hasAttribute("download")||e.getAttribute("rel")?.includes("external"))return;n.preventDefault();let o=D==="hash"?e.getAttribute("href"):e.pathname;$(o)}),P()},on(t,n){return L.push(n),()=>{let e=L.indexOf(n);e>=0&&L.splice(e,1)}}};var y={baseUrl:"",auth:!1,tokenKey:"tina4_token",headers:{}},G=[],B=[],ye=0;function Z(){try{return localStorage.getItem(y.tokenKey)}catch{return null}}function be(t){try{localStorage.setItem(y.tokenKey,t)}catch{}}async function N(t,n,e,o){let r={method:t,headers:{"Content-Type":"application/json",...y.headers}};if(y.auth){let u=Z();u&&(r.headers.Authorization=`Bearer ${u}`)}if(e!==void 0&&t!=="GET"){let u=typeof e=="object"&&e!==null?{...e}:e;if(y.auth&&typeof u=="object"&&u!==null){let g=Z();g&&(u.formToken=g)}r.body=JSON.stringify(u)}if(o?.headers&&Object.assign(r.headers,o.headers),o?.params){let u=Object.entries(o.params).map(([g,b])=>`${encodeURIComponent(g)}=${encodeURIComponent(String(b))}`).join("&");n+=(n.includes("?")?"&":"?")+u}let s=y.baseUrl+n;r._url=s,r._requestId=++ye;for(let u of G){let g=u(r);g&&(r=g)}let i=await fetch(s,r),a=i.headers.get("FreshToken");a&&be(a);let l=i.headers.get("Content-Type")??"",c;l.includes("json")?c=await i.json():c=await i.text();let d={status:i.status,data:c,ok:i.ok,headers:i.headers,_requestId:r._requestId};for(let u of B){let g=u(d);g&&(d=g)}if(!i.ok)throw d;return d.data}var ke={configure(t){Object.assign(y,t)},get(t,n){return N("GET",t,void 0,n)},post(t,n,e){return N("POST",t,n,e)},put(t,n,e){return N("PUT",t,n,e)},patch(t,n,e){return N("PATCH",t,n,e)},delete(t,n){return N("DELETE",t,void 0,n)},async graphql(t,n,e,o){return N("POST",t,{query:n,variables:e||{}},o)},async upload(t,n,e){let o={method:"POST",headers:{...y.headers},body:n};if(delete o.headers["Content-Type"],delete o.headers["content-type"],y.auth){let d=Z();d&&(o.headers.Authorization=`Bearer ${d}`)}if(e?.headers&&Object.assign(o.headers,e.headers),e?.params){let d=Object.entries(e.params).map(([u,g])=>`${encodeURIComponent(u)}=${encodeURIComponent(String(g))}`).join("&");t+=(t.includes("?")?"&":"?")+d}let r=y.baseUrl+t;o._url=r,o._requestId=++ye;for(let d of G){let u=d(o);u&&(o=u)}let s=await fetch(r,o),i=s.headers.get("FreshToken");i&&be(i);let a=s.headers.get("Content-Type")??"",l;a.includes("json")?l=await s.json():l=await s.text();let c={status:s.status,data:l,ok:s.ok,headers:s.headers,_requestId:o._requestId};for(let d of B){let u=d(c);u&&(c=u)}if(!s.ok)throw c;return c.data},intercept(t,n){t==="request"?G.push(n):B.push(n)},_reset(){y.baseUrl="",y.auth=!1,y.tokenKey="tina4_token",y.headers={},G.length=0,B.length=0}};function we(t){let n=t.cacheStrategy??"network-first",e=JSON.stringify(t.precache??[]),o=t.offlineRoute?`'${t.offlineRoute}'`:"null";return`
|
|
2
|
+
const CACHE = 'tina4-v1';
|
|
3
|
+
const PRECACHE = ${e};
|
|
4
|
+
const OFFLINE = ${o};
|
|
5
|
+
|
|
6
|
+
self.addEventListener('install', (e) => {
|
|
7
|
+
e.waitUntil(
|
|
8
|
+
caches.open(CACHE).then((c) => c.addAll(PRECACHE)).then(() => self.skipWaiting())
|
|
9
|
+
);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
self.addEventListener('activate', (e) => {
|
|
13
|
+
e.waitUntil(self.clients.claim());
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
self.addEventListener('fetch', (e) => {
|
|
17
|
+
const req = e.request;
|
|
18
|
+
if (req.method !== 'GET') return;
|
|
19
|
+
|
|
20
|
+
${n==="cache-first"?`
|
|
21
|
+
e.respondWith(
|
|
22
|
+
caches.match(req).then((cached) => cached || fetch(req).then((res) => {
|
|
23
|
+
const clone = res.clone();
|
|
24
|
+
caches.open(CACHE).then((c) => c.put(req, clone));
|
|
25
|
+
return res;
|
|
26
|
+
})).catch(() => OFFLINE ? caches.match(OFFLINE) : new Response('Offline', { status: 503 }))
|
|
27
|
+
);`:n==="stale-while-revalidate"?`
|
|
28
|
+
e.respondWith(
|
|
29
|
+
caches.match(req).then((cached) => {
|
|
30
|
+
const fetched = fetch(req).then((res) => {
|
|
31
|
+
caches.open(CACHE).then((c) => c.put(req, res.clone()));
|
|
32
|
+
return res;
|
|
33
|
+
});
|
|
34
|
+
return cached || fetched;
|
|
35
|
+
}).catch(() => OFFLINE ? caches.match(OFFLINE) : new Response('Offline', { status: 503 }))
|
|
36
|
+
);`:`
|
|
37
|
+
e.respondWith(
|
|
38
|
+
fetch(req).then((res) => {
|
|
39
|
+
const clone = res.clone();
|
|
40
|
+
caches.open(CACHE).then((c) => c.put(req, clone));
|
|
41
|
+
return res;
|
|
42
|
+
}).catch(() => caches.match(req).then((cached) =>
|
|
43
|
+
cached || (OFFLINE ? caches.match(OFFLINE) : new Response('Offline', { status: 503 }))
|
|
44
|
+
))
|
|
45
|
+
);`}
|
|
46
|
+
});
|
|
47
|
+
`.trim()}function Se(t){let n={name:t.name,short_name:t.shortName??t.name,start_url:"/",display:t.display??"standalone",background_color:t.backgroundColor??"#ffffff",theme_color:t.themeColor??"#000000"};return t.icon&&(n.icons=[{src:t.icon,sizes:"192x192",type:"image/png"},{src:t.icon,sizes:"512x512",type:"image/png"}]),n}var Te={register(t){let n=Se(t),e=new Blob([JSON.stringify(n)],{type:"application/json"}),o=document.createElement("link");o.rel="manifest",o.href=URL.createObjectURL(e),document.head.appendChild(o);let r=document.querySelector('meta[name="theme-color"]');if(r||(r=document.createElement("meta"),r.name="theme-color",document.head.appendChild(r)),r.content=t.themeColor??"#000000","serviceWorker"in navigator){let s=we(t),i=new Blob([s],{type:"text/javascript"}),a=URL.createObjectURL(i);navigator.serviceWorker.register(a).catch(l=>{console.warn("[tina4] Service worker registration failed:",l)})}},generateServiceWorker(t){return we(t)},generateManifest(t){return Se(t)}};var We={reconnect:!0,reconnectDelay:1e3,reconnectMaxDelay:3e4,reconnectAttempts:1/0,protocols:[]};function $e(t,n={}){let e={...We,...n},o=v("connecting"),r=v(!1),s=v(null),i=v(null),a=v(0),l={message:[],open:[],close:[],error:[]},c=null,d=!1,u=e.reconnectDelay,g=null,b=0;function E(p){if(typeof p!="string")return p;try{return JSON.parse(p)}catch{return p}}function S(){o.value=b>0?"reconnecting":"connecting";try{c=new WebSocket(t,e.protocols)}catch{o.value="closed",r.value=!1;return}c.onopen=()=>{o.value="open",r.value=!0,i.value=null,b=0,u=e.reconnectDelay,a.value=0;for(let p of l.open)p()},c.onmessage=p=>{let m=E(p.data);s.value=m;for(let w of l.message)w(m)},c.onclose=p=>{o.value="closed",r.value=!1;for(let m of l.close)m(p.code,p.reason);!d&&e.reconnect&&b<e.reconnectAttempts&&A()},c.onerror=p=>{i.value=p;for(let m of l.error)m(p)}}function A(){b++,a.value=b,o.value="reconnecting",g=setTimeout(()=>{g=null,S()},u),u=Math.min(u*2,e.reconnectMaxDelay)}let C={status:o,connected:r,lastMessage:s,error:i,reconnectCount:a,send(p){if(!c||c.readyState!==WebSocket.OPEN)throw new Error("[tina4] WebSocket is not connected");let m=typeof p=="string"?p:JSON.stringify(p);c.send(m)},on(p,m){return l[p].push(m),()=>{let w=l[p],M=w.indexOf(m);M>=0&&w.splice(M,1)}},pipe(p,m){let w=M=>{p.value=m(M,p.value)};return C.on("message",w)},close(p,m){d=!0,g&&(clearTimeout(g),g=null),c&&c.close(p??1e3,m??""),o.value="closed",r.value=!1}};return S(),C}var Ee={connect:$e};var Ge={mode:"eventsource",method:"GET",headers:{},body:void 0,reconnect:!0,reconnectDelay:1e3,reconnectMaxDelay:3e4,reconnectAttempts:1/0,events:[],json:!0};function Be(t,n={}){let e={...Ge,...n},o=v("connecting"),r=v(!1),s=v(null),i=v(null),a=v(null),l=v(0),c={message:[],open:[],close:[],error:[]},d=null,u=null,g=!1,b=e.reconnectDelay,E=null,S=0;function A(f){if(!e.json||typeof f!="string")return f;try{return JSON.parse(f)}catch{return f}}function C(f,h){s.value=f,i.value=h;for(let T of c.message)T(f,h??void 0)}function p(){o.value="open",r.value=!0,a.value=null,S=0,b=e.reconnectDelay,l.value=0;for(let f of c.open)f()}function m(){o.value="closed",r.value=!1;for(let f of c.close)f();!g&&e.reconnect&&S<e.reconnectAttempts&&xe()}function w(f){a.value=f;for(let h of c.error)h(f)}function M(){o.value=S>0?"reconnecting":"connecting";try{d=new EventSource(t)}catch{o.value="closed",r.value=!1;return}d.onopen=()=>p(),d.onmessage=f=>{C(A(f.data),null)};for(let f of e.events)d.addEventListener(f,h=>{C(A(h.data),f)});d.onerror=f=>{w(f),d&&d.readyState===2&&(d=null,m())}}function _e(){o.value=S>0?"reconnecting":"connecting",u=new AbortController;let f={method:e.method,headers:e.headers,signal:u.signal};e.body!==void 0&&(f.body=typeof e.body=="string"?e.body:JSON.stringify(e.body)),fetch(t,f).then(async h=>{if(!h.ok){w(new Error(`[tina4] SSE fetch ${h.status}`)),m();return}p();let T=h.body.getReader(),q=new TextDecoder,K="";for(;;){let{done:Re,value:Ae}=await T.read();if(Re)break;K+=q.decode(Ae,{stream:!0});let oe=K.split(`
|
|
48
|
+
`);K=oe.pop();for(let Me of oe){let re=Me.trim();re&&C(A(re),null)}}let ne=K.trim();ne&&C(A(ne),null),u=null,m()}).catch(h=>{h.name!=="AbortError"&&(u=null,w(h),m())})}function xe(){S++,l.value=S,o.value="reconnecting",E=setTimeout(()=>{E=null,ee()},b),b=Math.min(b*2,e.reconnectMaxDelay)}function ee(){e.mode==="fetch"?_e():M()}let te={status:o,connected:r,lastMessage:s,lastEvent:i,error:a,reconnectCount:l,on(f,h){return c[f].push(h),()=>{let T=c[f],q=T.indexOf(h);q>=0&&T.splice(q,1)}},pipe(f,h){let T=q=>{f.value=h(q,f.value)};return te.on("message",T)},close(){g=!0,E&&(clearTimeout(E),E=null),d&&(d.close(),d=null),u&&(u.abort(),u=null),o.value="closed",r.value=!1}};return ee(),te}var Ce={connect:Be};return De(Je);})();
|