@elysiajs/eden 0.1.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/LICENSE +7 -0
- package/README.md +46 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.js +48 -0
- package/dist/cjs/types.d.ts +25 -0
- package/dist/cjs/types.js +2 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +44 -0
- package/dist/types.d.ts +25 -0
- package/dist/types.js +1 -0
- package/package.json +51 -0
- package/src/index.ts +76 -0
- package/src/types.ts +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2022 saltyAom
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# @elysiajs/eden
|
|
2
|
+
Fully type-safe Elysia client.
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
```bash
|
|
6
|
+
bun add @elysiajs/eden
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Example
|
|
10
|
+
```typescript
|
|
11
|
+
// server.ts
|
|
12
|
+
import { Elysia, t } from 'elysia'
|
|
13
|
+
|
|
14
|
+
const app = new Elysia()
|
|
15
|
+
.get('/', () => 'Hi Elysia')
|
|
16
|
+
.get('/id/:id', ({ params: { id } }) => id)
|
|
17
|
+
.post('/mirror', ({ body }) => body, {
|
|
18
|
+
schema: {
|
|
19
|
+
body: t.Object({
|
|
20
|
+
id: t.Number(),
|
|
21
|
+
name: t.String()
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
.listen(8080)
|
|
26
|
+
|
|
27
|
+
export type App = typeof app
|
|
28
|
+
|
|
29
|
+
// client.ts
|
|
30
|
+
import { eden } from '@elysiajs/eden'
|
|
31
|
+
import type { App } from './server'
|
|
32
|
+
|
|
33
|
+
const client = eden<App>('http://localhost:8080')
|
|
34
|
+
|
|
35
|
+
// return: Hi Elysia (fully type-safe)
|
|
36
|
+
client.index.GET().then(console.log)
|
|
37
|
+
|
|
38
|
+
// return: 1895
|
|
39
|
+
client.id.1895.GET().then(console.log)
|
|
40
|
+
|
|
41
|
+
// return: { id: 1895, name: 'Skadi' }
|
|
42
|
+
client.mirror.POST({
|
|
43
|
+
id: 1895,
|
|
44
|
+
name: 'Skadi'
|
|
45
|
+
}).then(console.log)
|
|
46
|
+
```
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Elysia, SCHEMA } from 'elysia';
|
|
2
|
+
import { CreateEden, UnionToIntersection } from './types';
|
|
3
|
+
export declare function eden<App extends Elysia<any>>(domain: string): App['store'] extends {
|
|
4
|
+
[key in typeof SCHEMA]: any;
|
|
5
|
+
} ? UnionToIntersection<CreateEden<App['store'][typeof SCHEMA]>> : never;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.eden = void 0;
|
|
4
|
+
const composePath = (domain, path, query) => {
|
|
5
|
+
if (!domain.endsWith('/'))
|
|
6
|
+
domain += '/';
|
|
7
|
+
path = path.replace(/index/g, '');
|
|
8
|
+
if (!query || !Object.keys(query).length)
|
|
9
|
+
return `${domain}${path}`;
|
|
10
|
+
let q = '';
|
|
11
|
+
for (const [key, value] of Object.entries(query))
|
|
12
|
+
q += `${key}=${value}&`;
|
|
13
|
+
return `${domain}${path}?${q.slice(0, -1)}`;
|
|
14
|
+
};
|
|
15
|
+
const createProxy = (domain, path = '') => new Proxy(() => { }, {
|
|
16
|
+
get(target, key, value) {
|
|
17
|
+
return createProxy(domain, `${path}/${key.toString()}`);
|
|
18
|
+
},
|
|
19
|
+
apply(target, _, [{ $query, $fetch, ...body } = {
|
|
20
|
+
$fetch: undefined,
|
|
21
|
+
$query: undefined
|
|
22
|
+
}] = [{}]) {
|
|
23
|
+
const i = path.lastIndexOf('/');
|
|
24
|
+
return fetch(composePath(domain, path.slice(0, i), $query), {
|
|
25
|
+
method: path.slice(i + 1),
|
|
26
|
+
headers: {
|
|
27
|
+
'content-type': 'application/json',
|
|
28
|
+
...$fetch === null || $fetch === void 0 ? void 0 : $fetch['headers']
|
|
29
|
+
},
|
|
30
|
+
body: Object.keys(body).length
|
|
31
|
+
? JSON.stringify(body)
|
|
32
|
+
: undefined,
|
|
33
|
+
...$fetch
|
|
34
|
+
}).then((res) => {
|
|
35
|
+
if (res.headers.get('content-type') === 'application/json')
|
|
36
|
+
return res.json();
|
|
37
|
+
return res.text();
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
function eden(domain) {
|
|
42
|
+
return new Proxy({}, {
|
|
43
|
+
get(target, key, value) {
|
|
44
|
+
return createProxy(domain, key);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
exports.eden = eden;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/// <reference types="bun-types" />
|
|
2
|
+
import type { TypedRoute } from 'elysia';
|
|
3
|
+
export interface EdenCall {
|
|
4
|
+
[x: string]: any;
|
|
5
|
+
$fetch?: RequestInit;
|
|
6
|
+
$query?: Record<string, string | boolean | number>;
|
|
7
|
+
}
|
|
8
|
+
export declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
9
|
+
declare type TypedRouteToParams<Route extends TypedRoute> = (Route['body'] extends NonNullable<Route['body']> ? Route['body'] : {}) & (Route['query'] extends NonNullable<Route['query']> ? {
|
|
10
|
+
$query: Route['query'];
|
|
11
|
+
} : {});
|
|
12
|
+
export declare type CreateEden<Server extends Record<string, Record<string, TypedRoute>>, Path extends string = keyof Server, Full extends string = ''> = Path extends `/${infer Start}` ? CreateEden<Server, Start, Path> : Path extends `${infer A}/${infer B}` ? {
|
|
13
|
+
[key in A]: CreateEden<Server, B, Full>;
|
|
14
|
+
} : {
|
|
15
|
+
[key in Path extends '' ? 'index' : Path extends `:${infer params}` ? string : Path]: Full extends keyof Server ? {
|
|
16
|
+
[key in keyof Server[Full]]: keyof TypedRouteToParams<Server[Full][key]> extends never ? (params?: {
|
|
17
|
+
$query?: EdenCall['$query'];
|
|
18
|
+
$fetch?: EdenCall['$fetch'];
|
|
19
|
+
}) => Promise<Server[Full][key]['response']> : (params: TypedRouteToParams<Server[Full][key]> & {
|
|
20
|
+
$query?: EdenCall['$query'];
|
|
21
|
+
$fetch?: EdenCall['$fetch'];
|
|
22
|
+
}) => Promise<Server[Full][key]['response']>;
|
|
23
|
+
} : never;
|
|
24
|
+
};
|
|
25
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Elysia, SCHEMA } from 'elysia';
|
|
2
|
+
import { CreateEden, UnionToIntersection } from './types';
|
|
3
|
+
export declare function eden<App extends Elysia<any>>(domain: string): App['store'] extends {
|
|
4
|
+
[key in typeof SCHEMA]: any;
|
|
5
|
+
} ? UnionToIntersection<CreateEden<App['store'][typeof SCHEMA]>> : never;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const composePath = (domain, path, query) => {
|
|
2
|
+
if (!domain.endsWith('/'))
|
|
3
|
+
domain += '/';
|
|
4
|
+
path = path.replace(/index/g, '');
|
|
5
|
+
if (!query || !Object.keys(query).length)
|
|
6
|
+
return `${domain}${path}`;
|
|
7
|
+
let q = '';
|
|
8
|
+
for (const [key, value] of Object.entries(query))
|
|
9
|
+
q += `${key}=${value}&`;
|
|
10
|
+
return `${domain}${path}?${q.slice(0, -1)}`;
|
|
11
|
+
};
|
|
12
|
+
const createProxy = (domain, path = '') => new Proxy(() => { }, {
|
|
13
|
+
get(target, key, value) {
|
|
14
|
+
return createProxy(domain, `${path}/${key.toString()}`);
|
|
15
|
+
},
|
|
16
|
+
apply(target, _, [{ $query, $fetch, ...body } = {
|
|
17
|
+
$fetch: undefined,
|
|
18
|
+
$query: undefined
|
|
19
|
+
}] = [{}]) {
|
|
20
|
+
const i = path.lastIndexOf('/');
|
|
21
|
+
return fetch(composePath(domain, path.slice(0, i), $query), {
|
|
22
|
+
method: path.slice(i + 1),
|
|
23
|
+
headers: {
|
|
24
|
+
'content-type': 'application/json',
|
|
25
|
+
...$fetch?.['headers']
|
|
26
|
+
},
|
|
27
|
+
body: Object.keys(body).length
|
|
28
|
+
? JSON.stringify(body)
|
|
29
|
+
: undefined,
|
|
30
|
+
...$fetch
|
|
31
|
+
}).then((res) => {
|
|
32
|
+
if (res.headers.get('content-type') === 'application/json')
|
|
33
|
+
return res.json();
|
|
34
|
+
return res.text();
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
export function eden(domain) {
|
|
39
|
+
return new Proxy({}, {
|
|
40
|
+
get(target, key, value) {
|
|
41
|
+
return createProxy(domain, key);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/// <reference types="bun-types" />
|
|
2
|
+
import type { TypedRoute } from 'elysia';
|
|
3
|
+
export interface EdenCall {
|
|
4
|
+
[x: string]: any;
|
|
5
|
+
$fetch?: RequestInit;
|
|
6
|
+
$query?: Record<string, string | boolean | number>;
|
|
7
|
+
}
|
|
8
|
+
export declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
9
|
+
declare type TypedRouteToParams<Route extends TypedRoute> = (Route['body'] extends NonNullable<Route['body']> ? Route['body'] : {}) & (Route['query'] extends NonNullable<Route['query']> ? {
|
|
10
|
+
$query: Route['query'];
|
|
11
|
+
} : {});
|
|
12
|
+
export declare type CreateEden<Server extends Record<string, Record<string, TypedRoute>>, Path extends string = keyof Server, Full extends string = ''> = Path extends `/${infer Start}` ? CreateEden<Server, Start, Path> : Path extends `${infer A}/${infer B}` ? {
|
|
13
|
+
[key in A]: CreateEden<Server, B, Full>;
|
|
14
|
+
} : {
|
|
15
|
+
[key in Path extends '' ? 'index' : Path extends `:${infer params}` ? string : Path]: Full extends keyof Server ? {
|
|
16
|
+
[key in keyof Server[Full]]: keyof TypedRouteToParams<Server[Full][key]> extends never ? (params?: {
|
|
17
|
+
$query?: EdenCall['$query'];
|
|
18
|
+
$fetch?: EdenCall['$fetch'];
|
|
19
|
+
}) => Promise<Server[Full][key]['response']> : (params: TypedRouteToParams<Server[Full][key]> & {
|
|
20
|
+
$query?: EdenCall['$query'];
|
|
21
|
+
$fetch?: EdenCall['$fetch'];
|
|
22
|
+
}) => Promise<Server[Full][key]['response']>;
|
|
23
|
+
} : never;
|
|
24
|
+
};
|
|
25
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@elysiajs/eden",
|
|
3
|
+
"version": "0.1.0-rc.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
|
+
"exports": {
|
|
12
|
+
"require": "./dist/cjs/index.js",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"node": "./dist/index.js",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"types": "./src/index.ts",
|
|
18
|
+
"keywords": [
|
|
19
|
+
"elysia",
|
|
20
|
+
"eden",
|
|
21
|
+
"connector"
|
|
22
|
+
],
|
|
23
|
+
"homepage": "https://github.com/elysiajs/elysia-eden",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/elysiajs/elysia-eden"
|
|
27
|
+
},
|
|
28
|
+
"bugs": "https://github.com/elysiajs/elysia-eden/issues",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"scripts": {
|
|
31
|
+
"dev": "bun run --hot example/index.ts",
|
|
32
|
+
"test": "bun wiptest",
|
|
33
|
+
"build": "rimraf dist && npm run build:cjs && npm run build:esm",
|
|
34
|
+
"build:cjs": "tsc --project tsconfig.cjs.json",
|
|
35
|
+
"build:esm": "tsc --project tsconfig.esm.json",
|
|
36
|
+
"release": "npm run build && npm run test && npm publish --access public"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"elysia": ">= 0.1.0-rc.4"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@elysiajs/cors": "^0.1.0-rc.2",
|
|
43
|
+
"@sinclair/typebox": "^0.25.13",
|
|
44
|
+
"@types/node": "^18.11.7",
|
|
45
|
+
"bun-types": "^0.2.2",
|
|
46
|
+
"elysia": "^0.1.0-rc.4",
|
|
47
|
+
"eslint": "^8.26.0",
|
|
48
|
+
"rimraf": "^3.0.2",
|
|
49
|
+
"typescript": "^4.8.4"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { Elysia, SCHEMA } from 'elysia'
|
|
2
|
+
import type { HTTPMethod } from 'elysia'
|
|
3
|
+
|
|
4
|
+
import { CreateEden, EdenCall, UnionToIntersection } from './types'
|
|
5
|
+
|
|
6
|
+
const composePath = (
|
|
7
|
+
domain: string,
|
|
8
|
+
path: string,
|
|
9
|
+
query: EdenCall['$query'] | undefined
|
|
10
|
+
) => {
|
|
11
|
+
if (!domain.endsWith('/')) domain += '/'
|
|
12
|
+
|
|
13
|
+
path = path.replace(/index/g, '')
|
|
14
|
+
if (!query || !Object.keys(query).length) return `${domain}${path}`
|
|
15
|
+
|
|
16
|
+
let q = ''
|
|
17
|
+
for (const [key, value] of Object.entries(query)) q += `${key}=${value}&`
|
|
18
|
+
|
|
19
|
+
return `${domain}${path}?${q.slice(0, -1)}`
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const createProxy = (
|
|
23
|
+
domain: string,
|
|
24
|
+
path: string = ''
|
|
25
|
+
): Record<string, unknown> =>
|
|
26
|
+
new Proxy(() => {}, {
|
|
27
|
+
get(target, key, value) {
|
|
28
|
+
return createProxy(domain, `${path}/${key.toString()}`)
|
|
29
|
+
},
|
|
30
|
+
apply(
|
|
31
|
+
target,
|
|
32
|
+
_,
|
|
33
|
+
[
|
|
34
|
+
{ $query, $fetch, ...body } = {
|
|
35
|
+
$fetch: undefined,
|
|
36
|
+
$query: undefined
|
|
37
|
+
}
|
|
38
|
+
]: EdenCall[] = [{}]
|
|
39
|
+
) {
|
|
40
|
+
const i = path.lastIndexOf('/')
|
|
41
|
+
|
|
42
|
+
return fetch(composePath(domain, path.slice(0, i), $query), {
|
|
43
|
+
method: path.slice(i + 1),
|
|
44
|
+
headers: {
|
|
45
|
+
'content-type': 'application/json',
|
|
46
|
+
...$fetch?.['headers']
|
|
47
|
+
},
|
|
48
|
+
body: Object.keys(body).length
|
|
49
|
+
? JSON.stringify(body)
|
|
50
|
+
: undefined,
|
|
51
|
+
...$fetch
|
|
52
|
+
}).then((res) => {
|
|
53
|
+
if (res.headers.get('content-type') === 'application/json')
|
|
54
|
+
return res.json()
|
|
55
|
+
|
|
56
|
+
return res.text()
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
}) as unknown as Record<string, unknown>
|
|
60
|
+
|
|
61
|
+
export function eden<App extends Elysia<any>>(
|
|
62
|
+
domain: string
|
|
63
|
+
): App['store'] extends {
|
|
64
|
+
[key in typeof SCHEMA]: any
|
|
65
|
+
}
|
|
66
|
+
? UnionToIntersection<CreateEden<App['store'][typeof SCHEMA]>>
|
|
67
|
+
: never {
|
|
68
|
+
return new Proxy(
|
|
69
|
+
{},
|
|
70
|
+
{
|
|
71
|
+
get(target, key, value) {
|
|
72
|
+
return createProxy(domain, key as string)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
) as any
|
|
76
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { TypedRoute } from 'elysia'
|
|
2
|
+
|
|
3
|
+
export interface EdenCall {
|
|
4
|
+
[x: string]: any
|
|
5
|
+
$fetch?: RequestInit
|
|
6
|
+
$query?: Record<string, string | boolean | number>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type UnionToIntersection<U> = (
|
|
10
|
+
U extends any ? (k: U) => void : never
|
|
11
|
+
) extends (k: infer I) => void
|
|
12
|
+
? I
|
|
13
|
+
: never
|
|
14
|
+
|
|
15
|
+
type TypedRouteToParams<Route extends TypedRoute> =
|
|
16
|
+
(Route['body'] extends NonNullable<Route['body']> ? Route['body'] : {}) &
|
|
17
|
+
(Route['query'] extends NonNullable<Route['query']>
|
|
18
|
+
? {
|
|
19
|
+
$query: Route['query']
|
|
20
|
+
}
|
|
21
|
+
: {})
|
|
22
|
+
|
|
23
|
+
export type CreateEden<
|
|
24
|
+
Server extends Record<string, Record<string, TypedRoute>>,
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
Path extends string = keyof Server,
|
|
27
|
+
Full extends string = ''
|
|
28
|
+
> = Path extends `/${infer Start}`
|
|
29
|
+
? CreateEden<Server, Start, Path>
|
|
30
|
+
: Path extends `${infer A}/${infer B}`
|
|
31
|
+
? {
|
|
32
|
+
[key in A]: CreateEden<Server, B, Full>
|
|
33
|
+
}
|
|
34
|
+
: {
|
|
35
|
+
[key in Path extends ''
|
|
36
|
+
? 'index'
|
|
37
|
+
: Path extends `:${infer params}`
|
|
38
|
+
? string
|
|
39
|
+
: Path]: Full extends keyof Server
|
|
40
|
+
? {
|
|
41
|
+
[key in keyof Server[Full]]: keyof TypedRouteToParams<
|
|
42
|
+
Server[Full][key]
|
|
43
|
+
> extends never
|
|
44
|
+
? (params?: {
|
|
45
|
+
$query?: EdenCall['$query']
|
|
46
|
+
$fetch?: EdenCall['$fetch']
|
|
47
|
+
}) => Promise<Server[Full][key]['response']>
|
|
48
|
+
: (
|
|
49
|
+
params: TypedRouteToParams<Server[Full][key]> & {
|
|
50
|
+
$query?: EdenCall['$query']
|
|
51
|
+
$fetch?: EdenCall['$fetch']
|
|
52
|
+
}
|
|
53
|
+
) => Promise<Server[Full][key]['response']>
|
|
54
|
+
}
|
|
55
|
+
: never
|
|
56
|
+
}
|