elysia-autoload 1.0.0 → 1.0.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 CHANGED
@@ -28,11 +28,16 @@ bun install elysia-autoload
28
28
  import { Elysia } from "elysia";
29
29
  import { autoload } from "elysia-autoload";
30
30
 
31
- const app = new Elysia().use(autoload()).listen(3000);
31
+ const app = new Elysia().use(await autoload()).listen(3000);
32
32
 
33
33
  export type ElysiaApp = typeof app;
34
34
  ```
35
35
 
36
+ > [!IMPORTANT]
37
+ > We strictly recommend use `await` when registering plugin
38
+ >
39
+ > Read more about [Lazy-load plugins](https://elysiajs.com/patterns/lazy-loading-module.html)
40
+
36
41
  ## Create route
37
42
 
38
43
  ```ts
@@ -99,7 +104,7 @@ import { autoload } from "elysia-autoload";
99
104
 
100
105
  const app = new Elysia()
101
106
  .use(
102
- autoload({
107
+ await autoload({
103
108
  types: {
104
109
  output: "./routes.ts",
105
110
  typeName: "Routes",
@@ -129,8 +134,25 @@ const { data } = await app.test["some-path-param"].get({
129
134
  console.log(data);
130
135
  ```
131
136
 
137
+ `routes.ts` will be:
138
+
139
+ ```ts
140
+ // @filename: routes.ts
141
+
142
+ import type { ElysiaWithBaseUrl } from "elysia-autoload";
143
+ import type Route0 from "./routes/index";
144
+ import type Route1 from "./routes/test/[some]/index";
145
+
146
+ declare global {
147
+ export type Routes = ElysiaWithBaseUrl<"/api", ReturnType<typeof Route0>> &
148
+ ElysiaWithBaseUrl<"/api/test/:some", ReturnType<typeof Route1>>;
149
+ }
150
+ ```
151
+
132
152
  Example of app with types code-generation you can see in [example](https://github.com/kravetsone/elysia-autoload/tree/main/example)
133
153
 
154
+ **Currently, Eden types generation is broken!!**
155
+
134
156
  ### Bun build usage
135
157
 
136
158
  You can use this plugin with `Bun.build`, thanks to [esbuild-plugin-autoload](https://github.com/kravetsone/esbuild-plugin-autoload)!
@@ -159,7 +181,7 @@ import { autoload } from "elysia-autoload";
159
181
 
160
182
  const app = new Elysia()
161
183
  .use(
162
- autoload({
184
+ await autoload({
163
185
  schema: ({ path, url }) => {
164
186
  const tag = url.split("/").at(1)!;
165
187
 
package/dist/index.cjs CHANGED
@@ -40,7 +40,8 @@ var import_elysia = require("elysia");
40
40
  // src/utils.ts
41
41
  var import_node_path = __toESM(require("path"), 1);
42
42
  function getPath(dir) {
43
- if (import_node_path.default.isAbsolute(dir)) return dir;
43
+ if (import_node_path.default.isAbsolute(dir))
44
+ return dir;
44
45
  if (import_node_path.default.isAbsolute(process.argv[1]))
45
46
  return import_node_path.default.join(process.argv[1], "..", dir);
46
47
  return import_node_path.default.join(process.cwd(), process.argv[1], "..", dir);
@@ -80,17 +81,35 @@ function sortByNestedParams(routes) {
80
81
  return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
81
82
  }
82
83
  function fixSlashes(prefix) {
83
- if (!prefix?.endsWith("/")) return prefix;
84
+ if (!prefix?.endsWith("/"))
85
+ return prefix;
84
86
  return prefix.slice(0, -1);
85
87
  }
88
+ function addRelativeIfNotDot(path3) {
89
+ if (path3.at(0) !== ".")
90
+ return `./${path3}`;
91
+ return path3;
92
+ }
86
93
 
87
94
  // src/index.ts
95
+ var DIR_ROUTES_DEFAULT = "./routes";
88
96
  var TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
89
97
  var TYPES_TYPENAME_DEFAULT = "Routes";
98
+ var TYPES_OBJECT_DEFAULT = {
99
+ output: [TYPES_OUTPUT_DEFAULT],
100
+ typeName: TYPES_TYPENAME_DEFAULT
101
+ };
90
102
  async function autoload(options = {}) {
91
103
  const fileSources = {};
92
- const { pattern, dir, prefix, schema, types } = options;
93
- const directoryPath = getPath(dir || "./routes");
104
+ const { pattern, prefix, schema } = options;
105
+ const dir = options.dir ?? DIR_ROUTES_DEFAULT;
106
+ const types = options.types && options.types !== true ? {
107
+ ...TYPES_OBJECT_DEFAULT,
108
+ ...options.types,
109
+ // This code allows you to omit the output data or specify it as an string[] or string.
110
+ output: !options.types.output ? [TYPES_OUTPUT_DEFAULT] : Array.isArray(options.types.output) ? options.types.output : [options.types.output]
111
+ } : TYPES_OBJECT_DEFAULT;
112
+ const directoryPath = getPath(dir);
94
113
  if (!import_node_fs.default.existsSync(directoryPath))
95
114
  throw new Error(`Directory ${directoryPath} doesn't exists`);
96
115
  if (!import_node_fs.default.statSync(directoryPath).isDirectory())
@@ -120,24 +139,31 @@ async function autoload(options = {}) {
120
139
  const url = transformToUrl(filePath);
121
140
  const groupOptions = schema ? schema({ path: filePath, url }) : {};
122
141
  plugin.group(url, groupOptions, file.default);
123
- if (types) paths.push(fullPath.replace(directoryPath, ""));
142
+ if (types)
143
+ paths.push(fullPath.replace(directoryPath, ""));
124
144
  }
125
145
  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]) {
146
+ for await (const outputPath of types.output) {
147
+ const outputAbsolutePath = getPath(outputPath);
148
+ const imports = paths.map(
149
+ (x, index) => `import type Route${index} from "${addRelativeIfNotDot(
150
+ import_node_path2.default.relative(
151
+ import_node_path2.default.dirname(outputAbsolutePath),
152
+ directoryPath + x.replace(".ts", "").replace(".tsx", "")
153
+ ).replace(/\\/gu, "/")
154
+ )}";`
155
+ );
130
156
  await Bun.write(
131
- getPath(outputPath),
157
+ outputAbsolutePath,
132
158
  [
133
159
  `import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
134
160
  imports.join("\n"),
135
161
  "",
136
- types === true || !types.useExport ? "declare global {" : "",
137
- ` export type ${types === true || !types.typeName ? TYPES_TYPENAME_DEFAULT : types.typeName} = ${paths.map(
162
+ !types.useExport ? "declare global {" : "",
163
+ ` export type ${types.typeName} = ${paths.map(
138
164
  (x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ?? "") + transformToUrl(x) || "/"}", ReturnType<typeof Route${index}>>`
139
165
  ).join("\n & ")}`,
140
- types === true || !types.useExport ? "}" : ""
166
+ !types.useExport ? "}" : ""
141
167
  ].join("\n")
142
168
  );
143
169
  }
package/dist/index.js CHANGED
@@ -8,7 +8,8 @@ import {
8
8
  // src/utils.ts
9
9
  import path from "node:path";
10
10
  function getPath(dir) {
11
- if (path.isAbsolute(dir)) return dir;
11
+ if (path.isAbsolute(dir))
12
+ return dir;
12
13
  if (path.isAbsolute(process.argv[1]))
13
14
  return path.join(process.argv[1], "..", dir);
14
15
  return path.join(process.cwd(), process.argv[1], "..", dir);
@@ -48,17 +49,35 @@ function sortByNestedParams(routes) {
48
49
  return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
49
50
  }
50
51
  function fixSlashes(prefix) {
51
- if (!prefix?.endsWith("/")) return prefix;
52
+ if (!prefix?.endsWith("/"))
53
+ return prefix;
52
54
  return prefix.slice(0, -1);
53
55
  }
56
+ function addRelativeIfNotDot(path3) {
57
+ if (path3.at(0) !== ".")
58
+ return `./${path3}`;
59
+ return path3;
60
+ }
54
61
 
55
62
  // src/index.ts
63
+ var DIR_ROUTES_DEFAULT = "./routes";
56
64
  var TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
57
65
  var TYPES_TYPENAME_DEFAULT = "Routes";
66
+ var TYPES_OBJECT_DEFAULT = {
67
+ output: [TYPES_OUTPUT_DEFAULT],
68
+ typeName: TYPES_TYPENAME_DEFAULT
69
+ };
58
70
  async function autoload(options = {}) {
59
71
  const fileSources = {};
60
- const { pattern, dir, prefix, schema, types } = options;
61
- const directoryPath = getPath(dir || "./routes");
72
+ const { pattern, prefix, schema } = options;
73
+ const dir = options.dir ?? DIR_ROUTES_DEFAULT;
74
+ const types = options.types && options.types !== true ? {
75
+ ...TYPES_OBJECT_DEFAULT,
76
+ ...options.types,
77
+ // This code allows you to omit the output data or specify it as an string[] or string.
78
+ output: !options.types.output ? [TYPES_OUTPUT_DEFAULT] : Array.isArray(options.types.output) ? options.types.output : [options.types.output]
79
+ } : TYPES_OBJECT_DEFAULT;
80
+ const directoryPath = getPath(dir);
62
81
  if (!fs.existsSync(directoryPath))
63
82
  throw new Error(`Directory ${directoryPath} doesn't exists`);
64
83
  if (!fs.statSync(directoryPath).isDirectory())
@@ -88,24 +107,31 @@ async function autoload(options = {}) {
88
107
  const url = transformToUrl(filePath);
89
108
  const groupOptions = schema ? schema({ path: filePath, url }) : {};
90
109
  plugin.group(url, groupOptions, file.default);
91
- if (types) paths.push(fullPath.replace(directoryPath, ""));
110
+ if (types)
111
+ paths.push(fullPath.replace(directoryPath, ""));
92
112
  }
93
113
  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]) {
114
+ for await (const outputPath of types.output) {
115
+ const outputAbsolutePath = getPath(outputPath);
116
+ const imports = paths.map(
117
+ (x, index) => `import type Route${index} from "${addRelativeIfNotDot(
118
+ path2.relative(
119
+ path2.dirname(outputAbsolutePath),
120
+ directoryPath + x.replace(".ts", "").replace(".tsx", "")
121
+ ).replace(/\\/gu, "/")
122
+ )}";`
123
+ );
98
124
  await Bun.write(
99
- getPath(outputPath),
125
+ outputAbsolutePath,
100
126
  [
101
127
  `import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
102
128
  imports.join("\n"),
103
129
  "",
104
- types === true || !types.useExport ? "declare global {" : "",
105
- ` export type ${types === true || !types.typeName ? TYPES_TYPENAME_DEFAULT : types.typeName} = ${paths.map(
130
+ !types.useExport ? "declare global {" : "",
131
+ ` export type ${types.typeName} = ${paths.map(
106
132
  (x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ?? "") + transformToUrl(x) || "/"}", ReturnType<typeof Route${index}>>`
107
133
  ).join("\n & ")}`,
108
- types === true || !types.useExport ? "}" : ""
134
+ !types.useExport ? "}" : ""
109
135
  ].join("\n")
110
136
  );
111
137
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elysia-autoload",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "author": "kravetsone",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",