@elysiajs/eden 0.6.0 → 0.6.2
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/errors.js +10 -0
- package/dist/errors.js.map +1 -0
- package/dist/fetch/index.js +64 -0
- package/dist/fetch/index.js.map +1 -0
- package/dist/fetch/types.js +2 -0
- package/dist/fetch/types.js.map +1 -0
- package/dist/fetch.js +1 -1
- package/dist/fetch.mjs +26 -16
- package/dist/fn/index.js +35 -0
- package/dist/fn/index.js.map +1 -0
- package/dist/fn/types.js +2 -0
- package/dist/fn/types.js.map +1 -0
- package/dist/fn/utils.js +59 -0
- package/dist/fn/utils.js.map +1 -0
- package/dist/index.js +25 -1
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1 -1
- package/dist/treaty/index.js +205 -0
- package/dist/treaty/index.js.map +1 -0
- package/dist/treaty/types.js +2 -0
- package/dist/treaty/types.js.map +1 -0
- package/dist/treaty/utils.js +13 -0
- package/dist/treaty/utils.js.map +1 -0
- package/dist/treaty.js +1 -1
- package/dist/treaty.mjs +108 -112
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +5 -8
- package/src/fetch/index.ts +17 -6
- package/src/fetch/types.ts +33 -14
- package/src/fn/index.ts +1 -1
- package/src/fn/types.ts +1 -1
- package/src/treaty/index.ts +58 -53
- package/src/treaty/types.ts +117 -206
- package/src/types.ts +1 -1
- package/dist/fetch/index.d.ts +0 -5
- package/dist/fetch/index.d.ts.map +0 -1
- package/dist/fetch/types.d.ts +0 -36
- package/dist/fetch/types.d.ts.map +0 -1
- package/dist/fn/index.d.ts +0 -5
- package/dist/fn/index.d.ts.map +0 -1
- package/dist/fn/types.d.ts +0 -28
- package/dist/fn/types.d.ts.map +0 -1
- package/dist/fn/utils.d.ts +0 -14
- package/dist/fn/utils.d.ts.map +0 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.d.ts.map +0 -1
- package/dist/treaty/index.d.ts +0 -18
- package/dist/treaty/index.d.ts.map +0 -1
- package/dist/treaty/utils.d.ts +0 -2
- package/dist/treaty/utils.d.ts.map +0 -1
- package/dist/types.d.ts +0 -26
- package/dist/types.d.ts.map +0 -1
- package/dist/utils.d.ts +0 -6
- package/dist/utils.d.ts.map +0 -1
- /package/dist/{utils-5a40996f.mjs → errors-5a40996f.mjs} +0 -0
- /package/dist/{utils-a2cfc56a.js → errors-a2cfc56a.js} +0 -0
- /package/src/{utils.ts → errors.ts} +0 -0
package/dist/treaty.mjs
CHANGED
|
@@ -1,32 +1,34 @@
|
|
|
1
1
|
var A = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { E as
|
|
5
|
-
const
|
|
6
|
-
if (
|
|
7
|
-
return `${
|
|
2
|
+
var O = (s, e, t) => e in s ? A(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
|
|
3
|
+
var b = (s, e, t) => (O(s, typeof e != "symbol" ? e + "" : e, t), t);
|
|
4
|
+
import { E as k } from "./errors-5a40996f.mjs";
|
|
5
|
+
const j = (s, e, t) => {
|
|
6
|
+
if (s.endsWith("/") || (s += "/"), e === "index" && (e = ""), !t || !Object.keys(t).length)
|
|
7
|
+
return `${s}${e}`;
|
|
8
8
|
let n = "";
|
|
9
|
-
for (const [
|
|
10
|
-
n += `${
|
|
11
|
-
return `${
|
|
12
|
-
},
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
for (const [o, r] of Object.entries(t))
|
|
10
|
+
n += `${o}=${r}&`;
|
|
11
|
+
return `${s}${e}?${n.slice(0, -1)}`;
|
|
12
|
+
}, g = typeof FileList > "u", L = (s) => g ? s instanceof Blob : s instanceof FileList || s instanceof File, B = (s) => {
|
|
13
|
+
if (!s)
|
|
14
|
+
return !1;
|
|
15
|
+
for (const e in s) {
|
|
16
|
+
if (L(s[e]))
|
|
15
17
|
return !0;
|
|
16
|
-
if (Array.isArray(
|
|
18
|
+
if (Array.isArray(s[e]) && s[e].find((t) => L(t)))
|
|
17
19
|
return !0;
|
|
18
20
|
}
|
|
19
21
|
return !1;
|
|
20
|
-
},
|
|
22
|
+
}, N = (s) => g ? s : new Promise((e) => {
|
|
21
23
|
const t = new FileReader();
|
|
22
24
|
t.onload = () => {
|
|
23
|
-
e(new Blob([t.result], { type:
|
|
24
|
-
}, t.readAsArrayBuffer(
|
|
25
|
+
e(new Blob([t.result], { type: s.type }));
|
|
26
|
+
}, t.readAsArrayBuffer(s);
|
|
25
27
|
});
|
|
26
|
-
class
|
|
28
|
+
class P {
|
|
27
29
|
constructor(e) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
b(this, "ws");
|
|
31
|
+
b(this, "url");
|
|
30
32
|
this.ws = new WebSocket(e), this.url = e;
|
|
31
33
|
}
|
|
32
34
|
send(e) {
|
|
@@ -46,23 +48,23 @@ class J {
|
|
|
46
48
|
addEventListener(e, t, n) {
|
|
47
49
|
return this.ws.addEventListener(
|
|
48
50
|
e,
|
|
49
|
-
(
|
|
51
|
+
(o) => {
|
|
50
52
|
if (e === "message") {
|
|
51
|
-
let
|
|
52
|
-
const
|
|
53
|
-
if (
|
|
53
|
+
let r = o.data.toString();
|
|
54
|
+
const c = r.charCodeAt(0);
|
|
55
|
+
if (c === 47 || c === 123)
|
|
54
56
|
try {
|
|
55
|
-
|
|
57
|
+
r = JSON.parse(r);
|
|
56
58
|
} catch {
|
|
57
59
|
}
|
|
58
60
|
else
|
|
59
|
-
Number.isNaN(+
|
|
61
|
+
Number.isNaN(+r) ? r === "true" ? r = !0 : r === "fase" && (r = !1) : r = +r;
|
|
60
62
|
t({
|
|
61
|
-
...
|
|
62
|
-
data:
|
|
63
|
+
...o,
|
|
64
|
+
data: r
|
|
63
65
|
});
|
|
64
66
|
} else
|
|
65
|
-
t(
|
|
67
|
+
t(o);
|
|
66
68
|
},
|
|
67
69
|
n
|
|
68
70
|
), this;
|
|
@@ -74,104 +76,98 @@ class J {
|
|
|
74
76
|
return this.ws.close(), this;
|
|
75
77
|
}
|
|
76
78
|
}
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
79
|
+
const S = (s, e = "", t) => new Proxy(() => {
|
|
80
|
+
}, {
|
|
81
|
+
get(n, o, r) {
|
|
82
|
+
return S(s, `${e}/${o.toString()}`, t);
|
|
83
|
+
},
|
|
84
|
+
apply(n, o, [
|
|
85
|
+
{ $query: r, $fetch: c, $headers: $, ...E } = {
|
|
86
|
+
$fetch: void 0,
|
|
87
|
+
$headers: void 0,
|
|
88
|
+
$query: void 0
|
|
89
|
+
}
|
|
90
|
+
] = [{}]) {
|
|
91
|
+
const F = e.lastIndexOf("/"), h = e.slice(F + 1), w = j(s, e.slice(0, F), r);
|
|
92
|
+
return h === "subscribe" ? new P(
|
|
93
|
+
w.replace(
|
|
94
|
+
/^([^]+):\/\//,
|
|
95
|
+
w.startsWith("https://") ? "wss://" : "ws://"
|
|
96
|
+
)
|
|
97
|
+
) : (async () => {
|
|
98
|
+
var m, x;
|
|
99
|
+
let f;
|
|
100
|
+
const v = {
|
|
101
|
+
...(m = t.$fetch) == null ? void 0 : m.headers,
|
|
102
|
+
...c == null ? void 0 : c.headers,
|
|
103
|
+
...$
|
|
104
|
+
};
|
|
105
|
+
if (h !== "GET" && h !== "HEAD") {
|
|
106
|
+
f = Object.keys(E).length ? E : void 0;
|
|
107
|
+
const a = typeof f == "object";
|
|
108
|
+
if (a && B(f)) {
|
|
109
|
+
const d = new FormData();
|
|
110
|
+
for (const [y, l] of Object.entries(f))
|
|
111
|
+
if (g)
|
|
112
|
+
d.append(y, l);
|
|
108
113
|
else if (l instanceof File)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
await
|
|
114
|
+
d.append(
|
|
115
|
+
y,
|
|
116
|
+
await N(l)
|
|
112
117
|
);
|
|
113
118
|
else if (l instanceof FileList)
|
|
114
|
-
for (let
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
await
|
|
119
|
+
for (let p = 0; p < l.length; p++)
|
|
120
|
+
d.append(
|
|
121
|
+
y,
|
|
122
|
+
await N(l[p])
|
|
118
123
|
);
|
|
119
124
|
else
|
|
120
|
-
|
|
121
|
-
|
|
125
|
+
d.append(y, l);
|
|
126
|
+
f = d;
|
|
122
127
|
} else
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
} : {
|
|
153
|
-
data: u,
|
|
154
|
-
status: i.status,
|
|
155
|
-
raw: i,
|
|
156
|
-
headers: i.headers,
|
|
157
|
-
error: null,
|
|
158
|
-
retry: y
|
|
159
|
-
};
|
|
128
|
+
v["content-type"] = a ? "application/json" : "text/plain", a && (f = JSON.stringify(f));
|
|
129
|
+
}
|
|
130
|
+
const i = await (t.fetcher ?? fetch)(w, {
|
|
131
|
+
method: h,
|
|
132
|
+
body: f,
|
|
133
|
+
...t.$fetch,
|
|
134
|
+
...c,
|
|
135
|
+
headers: v
|
|
136
|
+
});
|
|
137
|
+
let u;
|
|
138
|
+
switch ((x = i.headers.get("Content-Type")) == null ? void 0 : x.split(";")[0]) {
|
|
139
|
+
case "application/json":
|
|
140
|
+
u = await i.json();
|
|
141
|
+
break;
|
|
142
|
+
default:
|
|
143
|
+
u = await i.text().then((a) => Number.isNaN(+a) ? a === "true" ? !0 : a === "false" ? !1 : a : +a);
|
|
144
|
+
}
|
|
145
|
+
return i.status > 300 ? {
|
|
146
|
+
data: u,
|
|
147
|
+
error: new k(i.status, await u),
|
|
148
|
+
status: i.status,
|
|
149
|
+
raw: i,
|
|
150
|
+
headers: i.headers
|
|
151
|
+
} : {
|
|
152
|
+
data: u,
|
|
153
|
+
status: i.status,
|
|
154
|
+
response: i,
|
|
155
|
+
headers: i.headers,
|
|
156
|
+
error: null
|
|
160
157
|
};
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
), D = (r, e = {
|
|
158
|
+
})();
|
|
159
|
+
}
|
|
160
|
+
}), C = (s, e = {
|
|
165
161
|
fetcher: fetch
|
|
166
162
|
}) => new Proxy(
|
|
167
163
|
{},
|
|
168
164
|
{
|
|
169
165
|
get(t, n) {
|
|
170
|
-
return
|
|
166
|
+
return S(s, n, e);
|
|
171
167
|
}
|
|
172
168
|
}
|
|
173
169
|
);
|
|
174
170
|
export {
|
|
175
|
-
|
|
176
|
-
|
|
171
|
+
P as EdenWS,
|
|
172
|
+
C as edenTreaty
|
|
177
173
|
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elysiajs/eden",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Fully type-safe Elysia client",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "saltyAom",
|
|
@@ -58,9 +58,7 @@
|
|
|
58
58
|
"release": "npm run build && npm run test && npm publish --access public"
|
|
59
59
|
},
|
|
60
60
|
"peerDependencies": {
|
|
61
|
-
"
|
|
62
|
-
"@sinclair/typebox": ">= 0.28.10",
|
|
63
|
-
"elysia": ">= 0.5.0"
|
|
61
|
+
"elysia": ">= 0.6.7"
|
|
64
62
|
},
|
|
65
63
|
"peerDependenciesMeta": {
|
|
66
64
|
"@sinclair/typebox": {
|
|
@@ -68,16 +66,15 @@
|
|
|
68
66
|
}
|
|
69
67
|
},
|
|
70
68
|
"devDependencies": {
|
|
71
|
-
"@elysiajs/cors": "0.
|
|
69
|
+
"@elysiajs/cors": "0.6.0",
|
|
72
70
|
"@elysiajs/fn": "^0.6.1",
|
|
73
71
|
"@sinclair/typebox": "^0.26.8",
|
|
74
72
|
"@types/node": "^18.15.5",
|
|
75
|
-
"
|
|
76
|
-
"elysia": "0.6.0",
|
|
73
|
+
"elysia": "0.6.10",
|
|
77
74
|
"eslint": "^8.26.0",
|
|
78
75
|
"rimraf": "^4.4.1",
|
|
79
76
|
"typescript": "^5.0.4",
|
|
80
|
-
"vite": "^4.
|
|
77
|
+
"vite": "^4.4.9",
|
|
81
78
|
"vite-plugin-dts": "^2.3.0"
|
|
82
79
|
},
|
|
83
80
|
"dependencies": {
|
package/src/fetch/index.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import type { Elysia } from 'elysia'
|
|
2
2
|
|
|
3
|
-
import { EdenFetchError } from '../
|
|
3
|
+
import { EdenFetchError } from '../errors'
|
|
4
4
|
import type { EdenFetch } from './types'
|
|
5
5
|
export type { EdenFetch } from './types'
|
|
6
6
|
|
|
7
7
|
export const edenFetch =
|
|
8
|
-
<App extends Elysia<any>>(
|
|
8
|
+
<App extends Elysia<any, any>>(
|
|
9
9
|
server: string,
|
|
10
10
|
config?: EdenFetch.Config
|
|
11
11
|
): EdenFetch.Create<App> =>
|
|
12
12
|
// @ts-ignore
|
|
13
|
-
|
|
13
|
+
(endpoint: string, { params, body, ...options } = {}) => {
|
|
14
14
|
if (params)
|
|
15
15
|
Object.entries(params).forEach(([key, value]) => {
|
|
16
|
-
endpoint = endpoint.replace(`:${key}`, value)
|
|
16
|
+
endpoint = endpoint.replace(`:${key}`, value as string)
|
|
17
17
|
})
|
|
18
18
|
|
|
19
19
|
const contentType = options.headers?.['Content-Type']
|
|
@@ -28,7 +28,7 @@ export const edenFetch =
|
|
|
28
28
|
const fetch = config?.fetcher || globalThis.fetch
|
|
29
29
|
|
|
30
30
|
// @ts-ignore
|
|
31
|
-
|
|
31
|
+
const execute = () => fetch(server + endpoint, {
|
|
32
32
|
...options,
|
|
33
33
|
headers: body
|
|
34
34
|
? {
|
|
@@ -59,9 +59,20 @@ export const edenFetch =
|
|
|
59
59
|
if (res.status > 300)
|
|
60
60
|
return {
|
|
61
61
|
data: null,
|
|
62
|
+
status: res.status,
|
|
63
|
+
headers: res.headers,
|
|
64
|
+
retry: execute,
|
|
62
65
|
error: new EdenFetchError(res.status, data)
|
|
63
66
|
}
|
|
64
67
|
|
|
65
|
-
return {
|
|
68
|
+
return {
|
|
69
|
+
data,
|
|
70
|
+
error: null,
|
|
71
|
+
status: res.status,
|
|
72
|
+
headers: res.headers,
|
|
73
|
+
retry: execute
|
|
74
|
+
}
|
|
66
75
|
})
|
|
76
|
+
|
|
77
|
+
return execute()
|
|
67
78
|
}
|
package/src/fetch/types.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import type { Elysia } from 'elysia'
|
|
2
|
-
import type { EdenFetchError } from '../
|
|
2
|
+
import type { EdenFetchError } from '../errors'
|
|
3
3
|
import type { MapError, IsUnknown, IsNever } from '../types'
|
|
4
4
|
|
|
5
|
+
type Prettify<T> = {
|
|
6
|
+
[K in keyof T]: T[K]
|
|
7
|
+
} & {}
|
|
8
|
+
|
|
5
9
|
export namespace EdenFetch {
|
|
6
|
-
export type Create<App extends Elysia<any>> = App['meta'] extends {
|
|
10
|
+
export type Create<App extends Elysia<any, any>> = App['meta'] extends {
|
|
7
11
|
schema: infer Schema extends Record<string, any>
|
|
8
12
|
}
|
|
9
13
|
? EdenFetch.Fn<Schema>
|
|
@@ -34,6 +38,13 @@ export namespace EdenFetch {
|
|
|
34
38
|
: {
|
|
35
39
|
params: Route['params']
|
|
36
40
|
}) &
|
|
41
|
+
(IsNever<keyof Route['query']> extends true
|
|
42
|
+
? {
|
|
43
|
+
query?: Record<never, string>
|
|
44
|
+
}
|
|
45
|
+
: {
|
|
46
|
+
query: Route['query']
|
|
47
|
+
}) &
|
|
37
48
|
(undefined extends Route['headers']
|
|
38
49
|
? {
|
|
39
50
|
headers?: Record<string, string>
|
|
@@ -45,17 +56,25 @@ export namespace EdenFetch {
|
|
|
45
56
|
? { body: Route['body'] }
|
|
46
57
|
: { body?: unknown })
|
|
47
58
|
) => Promise<
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
Prettify<
|
|
60
|
+
(
|
|
61
|
+
| {
|
|
62
|
+
data: Awaited<Route['response']['200']>
|
|
63
|
+
error: null
|
|
64
|
+
}
|
|
65
|
+
| {
|
|
66
|
+
data: null
|
|
67
|
+
error: MapError<Route['response']> extends infer Errors
|
|
68
|
+
? IsNever<Errors> extends true
|
|
69
|
+
? EdenFetchError<number, string>
|
|
70
|
+
: Errors
|
|
71
|
+
: EdenFetchError<number, string>
|
|
72
|
+
}
|
|
73
|
+
) & {
|
|
74
|
+
status: number
|
|
75
|
+
response: Response
|
|
76
|
+
headers: Record<string, string>
|
|
77
|
+
}
|
|
78
|
+
>
|
|
60
79
|
>
|
|
61
80
|
}
|
package/src/fn/index.ts
CHANGED
package/src/fn/types.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Elysia } from 'elysia'
|
|
2
2
|
|
|
3
3
|
export namespace EdenFn {
|
|
4
|
-
export type Create<App extends Elysia<any>> = App['meta'] extends {
|
|
4
|
+
export type Create<App extends Elysia<any, any>> = App['meta'] extends {
|
|
5
5
|
exposed: infer Schema extends Record<string, any>
|
|
6
6
|
}
|
|
7
7
|
? EdenFn.Compose<Schema>
|
package/src/treaty/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Elysia, TypedSchema } from 'elysia'
|
|
2
2
|
|
|
3
|
-
import { EdenFetchError } from '../
|
|
3
|
+
import { EdenFetchError } from '../errors'
|
|
4
4
|
|
|
5
5
|
import { composePath } from './utils'
|
|
6
6
|
import type { EdenTreaty } from './types'
|
|
@@ -22,6 +22,8 @@ const isFile = (v: any) => {
|
|
|
22
22
|
|
|
23
23
|
// FormData is 1 level deep
|
|
24
24
|
const hasFile = (obj: Record<string, any>) => {
|
|
25
|
+
if (!obj) return false
|
|
26
|
+
|
|
25
27
|
for (const key in obj) {
|
|
26
28
|
if (isFile(obj[key])) return true
|
|
27
29
|
else if (
|
|
@@ -155,7 +157,6 @@ const createProxy = (
|
|
|
155
157
|
path = '',
|
|
156
158
|
config: EdenTreaty.Config
|
|
157
159
|
): Record<string, unknown> =>
|
|
158
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
159
160
|
new Proxy(() => {}, {
|
|
160
161
|
get(target, key, value) {
|
|
161
162
|
return createProxy(domain, `${path}/${key.toString()}`, config)
|
|
@@ -164,12 +165,17 @@ const createProxy = (
|
|
|
164
165
|
target,
|
|
165
166
|
_,
|
|
166
167
|
[
|
|
167
|
-
{ $query, $fetch, $
|
|
168
|
+
{ $query, $fetch, $headers, ...bodyObj } = {
|
|
168
169
|
$fetch: undefined,
|
|
169
|
-
$
|
|
170
|
-
$
|
|
170
|
+
$headers: undefined,
|
|
171
|
+
$query: undefined
|
|
171
172
|
}
|
|
172
|
-
]:
|
|
173
|
+
]: {
|
|
174
|
+
[x: string]: any
|
|
175
|
+
$fetch?: RequestInit
|
|
176
|
+
$headers?: HeadersInit
|
|
177
|
+
$query?: Record<string, string>
|
|
178
|
+
}[] = [{}]
|
|
173
179
|
) {
|
|
174
180
|
const i = path.lastIndexOf('/'),
|
|
175
181
|
method = path.slice(i + 1),
|
|
@@ -184,44 +190,54 @@ const createProxy = (
|
|
|
184
190
|
)
|
|
185
191
|
|
|
186
192
|
const execute = async () => {
|
|
187
|
-
let body
|
|
188
|
-
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
else
|
|
193
|
+
let body: any
|
|
194
|
+
|
|
195
|
+
const headers = {
|
|
196
|
+
...config.$fetch?.headers,
|
|
197
|
+
...$fetch?.headers,
|
|
198
|
+
...$headers
|
|
199
|
+
} as Record<string, string>
|
|
200
|
+
|
|
201
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
202
|
+
body = Object.keys(bodyObj).length ? bodyObj : undefined
|
|
203
|
+
const isObject = typeof body === 'object'
|
|
204
|
+
const isFormData = isObject && hasFile(body)
|
|
205
|
+
|
|
206
|
+
if (isFormData) {
|
|
207
|
+
const newBody = new FormData()
|
|
208
|
+
|
|
209
|
+
// FormData is 1 level deep
|
|
210
|
+
for (const [key, field] of Object.entries(body)) {
|
|
211
|
+
if (isServer) {
|
|
212
|
+
newBody.append(key, field as any)
|
|
213
|
+
} else {
|
|
208
214
|
// @ts-ignore
|
|
209
|
-
|
|
215
|
+
if (field instanceof File)
|
|
210
216
|
newBody.append(
|
|
211
|
-
key
|
|
212
|
-
await fileToBlob(
|
|
217
|
+
key,
|
|
218
|
+
await fileToBlob(field as any)
|
|
213
219
|
)
|
|
214
|
-
|
|
215
|
-
|
|
220
|
+
// @ts-ignore
|
|
221
|
+
else if (field instanceof FileList) {
|
|
222
|
+
// @ts-ignore
|
|
223
|
+
for (let i = 0; i < field.length; i++) {
|
|
224
|
+
newBody.append(
|
|
225
|
+
key as any,
|
|
226
|
+
await fileToBlob((field as any)[i])
|
|
227
|
+
)
|
|
228
|
+
}
|
|
229
|
+
} else newBody.append(key, field as string)
|
|
230
|
+
}
|
|
216
231
|
}
|
|
217
|
-
}
|
|
218
232
|
|
|
219
|
-
|
|
220
|
-
|
|
233
|
+
body = newBody
|
|
234
|
+
} else {
|
|
235
|
+
headers['content-type'] = isObject
|
|
236
|
+
? 'application/json'
|
|
237
|
+
: 'text/plain'
|
|
221
238
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
...$fetch?.headers
|
|
239
|
+
if (isObject) body = JSON.stringify(body)
|
|
240
|
+
}
|
|
225
241
|
}
|
|
226
242
|
|
|
227
243
|
const response = await (config.fetcher ?? fetch)(url, {
|
|
@@ -229,16 +245,7 @@ const createProxy = (
|
|
|
229
245
|
body,
|
|
230
246
|
...config.$fetch,
|
|
231
247
|
...$fetch,
|
|
232
|
-
headers
|
|
233
|
-
? isFormData
|
|
234
|
-
? $headers
|
|
235
|
-
: {
|
|
236
|
-
'content-type': isObject
|
|
237
|
-
? 'application/json'
|
|
238
|
-
: 'text/plain',
|
|
239
|
-
...$headers
|
|
240
|
-
}
|
|
241
|
-
: $headers
|
|
248
|
+
headers
|
|
242
249
|
})
|
|
243
250
|
|
|
244
251
|
let data
|
|
@@ -265,16 +272,14 @@ const createProxy = (
|
|
|
265
272
|
status: response.status,
|
|
266
273
|
raw: response,
|
|
267
274
|
headers: response.headers,
|
|
268
|
-
retry: execute
|
|
269
275
|
}
|
|
270
276
|
|
|
271
277
|
return {
|
|
272
278
|
data,
|
|
273
279
|
status: response.status,
|
|
274
|
-
|
|
280
|
+
response: response,
|
|
275
281
|
headers: response.headers,
|
|
276
|
-
error: null
|
|
277
|
-
retry: execute
|
|
282
|
+
error: null as null
|
|
278
283
|
}
|
|
279
284
|
}
|
|
280
285
|
|
|
@@ -282,7 +287,7 @@ const createProxy = (
|
|
|
282
287
|
}
|
|
283
288
|
}) as unknown as Record<string, unknown>
|
|
284
289
|
|
|
285
|
-
export const edenTreaty = <App extends Elysia<any>>(
|
|
290
|
+
export const edenTreaty = <App extends Elysia<any, any>>(
|
|
286
291
|
domain: string,
|
|
287
292
|
config: EdenTreaty.Config = {
|
|
288
293
|
fetcher: fetch
|