@elysiajs/eden 0.3.0-rc.1 → 0.3.0

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 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 { eden } from '@elysiajs/eden'
30
+ import { edenTreaty } from '@elysiajs/eden'
35
31
  import type { App } from './server'
36
32
 
37
- const client = eden<App>('http://localhost:8080')
33
+ const app = edenTreaty<App>('http://localhost:8080')
38
34
 
39
- // return: Hi Elysia (fully type-safe)
40
- client.index.get().then(console.log)
35
+ // data: Hi Elysia (fully type-safe)
36
+ const { data: pong } = app.index.get()
41
37
 
42
- // return: 1895
43
- client.id.1895.get().then(console.log)
38
+ // data: 1895
39
+ const { data: id } = client.id.1895.get()
44
40
 
45
- // return: { id: 1895, name: 'Skadi' }
46
- client.mirror.post({
41
+ // data: { id: 1895, name: 'Skadi' }
42
+ const { data: nendoroid } = app.mirror.post({
47
43
  id: 1895,
48
44
  name: 'Skadi'
49
- }).then(console.log)
45
+ })
50
46
  ```
@@ -22,5 +22,11 @@ export declare namespace EdenFetch {
22
22
  body: Route['body'];
23
23
  } : {
24
24
  body?: unknown;
25
- })) => Promise<Route['response']['200']> | (MapError<Route['response']> extends infer Errors ? IsNever<Errors> extends true ? EdenFetchError<number, string> : Errors : never);
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 o=require("./utils-0d7d9b21.js"),h=(l,p)=>async(r,{params:s,body:c,...i}={})=>{var u;s&&Object.entries(s).forEach(([e,t])=>{r=r.replace(`:${e}`,t)});const a=(u=i.headers)==null?void 0:u["Content-Type"];return(!a||a==="application/json")&&(c=JSON.stringify(c)),fetch(l+r,{...i,headers:{"content-type":"application/json",...i.headers},body:c}).then(async e=>{var f;let t;switch((f=e.headers.get("Content-Type"))==null?void 0:f.split(";")[0]){case"application/json":t=e.json();break;default:t=e.text().then(n=>Number.isNaN(+n)?n==="true"?!0:n==="false"?!1:n:+n)}return e.status>300?new o.EdenFetchError(e.status,await t):t})};exports.edenFetch=h;
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 = (h, l) => (
2
+ const j = (l, h) => (
3
3
  // @ts-ignore
4
- async (r, { params: a, body: c, ...s } = {}) => {
5
- var f;
6
- a && Object.entries(a).forEach(([e, t]) => {
7
- r = r.replace(`:${e}`, t);
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 i = (f = s.headers) == null ? void 0 : f["Content-Type"];
10
- return (!i || i === "application/json") && (c = JSON.stringify(c)), fetch(h + r, {
11
- ...s,
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
- ...s.headers
14
+ ...c.headers
15
15
  },
16
- body: c
16
+ body: a
17
17
  }).then(async (e) => {
18
- var u;
18
+ var f;
19
19
  let t;
20
- switch ((u = e.headers.get("Content-Type")) == null ? void 0 : u.split(";")[0]) {
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((n) => Number.isNaN(+n) ? n === "true" ? !0 : n === "false" ? !1 : n : +n);
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 ? new o(e.status, await t) : t;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elysiajs/eden",
3
- "version": "0.3.0-rc.1",
3
+ "version": "0.3.0",
4
4
  "description": "Fully type-safe Elysia client",
5
5
  "author": {
6
6
  "name": "saltyAom",
@@ -55,7 +55,7 @@
55
55
  },
56
56
  "peerDependencies": {
57
57
  "@sinclair/typebox": ">= 0.25.24",
58
- "elysia": ">= 0.3.0-rc.1"
58
+ "elysia": ">= 0.3.0"
59
59
  },
60
60
  "peerDependenciesMeta": {
61
61
  "@sinclair/typebox": {
@@ -67,7 +67,7 @@
67
67
  "@sinclair/typebox": "^0.25.24",
68
68
  "@types/node": "^18.11.7",
69
69
  "bun-types": "^0.5.7",
70
- "elysia": "rc",
70
+ "elysia": "^0.3.0",
71
71
  "eslint": "^8.26.0",
72
72
  "rimraf": "^3.0.2",
73
73
  "typescript": "^4.8.4",
@@ -30,15 +30,15 @@ export const edenFetch =
30
30
  },
31
31
  body
32
32
  }).then(async (res) => {
33
- let data: Promise<unknown>
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 new EdenFetchError(res.status, await data)
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
  }
@@ -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
- | Promise<Route['response']['200']>
48
- | (MapError<Route['response']> extends infer Errors
49
- ? IsNever<Errors> extends true
50
- ? EdenFetchError<number, string>
51
- : Errors
52
- : never)
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
  }