@elysiajs/eden 0.2.0-rc.5 → 0.2.0-rc.6
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/{index.d.ts → eden/src/index.d.ts} +0 -0
- package/dist/{types.d.ts → eden/src/types.d.ts} +9 -4
- package/dist/index.js +1 -1
- package/dist/index.mjs +18 -18
- package/dist/index.umd.js +1 -1
- package/package.json +3 -2
- package/src/index.ts +12 -12
- package/src/types.ts +17 -6
|
File without changes
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/// <reference types="bun-types" />
|
|
2
|
-
import type { Elysia, SCHEMA, TypedRoute } from 'elysia';
|
|
3
|
-
import type { IsPathParameter } from 'elysia/dist/types';
|
|
2
|
+
import type { Elysia, SCHEMA, TypedRoute, IsPathParameter } from 'elysia';
|
|
4
3
|
import type { EdenWS } from '.';
|
|
5
4
|
export declare type Eden<App extends Elysia<any>> = App['store'] extends {
|
|
6
5
|
[key in typeof SCHEMA]: any;
|
|
@@ -34,7 +33,11 @@ export declare type CreateEden<Server extends Record<string, Record<string, Type
|
|
|
34
33
|
}) => EdenWS<Server[Full][key]> : (params?: {
|
|
35
34
|
$query?: EdenCall['$query'];
|
|
36
35
|
$fetch?: EdenCall['$fetch'];
|
|
37
|
-
}) => Promise<key extends string ? Server[Full][Uppercase<key>]['response']
|
|
36
|
+
}) => Promise<key extends string ? Server[Full][Uppercase<key>]['response'] extends {
|
|
37
|
+
200: infer ReturnedType;
|
|
38
|
+
} ? ReturnedType : unknown : Server[Full][key]['response'] extends {
|
|
39
|
+
200: infer ReturnedType;
|
|
40
|
+
} ? ReturnedType : unknown> : key extends 'subscribe' ? unknown extends NonNullable<Server[Full][key]['query']> ? (params?: {
|
|
38
41
|
$query?: EdenCall['$query'];
|
|
39
42
|
}) => EdenWS<Server[Full][key]> : Server[Full][key]['query'] extends NonNullable<Server[Full][key]['query']> ? (params: {
|
|
40
43
|
$query: Server[Full][key]['query'];
|
|
@@ -43,7 +46,9 @@ export declare type CreateEden<Server extends Record<string, Record<string, Type
|
|
|
43
46
|
}) => EdenWS<Server[Full][key]> : (params: TypedRouteToParams<Server[Full][key extends string ? Uppercase<key> : key]> & {
|
|
44
47
|
$query?: EdenCall['$query'];
|
|
45
48
|
$fetch?: EdenCall['$fetch'];
|
|
46
|
-
}) => Promise<Server[Full][key extends string ? Uppercase<key> : key]['response']
|
|
49
|
+
}) => Promise<Server[Full][key extends string ? Uppercase<key> : key]['response'] extends {
|
|
50
|
+
200: infer ReturnedType;
|
|
51
|
+
} ? ReturnedType : unknown>;
|
|
47
52
|
} : never;
|
|
48
53
|
} & (Path extends `:${infer params}` ? {
|
|
49
54
|
$params: `Expected path parameters ':${params}', replace this with any string`;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});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}addEventListener(e,t,s){return this.ws.addEventListener(e,i=>{if(e==="message"){let r=i.data.toString();const l=r.charCodeAt(0);if(l===47||l===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 b=n=>n.replace(/[A-Z]/g,e=>`-${e.toLowerCase()}`),p=(n,e,t)=>{if(n.endsWith("/")||(n+="/"),e=b(e.replace(/index/g,"")),!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)}`},w=(n,e="")=>new Proxy(()=>{},{get(t,s,i){return w(n,`${e}/${s.toString()}`)},apply(t,s,[{$query:i,$fetch:r,$body:l,...f}={$fetch:void 0,$query:void 0,$body:void 0}]=[{}]){const h=e.lastIndexOf("/"),d=e.slice(h+1),u=p(n,e.slice(0,h),i);if(d==="subscribe")return new v(u.replace(/^([^]+):\/\//,u.startsWith("https://")?"wss://":"ws://"));const o=l??(Object.keys(f).length?f:void 0),g=typeof o=="object";return fetch(u,{method:d,body:g?JSON.stringify(o):o,headers:o?{"content-type":g?"application/json":"text/plain","content-length":o==null?void 0:o.length,...r==null?void 0:r.headers}:void 0,...r}).then(async c=>{var y;if(c.status>300)throw new Error(await c.text());if((y=c.headers.get("content-type"))!=null&&y.includes("application/json"))try{return await c.json()}catch{}let a=await c.text();return Number.isNaN(+a)?a==="true"?!0:a==="false"?!1:a:+a})}}),S=n=>new Proxy({},{get(e,t,s){return w(n,t)}});exports.EdenWS=v;exports.eden=S;
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class
|
|
1
|
+
class w {
|
|
2
2
|
constructor(e) {
|
|
3
3
|
this.ws = new WebSocket(e), this.url = e;
|
|
4
4
|
}
|
|
@@ -44,8 +44,8 @@ class y {
|
|
|
44
44
|
return this.ws.close(), this;
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
-
const
|
|
48
|
-
if (n.endsWith("/") || (n += "/"), e =
|
|
47
|
+
const p = (n) => n.replace(/[A-Z]/g, (e) => `-${e.toLowerCase()}`), b = (n, e, t) => {
|
|
48
|
+
if (n.endsWith("/") || (n += "/"), e = p(e.replace(/index/g, "")), !t || !Object.keys(t).length)
|
|
49
49
|
return `${n}${e}`;
|
|
50
50
|
let s = "";
|
|
51
51
|
for (const [i, r] of Object.entries(t))
|
|
@@ -57,40 +57,40 @@ const w = (n) => n.replace(/[A-Z]/g, (e) => `-${e.toLowerCase()}`), p = (n, e, t
|
|
|
57
57
|
return v(n, `${e}/${s.toString()}`);
|
|
58
58
|
},
|
|
59
59
|
apply(t, s, [
|
|
60
|
-
{ $query: i, $fetch: r, $body: l, ...
|
|
60
|
+
{ $query: i, $fetch: r, $body: l, ...f } = {
|
|
61
61
|
$fetch: void 0,
|
|
62
62
|
$query: void 0,
|
|
63
63
|
$body: void 0
|
|
64
64
|
}
|
|
65
65
|
] = [{}]) {
|
|
66
|
-
const h = e.lastIndexOf("/"), d = e.slice(h + 1),
|
|
66
|
+
const h = e.lastIndexOf("/"), d = e.slice(h + 1), u = b(n, e.slice(0, h), i);
|
|
67
67
|
if (d === "subscribe")
|
|
68
|
-
return new
|
|
69
|
-
|
|
68
|
+
return new w(
|
|
69
|
+
u.replace(
|
|
70
70
|
/^([^]+):\/\//,
|
|
71
|
-
|
|
71
|
+
u.startsWith("https://") ? "wss://" : "ws://"
|
|
72
72
|
)
|
|
73
73
|
);
|
|
74
|
-
const o = l ?? (Object.keys(
|
|
75
|
-
return fetch(
|
|
74
|
+
const o = l ?? (Object.keys(f).length ? f : void 0), g = typeof o == "object";
|
|
75
|
+
return fetch(u, {
|
|
76
76
|
method: d,
|
|
77
|
-
body: JSON.stringify(o),
|
|
77
|
+
body: g ? JSON.stringify(o) : o,
|
|
78
78
|
headers: o ? {
|
|
79
|
-
"content-type":
|
|
79
|
+
"content-type": g ? "application/json" : "text/plain",
|
|
80
80
|
"content-length": o == null ? void 0 : o.length,
|
|
81
81
|
...r == null ? void 0 : r.headers
|
|
82
82
|
} : void 0,
|
|
83
83
|
...r
|
|
84
84
|
}).then(async (c) => {
|
|
85
|
-
var
|
|
86
|
-
if (c.status
|
|
85
|
+
var y;
|
|
86
|
+
if (c.status > 300)
|
|
87
87
|
throw new Error(await c.text());
|
|
88
|
-
if ((
|
|
88
|
+
if ((y = c.headers.get("content-type")) != null && y.includes("application/json"))
|
|
89
89
|
try {
|
|
90
|
-
return c.json();
|
|
90
|
+
return await c.json();
|
|
91
91
|
} catch {
|
|
92
92
|
}
|
|
93
|
-
|
|
93
|
+
let a = await c.text();
|
|
94
94
|
return Number.isNaN(+a) ? a === "true" ? !0 : a === "false" ? !1 : a : +a;
|
|
95
95
|
});
|
|
96
96
|
}
|
|
@@ -103,6 +103,6 @@ const w = (n) => n.replace(/[A-Z]/g, (e) => `-${e.toLowerCase()}`), p = (n, e, t
|
|
|
103
103
|
}
|
|
104
104
|
);
|
|
105
105
|
export {
|
|
106
|
-
|
|
106
|
+
w as EdenWS,
|
|
107
107
|
N as eden
|
|
108
108
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(o,
|
|
1
|
+
(function(o,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(o=typeof globalThis<"u"?globalThis:o||self,a(o["@elysia/eden"]={}))})(this,function(o){"use strict";class a{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}addEventListener(e,t,s){return this.ws.addEventListener(e,i=>{if(e==="message"){let n=i.data.toString();const l=n.charCodeAt(0);if(l===47||l===123)try{n=JSON.parse(n)}catch{}else Number.isNaN(+n)?n==="true"?n=!0:n==="fase"&&(n=!1):n=+n;t({...i,data:n})}else t(i)},s),this}removeEventListener(e,t,s){return this.off(e,t,s),this}close(){return this.ws.close(),this}}const b=r=>r.replace(/[A-Z]/g,e=>`-${e.toLowerCase()}`),S=(r,e,t)=>{if(r.endsWith("/")||(r+="/"),e=b(e.replace(/index/g,"")),!t||!Object.keys(t).length)return`${r}${e}`;let s="";for(const[i,n]of Object.entries(t))s+=`${i}=${n}&`;return`${r}${e}?${s.slice(0,-1)}`},h=(r,e="")=>new Proxy(()=>{},{get(t,s,i){return h(r,`${e}/${s.toString()}`)},apply(t,s,[{$query:i,$fetch:n,$body:l,...y}={$fetch:void 0,$query:void 0,$body:void 0}]=[{}]){const g=e.lastIndexOf("/"),p=e.slice(g+1),d=S(r,e.slice(0,g),i);if(p==="subscribe")return new a(d.replace(/^([^]+):\/\//,d.startsWith("https://")?"wss://":"ws://"));const c=l??(Object.keys(y).length?y:void 0),v=typeof c=="object";return fetch(d,{method:p,body:v?JSON.stringify(c):c,headers:c?{"content-type":v?"application/json":"text/plain","content-length":c==null?void 0:c.length,...n==null?void 0:n.headers}:void 0,...n}).then(async u=>{var w;if(u.status>300)throw new Error(await u.text());if((w=u.headers.get("content-type"))!=null&&w.includes("application/json"))try{return await u.json()}catch{}let f=await u.text();return Number.isNaN(+f)?f==="true"?!0:f==="false"?!1:f:+f})}}),j=r=>new Proxy({},{get(e,t,s){return h(r,t)}});o.EdenWS=a,o.eden=j,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elysiajs/eden",
|
|
3
|
-
"version": "0.2.0-rc.
|
|
3
|
+
"version": "0.2.0-rc.6",
|
|
4
4
|
"description": "Fully type-safe Elysia client",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "saltyAom",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"release": "npm run build && npm run test && npm publish --access public"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"elysia": ">= 0.
|
|
37
|
+
"elysia": ">= 0.2.0-rc.1"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@elysiajs/cors": "^0.1.0",
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"@types/node": "^18.11.7",
|
|
44
44
|
"bun-types": "^0.3.0",
|
|
45
45
|
"eslint": "^8.26.0",
|
|
46
|
+
"elysia": "^0.2.0-rc.1",
|
|
46
47
|
"rimraf": "^3.0.2",
|
|
47
48
|
"typescript": "^4.8.4",
|
|
48
49
|
"vite": "^4.0.1",
|
package/src/index.ts
CHANGED
|
@@ -154,23 +154,23 @@ const createProxy = (
|
|
|
154
154
|
|
|
155
155
|
const body =
|
|
156
156
|
$body ?? (Object.keys(bodyObj).length ? bodyObj : undefined)
|
|
157
|
+
const isObject = typeof body === 'object'
|
|
157
158
|
|
|
158
159
|
return fetch(url, {
|
|
159
160
|
method,
|
|
160
|
-
body: JSON.stringify(body),
|
|
161
|
+
body: isObject ? JSON.stringify(body) : body,
|
|
161
162
|
headers: body
|
|
162
163
|
? {
|
|
163
|
-
'content-type':
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
: 'text/plain',
|
|
164
|
+
'content-type': isObject
|
|
165
|
+
? 'application/json'
|
|
166
|
+
: 'text/plain',
|
|
167
167
|
'content-length': body?.length,
|
|
168
168
|
...$fetch?.['headers']
|
|
169
169
|
}
|
|
170
170
|
: undefined,
|
|
171
171
|
...$fetch
|
|
172
172
|
}).then(async (res) => {
|
|
173
|
-
if (res.status
|
|
173
|
+
if (res.status > 300) throw new Error(await res.text())
|
|
174
174
|
|
|
175
175
|
if (
|
|
176
176
|
res.headers
|
|
@@ -178,19 +178,19 @@ const createProxy = (
|
|
|
178
178
|
?.includes('application/json')
|
|
179
179
|
)
|
|
180
180
|
try {
|
|
181
|
-
return res.json()
|
|
181
|
+
return await res.json()
|
|
182
182
|
} catch (_) {
|
|
183
183
|
// if json is error then it's string
|
|
184
184
|
// flow down
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
|
|
187
|
+
let data = await res.text()
|
|
188
188
|
|
|
189
|
-
if (!Number.isNaN(+
|
|
190
|
-
if (
|
|
191
|
-
if (
|
|
189
|
+
if (!Number.isNaN(+data)) return +data
|
|
190
|
+
if (data === 'true') return true
|
|
191
|
+
if (data === 'false') return false
|
|
192
192
|
|
|
193
|
-
return
|
|
193
|
+
return data
|
|
194
194
|
})
|
|
195
195
|
}
|
|
196
196
|
}) as unknown as Record<string, unknown>
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { Elysia, SCHEMA, TypedRoute } from 'elysia'
|
|
2
|
-
import type {
|
|
3
|
-
import { TObject } from '@sinclair/typebox'
|
|
1
|
+
import type { Elysia, SCHEMA, TypedRoute, IsPathParameter } from 'elysia'
|
|
2
|
+
import type { TObject } from '@sinclair/typebox'
|
|
4
3
|
|
|
5
4
|
import type { EdenWS } from '.'
|
|
6
5
|
|
|
@@ -92,8 +91,16 @@ export type CreateEden<
|
|
|
92
91
|
$fetch?: EdenCall['$fetch']
|
|
93
92
|
}) => Promise<
|
|
94
93
|
key extends string
|
|
95
|
-
? Server[Full][Uppercase<key>]['response']
|
|
96
|
-
|
|
94
|
+
? Server[Full][Uppercase<key>]['response'] extends {
|
|
95
|
+
200: infer ReturnedType
|
|
96
|
+
}
|
|
97
|
+
? ReturnedType
|
|
98
|
+
: unknown
|
|
99
|
+
: Server[Full][key]['response'] extends {
|
|
100
|
+
200: infer ReturnedType
|
|
101
|
+
}
|
|
102
|
+
? ReturnedType
|
|
103
|
+
: unknown
|
|
97
104
|
>
|
|
98
105
|
: key extends 'subscribe'
|
|
99
106
|
? unknown extends NonNullable<
|
|
@@ -123,7 +130,11 @@ export type CreateEden<
|
|
|
123
130
|
) => Promise<
|
|
124
131
|
Server[Full][key extends string
|
|
125
132
|
? Uppercase<key>
|
|
126
|
-
: key]['response']
|
|
133
|
+
: key]['response'] extends {
|
|
134
|
+
200: infer ReturnedType
|
|
135
|
+
}
|
|
136
|
+
? ReturnedType
|
|
137
|
+
: unknown
|
|
127
138
|
>
|
|
128
139
|
}
|
|
129
140
|
: never
|