@elysiajs/eden 0.8.1 → 1.0.0-beta.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/dist/types.d.ts CHANGED
@@ -15,12 +15,24 @@ export type IsAny<T> = 0 extends 1 & T ? true : false;
15
15
  export type IsNever<T> = [T] extends [never] ? true : false;
16
16
  export type IsUnknown<T> = IsAny<T> extends true ? false : unknown extends T ? true : false;
17
17
  export type AnyTypedRoute = {
18
- body: unknown;
19
- headers: Record<string, any> | undefined;
20
- query: Record<string, any> | undefined;
21
- params: Record<string, any> | undefined;
22
- response: Record<string, unknown> & {
23
- '200': unknown;
24
- };
18
+ body?: unknown;
19
+ headers?: unknown;
20
+ query?: unknown;
21
+ params?: unknown;
22
+ response: Record<number, unknown>;
25
23
  };
24
+ export type Prettify<T> = {
25
+ [K in keyof T]: T[K];
26
+ } & {};
27
+ export type TreatyToPath<T, Path extends string = ''> = UnionToIntersect<T extends Record<string, unknown> ? {
28
+ [K in keyof T]: T[K] extends AnyTypedRoute ? {
29
+ [path in Path]: {
30
+ [method in K]: T[K];
31
+ };
32
+ } : unknown extends T[K] ? {
33
+ [path in Path]: {
34
+ [method in K]: T[K];
35
+ };
36
+ } : TreatyToPath<T[K], `${Path}/${K & string}`>;
37
+ }[keyof T] : {}>;
26
38
  export {};
package/package.json CHANGED
@@ -1,82 +1,74 @@
1
1
  {
2
- "name": "@elysiajs/eden",
3
- "version": "0.8.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": "1.0.0-beta.1",
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
+ "module": "./dist/index.mjs",
12
+ "exports": {
13
+ ".": {
14
+ "require": "./dist/index.js",
15
+ "import": "./dist/index.mjs",
16
+ "node": "./dist/index.js",
17
+ "default": "./dist/index.js",
18
+ "types": "./dist/index.d.ts"
9
19
  },
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
- }
20
+ "./treaty": {
21
+ "require": "./dist/treaty.js",
22
+ "import": "./dist/treaty.mjs",
23
+ "node": "./dist/treaty.js",
24
+ "default": "./dist/treaty.js",
25
+ "types": "./dist/treaty/index.d.ts"
33
26
  },
34
- "types": "./src/index.ts",
35
- "keywords": [
36
- "elysia",
37
- "eden",
38
- "connector"
39
- ],
40
- "homepage": "https://github.com/elysiajs/eden",
41
- "repository": {
42
- "type": "git",
43
- "url": "https://github.com/elysiajs/eden"
44
- },
45
- "bugs": "https://github.com/elysiajs/eden/issues",
46
- "license": "MIT",
47
- "scripts": {
48
- "dev": "bun run --watch example/index.ts",
49
- "test": "bun test",
50
- "build": "rimraf dist && tsup",
51
- "release": "npm run build && npm run test && npm publish --access public"
52
- },
53
- "peerDependencies": {
54
- "elysia": ">= 0.8.0"
55
- },
56
- "peerDependenciesMeta": {
57
- "@sinclair/typebox": {
58
- "optional": true
59
- }
60
- },
61
- "devDependencies": {
62
- "@elysiajs/cors": "0.7.0",
63
- "@sinclair/typebox": "^0.31.6",
64
- "@types/node": "^18.15.5",
65
- "bun-types": "^1.0.1",
66
- "elysia": "0.8.0",
67
- "esbuild": "^0.19.3",
68
- "eslint": "^8.26.0",
69
- "rimraf": "^4.4.1",
70
- "tsup": "^7.2.0",
71
- "typescript": "^5.2.2"
72
- },
73
- "prettier": {
74
- "semi": false,
75
- "tabWidth": 4,
76
- "singleQuote": true,
77
- "trailingComma": "none"
78
- },
79
- "dependencies": {
80
- "superjson": "^2.2.1"
27
+ "./fetch": {
28
+ "require": "./dist/fetch.js",
29
+ "import": "./dist/fetch.mjs",
30
+ "node": "./dist/fetch.js",
31
+ "default": "./dist/fetch.js",
32
+ "types": "./dist/fetch/index.d.ts"
81
33
  }
34
+ },
35
+ "types": "./dist/index.d.ts",
36
+ "keywords": [
37
+ "elysia",
38
+ "eden",
39
+ "connector"
40
+ ],
41
+ "homepage": "https://github.com/elysiajs/eden",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "https://github.com/elysiajs/eden"
45
+ },
46
+ "bugs": "https://github.com/elysiajs/eden/issues",
47
+ "license": "MIT",
48
+ "scripts": {
49
+ "dev": "bun run --watch example/index.ts",
50
+ "test": "bun test",
51
+ "build": "rimraf dist && tsup",
52
+ "release": "npm run build && npm run test && npm publish --access public"
53
+ },
54
+ "peerDependencies": {
55
+ "elysia": ">= 1.0.0-beta.0"
56
+ },
57
+ "devDependencies": {
58
+ "@elysiajs/cors": "0.7.0",
59
+ "@types/bun": "^1.0.3",
60
+ "@types/node": "^18.15.5",
61
+ "elysia": "1.0.0-beta.0",
62
+ "esbuild": "^0.19.3",
63
+ "eslint": "^8.26.0",
64
+ "rimraf": "^4.4.1",
65
+ "tsup": "^7.2.0",
66
+ "typescript": "^5.3.2"
67
+ },
68
+ "prettier": {
69
+ "semi": false,
70
+ "tabWidth": 4,
71
+ "singleQuote": true,
72
+ "trailingComma": "none"
73
+ }
82
74
  }
@@ -1 +0,0 @@
1
- import{a as L}from"./chunk-Y6SMVGJX.mjs";var W=(n,e,t)=>{if(n.endsWith("/")||(n+="/"),e==="index"&&(e=""),!t||!Object.keys(t).length)return`${n}${e}`;let s="";for(let[c,r]of Object.entries(t))s+=`${c}=${r}&`;return`${n}${e}?${s.slice(0,-1)}`},k=n=>!Number.isNaN(parseInt(n));var F=typeof FileList>"u",K=n=>F?n instanceof Blob:n instanceof FileList||n instanceof File,J=n=>{if(!n)return!1;for(let e in n){if(K(n[e]))return!0;if(Array.isArray(n[e])&&n[e].find(t=>K(t)))return!0}return!1},T=n=>F?n:new Promise(e=>{let t=new FileReader;t.onload=()=>{let s=new File([t.result],n.name,{lastModified:n.lastModified,type:n.type});e(s)},t.readAsArrayBuffer(n)}),$=class{ws;url;constructor(e){this.ws=new WebSocket(e),this.url=e}send(e){return Array.isArray(e)?(e.forEach(t=>this.send(t)),this):(this.ws.send(typeof e=="object"?JSON.stringify(e):e.toString()),this)}on(e,t,s){return this.addEventListener(e,t,s)}off(e,t,s){return this.ws.removeEventListener(e,t,s),this}subscribe(e,t){return this.addEventListener("message",e,t)}addEventListener(e,t,s){return this.ws.addEventListener(e,c=>{if(e==="message"){let r=c.data.toString(),u=r.charCodeAt(0);if(u===47||u===123)try{r=JSON.parse(r)}catch{}else k(r)?r=+r:r==="true"?r=!0:r==="false"&&(r=!1);t({...c,data:r})}else t(c)},s),this}removeEventListener(e,t,s){return this.off(e,t,s),this}close(){return this.ws.close(),this}},j=(n,e="",t)=>new Proxy(()=>{},{get(s,c,r){return j(n,`${e}/${c.toString()}`,t)},apply(s,c,[r,u={}]=[{},{}]){let y=r!==void 0&&(typeof r!="object"||Array.isArray(r))?r:void 0,{$query:I,$fetch:O,$headers:M,$transform:g,getRaw:N,...P}=r??{};y??=P;let w=e.lastIndexOf("/"),E=e.slice(w+1).toUpperCase(),S=W(n,w===-1?"/":e.slice(0,w),Object.assign(u.query??{},I)),C=t.fetcher??fetch,p=t.transform?Array.isArray(t.transform)?t.transform:[t.transform]:void 0,v=g?Array.isArray(g)?g:[g]:void 0;return v&&(p?p=v.concat(p):p=v),E==="SUBSCRIBE"?new $(S.replace(/^([^]+):\/\//,S.startsWith("https://")?"wss://":"ws://")):(async q=>{let f,R={...t.$fetch?.headers,...O?.headers,...u.headers,...M};if(E!=="GET"&&E!=="HEAD"){f=Object.keys(y).length||Array.isArray(y)?y:void 0;let a=typeof f=="object"||Array.isArray(y);if(a&&J(f)){let h=new FormData;for(let[m,o]of Object.entries(f))if(F)h.append(m,o);else if(o instanceof File)h.append(m,await T(o));else if(o instanceof FileList)for(let l=0;l<o.length;l++)h.append(m,await T(o[l]));else if(Array.isArray(o))for(let l=0;l<o.length;l++){let A=o[l];h.append(m,A instanceof File?await T(A):A)}else h.append(m,o);f=h}else R["content-type"]=a?"application/json":"text/plain",f=a?JSON.stringify(f):y}let i=await C(S,{method:E,body:f,...t.$fetch,...u.fetch,...O,headers:R}),b;if(q.getRaw)return i;switch(i.headers.get("Content-Type")?.split(";")[0]){case"application/json":b=await i.json();break;default:b=await i.text().then(a=>k(a)?+a:a==="true"?!0:a==="false"?!1:a)}let D=i.status>=300||i.status<200?new L(i.status,b):null,x={data:b,error:D,response:i,status:i.status,headers:i.headers};if(p)for(let a of p){let d=a(x);d instanceof Promise&&(d=await d),d!=null&&(x=d)}return x})({getRaw:N})}}),_=(n,e={fetcher:fetch})=>new Proxy({},{get(t,s){return j(n,s,e)}});export{$ as a,_ as b};
@@ -1 +0,0 @@
1
- import{a as y}from"./chunk-Y6SMVGJX.mjs";var m=(p,u)=>(s,{query:h,params:i,body:n,...r}={})=>{i&&Object.entries(i).forEach(([e,t])=>{s=s.replace(`:${e}`,t)});let o=r.headers?.["Content-Type"];if(!o||o==="application/json")try{n=JSON.stringify(n)}catch{}let f=u?.fetcher||globalThis.fetch,l=h?`?${new URLSearchParams(h).toString()}`:"",c=()=>f(p+s+l,{...r,method:r.method?.toUpperCase()||"GET",headers:n?{"content-type":"application/json",...r.headers}:r.headers,body:n}).then(async e=>{let t;switch(e.headers.get("Content-Type")?.split(";")[0]){case"application/json":t=await e.json();break;default:t=await e.text().then(a=>Number.isNaN(+a)?a==="true"?!0:a==="false"?!1:a:+a);break}return e.status>300?{data:null,status:e.status,headers:e.headers,retry:c,error:new y(e.status,t)}:{data:t,error:null,status:e.status,headers:e.headers,retry:c}});return c()};export{m as a};
@@ -1 +0,0 @@
1
- var s=class extends Error{status;value;constructor(e,t){super(),this.status=e,this.value=t}};export{s as a};
package/src/errors.ts DELETED
@@ -1,16 +0,0 @@
1
- import type { serialize, deserialize } from 'superjson'
2
-
3
- export class EdenFetchError<
4
- Status extends number = number,
5
- Value = unknown
6
- > extends Error {
7
- status: Status
8
- value: Value
9
-
10
- constructor(status: Status, value: Value) {
11
- super()
12
-
13
- this.status = status
14
- this.value = value
15
- }
16
- }
@@ -1,82 +0,0 @@
1
- import type { Elysia } from 'elysia'
2
-
3
- import { EdenFetchError } from '../errors'
4
- import type { EdenFetch } from './types'
5
- export type { EdenFetch } from './types'
6
-
7
- export const edenFetch =
8
- <App extends Elysia<any, any, any, any, any, any>>(
9
- server: string,
10
- config?: EdenFetch.Config
11
- ): EdenFetch.Create<App> =>
12
- // @ts-ignore
13
- (endpoint: string, { query, params, body, ...options } = {}) => {
14
- if (params)
15
- Object.entries(params).forEach(([key, value]) => {
16
- endpoint = endpoint.replace(`:${key}`, value as string)
17
- })
18
-
19
- const contentType = options.headers?.['Content-Type']
20
-
21
- if (!contentType || contentType === 'application/json')
22
- try {
23
- body = JSON.stringify(body)
24
- } catch (error) {
25
- //
26
- }
27
-
28
- const fetch = config?.fetcher || globalThis.fetch
29
- const queryStr = query
30
- ? `?${new URLSearchParams(query).toString()}`
31
- : ''
32
-
33
- // @ts-ignore
34
- const execute = () => fetch(server + endpoint + queryStr, {
35
- ...options,
36
- method: options.method?.toUpperCase() || 'GET',
37
- headers: body
38
- ? {
39
- 'content-type': 'application/json',
40
- ...options.headers
41
- }
42
- : options.headers,
43
- body: body as any
44
- }).then(async (res) => {
45
- let data
46
-
47
- switch (res.headers.get('Content-Type')?.split(';')[0]) {
48
- case 'application/json':
49
- data = await res.json()
50
- break
51
-
52
- default:
53
- data = await res.text().then((d) => {
54
- if (!Number.isNaN(+d)) return +d
55
- if (d === 'true') return true
56
- if (d === 'false') return false
57
-
58
- return d
59
- })
60
- break
61
- }
62
-
63
- if (res.status > 300)
64
- return {
65
- data: null,
66
- status: res.status,
67
- headers: res.headers,
68
- retry: execute,
69
- error: new EdenFetchError(res.status, data)
70
- }
71
-
72
- return {
73
- data,
74
- error: null,
75
- status: res.status,
76
- headers: res.headers,
77
- retry: execute
78
- }
79
- })
80
-
81
- return execute()
82
- }
@@ -1,81 +0,0 @@
1
- import type { Elysia } from 'elysia'
2
- import type { EdenFetchError } from '../errors'
3
- import type { MapError, IsUnknown, IsNever } from '../types'
4
-
5
- type Prettify<T> = {
6
- [K in keyof T]: T[K]
7
- } & {}
8
-
9
- export namespace EdenFetch {
10
- export type Create<App extends Elysia<any, any, any, any, any, any>> =
11
- App extends {
12
- schema: infer Schema extends Record<string, any>
13
- }
14
- ? EdenFetch.Fn<Schema>
15
- : 'Please install Elysia before using Eden'
16
-
17
- export interface Config {
18
- fetcher?: typeof globalThis.fetch
19
- }
20
-
21
- export type Fn<Schema extends Record<string, any>> = <
22
- Endpoint extends keyof Schema,
23
- Method extends Uppercase<Extract<keyof Schema[Endpoint], string>>,
24
- Route extends Schema[Endpoint][Lowercase<Method>]
25
- >(
26
- endpoint: Endpoint,
27
- options: Omit<RequestInit, 'body' | 'method' | 'headers'> &
28
- ('GET' extends Method
29
- ? {
30
- method?: Method
31
- }
32
- : {
33
- method: Method
34
- }) &
35
- (IsNever<Route['params']> extends true
36
- ? {
37
- params?: Record<never, string>
38
- }
39
- : {
40
- params: Route['params']
41
- }) &
42
- (IsNever<keyof Route['query']> extends true
43
- ? {
44
- query?: Record<never, string>
45
- }
46
- : {
47
- query: Route['query']
48
- }) &
49
- (undefined extends Route['headers']
50
- ? {
51
- headers?: Record<string, string>
52
- }
53
- : {
54
- headers: Route['headers']
55
- }) &
56
- (IsUnknown<Route['body']> extends false
57
- ? { body: Route['body'] }
58
- : { body?: unknown })
59
- ) => Promise<
60
- Prettify<
61
- (
62
- | {
63
- data: Awaited<Route['response']['200']>
64
- error: null
65
- }
66
- | {
67
- data: null
68
- error: MapError<Route['response']> extends infer Errors
69
- ? IsNever<Errors> extends true
70
- ? EdenFetchError<number, string>
71
- : Errors
72
- : EdenFetchError<number, string>
73
- }
74
- ) & {
75
- status: number
76
- response: Response
77
- headers: Record<string, string>
78
- }
79
- >
80
- >
81
- }
package/src/index.ts DELETED
@@ -1,7 +0,0 @@
1
- import type { Elysia } from 'elysia'
2
-
3
- import type { EdenTreaty } from './treaty'
4
- import type { EdenFetch } from './fetch'
5
-
6
- export { edenTreaty } from './treaty'
7
- export { edenFetch } from './fetch'