@elysiajs/eden 0.3.0-rc.1 → 0.3.0-rc.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/README.md +11 -15
- package/dist/fetch/types.d.ts +7 -1
- package/dist/fetch.js +1 -1
- package/dist/fetch.mjs +18 -15
- package/package.json +1 -1
- package/src/fetch/index.ts +8 -5
- package/src/fetch/types.ts +14 -7
package/README.md
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
# @elysiajs/eden
|
|
2
|
-
Fully type-safe Elysia client.
|
|
2
|
+
Fully type-safe Elysia client refers to the [documentation](https://elysiajs.com/plugins/eden/overview)
|
|
3
3
|
|
|
4
4
|
## Installation
|
|
5
5
|
```bash
|
|
6
|
-
bun add @elysiajs/eden
|
|
7
|
-
bun add -d elysia
|
|
6
|
+
bun add elysia @elysiajs/eden
|
|
8
7
|
```
|
|
9
8
|
|
|
10
|
-
### note
|
|
11
|
-
Eden requires type definition from elysia to infer type accurately, it's recommended to install elysia as devDependencies with the same version as your API.
|
|
12
|
-
|
|
13
9
|
## Example
|
|
14
10
|
```typescript
|
|
15
11
|
// server.ts
|
|
@@ -31,20 +27,20 @@ const app = new Elysia()
|
|
|
31
27
|
export type App = typeof app
|
|
32
28
|
|
|
33
29
|
// client.ts
|
|
34
|
-
import {
|
|
30
|
+
import { edenTreaty } from '@elysiajs/eden'
|
|
35
31
|
import type { App } from './server'
|
|
36
32
|
|
|
37
|
-
const
|
|
33
|
+
const app = edenTreaty<App>('http://localhost:8080')
|
|
38
34
|
|
|
39
|
-
//
|
|
40
|
-
|
|
35
|
+
// data: Hi Elysia (fully type-safe)
|
|
36
|
+
const { data: pong } = app.index.get()
|
|
41
37
|
|
|
42
|
-
//
|
|
43
|
-
client.id.1895.get()
|
|
38
|
+
// data: 1895
|
|
39
|
+
const { data: id } = client.id.1895.get()
|
|
44
40
|
|
|
45
|
-
//
|
|
46
|
-
|
|
41
|
+
// data: { id: 1895, name: 'Skadi' }
|
|
42
|
+
const { data: nendoroid } = app.mirror.post({
|
|
47
43
|
id: 1895,
|
|
48
44
|
name: 'Skadi'
|
|
49
|
-
})
|
|
45
|
+
})
|
|
50
46
|
```
|
package/dist/fetch/types.d.ts
CHANGED
|
@@ -22,5 +22,11 @@ export declare namespace EdenFetch {
|
|
|
22
22
|
body: Route['body'];
|
|
23
23
|
} : {
|
|
24
24
|
body?: unknown;
|
|
25
|
-
})) => Promise<
|
|
25
|
+
})) => Promise<{
|
|
26
|
+
data: Route['response']['200'];
|
|
27
|
+
error: null;
|
|
28
|
+
} | {
|
|
29
|
+
data: null;
|
|
30
|
+
error: MapError<Route['response']> extends infer Errors ? IsNever<Errors> extends true ? EdenFetchError<number, string> : Errors : EdenFetchError<number, string>;
|
|
31
|
+
}>;
|
|
26
32
|
}
|
package/dist/fetch.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("./utils-0d7d9b21.js"),h=(o,p)=>async(n,{params:i,body:a,...c}={})=>{var u;i&&Object.entries(i).forEach(([e,t])=>{n=n.replace(`:${e}`,t)});const s=(u=c.headers)==null?void 0:u["Content-Type"];return(!s||s==="application/json")&&(a=JSON.stringify(a)),fetch(o+n,{...c,headers:{"content-type":"application/json",...c.headers},body:a}).then(async e=>{var l;let t;switch((l=e.headers.get("Content-Type"))==null?void 0:l.split(";")[0]){case"application/json":t=await e.json();break;default:t=await e.text().then(r=>Number.isNaN(+r)?r==="true"?!0:r==="false"?!1:r:+r)}return e.status>300?{data:null,error:new f.EdenFetchError(e.status,t)}:{data:t,error:null}})};exports.edenFetch=h;
|
package/dist/fetch.mjs
CHANGED
|
@@ -1,30 +1,33 @@
|
|
|
1
1
|
import { E as o } from "./utils-0d2b8b1a.mjs";
|
|
2
|
-
const j = (
|
|
2
|
+
const j = (l, h) => (
|
|
3
3
|
// @ts-ignore
|
|
4
|
-
async (
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
async (n, { params: i, body: a, ...c } = {}) => {
|
|
5
|
+
var u;
|
|
6
|
+
i && Object.entries(i).forEach(([e, t]) => {
|
|
7
|
+
n = n.replace(`:${e}`, t);
|
|
8
8
|
});
|
|
9
|
-
const
|
|
10
|
-
return (!
|
|
11
|
-
...
|
|
9
|
+
const s = (u = c.headers) == null ? void 0 : u["Content-Type"];
|
|
10
|
+
return (!s || s === "application/json") && (a = JSON.stringify(a)), fetch(l + n, {
|
|
11
|
+
...c,
|
|
12
12
|
headers: {
|
|
13
13
|
"content-type": "application/json",
|
|
14
|
-
...
|
|
14
|
+
...c.headers
|
|
15
15
|
},
|
|
16
|
-
body:
|
|
16
|
+
body: a
|
|
17
17
|
}).then(async (e) => {
|
|
18
|
-
var
|
|
18
|
+
var f;
|
|
19
19
|
let t;
|
|
20
|
-
switch ((
|
|
20
|
+
switch ((f = e.headers.get("Content-Type")) == null ? void 0 : f.split(";")[0]) {
|
|
21
21
|
case "application/json":
|
|
22
|
-
t = e.json();
|
|
22
|
+
t = await e.json();
|
|
23
23
|
break;
|
|
24
24
|
default:
|
|
25
|
-
t = e.text().then((
|
|
25
|
+
t = await e.text().then((r) => Number.isNaN(+r) ? r === "true" ? !0 : r === "false" ? !1 : r : +r);
|
|
26
26
|
}
|
|
27
|
-
return e.status > 300 ?
|
|
27
|
+
return e.status > 300 ? {
|
|
28
|
+
data: null,
|
|
29
|
+
error: new o(e.status, t)
|
|
30
|
+
} : { data: t, error: null };
|
|
28
31
|
});
|
|
29
32
|
}
|
|
30
33
|
);
|
package/package.json
CHANGED
package/src/fetch/index.ts
CHANGED
|
@@ -30,15 +30,15 @@ export const edenFetch =
|
|
|
30
30
|
},
|
|
31
31
|
body
|
|
32
32
|
}).then(async (res) => {
|
|
33
|
-
let data
|
|
33
|
+
let data
|
|
34
34
|
|
|
35
35
|
switch (res.headers.get('Content-Type')?.split(';')[0]) {
|
|
36
36
|
case 'application/json':
|
|
37
|
-
data = res.json()
|
|
37
|
+
data = await res.json()
|
|
38
38
|
break
|
|
39
39
|
|
|
40
40
|
default:
|
|
41
|
-
data = res.text().then((data) => {
|
|
41
|
+
data = await res.text().then((data) => {
|
|
42
42
|
if (!Number.isNaN(+data)) return +data
|
|
43
43
|
if (data === 'true') return true
|
|
44
44
|
if (data === 'false') return false
|
|
@@ -48,8 +48,11 @@ export const edenFetch =
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
if (res.status > 300)
|
|
51
|
-
return
|
|
51
|
+
return {
|
|
52
|
+
data: null,
|
|
53
|
+
error: new EdenFetchError(res.status, data)
|
|
54
|
+
}
|
|
52
55
|
|
|
53
|
-
return data
|
|
56
|
+
return { data, error: null }
|
|
54
57
|
})
|
|
55
58
|
}
|
package/src/fetch/types.ts
CHANGED
|
@@ -43,11 +43,18 @@ export namespace EdenFetch {
|
|
|
43
43
|
(IsUnknown<Route['body']> extends false
|
|
44
44
|
? { body: Route['body'] }
|
|
45
45
|
: { body?: unknown })
|
|
46
|
-
) =>
|
|
47
|
-
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
:
|
|
46
|
+
) => Promise<
|
|
47
|
+
| {
|
|
48
|
+
data: Route['response']['200']
|
|
49
|
+
error: null
|
|
50
|
+
}
|
|
51
|
+
| {
|
|
52
|
+
data: null
|
|
53
|
+
error: MapError<Route['response']> extends infer Errors
|
|
54
|
+
? IsNever<Errors> extends true
|
|
55
|
+
? EdenFetchError<number, string>
|
|
56
|
+
: Errors
|
|
57
|
+
: EdenFetchError<number, string>
|
|
58
|
+
}
|
|
59
|
+
>
|
|
53
60
|
}
|