@elysiajs/eden 0.3.0-rc.0 → 0.3.0-rc.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/treaty/index.d.ts +1 -0
- package/dist/treaty/types.d.ts +33 -9
- package/dist/treaty.js +1 -1
- package/dist/treaty.mjs +19 -13
- package/package.json +4 -4
- package/src/treaty/index.ts +17 -6
- package/src/treaty/types.ts +85 -30
package/dist/treaty/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export declare class EdenWS<Schema extends TypedSchema<any> = TypedSchema> {
|
|
|
9
9
|
send(data: Schema['body'] | Schema['body'][]): this;
|
|
10
10
|
on<K extends keyof WebSocketEventMap>(type: K, listener: (event: EdenTreaty.WSEvent<K, Schema['response']>) => void, options?: boolean | AddEventListenerOptions): this;
|
|
11
11
|
off<K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | EventListenerOptions): this;
|
|
12
|
+
subscribe(onMessage: (event: EdenTreaty.WSEvent<'message', Schema['response']>) => void, options?: boolean | AddEventListenerOptions): this;
|
|
12
13
|
addEventListener<K extends keyof WebSocketEventMap>(type: K, listener: (event: EdenTreaty.WSEvent<K, Schema['response']>) => void, options?: boolean | AddEventListenerOptions): this;
|
|
13
14
|
removeEventListener<K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | EventListenerOptions): this;
|
|
14
15
|
close(): this;
|
package/dist/treaty/types.d.ts
CHANGED
|
@@ -18,12 +18,24 @@ export declare namespace EdenTreaty {
|
|
|
18
18
|
}) => EdenWS<Route> : IsUnknown<Route['body']> extends true ? (params?: {
|
|
19
19
|
$query?: Record<string, string>;
|
|
20
20
|
$fetch?: RequestInit;
|
|
21
|
-
}) => Promise<
|
|
21
|
+
}) => Promise<{
|
|
22
|
+
data: Route['response']['200'];
|
|
23
|
+
error: null;
|
|
24
|
+
} | {
|
|
25
|
+
data: null;
|
|
26
|
+
error: MapError<Route['response']> extends infer Errors ? IsNever<Errors> extends true ? EdenFetchError<number, string> : Errors : EdenFetchError<number, string>;
|
|
27
|
+
}> : (params: Route['body'] & {
|
|
22
28
|
$query?: Record<string, string>;
|
|
23
29
|
$fetch?: RequestInit;
|
|
24
|
-
}) => Promise<
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
}) => Promise<{
|
|
31
|
+
data: Route['response'] extends {
|
|
32
|
+
200: infer ReturnedType;
|
|
33
|
+
} ? ReturnedType : unknown;
|
|
34
|
+
error: null;
|
|
35
|
+
} | {
|
|
36
|
+
data: null;
|
|
37
|
+
error: MapError<Route['response']> extends infer Errors ? IsNever<Errors> extends true ? EdenFetchError<number, string> : Errors : EdenFetchError<number, string>;
|
|
38
|
+
}> : never;
|
|
27
39
|
}> : {
|
|
28
40
|
[Method in keyof A[Path]]: A[Path][Method] extends infer Route extends AnyTypedSchema ? Method extends 'subscribe' ? IsUnknown<Route['query']> extends true ? (params?: {
|
|
29
41
|
$query?: Record<string, string>;
|
|
@@ -34,12 +46,24 @@ export declare namespace EdenTreaty {
|
|
|
34
46
|
}) => EdenWS<Route> : IsUnknown<Route['body']> extends true ? (params?: {
|
|
35
47
|
$query?: Record<string, string>;
|
|
36
48
|
$fetch?: RequestInit;
|
|
37
|
-
}) => Promise<
|
|
49
|
+
}) => Promise<{
|
|
50
|
+
data: Route['response']['200'];
|
|
51
|
+
error: null;
|
|
52
|
+
} | {
|
|
53
|
+
data: null;
|
|
54
|
+
error: MapError<Route['response']> extends infer Errors ? IsNever<Errors> extends true ? EdenFetchError<number, string> : Errors : EdenFetchError<number, string>;
|
|
55
|
+
}> : (params: Route['body'] & {
|
|
38
56
|
$query?: Record<string, string>;
|
|
39
57
|
$fetch?: RequestInit;
|
|
40
|
-
}) => Promise<
|
|
41
|
-
|
|
42
|
-
|
|
58
|
+
}) => Promise<{
|
|
59
|
+
data: Route['response'] extends {
|
|
60
|
+
200: infer ReturnedType;
|
|
61
|
+
} ? ReturnedType : unknown;
|
|
62
|
+
error: null;
|
|
63
|
+
} | {
|
|
64
|
+
data: null;
|
|
65
|
+
error: MapError<Route['response']> extends infer Errors ? IsNever<Errors> extends true ? EdenFetchError<number, string> : Errors : EdenFetchError<number, string>;
|
|
66
|
+
}> : never;
|
|
43
67
|
}>;
|
|
44
68
|
};
|
|
45
69
|
interface OnMessage<Data = unknown> extends MessageEvent {
|
|
@@ -53,5 +77,5 @@ export declare namespace EdenTreaty {
|
|
|
53
77
|
$query?: Record<string, string>;
|
|
54
78
|
}
|
|
55
79
|
}
|
|
56
|
-
type NestPath<T extends string, V> = T extends `${infer First}/${infer Rest}` ? First extends `:${infer Parameter}` ? Record<
|
|
80
|
+
type NestPath<T extends string, V> = T extends `${infer First}/${infer Rest}` ? First extends `:${infer Parameter}` ? Record<string | number | `:${Parameter}`, NestPath<Rest, V>> : Record<First, NestPath<Rest, V>> : T extends `:${infer Parameter}` ? Record<string | number | T, V> : Record<T, V>;
|
|
57
81
|
export {};
|
package/dist/treaty.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 S=require("./utils-0d7d9b21.js"),p=(n,e,t)=>{if(n.endsWith("/")||(n+="/"),e==="index"&&(e=""),!t||!Object.keys(t).length)return`${n}${e}`;let s="";for(const[i,r]of Object.entries(t))s+=`${i}=${r}&`;return`${n}${e}?${s.slice(0,-1)}`};class v{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,i=>{if(e==="message"){let r=i.data.toString();const o=r.charCodeAt(0);if(o===47||o===123)try{r=JSON.parse(r)}catch{}else Number.isNaN(+r)?r==="true"?r=!0:r==="fase"&&(r=!1):r=+r;t({...i,data:r})}else t(i)},s),this}removeEventListener(e,t,s){return this.off(e,t,s),this}close(){return this.ws.close(),this}}const w=(n,e="",t)=>new Proxy(()=>{},{get(s,i,r){return w(n,`${e}/${i.toString()}`)},apply(s,i,[{$query:r,$fetch:o,$body:E,...d}={$fetch:void 0,$query:void 0,$body:void 0}]=[{}]){const h=e.lastIndexOf("/"),y=e.slice(h+1),f=p(n,e.slice(0,h),r);if(y==="subscribe")return new v(f.replace(/^([^]+):\/\//,f.startsWith("https://")?"wss://":"ws://"));const l=E??(Object.keys(d).length?d:void 0),g=typeof l=="object";return fetch(f,{method:y,body:g?JSON.stringify(l):l,...o,headers:l?{"content-type":g?"application/json":"text/plain",...o==null?void 0:o.headers}:void 0}).then(async c=>{var b;let u;switch((b=c.headers.get("Content-Type"))==null?void 0:b.split(";")[0]){case"application/json":u=await c.json();break;default:u=await c.text().then(a=>Number.isNaN(+a)?a==="true"?!0:a==="false"?!1:a:+a)}return c.status>300?{data:u,error:new S.EdenFetchError(c.status,await u)}:{data:u,error:null}})}}),j=(n,e={})=>new Proxy({},{get(t,s){return w(n,s)}});exports.EdenWS=v;exports.edenTreaty=j;
|
package/dist/treaty.mjs
CHANGED
|
@@ -22,6 +22,9 @@ class N {
|
|
|
22
22
|
off(e, t, s) {
|
|
23
23
|
return this.ws.removeEventListener(e, t, s), this;
|
|
24
24
|
}
|
|
25
|
+
subscribe(e, t) {
|
|
26
|
+
return this.addEventListener("message", e, t);
|
|
27
|
+
}
|
|
25
28
|
addEventListener(e, t, s) {
|
|
26
29
|
return this.ws.addEventListener(
|
|
27
30
|
e,
|
|
@@ -53,10 +56,10 @@ class N {
|
|
|
53
56
|
return this.ws.close(), this;
|
|
54
57
|
}
|
|
55
58
|
}
|
|
56
|
-
const
|
|
59
|
+
const v = (n, e = "", t) => new Proxy(() => {
|
|
57
60
|
}, {
|
|
58
61
|
get(s, i, r) {
|
|
59
|
-
return
|
|
62
|
+
return v(n, `${e}/${i.toString()}`);
|
|
60
63
|
},
|
|
61
64
|
apply(s, i, [
|
|
62
65
|
{ $query: r, $fetch: o, $body: w, ...h } = {
|
|
@@ -65,16 +68,16 @@ const b = (n, e = "", t) => new Proxy(() => {
|
|
|
65
68
|
$body: void 0
|
|
66
69
|
}
|
|
67
70
|
] = [{}]) {
|
|
68
|
-
const d = e.lastIndexOf("/"), y = e.slice(d + 1),
|
|
71
|
+
const d = e.lastIndexOf("/"), y = e.slice(d + 1), l = E(n, e.slice(0, d), r);
|
|
69
72
|
if (y === "subscribe")
|
|
70
73
|
return new N(
|
|
71
|
-
|
|
74
|
+
l.replace(
|
|
72
75
|
/^([^]+):\/\//,
|
|
73
|
-
|
|
76
|
+
l.startsWith("https://") ? "wss://" : "ws://"
|
|
74
77
|
)
|
|
75
78
|
);
|
|
76
79
|
const f = w ?? (Object.keys(h).length ? h : void 0), g = typeof f == "object";
|
|
77
|
-
return fetch(
|
|
80
|
+
return fetch(l, {
|
|
78
81
|
method: y,
|
|
79
82
|
body: g ? JSON.stringify(f) : f,
|
|
80
83
|
// ...config.fetch,
|
|
@@ -85,23 +88,26 @@ const b = (n, e = "", t) => new Proxy(() => {
|
|
|
85
88
|
...o == null ? void 0 : o.headers
|
|
86
89
|
} : void 0
|
|
87
90
|
}).then(async (c) => {
|
|
88
|
-
var
|
|
89
|
-
let
|
|
90
|
-
switch ((
|
|
91
|
+
var b;
|
|
92
|
+
let u;
|
|
93
|
+
switch ((b = c.headers.get("Content-Type")) == null ? void 0 : b.split(";")[0]) {
|
|
91
94
|
case "application/json":
|
|
92
|
-
|
|
95
|
+
u = await c.json();
|
|
93
96
|
break;
|
|
94
97
|
default:
|
|
95
|
-
|
|
98
|
+
u = await c.text().then((a) => Number.isNaN(+a) ? a === "true" ? !0 : a === "false" ? !1 : a : +a);
|
|
96
99
|
}
|
|
97
|
-
return c.status > 300 ?
|
|
100
|
+
return c.status > 300 ? {
|
|
101
|
+
data: u,
|
|
102
|
+
error: new p(c.status, await u)
|
|
103
|
+
} : { data: u, error: null };
|
|
98
104
|
});
|
|
99
105
|
}
|
|
100
106
|
}), x = (n, e = {}) => new Proxy(
|
|
101
107
|
{},
|
|
102
108
|
{
|
|
103
109
|
get(t, s) {
|
|
104
|
-
return
|
|
110
|
+
return v(n, s);
|
|
105
111
|
}
|
|
106
112
|
}
|
|
107
113
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elysiajs/eden",
|
|
3
|
-
"version": "0.3.0-rc.
|
|
3
|
+
"version": "0.3.0-rc.1",
|
|
4
4
|
"description": "Fully type-safe Elysia client",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "saltyAom",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"release": "npm run build && npm run test && npm publish --access public"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"@sinclair/typebox": ">= 0.25.
|
|
57
|
+
"@sinclair/typebox": ">= 0.25.24",
|
|
58
58
|
"elysia": ">= 0.3.0-rc.1"
|
|
59
59
|
},
|
|
60
60
|
"peerDependenciesMeta": {
|
|
@@ -64,10 +64,10 @@
|
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@elysiajs/cors": "^0.1.0",
|
|
67
|
-
"@sinclair/typebox": "^0.25.
|
|
67
|
+
"@sinclair/typebox": "^0.25.24",
|
|
68
68
|
"@types/node": "^18.11.7",
|
|
69
69
|
"bun-types": "^0.5.7",
|
|
70
|
-
"elysia": "
|
|
70
|
+
"elysia": "rc",
|
|
71
71
|
"eslint": "^8.26.0",
|
|
72
72
|
"rimraf": "^3.0.2",
|
|
73
73
|
"typescript": "^4.8.4",
|
package/src/treaty/index.ts
CHANGED
|
@@ -48,6 +48,15 @@ export class EdenWS<Schema extends TypedSchema<any> = TypedSchema> {
|
|
|
48
48
|
return this
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
subscribe(
|
|
52
|
+
onMessage: (
|
|
53
|
+
event: EdenTreaty.WSEvent<'message', Schema['response']>
|
|
54
|
+
) => void,
|
|
55
|
+
options?: boolean | AddEventListenerOptions
|
|
56
|
+
) {
|
|
57
|
+
return this.addEventListener('message', onMessage, options)
|
|
58
|
+
}
|
|
59
|
+
|
|
51
60
|
addEventListener<K extends keyof WebSocketEventMap>(
|
|
52
61
|
type: K,
|
|
53
62
|
listener: (event: EdenTreaty.WSEvent<K, Schema['response']>) => void,
|
|
@@ -152,15 +161,15 @@ const createProxy = (
|
|
|
152
161
|
}
|
|
153
162
|
: undefined
|
|
154
163
|
}).then(async (res) => {
|
|
155
|
-
let data
|
|
164
|
+
let data
|
|
156
165
|
|
|
157
166
|
switch (res.headers.get('Content-Type')?.split(';')[0]) {
|
|
158
167
|
case 'application/json':
|
|
159
|
-
data = res.json()
|
|
168
|
+
data = await res.json()
|
|
160
169
|
break
|
|
161
170
|
|
|
162
171
|
default:
|
|
163
|
-
data = res.text().then((data) => {
|
|
172
|
+
data = await res.text().then((data) => {
|
|
164
173
|
if (!Number.isNaN(+data)) return +data
|
|
165
174
|
if (data === 'true') return true
|
|
166
175
|
if (data === 'false') return false
|
|
@@ -170,9 +179,12 @@ const createProxy = (
|
|
|
170
179
|
}
|
|
171
180
|
|
|
172
181
|
if (res.status > 300)
|
|
173
|
-
return
|
|
182
|
+
return {
|
|
183
|
+
data,
|
|
184
|
+
error: new EdenFetchError(res.status, await data)
|
|
185
|
+
}
|
|
174
186
|
|
|
175
|
-
return data
|
|
187
|
+
return { data, error: null }
|
|
176
188
|
})
|
|
177
189
|
}
|
|
178
190
|
}) as unknown as Record<string, unknown>
|
|
@@ -189,4 +201,3 @@ export const edenTreaty = <App extends Elysia<any>>(
|
|
|
189
201
|
}
|
|
190
202
|
}
|
|
191
203
|
) as any
|
|
192
|
-
|
package/src/treaty/types.ts
CHANGED
|
@@ -43,29 +43,58 @@ export namespace EdenTreaty {
|
|
|
43
43
|
? (params?: {
|
|
44
44
|
$query?: Record<string, string>
|
|
45
45
|
$fetch?: RequestInit
|
|
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<
|
|
54
|
+
Route['response']
|
|
55
|
+
> extends infer Errors
|
|
56
|
+
? IsNever<Errors> extends true
|
|
57
|
+
? EdenFetchError<
|
|
58
|
+
number,
|
|
59
|
+
string
|
|
60
|
+
>
|
|
61
|
+
: Errors
|
|
62
|
+
: EdenFetchError<
|
|
53
63
|
number,
|
|
54
64
|
string
|
|
55
65
|
>
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
}
|
|
67
|
+
>
|
|
58
68
|
: (
|
|
59
69
|
params: Route['body'] & {
|
|
60
70
|
$query?: Record<string, string>
|
|
61
71
|
$fetch?: RequestInit
|
|
62
72
|
}
|
|
63
73
|
) => Promise<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
74
|
+
| {
|
|
75
|
+
data: Route['response'] extends {
|
|
76
|
+
200: infer ReturnedType
|
|
77
|
+
}
|
|
78
|
+
? ReturnedType
|
|
79
|
+
: unknown
|
|
80
|
+
error: null
|
|
81
|
+
}
|
|
82
|
+
| {
|
|
83
|
+
data: null
|
|
84
|
+
error: MapError<
|
|
85
|
+
Route['response']
|
|
86
|
+
> extends infer Errors
|
|
87
|
+
? IsNever<Errors> extends true
|
|
88
|
+
? EdenFetchError<
|
|
89
|
+
number,
|
|
90
|
+
string
|
|
91
|
+
>
|
|
92
|
+
: Errors
|
|
93
|
+
: EdenFetchError<
|
|
94
|
+
number,
|
|
95
|
+
string
|
|
96
|
+
>
|
|
97
|
+
}
|
|
69
98
|
>
|
|
70
99
|
: never
|
|
71
100
|
}
|
|
@@ -88,26 +117,52 @@ export namespace EdenTreaty {
|
|
|
88
117
|
? (params?: {
|
|
89
118
|
$query?: Record<string, string>
|
|
90
119
|
$fetch?: RequestInit
|
|
91
|
-
}) =>
|
|
92
|
-
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
120
|
+
}) => Promise<
|
|
121
|
+
| {
|
|
122
|
+
data: Route['response']['200']
|
|
123
|
+
error: null
|
|
124
|
+
}
|
|
125
|
+
| {
|
|
126
|
+
data: null
|
|
127
|
+
error: MapError<
|
|
128
|
+
Route['response']
|
|
129
|
+
> extends infer Errors
|
|
130
|
+
? IsNever<Errors> extends true
|
|
131
|
+
? EdenFetchError<
|
|
132
|
+
number,
|
|
133
|
+
string
|
|
134
|
+
>
|
|
135
|
+
: Errors
|
|
136
|
+
: EdenFetchError<number, string>
|
|
137
|
+
}
|
|
138
|
+
>
|
|
100
139
|
: (
|
|
101
140
|
params: Route['body'] & {
|
|
102
141
|
$query?: Record<string, string>
|
|
103
142
|
$fetch?: RequestInit
|
|
104
143
|
}
|
|
105
144
|
) => Promise<
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
145
|
+
| {
|
|
146
|
+
data: Route['response'] extends {
|
|
147
|
+
200: infer ReturnedType
|
|
148
|
+
}
|
|
149
|
+
? ReturnedType
|
|
150
|
+
: unknown
|
|
151
|
+
error: null
|
|
152
|
+
}
|
|
153
|
+
| {
|
|
154
|
+
data: null
|
|
155
|
+
error: MapError<
|
|
156
|
+
Route['response']
|
|
157
|
+
> extends infer Errors
|
|
158
|
+
? IsNever<Errors> extends true
|
|
159
|
+
? EdenFetchError<
|
|
160
|
+
number,
|
|
161
|
+
string
|
|
162
|
+
>
|
|
163
|
+
: Errors
|
|
164
|
+
: EdenFetchError<number, string>
|
|
165
|
+
}
|
|
111
166
|
>
|
|
112
167
|
: never
|
|
113
168
|
}
|
|
@@ -133,8 +188,8 @@ export namespace EdenTreaty {
|
|
|
133
188
|
|
|
134
189
|
type NestPath<T extends string, V> = T extends `${infer First}/${infer Rest}`
|
|
135
190
|
? First extends `:${infer Parameter}`
|
|
136
|
-
? Record<
|
|
191
|
+
? Record<string | number | `:${Parameter}`, NestPath<Rest, V>>
|
|
137
192
|
: Record<First, NestPath<Rest, V>>
|
|
138
193
|
: T extends `:${infer Parameter}`
|
|
139
|
-
? Record<
|
|
194
|
+
? Record<string | number | T, V>
|
|
140
195
|
: Record<T, V>
|