elysia-autoload 0.2.3 → 0.2.4

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/dist/index.cjs CHANGED
@@ -30,10 +30,8 @@ var import_elysia = require("elysia");
30
30
  // src/utils.ts
31
31
  var import_node_path = require("path");
32
32
  function getPath(dir) {
33
- if ((0, import_node_path.isAbsolute)(dir))
34
- return dir;
35
- if ((0, import_node_path.isAbsolute)(process.argv[1]))
36
- return (0, import_node_path.join)(process.argv[1], "..", dir);
33
+ if ((0, import_node_path.isAbsolute)(dir)) return dir;
34
+ if ((0, import_node_path.isAbsolute)(process.argv[1])) return (0, import_node_path.join)(process.argv[1], "..", dir);
37
35
  return (0, import_node_path.join)(process.cwd(), process.argv[1], "..", dir);
38
36
  }
39
37
  function transformToUrl(path) {
@@ -71,72 +69,68 @@ function sortByNestedParams(routes) {
71
69
  return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
72
70
  }
73
71
  function fixSlashes(prefix) {
74
- if (!prefix?.endsWith("/"))
75
- return prefix;
72
+ if (!prefix?.endsWith("/")) return prefix;
76
73
  return prefix.slice(0, -1);
77
74
  }
78
75
 
79
76
  // src/index.ts
80
77
  var TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
81
78
  var TYPES_TYPENAME_DEFAULT = "Routes";
82
- function autoload(options = {}) {
83
- return async () => {
84
- const { pattern, dir, prefix, schema, types } = options;
85
- const directoryPath = getPath(dir || "./routes");
86
- if (!(0, import_node_fs.existsSync)(directoryPath))
87
- throw new Error(`Directory ${directoryPath} doesn't exists`);
88
- if (!(0, import_node_fs.statSync)(directoryPath).isDirectory())
89
- throw new Error(`${directoryPath} isn't a directory`);
90
- const plugin = new import_elysia.Elysia({
91
- name: "elysia-autoload",
92
- prefix: fixSlashes(prefix),
93
- seed: {
94
- pattern,
95
- dir,
96
- prefix,
97
- types
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, ""));
79
+ async function autoload(options = {}) {
80
+ const { pattern, dir, prefix, schema, types } = options;
81
+ const directoryPath = getPath(dir || "./routes");
82
+ if (!(0, import_node_fs.existsSync)(directoryPath))
83
+ throw new Error(`Directory ${directoryPath} doesn't exists`);
84
+ if (!(0, import_node_fs.statSync)(directoryPath).isDirectory())
85
+ throw new Error(`${directoryPath} isn't a directory`);
86
+ const plugin = new import_elysia.Elysia({
87
+ name: "elysia-autoload",
88
+ prefix: fixSlashes(prefix),
89
+ seed: {
90
+ pattern,
91
+ dir,
92
+ prefix,
93
+ types
117
94
  }
118
- if (types) {
119
- const imports = paths.map(
120
- (x, index) => `import type Route${index} from "${(directoryPath + x.replace(".ts", "").replace(".tsx", "")).replace(/\\/gu, "/")}";`
95
+ });
96
+ const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
97
+ const files = await Array.fromAsync(
98
+ glob.scan({
99
+ cwd: directoryPath
100
+ })
101
+ );
102
+ const paths = [];
103
+ for await (const path of sortByNestedParams(files)) {
104
+ const fullPath = (0, import_node_path2.join)(directoryPath, path);
105
+ const file = await import(fullPath);
106
+ if (!file.default)
107
+ throw new Error(`${path} doesn't provide default export`);
108
+ const url = transformToUrl(path);
109
+ const groupOptions = schema ? schema({ path, url }) : {};
110
+ plugin.group(url, groupOptions, file.default);
111
+ if (types) paths.push(fullPath.replace(directoryPath, ""));
112
+ }
113
+ if (types) {
114
+ const imports = paths.map(
115
+ (x, index) => `import type Route${index} from "${(directoryPath + x.replace(".ts", "").replace(".tsx", "")).replace(/\\/gu, "/")}";`
116
+ );
117
+ for await (const outputPath of types === true || !types.output ? [TYPES_OUTPUT_DEFAULT] : Array.isArray(types.output) ? types.output : [types.output]) {
118
+ await Bun.write(
119
+ getPath(outputPath),
120
+ [
121
+ `import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
122
+ imports.join("\n"),
123
+ "",
124
+ types === true || !types.useExport ? "declare global {" : "",
125
+ ` export type ${types === true || !types.typeName ? TYPES_TYPENAME_DEFAULT : types.typeName} = ${paths.map(
126
+ (x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ?? "") + transformToUrl(x) || "/"}", ReturnType<typeof Route${index}>>`
127
+ ).join("\n & ")}`,
128
+ types === true || !types.useExport ? "}" : ""
129
+ ].join("\n")
121
130
  );
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
131
  }
138
- return plugin;
139
- };
132
+ }
133
+ return plugin;
140
134
  }
141
135
  // Annotate the CommonJS export names for ESM import in node:
142
136
  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): () => Promise<Elysia$1<string, false, {
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): () => Promise<Elysia$1<string, false, {
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
@@ -8,10 +8,8 @@ import {
8
8
  // src/utils.ts
9
9
  import { isAbsolute, join } from "node:path";
10
10
  function getPath(dir) {
11
- if (isAbsolute(dir))
12
- return dir;
13
- if (isAbsolute(process.argv[1]))
14
- return join(process.argv[1], "..", dir);
11
+ if (isAbsolute(dir)) return dir;
12
+ if (isAbsolute(process.argv[1])) return join(process.argv[1], "..", dir);
15
13
  return join(process.cwd(), process.argv[1], "..", dir);
16
14
  }
17
15
  function transformToUrl(path) {
@@ -49,72 +47,68 @@ function sortByNestedParams(routes) {
49
47
  return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
50
48
  }
51
49
  function fixSlashes(prefix) {
52
- if (!prefix?.endsWith("/"))
53
- return prefix;
50
+ if (!prefix?.endsWith("/")) return prefix;
54
51
  return prefix.slice(0, -1);
55
52
  }
56
53
 
57
54
  // src/index.ts
58
55
  var TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
59
56
  var TYPES_TYPENAME_DEFAULT = "Routes";
60
- function autoload(options = {}) {
61
- return async () => {
62
- const { pattern, dir, prefix, schema, types } = options;
63
- const directoryPath = getPath(dir || "./routes");
64
- if (!existsSync(directoryPath))
65
- throw new Error(`Directory ${directoryPath} doesn't exists`);
66
- if (!statSync(directoryPath).isDirectory())
67
- throw new Error(`${directoryPath} isn't a directory`);
68
- const plugin = new Elysia({
69
- name: "elysia-autoload",
70
- prefix: fixSlashes(prefix),
71
- seed: {
72
- pattern,
73
- dir,
74
- prefix,
75
- types
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, ""));
57
+ async function autoload(options = {}) {
58
+ const { pattern, dir, prefix, schema, types } = options;
59
+ const directoryPath = getPath(dir || "./routes");
60
+ if (!existsSync(directoryPath))
61
+ throw new Error(`Directory ${directoryPath} doesn't exists`);
62
+ if (!statSync(directoryPath).isDirectory())
63
+ throw new Error(`${directoryPath} isn't a directory`);
64
+ const plugin = new Elysia({
65
+ name: "elysia-autoload",
66
+ prefix: fixSlashes(prefix),
67
+ seed: {
68
+ pattern,
69
+ dir,
70
+ prefix,
71
+ types
95
72
  }
96
- if (types) {
97
- const imports = paths.map(
98
- (x, index) => `import type Route${index} from "${(directoryPath + x.replace(".ts", "").replace(".tsx", "")).replace(/\\/gu, "/")}";`
73
+ });
74
+ const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
75
+ const files = await Array.fromAsync(
76
+ glob.scan({
77
+ cwd: directoryPath
78
+ })
79
+ );
80
+ const paths = [];
81
+ for await (const path of sortByNestedParams(files)) {
82
+ const fullPath = join2(directoryPath, path);
83
+ const file = await import(fullPath);
84
+ if (!file.default)
85
+ throw new Error(`${path} doesn't provide default export`);
86
+ const url = transformToUrl(path);
87
+ const groupOptions = schema ? schema({ path, url }) : {};
88
+ plugin.group(url, groupOptions, file.default);
89
+ if (types) paths.push(fullPath.replace(directoryPath, ""));
90
+ }
91
+ if (types) {
92
+ const imports = paths.map(
93
+ (x, index) => `import type Route${index} from "${(directoryPath + x.replace(".ts", "").replace(".tsx", "")).replace(/\\/gu, "/")}";`
94
+ );
95
+ for await (const outputPath of types === true || !types.output ? [TYPES_OUTPUT_DEFAULT] : Array.isArray(types.output) ? types.output : [types.output]) {
96
+ await Bun.write(
97
+ getPath(outputPath),
98
+ [
99
+ `import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
100
+ imports.join("\n"),
101
+ "",
102
+ types === true || !types.useExport ? "declare global {" : "",
103
+ ` export type ${types === true || !types.typeName ? TYPES_TYPENAME_DEFAULT : types.typeName} = ${paths.map(
104
+ (x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ?? "") + transformToUrl(x) || "/"}", ReturnType<typeof Route${index}>>`
105
+ ).join("\n & ")}`,
106
+ types === true || !types.useExport ? "}" : ""
107
+ ].join("\n")
99
108
  );
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
109
  }
116
- return plugin;
117
- };
110
+ }
111
+ return plugin;
118
112
  }
119
113
  export {
120
114
  autoload
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elysia-autoload",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
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.7",
36
- "@elysiajs/swagger": "^1.0.3",
37
- "@microsoft/api-extractor": "^7.43.0",
38
- "@types/bun": "^1.0.11",
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.3"
41
+ "typescript": "^5.4.5"
43
42
  },
44
43
  "peerDependencies": {
45
44
  "elysia": "^1.0.0"