elysia-autoload 0.2.3 → 1.0.0
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 +20 -1
- package/dist/index.cjs +75 -69
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +65 -69
- package/package.json +6 -7
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 and code-generate types for [Eden](https://elysiajs.com/eden/overview.html)
|
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) with [`Bun.build`](https://bun.sh/docs/bundler) support!
|
4
4
|
|
5
5
|
**Currently, Eden types generation is broken!!**
|
6
6
|
|
@@ -131,6 +131,25 @@ console.log(data);
|
|
131
131
|
|
132
132
|
Example of app with types code-generation you can see in [example](https://github.com/kravetsone/elysia-autoload/tree/main/example)
|
133
133
|
|
134
|
+
### Bun build usage
|
135
|
+
|
136
|
+
You can use this plugin with `Bun.build`, thanks to [esbuild-plugin-autoload](https://github.com/kravetsone/esbuild-plugin-autoload)!
|
137
|
+
|
138
|
+
```ts
|
139
|
+
// @filename: build.ts
|
140
|
+
import { autoload } from "esbuild-plugin-autoload"; // default import also supported
|
141
|
+
|
142
|
+
await Bun.build({
|
143
|
+
entrypoints: ["src/index.ts"],
|
144
|
+
outdir: "out",
|
145
|
+
plugins: [autoload()],
|
146
|
+
}).then(console.log);
|
147
|
+
```
|
148
|
+
|
149
|
+
Then, build it with `bun build.ts` and run with `bun out/index.ts`.
|
150
|
+
|
151
|
+
[Read more](https://github.com/kravetsone/esbuild-plugin-autoload)
|
152
|
+
|
134
153
|
### Usage of schema handler
|
135
154
|
|
136
155
|
```ts
|
package/dist/index.cjs
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __create = Object.create;
|
2
3
|
var __defProp = Object.defineProperty;
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
8
|
var __export = (target, all) => {
|
7
9
|
for (var name in all)
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
15
17
|
}
|
16
18
|
return to;
|
17
19
|
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
29
|
|
20
30
|
// src/index.ts
|
@@ -23,20 +33,19 @@ __export(src_exports, {
|
|
23
33
|
autoload: () => autoload
|
24
34
|
});
|
25
35
|
module.exports = __toCommonJS(src_exports);
|
26
|
-
var import_node_fs = require("fs");
|
27
|
-
var import_node_path2 = require("path");
|
36
|
+
var import_node_fs = __toESM(require("fs"), 1);
|
37
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
28
38
|
var import_elysia = require("elysia");
|
29
39
|
|
30
40
|
// src/utils.ts
|
31
|
-
var import_node_path = require("path");
|
41
|
+
var import_node_path = __toESM(require("path"), 1);
|
32
42
|
function getPath(dir) {
|
33
|
-
if (
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
return (0, import_node_path.join)(process.cwd(), process.argv[1], "..", dir);
|
43
|
+
if (import_node_path.default.isAbsolute(dir)) return dir;
|
44
|
+
if (import_node_path.default.isAbsolute(process.argv[1]))
|
45
|
+
return import_node_path.default.join(process.argv[1], "..", dir);
|
46
|
+
return import_node_path.default.join(process.cwd(), process.argv[1], "..", dir);
|
38
47
|
}
|
39
|
-
function transformToUrl(
|
48
|
+
function transformToUrl(path3) {
|
40
49
|
const replacements = [
|
41
50
|
// Clean the url extensions
|
42
51
|
{ regex: /\.(ts|tsx|js|jsx|mjs|cjs)$/u, replacement: "" },
|
@@ -58,85 +67,82 @@ function transformToUrl(path) {
|
|
58
67
|
// remove index from end of path
|
59
68
|
{ regex: /\/?index$/, replacement: "" }
|
60
69
|
];
|
61
|
-
let url =
|
70
|
+
let url = path3;
|
62
71
|
for (const { regex, replacement } of replacements) {
|
63
72
|
url = url.replace(regex, replacement);
|
64
73
|
}
|
65
74
|
return url;
|
66
75
|
}
|
67
|
-
function getParamsCount(
|
68
|
-
return
|
76
|
+
function getParamsCount(path3) {
|
77
|
+
return path3.match(/\[(.*?)\]/gu)?.length || 0;
|
69
78
|
}
|
70
79
|
function sortByNestedParams(routes) {
|
71
80
|
return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
|
72
81
|
}
|
73
82
|
function fixSlashes(prefix) {
|
74
|
-
if (!prefix?.endsWith("/"))
|
75
|
-
return prefix;
|
83
|
+
if (!prefix?.endsWith("/")) return prefix;
|
76
84
|
return prefix.slice(0, -1);
|
77
85
|
}
|
78
86
|
|
79
87
|
// src/index.ts
|
80
88
|
var TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
|
81
89
|
var TYPES_TYPENAME_DEFAULT = "Routes";
|
82
|
-
function autoload(options = {}) {
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
}
|
99
|
-
});
|
100
|
-
const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
|
101
|
-
const files = await Array.fromAsync(
|
102
|
-
glob.scan({
|
103
|
-
cwd: directoryPath
|
104
|
-
})
|
105
|
-
);
|
106
|
-
const paths = [];
|
107
|
-
for await (const path of sortByNestedParams(files)) {
|
108
|
-
const fullPath = (0, import_node_path2.join)(directoryPath, path);
|
109
|
-
const file = await import(fullPath);
|
110
|
-
if (!file.default)
|
111
|
-
throw new Error(`${path} doesn't provide default export`);
|
112
|
-
const url = transformToUrl(path);
|
113
|
-
const groupOptions = schema ? schema({ path, url }) : {};
|
114
|
-
plugin.group(url, groupOptions, file.default);
|
115
|
-
if (types)
|
116
|
-
paths.push(fullPath.replace(directoryPath, ""));
|
90
|
+
async function autoload(options = {}) {
|
91
|
+
const fileSources = {};
|
92
|
+
const { pattern, dir, prefix, schema, types } = options;
|
93
|
+
const directoryPath = getPath(dir || "./routes");
|
94
|
+
if (!import_node_fs.default.existsSync(directoryPath))
|
95
|
+
throw new Error(`Directory ${directoryPath} doesn't exists`);
|
96
|
+
if (!import_node_fs.default.statSync(directoryPath).isDirectory())
|
97
|
+
throw new Error(`${directoryPath} isn't a directory`);
|
98
|
+
const plugin = new import_elysia.Elysia({
|
99
|
+
name: "elysia-autoload",
|
100
|
+
prefix: fixSlashes(prefix),
|
101
|
+
seed: {
|
102
|
+
pattern,
|
103
|
+
dir,
|
104
|
+
prefix,
|
105
|
+
types
|
117
106
|
}
|
118
|
-
|
119
|
-
|
120
|
-
|
107
|
+
});
|
108
|
+
const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
|
109
|
+
const files = await Array.fromAsync(
|
110
|
+
glob.scan({
|
111
|
+
cwd: directoryPath
|
112
|
+
})
|
113
|
+
);
|
114
|
+
const paths = [];
|
115
|
+
for await (const filePath of sortByNestedParams(files)) {
|
116
|
+
const fullPath = import_node_path2.default.join(directoryPath, filePath);
|
117
|
+
const file = await import(fullPath);
|
118
|
+
if (!file.default)
|
119
|
+
throw new Error(`${filePath} doesn't provide default export`);
|
120
|
+
const url = transformToUrl(filePath);
|
121
|
+
const groupOptions = schema ? schema({ path: filePath, url }) : {};
|
122
|
+
plugin.group(url, groupOptions, file.default);
|
123
|
+
if (types) paths.push(fullPath.replace(directoryPath, ""));
|
124
|
+
}
|
125
|
+
if (types) {
|
126
|
+
const imports = paths.map(
|
127
|
+
(x, index) => `import type Route${index} from "${(directoryPath + x.replace(".ts", "").replace(".tsx", "")).replace(/\\/gu, "/")}";`
|
128
|
+
);
|
129
|
+
for await (const outputPath of types === true || !types.output ? [TYPES_OUTPUT_DEFAULT] : Array.isArray(types.output) ? types.output : [types.output]) {
|
130
|
+
await Bun.write(
|
131
|
+
getPath(outputPath),
|
132
|
+
[
|
133
|
+
`import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
|
134
|
+
imports.join("\n"),
|
135
|
+
"",
|
136
|
+
types === true || !types.useExport ? "declare global {" : "",
|
137
|
+
` export type ${types === true || !types.typeName ? TYPES_TYPENAME_DEFAULT : types.typeName} = ${paths.map(
|
138
|
+
(x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ?? "") + transformToUrl(x) || "/"}", ReturnType<typeof Route${index}>>`
|
139
|
+
).join("\n & ")}`,
|
140
|
+
types === true || !types.useExport ? "}" : ""
|
141
|
+
].join("\n")
|
121
142
|
);
|
122
|
-
for await (const outputPath of types === true || !types.output ? [TYPES_OUTPUT_DEFAULT] : Array.isArray(types.output) ? types.output : [types.output]) {
|
123
|
-
await Bun.write(
|
124
|
-
getPath(outputPath),
|
125
|
-
[
|
126
|
-
`import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
|
127
|
-
imports.join("\n"),
|
128
|
-
"",
|
129
|
-
types === true || !types.useExport ? "declare global {" : "",
|
130
|
-
` export type ${types === true || !types.typeName ? TYPES_TYPENAME_DEFAULT : types.typeName} = ${paths.map(
|
131
|
-
(x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ?? "") + transformToUrl(x) || "/"}", ReturnType<typeof Route${index}>>`
|
132
|
-
).join("\n & ")}`,
|
133
|
-
types === true || !types.useExport ? "}" : ""
|
134
|
-
].join("\n")
|
135
|
-
);
|
136
|
-
}
|
137
143
|
}
|
138
|
-
|
139
|
-
|
144
|
+
}
|
145
|
+
return plugin;
|
140
146
|
}
|
141
147
|
// Annotate the CommonJS export names for ESM import in node:
|
142
148
|
0 && (module.exports = {
|
package/dist/index.d.cts
CHANGED
@@ -23,7 +23,7 @@ interface IAutoloadOptions {
|
|
23
23
|
schema?: TSchemaHandler;
|
24
24
|
types?: ITypesOptions | true;
|
25
25
|
}
|
26
|
-
declare function autoload(options?: IAutoloadOptions):
|
26
|
+
declare function autoload(options?: IAutoloadOptions): Promise<Elysia$1<string, false, {
|
27
27
|
decorator: {};
|
28
28
|
store: {};
|
29
29
|
derive: {};
|
package/dist/index.d.ts
CHANGED
@@ -23,7 +23,7 @@ interface IAutoloadOptions {
|
|
23
23
|
schema?: TSchemaHandler;
|
24
24
|
types?: ITypesOptions | true;
|
25
25
|
}
|
26
|
-
declare function autoload(options?: IAutoloadOptions):
|
26
|
+
declare function autoload(options?: IAutoloadOptions): Promise<Elysia$1<string, false, {
|
27
27
|
decorator: {};
|
28
28
|
store: {};
|
29
29
|
derive: {};
|
package/dist/index.js
CHANGED
@@ -1,20 +1,19 @@
|
|
1
1
|
// src/index.ts
|
2
|
-
import
|
3
|
-
import
|
2
|
+
import fs from "node:fs";
|
3
|
+
import path2 from "node:path";
|
4
4
|
import {
|
5
5
|
Elysia
|
6
6
|
} from "elysia";
|
7
7
|
|
8
8
|
// src/utils.ts
|
9
|
-
import
|
9
|
+
import path from "node:path";
|
10
10
|
function getPath(dir) {
|
11
|
-
if (isAbsolute(dir))
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
return join(process.cwd(), process.argv[1], "..", dir);
|
11
|
+
if (path.isAbsolute(dir)) return dir;
|
12
|
+
if (path.isAbsolute(process.argv[1]))
|
13
|
+
return path.join(process.argv[1], "..", dir);
|
14
|
+
return path.join(process.cwd(), process.argv[1], "..", dir);
|
16
15
|
}
|
17
|
-
function transformToUrl(
|
16
|
+
function transformToUrl(path3) {
|
18
17
|
const replacements = [
|
19
18
|
// Clean the url extensions
|
20
19
|
{ regex: /\.(ts|tsx|js|jsx|mjs|cjs)$/u, replacement: "" },
|
@@ -36,85 +35,82 @@ function transformToUrl(path) {
|
|
36
35
|
// remove index from end of path
|
37
36
|
{ regex: /\/?index$/, replacement: "" }
|
38
37
|
];
|
39
|
-
let url =
|
38
|
+
let url = path3;
|
40
39
|
for (const { regex, replacement } of replacements) {
|
41
40
|
url = url.replace(regex, replacement);
|
42
41
|
}
|
43
42
|
return url;
|
44
43
|
}
|
45
|
-
function getParamsCount(
|
46
|
-
return
|
44
|
+
function getParamsCount(path3) {
|
45
|
+
return path3.match(/\[(.*?)\]/gu)?.length || 0;
|
47
46
|
}
|
48
47
|
function sortByNestedParams(routes) {
|
49
48
|
return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
|
50
49
|
}
|
51
50
|
function fixSlashes(prefix) {
|
52
|
-
if (!prefix?.endsWith("/"))
|
53
|
-
return prefix;
|
51
|
+
if (!prefix?.endsWith("/")) return prefix;
|
54
52
|
return prefix.slice(0, -1);
|
55
53
|
}
|
56
54
|
|
57
55
|
// src/index.ts
|
58
56
|
var TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
|
59
57
|
var TYPES_TYPENAME_DEFAULT = "Routes";
|
60
|
-
function autoload(options = {}) {
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
}
|
77
|
-
});
|
78
|
-
const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
|
79
|
-
const files = await Array.fromAsync(
|
80
|
-
glob.scan({
|
81
|
-
cwd: directoryPath
|
82
|
-
})
|
83
|
-
);
|
84
|
-
const paths = [];
|
85
|
-
for await (const path of sortByNestedParams(files)) {
|
86
|
-
const fullPath = join2(directoryPath, path);
|
87
|
-
const file = await import(fullPath);
|
88
|
-
if (!file.default)
|
89
|
-
throw new Error(`${path} doesn't provide default export`);
|
90
|
-
const url = transformToUrl(path);
|
91
|
-
const groupOptions = schema ? schema({ path, url }) : {};
|
92
|
-
plugin.group(url, groupOptions, file.default);
|
93
|
-
if (types)
|
94
|
-
paths.push(fullPath.replace(directoryPath, ""));
|
58
|
+
async function autoload(options = {}) {
|
59
|
+
const fileSources = {};
|
60
|
+
const { pattern, dir, prefix, schema, types } = options;
|
61
|
+
const directoryPath = getPath(dir || "./routes");
|
62
|
+
if (!fs.existsSync(directoryPath))
|
63
|
+
throw new Error(`Directory ${directoryPath} doesn't exists`);
|
64
|
+
if (!fs.statSync(directoryPath).isDirectory())
|
65
|
+
throw new Error(`${directoryPath} isn't a directory`);
|
66
|
+
const plugin = new Elysia({
|
67
|
+
name: "elysia-autoload",
|
68
|
+
prefix: fixSlashes(prefix),
|
69
|
+
seed: {
|
70
|
+
pattern,
|
71
|
+
dir,
|
72
|
+
prefix,
|
73
|
+
types
|
95
74
|
}
|
96
|
-
|
97
|
-
|
98
|
-
|
75
|
+
});
|
76
|
+
const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
|
77
|
+
const files = await Array.fromAsync(
|
78
|
+
glob.scan({
|
79
|
+
cwd: directoryPath
|
80
|
+
})
|
81
|
+
);
|
82
|
+
const paths = [];
|
83
|
+
for await (const filePath of sortByNestedParams(files)) {
|
84
|
+
const fullPath = path2.join(directoryPath, filePath);
|
85
|
+
const file = await import(fullPath);
|
86
|
+
if (!file.default)
|
87
|
+
throw new Error(`${filePath} doesn't provide default export`);
|
88
|
+
const url = transformToUrl(filePath);
|
89
|
+
const groupOptions = schema ? schema({ path: filePath, url }) : {};
|
90
|
+
plugin.group(url, groupOptions, file.default);
|
91
|
+
if (types) paths.push(fullPath.replace(directoryPath, ""));
|
92
|
+
}
|
93
|
+
if (types) {
|
94
|
+
const imports = paths.map(
|
95
|
+
(x, index) => `import type Route${index} from "${(directoryPath + x.replace(".ts", "").replace(".tsx", "")).replace(/\\/gu, "/")}";`
|
96
|
+
);
|
97
|
+
for await (const outputPath of types === true || !types.output ? [TYPES_OUTPUT_DEFAULT] : Array.isArray(types.output) ? types.output : [types.output]) {
|
98
|
+
await Bun.write(
|
99
|
+
getPath(outputPath),
|
100
|
+
[
|
101
|
+
`import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
|
102
|
+
imports.join("\n"),
|
103
|
+
"",
|
104
|
+
types === true || !types.useExport ? "declare global {" : "",
|
105
|
+
` export type ${types === true || !types.typeName ? TYPES_TYPENAME_DEFAULT : types.typeName} = ${paths.map(
|
106
|
+
(x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ?? "") + transformToUrl(x) || "/"}", ReturnType<typeof Route${index}>>`
|
107
|
+
).join("\n & ")}`,
|
108
|
+
types === true || !types.useExport ? "}" : ""
|
109
|
+
].join("\n")
|
99
110
|
);
|
100
|
-
for await (const outputPath of types === true || !types.output ? [TYPES_OUTPUT_DEFAULT] : Array.isArray(types.output) ? types.output : [types.output]) {
|
101
|
-
await Bun.write(
|
102
|
-
getPath(outputPath),
|
103
|
-
[
|
104
|
-
`import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
|
105
|
-
imports.join("\n"),
|
106
|
-
"",
|
107
|
-
types === true || !types.useExport ? "declare global {" : "",
|
108
|
-
` export type ${types === true || !types.typeName ? TYPES_TYPENAME_DEFAULT : types.typeName} = ${paths.map(
|
109
|
-
(x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ?? "") + transformToUrl(x) || "/"}", ReturnType<typeof Route${index}>>`
|
110
|
-
).join("\n & ")}`,
|
111
|
-
types === true || !types.useExport ? "}" : ""
|
112
|
-
].join("\n")
|
113
|
-
);
|
114
|
-
}
|
115
111
|
}
|
116
|
-
|
117
|
-
|
112
|
+
}
|
113
|
+
return plugin;
|
118
114
|
}
|
119
115
|
export {
|
120
116
|
autoload
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "elysia-autoload",
|
3
|
-
"version": "0.
|
3
|
+
"version": "1.0.0",
|
4
4
|
"author": "kravetsone",
|
5
5
|
"type": "module",
|
6
6
|
"types": "./dist/index.d.ts",
|
@@ -32,14 +32,13 @@
|
|
32
32
|
],
|
33
33
|
"devDependencies": {
|
34
34
|
"@biomejs/biome": "1.6.3",
|
35
|
-
"@elysiajs/eden": "^1.0.
|
36
|
-
"@elysiajs/swagger": "^1.0.
|
37
|
-
"@
|
38
|
-
"
|
39
|
-
"elysia": "^1.0.9",
|
35
|
+
"@elysiajs/eden": "^1.0.14",
|
36
|
+
"@elysiajs/swagger": "^1.0.5",
|
37
|
+
"@types/bun": "^1.1.3",
|
38
|
+
"elysia": "^1.0.23",
|
40
39
|
"husky": "^9.0.11",
|
41
40
|
"tsup": "^8.0.2",
|
42
|
-
"typescript": "^5.4.
|
41
|
+
"typescript": "^5.4.5"
|
43
42
|
},
|
44
43
|
"peerDependencies": {
|
45
44
|
"elysia": "^1.0.0"
|