elysia-autoload 0.0.3 → 0.1.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/README.md +57 -7
- package/dist/index.d.ts +14 -6
- package/dist/index.js +40 -1
- package/dist/types.d.ts +8 -0
- package/dist/types.js +2 -0
- package/dist/utils.js +1 -1
- package/package.json +20 -12
package/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# elysia-autoload
|
2
2
|
|
3
|
-
Plugin for [Elysia](https://elysiajs.com/) which autoload all routes in directory
|
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
5
|
## Installation
|
6
6
|
|
@@ -55,12 +55,62 @@ Guide how `elysia-autoload` match routes
|
|
55
55
|
|
56
56
|
## Options
|
57
57
|
|
58
|
-
| Key | Type
|
59
|
-
| -------- |
|
60
|
-
| pattern? | string
|
61
|
-
| dir? | string
|
62
|
-
| prefix? | string
|
63
|
-
|
|
58
|
+
| Key | Type | Default | Description |
|
59
|
+
| -------- | ------------------------------------------ | --------------------------- | ----------------------------------------------------------------------------------- |
|
60
|
+
| pattern? | string | "\*\*_/\*_.{ts,js,mjs,cjs}" | [Glob patterns](<https://en.wikipedia.org/wiki/Glob_(programming)>) |
|
61
|
+
| dir? | string | "./routes" | The folder where routes are located |
|
62
|
+
| prefix? | string | | Prefix for routes |
|
63
|
+
| types? | boolean \| [Types Options](#types-options) | false | Options to configure type code-generation. if boolean - enables/disables generation |
|
64
|
+
| schema? | Function | | Handler for providing routes guard schema |
|
65
|
+
|
66
|
+
### Types Options
|
67
|
+
|
68
|
+
| Key | Type | Default | Description |
|
69
|
+
| ---------- | ------- | ------------------- | --------------------------------------------------------------------------------------- |
|
70
|
+
| output? | string | "./routes-types.ts" | Type code-generation output |
|
71
|
+
| typeName? | string | "Routes" | Name for code-generated global type for [Eden](https://elysiajs.com/eden/overview.html) |
|
72
|
+
| useExport? | boolean | false | Use export instead of global type |
|
73
|
+
|
74
|
+
### Usage of types code-generation for [Eden](https://elysiajs.com/eden/overview.html)
|
75
|
+
|
76
|
+
```ts
|
77
|
+
// app.ts
|
78
|
+
import { Elysia } from "elysia";
|
79
|
+
import { autoload } from "elysia-autoload";
|
80
|
+
|
81
|
+
const app = new Elysia()
|
82
|
+
.use(
|
83
|
+
autoload({
|
84
|
+
types: {
|
85
|
+
output: "./routes.ts",
|
86
|
+
typeName: "Routes",
|
87
|
+
}, // or pass true for use default params
|
88
|
+
}),
|
89
|
+
)
|
90
|
+
.listen(3000);
|
91
|
+
|
92
|
+
export type ElysiaApp = typeof app;
|
93
|
+
```
|
94
|
+
|
95
|
+
```ts
|
96
|
+
// client.ts
|
97
|
+
|
98
|
+
import { edenTreaty } from "@elysiajs/eden";
|
99
|
+
|
100
|
+
// Routes are a global type so you don't need to import it.
|
101
|
+
|
102
|
+
const app = edenTreaty<Routes>("http://localhost:3002");
|
103
|
+
|
104
|
+
const { data } = await app.test["some-path-param"].get({
|
105
|
+
$query: {
|
106
|
+
key: 2,
|
107
|
+
},
|
108
|
+
});
|
109
|
+
|
110
|
+
console.log(data);
|
111
|
+
```
|
112
|
+
|
113
|
+
Example of app with types code-generation you can see in [example](https://github.com/kravetsone/elysia-autoload/tree/main/example)
|
64
114
|
|
65
115
|
### Usage of schema handler
|
66
116
|
|
package/dist/index.d.ts
CHANGED
@@ -1,14 +1,21 @@
|
|
1
1
|
import Elysia from "elysia";
|
2
|
-
|
2
|
+
type TSchemaHandler = ({ path, url, }: {
|
3
|
+
path: string;
|
4
|
+
url: string;
|
5
|
+
}) => Parameters<InstanceType<typeof Elysia>["group"]>[1];
|
6
|
+
export interface ITypesOptions {
|
7
|
+
output?: string;
|
8
|
+
typeName?: string;
|
9
|
+
useExport?: boolean;
|
10
|
+
}
|
11
|
+
export interface IAutoloadOptions {
|
3
12
|
pattern?: string;
|
4
13
|
dir?: string;
|
5
14
|
prefix?: string;
|
6
|
-
schema?:
|
7
|
-
|
8
|
-
url: string;
|
9
|
-
}) => Parameters<InstanceType<typeof Elysia>["group"]>[1];
|
15
|
+
schema?: TSchemaHandler;
|
16
|
+
types?: ITypesOptions | true;
|
10
17
|
}
|
11
|
-
export declare function autoload({ pattern, dir, prefix, schema, }?:
|
18
|
+
export declare function autoload({ pattern, dir, prefix, schema, types, }?: IAutoloadOptions): Promise<Elysia<"", {
|
12
19
|
request: {};
|
13
20
|
store: {};
|
14
21
|
derive: {};
|
@@ -17,3 +24,4 @@ export declare function autoload({ pattern, dir, prefix, schema, }?: AutoloadOpt
|
|
17
24
|
type: {};
|
18
25
|
error: {};
|
19
26
|
}, {}, {}, {}, false>>;
|
27
|
+
export * from "./types";
|
package/dist/index.js
CHANGED
@@ -1,11 +1,27 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
17
|
exports.autoload = void 0;
|
4
18
|
const elysia_1 = require("elysia");
|
5
19
|
const node_fs_1 = require("node:fs");
|
6
20
|
const node_path_1 = require("node:path");
|
7
21
|
const utils_1 = require("./utils");
|
8
|
-
|
22
|
+
const TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
|
23
|
+
const TYPES_TYPENAME_DEFAULT = "Routes";
|
24
|
+
async function autoload({ pattern, dir, prefix, schema, types, } = {}) {
|
9
25
|
const directoryPath = (0, utils_1.getPath)(dir || "./routes");
|
10
26
|
if (!(0, node_fs_1.existsSync)(directoryPath))
|
11
27
|
throw new Error(`Directory ${directoryPath} doesn't exists`);
|
@@ -17,13 +33,16 @@ async function autoload({ pattern, dir, prefix, schema, } = {}) {
|
|
17
33
|
pattern,
|
18
34
|
dir,
|
19
35
|
prefix,
|
36
|
+
types,
|
20
37
|
},
|
21
38
|
});
|
22
39
|
const glob = new Bun.Glob(pattern || "**/*.{ts,js,mjs,cjs}");
|
23
40
|
const files = await Array.fromAsync(glob.scan({
|
24
41
|
cwd: directoryPath,
|
25
42
|
}));
|
43
|
+
const paths = [];
|
26
44
|
for await (const path of (0, utils_1.sortByNestedParams)(files)) {
|
45
|
+
const fullPath = (0, node_path_1.join)(directoryPath, path);
|
27
46
|
const file = await Promise.resolve(`${(0, node_path_1.join)(directoryPath, path)}`).then(s => require(s));
|
28
47
|
if (!file.default)
|
29
48
|
throw new Error(`${path} don't provide export default`);
|
@@ -34,7 +53,27 @@ async function autoload({ pattern, dir, prefix, schema, } = {}) {
|
|
34
53
|
// Тип "string" не может быть назначен для типа "TSchema".ts(2345)
|
35
54
|
// @ts-expect-error why....
|
36
55
|
app.group((prefix ?? "") + url, groupOptions, file.default);
|
56
|
+
if (types)
|
57
|
+
paths.push(fullPath.replace(directoryPath, ""));
|
58
|
+
}
|
59
|
+
if (types) {
|
60
|
+
const imports = paths.map((x, index) => `import Route${index} from "${directoryPath + x.replace(".ts", "")}";`);
|
61
|
+
await Bun.write((0, utils_1.getPath)(types === true || !types.output
|
62
|
+
? TYPES_OUTPUT_DEFAULT
|
63
|
+
: types.output), [
|
64
|
+
`import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
|
65
|
+
imports.join("\n"),
|
66
|
+
"",
|
67
|
+
types === true || !types.useExport ? "declare global {" : "",
|
68
|
+
` export type ${types === true || !types.typeName
|
69
|
+
? TYPES_TYPENAME_DEFAULT
|
70
|
+
: types.typeName} = ${paths
|
71
|
+
.map((x, index) => `ElysiaWithBaseUrl<"${(0, utils_1.transformToUrl)(x) || "/"}", ReturnType<typeof Route${index}>>`)
|
72
|
+
.join("\n & ")}`,
|
73
|
+
types === true || !types.useExport ? "}" : "",
|
74
|
+
].join("\n"));
|
37
75
|
}
|
38
76
|
return app;
|
39
77
|
}
|
40
78
|
exports.autoload = autoload;
|
79
|
+
__exportStar(require("./types"), exports);
|
package/dist/types.d.ts
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
import type Elysia from "elysia";
|
2
|
+
import type { RouteBase } from "elysia";
|
3
|
+
type RemoveLastChar<T extends string> = T extends `${infer V}/` ? V : T;
|
4
|
+
type RoutesWithPrefix<Routes extends RouteBase, Prefix extends string> = {
|
5
|
+
[K in keyof Routes as `${Prefix}${RemoveLastChar<K & string>}`]: Routes[K];
|
6
|
+
};
|
7
|
+
export type ElysiaWithBaseUrl<BaseUrl extends string, ElysiaType extends Elysia> = ElysiaType extends Elysia<infer BasePath, infer Decorators, infer Definitions, infer ParentSchema, infer Macro, infer Routes, infer Scoped> ? Elysia<BasePath, Decorators, Definitions, ParentSchema, Macro, RoutesWithPrefix<Routes, BaseUrl>, Scoped> : never;
|
8
|
+
export {};
|
package/dist/types.js
ADDED
package/dist/utils.js
CHANGED
package/package.json
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
{
|
2
2
|
"name": "elysia-autoload",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.1.1",
|
4
4
|
"author": "kravetsone",
|
5
|
-
"
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"description": "Plugin for Elysia which autoload all routes in directory and code-generate types for Eden",
|
7
|
+
"homepage": "https://github.com/kravetsone/elysia-autoload",
|
6
8
|
"keywords": [
|
7
9
|
"bun",
|
8
10
|
"elysia",
|
@@ -10,25 +12,31 @@
|
|
10
12
|
"autoload",
|
11
13
|
"nextjs",
|
12
14
|
"filerouter",
|
13
|
-
"autoroutes"
|
15
|
+
"autoroutes",
|
16
|
+
"eden",
|
17
|
+
"treaty",
|
18
|
+
"trpc",
|
19
|
+
"codegeneration"
|
14
20
|
],
|
15
|
-
"main": "dist/index.js",
|
16
21
|
"scripts": {
|
17
22
|
"prepublishOnly": "tsc",
|
18
|
-
"lint": "eslint \"src/**/*.{ts,tsx,js,mjs,cjs}\"",
|
19
|
-
"lint:fix": "eslint \"src/**/*.{ts,tsx,js,mjs,cjs}\" --fix"
|
23
|
+
"lint": "eslint \"{src,example}/**/*.{ts,tsx,js,mjs,cjs}\"",
|
24
|
+
"lint:fix": "eslint \"{src,example}/**/*.{ts,tsx,js,mjs,cjs}\" --fix"
|
20
25
|
},
|
26
|
+
"files": [
|
27
|
+
"dist"
|
28
|
+
],
|
21
29
|
"devDependencies": {
|
30
|
+
"@elysiajs/eden": "^0.8.0",
|
31
|
+
"@elysiajs/swagger": "^0.8.0",
|
22
32
|
"bun-types": "latest",
|
33
|
+
"elysia": "^0.8.3",
|
23
34
|
"eslint": "^8.41.0",
|
24
35
|
"eslint-kit": "^10.0.0",
|
25
|
-
"prettier": "^3.0.0"
|
36
|
+
"prettier": "^3.0.0",
|
37
|
+
"typescript": "^5.0.0"
|
26
38
|
},
|
27
39
|
"peerDependencies": {
|
28
|
-
"typescript": "^5.0.0",
|
29
40
|
"elysia": "^0.8.0"
|
30
|
-
}
|
31
|
-
"files": [
|
32
|
-
"dist"
|
33
|
-
]
|
41
|
+
}
|
34
42
|
}
|