@elysiajs/eden 0.6.1 → 0.6.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.
Files changed (56) hide show
  1. package/dist/errors.js +10 -0
  2. package/dist/errors.js.map +1 -0
  3. package/dist/fetch/index.js +67 -0
  4. package/dist/fetch/index.js.map +1 -0
  5. package/dist/fetch/types.js +2 -0
  6. package/dist/fetch/types.js.map +1 -0
  7. package/dist/fetch.js +1 -1
  8. package/dist/fetch.mjs +32 -22
  9. package/dist/fn/index.js +35 -0
  10. package/dist/fn/index.js.map +1 -0
  11. package/dist/fn/types.js +2 -0
  12. package/dist/fn/types.js.map +1 -0
  13. package/dist/fn/utils.js +59 -0
  14. package/dist/fn/utils.js.map +1 -0
  15. package/dist/index.js +25 -1
  16. package/dist/index.js.map +1 -0
  17. package/dist/index.mjs +1 -1
  18. package/dist/treaty/index.js +205 -0
  19. package/dist/treaty/index.js.map +1 -0
  20. package/dist/treaty/types.js +2 -0
  21. package/dist/treaty/types.js.map +1 -0
  22. package/dist/treaty/utils.js +13 -0
  23. package/dist/treaty/utils.js.map +1 -0
  24. package/dist/treaty.js +1 -1
  25. package/dist/treaty.mjs +108 -112
  26. package/dist/types.js +2 -0
  27. package/dist/types.js.map +1 -0
  28. package/package.json +84 -79
  29. package/src/fetch/index.ts +19 -5
  30. package/src/fetch/types.ts +32 -13
  31. package/src/treaty/index.ts +57 -52
  32. package/src/treaty/types.ts +116 -205
  33. package/src/types.ts +1 -1
  34. package/dist/fetch/index.d.ts +0 -5
  35. package/dist/fetch/index.d.ts.map +0 -1
  36. package/dist/fetch/types.d.ts +0 -36
  37. package/dist/fetch/types.d.ts.map +0 -1
  38. package/dist/fn/index.d.ts +0 -5
  39. package/dist/fn/index.d.ts.map +0 -1
  40. package/dist/fn/types.d.ts +0 -28
  41. package/dist/fn/types.d.ts.map +0 -1
  42. package/dist/fn/utils.d.ts +0 -14
  43. package/dist/fn/utils.d.ts.map +0 -1
  44. package/dist/index.d.ts +0 -4
  45. package/dist/index.d.ts.map +0 -1
  46. package/dist/treaty/index.d.ts +0 -18
  47. package/dist/treaty/index.d.ts.map +0 -1
  48. package/dist/treaty/utils.d.ts +0 -2
  49. package/dist/treaty/utils.d.ts.map +0 -1
  50. package/dist/types.d.ts +0 -26
  51. package/dist/types.d.ts.map +0 -1
  52. package/dist/utils.d.ts +0 -6
  53. package/dist/utils.d.ts.map +0 -1
  54. /package/dist/{utils-5a40996f.mjs → errors-5a40996f.mjs} +0 -0
  55. /package/dist/{utils-a2cfc56a.js → errors-a2cfc56a.js} +0 -0
  56. /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 j = (r, e, t) => e in r ? A(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
- var g = (r, e, t) => (j(r, typeof e != "symbol" ? e + "" : e, t), t);
4
- import { E as B } from "./utils-5a40996f.mjs";
5
- const P = (r, e, t) => {
6
- if (r.endsWith("/") || (r += "/"), e === "index" && (e = ""), !t || !Object.keys(t).length)
7
- return `${r}${e}`;
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 [a, s] of Object.entries(t))
10
- n += `${a}=${s}&`;
11
- return `${r}${e}?${n.slice(0, -1)}`;
12
- }, v = typeof FileList > "u", N = (r) => v ? r instanceof Blob : r instanceof FileList || r instanceof File, W = (r) => {
13
- for (const e in r) {
14
- if (N(r[e]))
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(r[e]) && r[e].find((t) => N(t)))
18
+ if (Array.isArray(s[e]) && s[e].find((t) => L(t)))
17
19
  return !0;
18
20
  }
19
21
  return !1;
20
- }, S = (r) => v ? r : new Promise((e) => {
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: r.type }));
24
- }, t.readAsArrayBuffer(r);
25
+ e(new Blob([t.result], { type: s.type }));
26
+ }, t.readAsArrayBuffer(s);
25
27
  });
26
- class J {
28
+ class P {
27
29
  constructor(e) {
28
- g(this, "ws");
29
- g(this, "url");
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
- (a) => {
51
+ (o) => {
50
52
  if (e === "message") {
51
- let s = a.data.toString();
52
- const f = s.charCodeAt(0);
53
- if (f === 47 || f === 123)
53
+ let r = o.data.toString();
54
+ const c = r.charCodeAt(0);
55
+ if (c === 47 || c === 123)
54
56
  try {
55
- s = JSON.parse(s);
57
+ r = JSON.parse(r);
56
58
  } catch {
57
59
  }
58
60
  else
59
- Number.isNaN(+s) ? s === "true" ? s = !0 : s === "fase" && (s = !1) : s = +s;
61
+ Number.isNaN(+r) ? r === "true" ? r = !0 : r === "fase" && (r = !1) : r = +r;
60
62
  t({
61
- ...a,
62
- data: s
63
+ ...o,
64
+ data: r
63
65
  });
64
66
  } else
65
- t(a);
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 O = (r, e = "", t) => (
78
- // eslint-disable-next-line @typescript-eslint/no-empty-function
79
- new Proxy(() => {
80
- }, {
81
- get(n, a, s) {
82
- return O(r, `${e}/${a.toString()}`, t);
83
- },
84
- apply(n, a, [
85
- { $query: s, $fetch: f, $body: k, ...E } = {
86
- $fetch: void 0,
87
- $query: void 0,
88
- $body: void 0
89
- }
90
- ] = [{}]) {
91
- const F = e.lastIndexOf("/"), $ = e.slice(F + 1), h = P(r, e.slice(0, F), s);
92
- if ($ === "subscribe")
93
- return new J(
94
- h.replace(
95
- /^([^]+):\/\//,
96
- h.startsWith("https://") ? "wss://" : "ws://"
97
- )
98
- );
99
- const y = async () => {
100
- var x, L;
101
- let c = k ?? (Object.keys(E).length ? E : void 0);
102
- const w = typeof c == "object", m = w && W(c);
103
- if (m) {
104
- const o = new FormData();
105
- for (const [d, l] of Object.entries(c))
106
- if (v)
107
- o.append(d, l);
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
- o.append(
110
- d,
111
- await S(l)
114
+ d.append(
115
+ y,
116
+ await N(l)
112
117
  );
113
118
  else if (l instanceof FileList)
114
- for (let b = 0; b < l.length; b++)
115
- o.append(
116
- d,
117
- await S(l[b])
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
- o.append(d, l);
121
- c = o;
125
+ d.append(y, l);
126
+ f = d;
122
127
  } else
123
- w && (c = JSON.stringify(c));
124
- const p = {
125
- ...(x = t.$fetch) == null ? void 0 : x.headers,
126
- ...f == null ? void 0 : f.headers
127
- }, i = await (t.fetcher ?? fetch)(h, {
128
- method: $,
129
- body: c,
130
- ...t.$fetch,
131
- ...f,
132
- headers: c ? m ? p : {
133
- "content-type": w ? "application/json" : "text/plain",
134
- ...p
135
- } : p
136
- });
137
- let u;
138
- switch ((L = i.headers.get("Content-Type")) == null ? void 0 : L.split(";")[0]) {
139
- case "application/json":
140
- u = await i.json();
141
- break;
142
- default:
143
- u = await i.text().then((o) => Number.isNaN(+o) ? o === "true" ? !0 : o === "false" ? !1 : o : +o);
144
- }
145
- return i.status > 300 ? {
146
- data: u,
147
- error: new B(i.status, await u),
148
- status: i.status,
149
- raw: i,
150
- headers: i.headers,
151
- retry: y
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
- return y();
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 O(r, n, e);
166
+ return S(s, n, e);
171
167
  }
172
168
  }
173
169
  );
174
170
  export {
175
- J as EdenWS,
176
- D as edenTreaty
171
+ P as EdenWS,
172
+ C as edenTreaty
177
173
  };
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,84 +1,89 @@
1
1
  {
2
- "name": "@elysiajs/eden",
3
- "version": "0.6.1",
4
- "description": "Fully type-safe Elysia client",
5
- "author": {
6
- "name": "saltyAom",
7
- "url": "https://github.com/SaltyAom",
8
- "email": "saltyaom@gmail.com"
2
+ "name": "@elysiajs/eden",
3
+ "version": "0.6.3",
4
+ "description": "Fully type-safe Elysia client",
5
+ "author": {
6
+ "name": "saltyAom",
7
+ "url": "https://github.com/SaltyAom",
8
+ "email": "saltyaom@gmail.com"
9
+ },
10
+ "main": "./dist/index.js",
11
+ "exports": {
12
+ ".": {
13
+ "require": "./dist/index.js",
14
+ "import": "./dist/index.mjs",
15
+ "node": "./dist/index.js",
16
+ "default": "./dist/index.js",
17
+ "types": "./dist/index.d.ts"
9
18
  },
10
- "main": "./dist/index.js",
11
- "exports": {
12
- ".": {
13
- "require": "./dist/index.js",
14
- "import": "./dist/index.mjs",
15
- "node": "./dist/index.js",
16
- "default": "./dist/index.js",
17
- "types": "./dist/index.d.ts"
18
- },
19
- "./treaty": {
20
- "require": "./dist/treaty.js",
21
- "import": "./dist/treaty.mjs",
22
- "node": "./dist/treaty.js",
23
- "default": "./dist/treaty.js",
24
- "types": "./dist/treaty/index.d.ts"
25
- },
26
- "./fetch": {
27
- "require": "./dist/fetch.js",
28
- "import": "./dist/fetch.mjs",
29
- "node": "./dist/fetch.js",
30
- "default": "./dist/fetch.js",
31
- "types": "./dist/fetch/index.d.ts"
32
- },
33
- "./fn": {
34
- "require": "./dist/fn.js",
35
- "import": "./dist/fn.mjs",
36
- "node": "./dist/fn.js",
37
- "default": "./dist/fn.js",
38
- "types": "./dist/fn/index.d.ts"
39
- }
19
+ "./treaty": {
20
+ "require": "./dist/treaty.js",
21
+ "import": "./dist/treaty.mjs",
22
+ "node": "./dist/treaty.js",
23
+ "default": "./dist/treaty.js",
24
+ "types": "./dist/treaty/index.d.ts"
40
25
  },
41
- "types": "./src/index.ts",
42
- "keywords": [
43
- "elysia",
44
- "eden",
45
- "connector"
46
- ],
47
- "homepage": "https://github.com/elysiajs/elysia-eden",
48
- "repository": {
49
- "type": "git",
50
- "url": "https://github.com/elysiajs/elysia-eden"
26
+ "./fetch": {
27
+ "require": "./dist/fetch.js",
28
+ "import": "./dist/fetch.mjs",
29
+ "node": "./dist/fetch.js",
30
+ "default": "./dist/fetch.js",
31
+ "types": "./dist/fetch/index.d.ts"
51
32
  },
52
- "bugs": "https://github.com/elysiajs/elysia-eden/issues",
53
- "license": "MIT",
54
- "scripts": {
55
- "dev": "bun run --hot example/index.ts",
56
- "test": "bun wiptest",
57
- "build": "vite build && tsc",
58
- "release": "npm run build && npm run test && npm publish --access public"
59
- },
60
- "peerDependencies": {
61
- "elysia": ">= 0.6.7"
62
- },
63
- "peerDependenciesMeta": {
64
- "@sinclair/typebox": {
65
- "optional": true
66
- }
67
- },
68
- "devDependencies": {
69
- "@elysiajs/cors": "0.5.0",
70
- "@elysiajs/fn": "^0.6.1",
71
- "@sinclair/typebox": "^0.26.8",
72
- "@types/node": "^18.15.5",
73
- "bun-types": "^0.5.8",
74
- "elysia": "0.6.10",
75
- "eslint": "^8.26.0",
76
- "rimraf": "^4.4.1",
77
- "typescript": "^5.0.4",
78
- "vite": "^4.3.5",
79
- "vite-plugin-dts": "^2.3.0"
80
- },
81
- "dependencies": {
82
- "superjson": "^1.12.2"
33
+ "./fn": {
34
+ "require": "./dist/fn.js",
35
+ "import": "./dist/fn.mjs",
36
+ "node": "./dist/fn.js",
37
+ "default": "./dist/fn.js",
38
+ "types": "./dist/fn/index.d.ts"
39
+ }
40
+ },
41
+ "types": "./src/index.ts",
42
+ "keywords": [
43
+ "elysia",
44
+ "eden",
45
+ "connector"
46
+ ],
47
+ "homepage": "https://github.com/elysiajs/elysia-eden",
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "https://github.com/elysiajs/elysia-eden"
51
+ },
52
+ "bugs": "https://github.com/elysiajs/elysia-eden/issues",
53
+ "license": "MIT",
54
+ "scripts": {
55
+ "dev": "bun run --hot example/index.ts",
56
+ "test": "bun wiptest",
57
+ "build": "vite build && tsc",
58
+ "release": "npm run build && npm run test && npm publish --access public"
59
+ },
60
+ "peerDependencies": {
61
+ "elysia": ">= 0.6.7"
62
+ },
63
+ "peerDependenciesMeta": {
64
+ "@sinclair/typebox": {
65
+ "optional": true
83
66
  }
84
- }
67
+ },
68
+ "devDependencies": {
69
+ "@elysiajs/cors": "0.6.0",
70
+ "@elysiajs/fn": "^0.6.1",
71
+ "@sinclair/typebox": "^0.26.8",
72
+ "@types/node": "^18.15.5",
73
+ "elysia": "0.6.10",
74
+ "eslint": "^8.26.0",
75
+ "rimraf": "^4.4.1",
76
+ "typescript": "^5.0.4",
77
+ "vite": "^4.4.9",
78
+ "vite-plugin-dts": "^2.3.0"
79
+ },
80
+ "dependencies": {
81
+ "superjson": "^1.12.2"
82
+ },
83
+ "prettier": {
84
+ "semi": false,
85
+ "tabWidth": 4,
86
+ "singleQuote": true,
87
+ "trailingComma": "none"
88
+ }
89
+ }
@@ -1,6 +1,6 @@
1
1
  import type { Elysia } from 'elysia'
2
2
 
3
- import { EdenFetchError } from '../utils'
3
+ import { EdenFetchError } from '../errors'
4
4
  import type { EdenFetch } from './types'
5
5
  export type { EdenFetch } from './types'
6
6
 
@@ -10,10 +10,10 @@ export const edenFetch =
10
10
  config?: EdenFetch.Config
11
11
  ): EdenFetch.Create<App> =>
12
12
  // @ts-ignore
13
- async (endpoint: string, { params, body, ...options } = {}) => {
13
+ (endpoint: string, { query, 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']
@@ -26,9 +26,12 @@ export const edenFetch =
26
26
  }
27
27
 
28
28
  const fetch = config?.fetcher || globalThis.fetch
29
+ const queryStr = query
30
+ ? `?${new URLSearchParams(query).toString()}`
31
+ : ''
29
32
 
30
33
  // @ts-ignore
31
- return fetch(server + endpoint, {
34
+ const execute = () => fetch(server + endpoint + queryStr, {
32
35
  ...options,
33
36
  headers: body
34
37
  ? {
@@ -59,9 +62,20 @@ export const edenFetch =
59
62
  if (res.status > 300)
60
63
  return {
61
64
  data: null,
65
+ status: res.status,
66
+ headers: res.headers,
67
+ retry: execute,
62
68
  error: new EdenFetchError(res.status, data)
63
69
  }
64
70
 
65
- return { data, error: null }
71
+ return {
72
+ data,
73
+ error: null,
74
+ status: res.status,
75
+ headers: res.headers,
76
+ retry: execute
77
+ }
66
78
  })
79
+
80
+ return execute()
67
81
  }
@@ -1,7 +1,11 @@
1
1
  import type { Elysia } from 'elysia'
2
- import type { EdenFetchError } from '../utils'
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
10
  export type Create<App extends Elysia<any, any>> = App['meta'] extends {
7
11
  schema: infer Schema extends Record<string, any>
@@ -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
- data: Awaited<Route['response']['200']>
50
- error: null
51
- }
52
- | {
53
- data: null
54
- error: MapError<Route['response']> extends infer Errors
55
- ? IsNever<Errors> extends true
56
- ? EdenFetchError<number, string>
57
- : Errors
58
- : EdenFetchError<number, string>
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
  }