@elysiajs/eden 0.5.6 → 0.6.1

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/package.json CHANGED
@@ -1,86 +1,84 @@
1
1
  {
2
- "name": "@elysiajs/eden",
3
- "version": "0.5.6",
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"
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"
18
9
  },
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"
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
+ }
25
40
  },
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"
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"
32
51
  },
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
- "@elysiajs/fn": ">= 0.5.0",
62
- "@sinclair/typebox": ">= 0.28.10",
63
- "elysia": ">= 0.5.0"
64
- },
65
- "peerDependenciesMeta": {
66
- "@sinclair/typebox": {
67
- "optional": true
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"
68
83
  }
69
- },
70
- "devDependencies": {
71
- "@elysiajs/cors": "0.5.0",
72
- "@elysiajs/fn": "^0.5.2",
73
- "@sinclair/typebox": "^0.26.8",
74
- "@types/node": "^18.15.5",
75
- "bun-types": "^0.5.8",
76
- "elysia": "0.5.17",
77
- "eslint": "^8.26.0",
78
- "rimraf": "^4.4.1",
79
- "typescript": "^5.0.4",
80
- "vite": "^4.3.5",
81
- "vite-plugin-dts": "^2.3.0"
82
- },
83
- "dependencies": {
84
- "superjson": "^1.12.2"
85
- }
86
- }
84
+ }
@@ -5,7 +5,7 @@ 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> =>
@@ -25,6 +25,8 @@ export const edenFetch =
25
25
  //
26
26
  }
27
27
 
28
+ const fetch = config?.fetcher || globalThis.fetch
29
+
28
30
  // @ts-ignore
29
31
  return fetch(server + endpoint, {
30
32
  ...options,
@@ -1,16 +1,17 @@
1
- import type { Elysia, SCHEMA } from 'elysia'
1
+ import type { Elysia } from 'elysia'
2
2
  import type { EdenFetchError } from '../utils'
3
- import type { MapError, IsUnknown, IsNever, AnyTypedRoute } from '../types'
3
+ import type { MapError, IsUnknown, IsNever } from '../types'
4
4
 
5
5
  export namespace EdenFetch {
6
- export type Create<App extends Elysia<any>> = App['meta'] extends Record<
7
- typeof SCHEMA,
8
- infer Schema extends Record<string, any>
9
- >
6
+ export type Create<App extends Elysia<any, any>> = App['meta'] extends {
7
+ schema: infer Schema extends Record<string, any>
8
+ }
10
9
  ? EdenFetch.Fn<Schema>
11
10
  : 'Please install Elysia before using Eden'
12
11
 
13
- export interface Config {}
12
+ export interface Config {
13
+ fetcher?: typeof globalThis.fetch
14
+ }
14
15
 
15
16
  export type Fn<Schema extends Record<string, any>> = <
16
17
  Endpoint extends keyof Schema,
package/src/fn/index.ts CHANGED
@@ -47,7 +47,7 @@ const createProxy = (
47
47
  }
48
48
  }) as any
49
49
 
50
- export const edenFn = <App extends Elysia<any>>(
50
+ export const edenFn = <App extends Elysia<any, any>>(
51
51
  domain: string,
52
52
  config?: EdenFn.Config
53
53
  ): EdenFn.Create<App> =>
package/src/fn/types.ts CHANGED
@@ -1,10 +1,9 @@
1
- import type { Elysia, EXPOSED } from 'elysia'
1
+ import type { Elysia } from 'elysia'
2
2
 
3
3
  export namespace EdenFn {
4
- export type Create<App extends Elysia<any>> = App['meta'] extends Record<
5
- typeof EXPOSED,
6
- infer Schema extends Record<string, any>
7
- >
4
+ export type Create<App extends Elysia<any, any>> = App['meta'] extends {
5
+ exposed: infer Schema extends Record<string, any>
6
+ }
8
7
  ? EdenFn.Compose<Schema>
9
8
  : 'Please install Elysia before using Eden'
10
9
 
@@ -16,7 +15,7 @@ export namespace EdenFn {
16
15
  }
17
16
 
18
17
  export type Fn<T> = T extends {
19
- [EXPOSED]: any
18
+ exposed: any
20
19
  value: infer Value
21
20
  }
22
21
  ? Asynctify<Value>
@@ -25,6 +24,7 @@ export namespace EdenFn {
25
24
  export interface Config {
26
25
  fn?: string
27
26
  fetch?: Omit<RequestInit, 'body'>
27
+ fetcher?: typeof globalThis.fetch
28
28
  }
29
29
  }
30
30
 
package/src/fn/utils.ts CHANGED
@@ -52,6 +52,7 @@ export class Signal {
52
52
 
53
53
  const { serialize, deserialize } = await this.sJson
54
54
 
55
+ const fetch = this.config?.fetcher || globalThis.fetch
55
56
  const results = await fetch(
56
57
  `${this.url}${this.config.fn ?? '/~fn'}`,
57
58
  {
@@ -22,7 +22,7 @@ const isFile = (v: any) => {
22
22
 
23
23
  // FormData is 1 level deep
24
24
  const hasFile = (obj: Record<string, any>) => {
25
- for (let key in obj) {
25
+ for (const key in obj) {
26
26
  if (isFile(obj[key])) return true
27
27
  else if (
28
28
  Array.isArray(obj[key]) &&
@@ -114,7 +114,9 @@ export class EdenWS<Schema extends TypedSchema<any> = TypedSchema> {
114
114
  if (start === 47 || start === 123)
115
115
  try {
116
116
  data = JSON.parse(data)
117
- } catch {}
117
+ } catch {
118
+ // Not Empty
119
+ }
118
120
  else if (!Number.isNaN(+data)) data = +data
119
121
  else if (data === 'true') data = true
120
122
  else if (data === 'fase') data = false
@@ -150,9 +152,10 @@ export class EdenWS<Schema extends TypedSchema<any> = TypedSchema> {
150
152
 
151
153
  const createProxy = (
152
154
  domain: string,
153
- path: string = '',
155
+ path = '',
154
156
  config: EdenTreaty.Config
155
157
  ): Record<string, unknown> =>
158
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
156
159
  new Proxy(() => {}, {
157
160
  get(target, key, value) {
158
161
  return createProxy(domain, `${path}/${key.toString()}`, config)
@@ -180,7 +183,7 @@ const createProxy = (
180
183
  )
181
184
  )
182
185
 
183
- return (async () => {
186
+ const execute = async () => {
184
187
  let body =
185
188
  $body ?? (Object.keys(bodyObj).length ? bodyObj : undefined)
186
189
  const isObject = typeof body === 'object'
@@ -216,56 +219,72 @@ const createProxy = (
216
219
  body = newBody
217
220
  } else if (isObject) body = JSON.stringify(body)
218
221
 
219
- return (config.fetcher ?? fetch)(url, {
222
+ const $headers = {
223
+ ...config.$fetch?.headers,
224
+ ...$fetch?.headers
225
+ }
226
+
227
+ const response = await (config.fetcher ?? fetch)(url, {
220
228
  method,
221
229
  body,
222
- // ...config.fetch,
230
+ ...config.$fetch,
223
231
  ...$fetch,
224
232
  headers: body
225
233
  ? isFormData
226
- ? $fetch?.['headers']
234
+ ? $headers
227
235
  : {
228
236
  'content-type': isObject
229
237
  ? 'application/json'
230
238
  : 'text/plain',
231
- ...$fetch?.['headers']
239
+ ...$headers
232
240
  }
233
- : $fetch?.['headers']
234
- }).then(async (res) => {
235
- let data
236
-
237
- switch (res.headers.get('Content-Type')?.split(';')[0]) {
238
- case 'application/json':
239
- data = await res.json()
240
- break
241
-
242
- default:
243
- data = await res.text().then((data) => {
244
- if (!Number.isNaN(+data)) return +data
245
- if (data === 'true') return true
246
- if (data === 'false') return false
247
-
248
- return data
249
- })
241
+ : $headers
242
+ })
243
+
244
+ let data
245
+
246
+ switch (response.headers.get('Content-Type')?.split(';')[0]) {
247
+ case 'application/json':
248
+ data = await response.json()
249
+ break
250
+
251
+ default:
252
+ data = await response.text().then((data) => {
253
+ if (!Number.isNaN(+data)) return +data
254
+ if (data === 'true') return true
255
+ if (data === 'false') return false
256
+
257
+ return data
258
+ })
259
+ }
260
+
261
+ if (response.status > 300)
262
+ return {
263
+ data,
264
+ error: new EdenFetchError(response.status, await data),
265
+ status: response.status,
266
+ raw: response,
267
+ headers: response.headers,
268
+ retry: execute
250
269
  }
251
270
 
252
- if (res.status > 300)
253
- return {
254
- data,
255
- error: new EdenFetchError(res.status, await data)
256
- }
271
+ return {
272
+ data,
273
+ status: response.status,
274
+ raw: response,
275
+ headers: response.headers,
276
+ error: null,
277
+ retry: execute
278
+ }
279
+ }
257
280
 
258
- return { data, error: null }
259
- })
260
- })()
281
+ return execute()
261
282
  }
262
283
  }) as unknown as Record<string, unknown>
263
284
 
264
- export const edenTreaty = <App extends Elysia<any>>(
285
+ export const edenTreaty = <App extends Elysia<any, any>>(
265
286
  domain: string,
266
- config: {
267
- fetcher?: typeof fetch
268
- } = {
287
+ config: EdenTreaty.Config = {
269
288
  fetcher: fetch
270
289
  }
271
290
  ): EdenTreaty.Create<App> =>