@elysiajs/eden 0.5.2 → 0.5.3
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/treaty.js +1 -1
- package/dist/treaty.mjs +30 -25
- package/package.json +2 -2
- package/src/treaty/index.ts +36 -22
package/dist/treaty.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var O=Object.defineProperty;var m=(r,e,t)=>e in r?O(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var w=(r,e,t)=>(m(r,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("./utils-a2cfc56a.js"),k=(r,e,t)=>{if(r.endsWith("/")||(r+="/"),e==="index"&&(e=""),!t||!Object.keys(t).length)return`${r}${e}`;let n="";for(const[f,s]of Object.entries(t))n+=`${f}=${s}&`;return`${r}${e}?${n.slice(0,-1)}`},p=typeof FileList>"u",S=r=>p?r instanceof Blob:r instanceof FileList||r instanceof File,x=r=>{for(let e in r){if(S(r[e]))return!0;if(Array.isArray(r[e])&&r[e].find(t=>S(t)))return!0}return!1},E=r=>p?r:new Promise(e=>{const t=new FileReader;t.onload=()=>{e(new Blob([t.result],{type:r.type}))},t.readAsArrayBuffer(r)});class F{constructor(e){w(this,"ws");w(this,"url");this.ws=new WebSocket(e),this.url=e}send(e){return Array.isArray(e)?(e.forEach(t=>this.send(t)),this):(this.ws.send(typeof e=="object"?JSON.stringify(e):e.toString()),this)}on(e,t,n){return this.addEventListener(e,t,n)}off(e,t,n){return this.ws.removeEventListener(e,t,n),this}subscribe(e,t){return this.addEventListener("message",e,t)}addEventListener(e,t,n){return this.ws.addEventListener(e,f=>{if(e==="message"){let s=f.data.toString();const i=s.charCodeAt(0);if(i===47||i===123)try{s=JSON.parse(s)}catch{}else Number.isNaN(+s)?s==="true"?s=!0:s==="fase"&&(s=!1):s=+s;t({...f,data:s})}else t(f)},n),this}removeEventListener(e,t,n){return this.off(e,t,n),this}close(){return this.ws.close(),this}}const L=(r,e="",t)=>new Proxy(()=>{},{get(n,f,s){return L(r,`${e}/${f.toString()}`)},apply(n,f,[{$query:s,$fetch:i,$body:N,...g}={$fetch:void 0,$query:void 0,$body:void 0}]=[{}]){const h=e.lastIndexOf("/"),b=e.slice(h+1),d=k(r,e.slice(0,h),s);return b==="subscribe"?new F(d.replace(/^([^]+):\/\//,d.startsWith("https://")?"wss://":"ws://")):(async()=>{let c=N??(Object.keys(g).length?g:void 0);const y=typeof c=="object",v=y&&x(c);if(v){const o=new FormData;for(const[a,l]of Object.entries(c))if(p)o.append(a,l);else if(l instanceof File)o.append(a,await E(l));else if(l instanceof FileList)for(let u=0;u<l.length;u++)o.append(a,await E(l[u]));else o.append(a,l);c=o}else y&&(c=JSON.stringify(c));return fetch(d,{method:b,body:c,...i,headers:c?v?i==null?void 0:i.headers:{"content-type":y?"application/json":"text/plain",...i==null?void 0:i.headers}:i==null?void 0:i.headers}).then(async o=>{var l;let a;switch((l=o.headers.get("Content-Type"))==null?void 0:l.split(";")[0]){case"application/json":a=await o.json();break;default:a=await o.text().then(u=>Number.isNaN(+u)?u==="true"?!0:u==="false"?!1:u:+u)}return o.status>300?{data:a,error:new j.EdenFetchError(o.status,await a)}:{data:a,error:null}})})()}}),A=(r,e={})=>new Proxy({},{get(t,n){return L(r,n)}});exports.EdenWS=F;exports.edenTreaty=A;
|
package/dist/treaty.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
1
|
+
var N = Object.defineProperty;
|
|
2
|
+
var S = (r, e, t) => e in r ? N(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
|
|
3
|
+
var p = (r, e, t) => (S(r, typeof e != "symbol" ? e + "" : e, t), t);
|
|
4
4
|
import { E as x } from "./utils-5a40996f.mjs";
|
|
5
5
|
const O = (r, e, t) => {
|
|
6
6
|
if (r.endsWith("/") || (r += "/"), e === "index" && (e = ""), !t || !Object.keys(t).length)
|
|
@@ -9,24 +9,24 @@ const O = (r, e, t) => {
|
|
|
9
9
|
for (const [f, s] of Object.entries(t))
|
|
10
10
|
n += `${f}=${s}&`;
|
|
11
11
|
return `${r}${e}?${n.slice(0, -1)}`;
|
|
12
|
-
},
|
|
12
|
+
}, w = typeof FileList > "u", E = (r) => w ? r instanceof Blob : r instanceof FileList || r instanceof File, k = (r) => {
|
|
13
13
|
for (let e in r) {
|
|
14
|
-
if (
|
|
14
|
+
if (E(r[e]))
|
|
15
15
|
return !0;
|
|
16
|
-
if (Array.isArray(r[e]) && r[e].find((t) =>
|
|
16
|
+
if (Array.isArray(r[e]) && r[e].find((t) => E(t)))
|
|
17
17
|
return !0;
|
|
18
18
|
}
|
|
19
19
|
return !1;
|
|
20
|
-
},
|
|
20
|
+
}, F = (r) => w ? r : new Promise((e) => {
|
|
21
21
|
const t = new FileReader();
|
|
22
22
|
t.onload = () => {
|
|
23
23
|
e(new Blob([t.result], { type: r.type }));
|
|
24
24
|
}, t.readAsArrayBuffer(r);
|
|
25
25
|
});
|
|
26
|
-
class
|
|
26
|
+
class A {
|
|
27
27
|
constructor(e) {
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
p(this, "ws");
|
|
29
|
+
p(this, "url");
|
|
30
30
|
this.ws = new WebSocket(e), this.url = e;
|
|
31
31
|
}
|
|
32
32
|
send(e) {
|
|
@@ -74,37 +74,42 @@ class k {
|
|
|
74
74
|
return this.ws.close(), this;
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
-
const
|
|
77
|
+
const m = (r, e = "", t) => new Proxy(() => {
|
|
78
78
|
}, {
|
|
79
79
|
get(n, f, s) {
|
|
80
|
-
return
|
|
80
|
+
return m(r, `${e}/${f.toString()}`);
|
|
81
81
|
},
|
|
82
82
|
apply(n, f, [
|
|
83
|
-
{ $query: s, $fetch: i, $body:
|
|
83
|
+
{ $query: s, $fetch: i, $body: L, ...g } = {
|
|
84
84
|
$fetch: void 0,
|
|
85
85
|
$query: void 0,
|
|
86
86
|
$body: void 0
|
|
87
87
|
}
|
|
88
88
|
] = [{}]) {
|
|
89
|
-
const
|
|
90
|
-
return
|
|
89
|
+
const h = e.lastIndexOf("/"), b = e.slice(h + 1), d = O(r, e.slice(0, h), s);
|
|
90
|
+
return b === "subscribe" ? new A(
|
|
91
91
|
d.replace(
|
|
92
92
|
/^([^]+):\/\//,
|
|
93
93
|
d.startsWith("https://") ? "wss://" : "ws://"
|
|
94
94
|
)
|
|
95
95
|
) : (async () => {
|
|
96
|
-
let c =
|
|
97
|
-
const y = typeof c == "object",
|
|
98
|
-
if (
|
|
96
|
+
let c = L ?? (Object.keys(g).length ? g : void 0);
|
|
97
|
+
const y = typeof c == "object", v = y && k(c);
|
|
98
|
+
if (v) {
|
|
99
99
|
const o = new FormData();
|
|
100
100
|
for (const [a, l] of Object.entries(c))
|
|
101
|
-
if (
|
|
102
|
-
o.append(a,
|
|
101
|
+
if (w)
|
|
102
|
+
o.append(a, l);
|
|
103
|
+
else if (l instanceof File)
|
|
104
|
+
o.append(
|
|
105
|
+
a,
|
|
106
|
+
await F(l)
|
|
107
|
+
);
|
|
103
108
|
else if (l instanceof FileList)
|
|
104
109
|
for (let u = 0; u < l.length; u++)
|
|
105
110
|
o.append(
|
|
106
111
|
a,
|
|
107
|
-
await
|
|
112
|
+
await F(l[u])
|
|
108
113
|
);
|
|
109
114
|
else
|
|
110
115
|
o.append(a, l);
|
|
@@ -112,11 +117,11 @@ const F = (r, e = "", t) => new Proxy(() => {
|
|
|
112
117
|
} else
|
|
113
118
|
y && (c = JSON.stringify(c));
|
|
114
119
|
return fetch(d, {
|
|
115
|
-
method:
|
|
120
|
+
method: b,
|
|
116
121
|
body: c,
|
|
117
122
|
// ...config.fetch,
|
|
118
123
|
...i,
|
|
119
|
-
headers: c ?
|
|
124
|
+
headers: c ? v ? i == null ? void 0 : i.headers : {
|
|
120
125
|
"content-type": y ? "application/json" : "text/plain",
|
|
121
126
|
...i == null ? void 0 : i.headers
|
|
122
127
|
} : i == null ? void 0 : i.headers
|
|
@@ -141,11 +146,11 @@ const F = (r, e = "", t) => new Proxy(() => {
|
|
|
141
146
|
{},
|
|
142
147
|
{
|
|
143
148
|
get(t, n) {
|
|
144
|
-
return
|
|
149
|
+
return m(r, n);
|
|
145
150
|
}
|
|
146
151
|
}
|
|
147
152
|
);
|
|
148
153
|
export {
|
|
149
|
-
|
|
154
|
+
A as EdenWS,
|
|
150
155
|
P as edenTreaty
|
|
151
156
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elysiajs/eden",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "Fully type-safe Elysia client",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "saltyAom",
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
"@sinclair/typebox": "^0.26.8",
|
|
74
74
|
"@types/node": "^18.15.5",
|
|
75
75
|
"bun-types": "^0.5.8",
|
|
76
|
-
"elysia": "0.5.
|
|
76
|
+
"elysia": "0.5.17",
|
|
77
77
|
"eslint": "^8.26.0",
|
|
78
78
|
"rimraf": "^4.4.1",
|
|
79
79
|
"typescript": "^5.0.4",
|
package/src/treaty/index.ts
CHANGED
|
@@ -7,12 +7,17 @@ import type { EdenTreaty } from './types'
|
|
|
7
7
|
|
|
8
8
|
export type { EdenTreaty } from './types'
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (typeof FileList === 'undefined') return false
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
const isServer = typeof FileList === 'undefined'
|
|
13
12
|
|
|
13
|
+
const isFile = (v: any) => {
|
|
14
14
|
// @ts-ignore
|
|
15
|
-
|
|
15
|
+
if (isServer) {
|
|
16
|
+
return v instanceof Blob
|
|
17
|
+
} else {
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
return v instanceof FileList || v instanceof File
|
|
20
|
+
}
|
|
16
21
|
}
|
|
17
22
|
|
|
18
23
|
// FormData is 1 level deep
|
|
@@ -31,16 +36,18 @@ const hasFile = (obj: Record<string, any>) => {
|
|
|
31
36
|
|
|
32
37
|
// @ts-ignore
|
|
33
38
|
const fileToBlob = (v: File) =>
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
39
|
+
isServer
|
|
40
|
+
? v
|
|
41
|
+
: new Promise<Blob>((resolve) => {
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
const reader = new FileReader()
|
|
37
44
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
reader.onload = () => {
|
|
46
|
+
resolve(new Blob([reader.result!], { type: v.type }))
|
|
47
|
+
}
|
|
41
48
|
|
|
42
|
-
|
|
43
|
-
|
|
49
|
+
reader.readAsArrayBuffer(v)
|
|
50
|
+
})
|
|
44
51
|
|
|
45
52
|
export class EdenWS<Schema extends TypedSchema<any> = TypedSchema> {
|
|
46
53
|
ws: WebSocket
|
|
@@ -184,19 +191,26 @@ const createProxy = (
|
|
|
184
191
|
|
|
185
192
|
// FormData is 1 level deep
|
|
186
193
|
for (const [key, field] of Object.entries(body)) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
// @ts-ignore
|
|
191
|
-
else if (field instanceof FileList) {
|
|
194
|
+
if (isServer) {
|
|
195
|
+
newBody.append(key, field as any)
|
|
196
|
+
} else {
|
|
192
197
|
// @ts-ignore
|
|
193
|
-
|
|
198
|
+
if (field instanceof File)
|
|
194
199
|
newBody.append(
|
|
195
|
-
key
|
|
196
|
-
await fileToBlob(
|
|
200
|
+
key,
|
|
201
|
+
await fileToBlob(field as any)
|
|
197
202
|
)
|
|
198
|
-
|
|
199
|
-
|
|
203
|
+
// @ts-ignore
|
|
204
|
+
else if (field instanceof FileList) {
|
|
205
|
+
// @ts-ignore
|
|
206
|
+
for (let i = 0; i < field.length; i++) {
|
|
207
|
+
newBody.append(
|
|
208
|
+
key as any,
|
|
209
|
+
await fileToBlob((field as any)[i])
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
} else newBody.append(key, field as string)
|
|
213
|
+
}
|
|
200
214
|
}
|
|
201
215
|
|
|
202
216
|
body = newBody
|