elysia-autoload 0.1.6 → 0.1.8
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/README.md +4 -2
- package/dist/index.d.ts +15 -4
- package/dist/index.js +10 -13
- package/dist/types.d.ts +1 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +7 -1
- package/package.json +43 -43
package/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Plugin for [Elysia](https://elysiajs.com/) which autoload all routes in directory and code-generate types for [Eden](https://elysiajs.com/eden/overview.html)
|
4
4
|
|
5
|
+
**Currently, Eden types generation is broken!!**
|
6
|
+
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
### Start new project with [create-elysiajs](https://github.com/kravetsone/create-elysiajs)
|
@@ -102,7 +104,7 @@ const app = new Elysia()
|
|
102
104
|
output: "./routes.ts",
|
103
105
|
typeName: "Routes",
|
104
106
|
}, // or pass true for use default params
|
105
|
-
})
|
107
|
+
})
|
106
108
|
)
|
107
109
|
.listen(3000);
|
108
110
|
|
@@ -152,7 +154,7 @@ const app = new Elysia()
|
|
152
154
|
},
|
153
155
|
};
|
154
156
|
},
|
155
|
-
})
|
157
|
+
})
|
156
158
|
)
|
157
159
|
.use(swagger());
|
158
160
|
|
package/dist/index.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import Elysia from "elysia";
|
1
|
+
import { Elysia } from "elysia";
|
2
2
|
type TSchemaHandler = ({ path, url, }: {
|
3
3
|
path: string;
|
4
4
|
url: string;
|
@@ -15,13 +15,24 @@ export interface IAutoloadOptions {
|
|
15
15
|
schema?: TSchemaHandler;
|
16
16
|
types?: ITypesOptions | true;
|
17
17
|
}
|
18
|
-
export declare function autoload(
|
19
|
-
|
18
|
+
export declare function autoload(options?: IAutoloadOptions): Promise<Elysia<string, false, {
|
19
|
+
decorator: {};
|
20
20
|
store: {};
|
21
21
|
derive: {};
|
22
22
|
resolve: {};
|
23
23
|
}, {
|
24
24
|
type: {};
|
25
25
|
error: {};
|
26
|
-
}, {
|
26
|
+
}, {
|
27
|
+
schema: {};
|
28
|
+
macro: {};
|
29
|
+
}, {}, {
|
30
|
+
derive: {};
|
31
|
+
resolve: {};
|
32
|
+
schema: {};
|
33
|
+
}, {
|
34
|
+
derive: {};
|
35
|
+
resolve: {};
|
36
|
+
schema: {};
|
37
|
+
}>>;
|
27
38
|
export * from "./types";
|
package/dist/index.js
CHANGED
@@ -21,15 +21,16 @@ const elysia_1 = require("elysia");
|
|
21
21
|
const utils_1 = require("./utils");
|
22
22
|
const TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
|
23
23
|
const TYPES_TYPENAME_DEFAULT = "Routes";
|
24
|
-
async function autoload(
|
24
|
+
async function autoload(options = {}) {
|
25
|
+
const { pattern, dir, prefix, schema, types } = options;
|
25
26
|
const directoryPath = (0, utils_1.getPath)(dir || "./routes");
|
26
27
|
if (!(0, node_fs_1.existsSync)(directoryPath))
|
27
28
|
throw new Error(`Directory ${directoryPath} doesn't exists`);
|
28
29
|
if (!(0, node_fs_1.statSync)(directoryPath).isDirectory())
|
29
30
|
throw new Error(`${directoryPath} isn't a directory`);
|
30
|
-
const
|
31
|
+
const plugin = new elysia_1.Elysia({
|
31
32
|
name: "elysia-autoload",
|
32
|
-
prefix:
|
33
|
+
prefix: (0, utils_1.fixSlashes)(prefix),
|
33
34
|
seed: {
|
34
35
|
pattern,
|
35
36
|
dir,
|
@@ -44,17 +45,12 @@ async function autoload({ pattern, dir, prefix, schema, types, } = {}) {
|
|
44
45
|
const paths = [];
|
45
46
|
for await (const path of (0, utils_1.sortByNestedParams)(files)) {
|
46
47
|
const fullPath = (0, node_path_1.join)(directoryPath, path);
|
47
|
-
const file = await Promise.resolve(`${
|
48
|
+
const file = await Promise.resolve(`${fullPath}`).then(s => require(s));
|
48
49
|
if (!file.default)
|
49
|
-
throw new Error(`${path}
|
50
|
+
throw new Error(`${path} doesn't provide default export`);
|
50
51
|
const url = (0, utils_1.transformToUrl)(path);
|
51
52
|
const groupOptions = schema ? schema({ path, url }) : {};
|
52
|
-
|
53
|
-
// Тип "string | TSchema | undefined" не может быть назначен для типа "TSchema | undefined".
|
54
|
-
// Тип "string" не может быть назначен для типа "TSchema".ts(2345)
|
55
|
-
app.group(url,
|
56
|
-
// @ts-expect-error why....
|
57
|
-
groupOptions, file.default);
|
53
|
+
plugin.group(url, groupOptions, file.default);
|
58
54
|
if (types)
|
59
55
|
paths.push(fullPath.replace(directoryPath, ""));
|
60
56
|
}
|
@@ -73,13 +69,14 @@ async function autoload({ pattern, dir, prefix, schema, types, } = {}) {
|
|
73
69
|
` export type ${types === true || !types.typeName
|
74
70
|
? TYPES_TYPENAME_DEFAULT
|
75
71
|
: types.typeName} = ${paths
|
76
|
-
.map((x, index) => `ElysiaWithBaseUrl<"${(
|
72
|
+
.map((x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ??
|
73
|
+
"") + (0, utils_1.transformToUrl)(x) || "/"}", ReturnType<typeof Route${index}>>`)
|
77
74
|
.join("\n & ")}`,
|
78
75
|
types === true || !types.useExport ? "}" : "",
|
79
76
|
].join("\n"));
|
80
77
|
}
|
81
78
|
}
|
82
|
-
return
|
79
|
+
return plugin;
|
83
80
|
}
|
84
81
|
exports.autoload = autoload;
|
85
82
|
__exportStar(require("./types"), exports);
|
package/dist/types.d.ts
CHANGED
@@ -4,5 +4,5 @@ type RemoveLastChar<T extends string> = T extends `${infer V}/` ? V : T;
|
|
4
4
|
type RoutesWithPrefix<Routes extends RouteBase, Prefix extends string> = {
|
5
5
|
[K in keyof Routes as `${Prefix}${RemoveLastChar<K & string>}`]: Routes[K];
|
6
6
|
};
|
7
|
-
export type ElysiaWithBaseUrl<BaseUrl extends string, ElysiaType extends Elysia
|
7
|
+
export type ElysiaWithBaseUrl<BaseUrl extends string, ElysiaType extends Elysia<any, any, any, any, any, any, any, any>> = ElysiaType extends Elysia<infer BasePath, infer Scoped, infer Singleton, infer Definitions, infer Metadata, infer Routes, infer Ephemeral, infer Volatile> ? Elysia<BasePath, Scoped, Singleton, Definitions, Metadata, RoutesWithPrefix<Routes, BaseUrl>, Ephemeral, Volatile> : never;
|
8
8
|
export {};
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.sortByNestedParams = exports.transformToUrl = exports.getPath = void 0;
|
3
|
+
exports.fixSlashes = exports.sortByNestedParams = exports.transformToUrl = exports.getPath = void 0;
|
4
4
|
const node_path_1 = require("node:path");
|
5
5
|
function getPath(dir) {
|
6
6
|
if ((0, node_path_1.isAbsolute)(dir))
|
@@ -49,3 +49,9 @@ function sortByNestedParams(routes) {
|
|
49
49
|
return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
|
50
50
|
}
|
51
51
|
exports.sortByNestedParams = sortByNestedParams;
|
52
|
+
function fixSlashes(prefix) {
|
53
|
+
if (!prefix?.endsWith("/"))
|
54
|
+
return prefix;
|
55
|
+
return prefix.slice(0, -1);
|
56
|
+
}
|
57
|
+
exports.fixSlashes = fixSlashes;
|
package/package.json
CHANGED
@@ -1,43 +1,43 @@
|
|
1
|
-
{
|
2
|
-
"name": "elysia-autoload",
|
3
|
-
"version": "0.1.
|
4
|
-
"author": "kravetsone",
|
5
|
-
"type": "commonjs",
|
6
|
-
"main": "dist/index.js",
|
7
|
-
"description": "Plugin for Elysia which autoload all routes in directory and code-generate types for Eden",
|
8
|
-
"homepage": "https://github.com/kravetsone/elysia-autoload",
|
9
|
-
"keywords": [
|
10
|
-
"bun",
|
11
|
-
"elysia",
|
12
|
-
"autoimports",
|
13
|
-
"autoload",
|
14
|
-
"nextjs",
|
15
|
-
"filerouter",
|
16
|
-
"autoroutes",
|
17
|
-
"eden",
|
18
|
-
"treaty",
|
19
|
-
"trpc",
|
20
|
-
"codegeneration"
|
21
|
-
],
|
22
|
-
"scripts": {
|
23
|
-
"prepublishOnly": "bun test && rm -rf dist && tsc",
|
24
|
-
"lint": "bunx @biomejs/biome check src",
|
25
|
-
"lint:fix": "bun lint --apply",
|
26
|
-
"prepare": "husky"
|
27
|
-
},
|
28
|
-
"files": [
|
29
|
-
"dist"
|
30
|
-
],
|
31
|
-
"devDependencies": {
|
32
|
-
"@biomejs/biome": "1.6.
|
33
|
-
"@elysiajs/eden": "^0.
|
34
|
-
"@elysiajs/swagger": "^0.
|
35
|
-
"@types/bun": "^1.0.8",
|
36
|
-
"elysia": "^0.
|
37
|
-
"typescript": "^5.4.2",
|
38
|
-
"husky": "^9.0.11"
|
39
|
-
},
|
40
|
-
"peerDependencies": {
|
41
|
-
"elysia": "^0.
|
42
|
-
}
|
43
|
-
}
|
1
|
+
{
|
2
|
+
"name": "elysia-autoload",
|
3
|
+
"version": "0.1.8",
|
4
|
+
"author": "kravetsone",
|
5
|
+
"type": "commonjs",
|
6
|
+
"main": "dist/index.js",
|
7
|
+
"description": "Plugin for Elysia which autoload all routes in directory and code-generate types for Eden",
|
8
|
+
"homepage": "https://github.com/kravetsone/elysia-autoload",
|
9
|
+
"keywords": [
|
10
|
+
"bun",
|
11
|
+
"elysia",
|
12
|
+
"autoimports",
|
13
|
+
"autoload",
|
14
|
+
"nextjs",
|
15
|
+
"filerouter",
|
16
|
+
"autoroutes",
|
17
|
+
"eden",
|
18
|
+
"treaty",
|
19
|
+
"trpc",
|
20
|
+
"codegeneration"
|
21
|
+
],
|
22
|
+
"scripts": {
|
23
|
+
"prepublishOnly": "bun test && rm -rf dist && tsc",
|
24
|
+
"lint": "bunx @biomejs/biome check src",
|
25
|
+
"lint:fix": "bun lint --apply",
|
26
|
+
"prepare": "husky"
|
27
|
+
},
|
28
|
+
"files": [
|
29
|
+
"dist"
|
30
|
+
],
|
31
|
+
"devDependencies": {
|
32
|
+
"@biomejs/biome": "1.6.1",
|
33
|
+
"@elysiajs/eden": "^1.0.4",
|
34
|
+
"@elysiajs/swagger": "^1.0.2",
|
35
|
+
"@types/bun": "^1.0.8",
|
36
|
+
"elysia": "^1.0.5",
|
37
|
+
"typescript": "^5.4.2",
|
38
|
+
"husky": "^9.0.11"
|
39
|
+
},
|
40
|
+
"peerDependencies": {
|
41
|
+
"elysia": "^1.0.0"
|
42
|
+
}
|
43
|
+
}
|