elysia-autoload 1.0.1 → 1.1.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 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) with [`Bun.build`](https://bun.sh/docs/bundler) support!
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`](#bun-build-usage) support!
4
4
 
5
5
  **Currently, Eden types generation is broken!!**
6
6
 
@@ -153,9 +153,9 @@ Example of app with types code-generation you can see in [example](https://githu
153
153
 
154
154
  **Currently, Eden types generation is broken!!**
155
155
 
156
- ### Bun build usage
156
+ ### [Bun build](https://bun.sh/docs/bundler) usage
157
157
 
158
- You can use this plugin with `Bun.build`, thanks to [esbuild-plugin-autoload](https://github.com/kravetsone/esbuild-plugin-autoload)!
158
+ You can use this plugin with [`Bun.build`](https://bun.sh/docs/bundler), thanks to [esbuild-plugin-autoload](https://github.com/kravetsone/esbuild-plugin-autoload)!
159
159
 
160
160
  ```ts
161
161
  // @filename: build.ts
@@ -163,6 +163,7 @@ import { autoload } from "esbuild-plugin-autoload"; // default import also suppo
163
163
 
164
164
  await Bun.build({
165
165
  entrypoints: ["src/index.ts"],
166
+ target: "bun",
166
167
  outdir: "out",
167
168
  plugins: [autoload()],
168
169
  }).then(console.log);
@@ -170,6 +171,26 @@ await Bun.build({
170
171
 
171
172
  Then, build it with `bun build.ts` and run with `bun out/index.ts`.
172
173
 
174
+ ### [Bun compile](https://bun.sh/docs/bundler/executables) usage
175
+
176
+ You can bundle and then compile it into a [single executable binary file](https://bun.sh/docs/bundler/executables)
177
+
178
+ ```ts
179
+ import { autoload } from "esbuild-plugin-autoload"; // default import also supported
180
+
181
+ await Bun.build({
182
+ entrypoints: ["src/index.ts"],
183
+ target: "bun",
184
+ outdir: "out",
185
+ plugins: [autoload()],
186
+ }).then(console.log);
187
+
188
+ await Bun.$`bun build --compile out/index.js`;
189
+ ```
190
+
191
+ > [!WARNING]
192
+ > You cannot use it in `bun build --compile` mode without extra step ([Feature issue](https://github.com/oven-sh/bun/issues/11895))
193
+
173
194
  [Read more](https://github.com/kravetsone/esbuild-plugin-autoload)
174
195
 
175
196
  ### Usage of schema handler
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import Elysia, { RouteBase, Elysia as Elysia$1, LocalHook, InputSchema, RouteSchema, SingletonBase } from 'elysia';
2
- import { BaseMacro } from 'elysia/dist/types';
1
+ import Elysia, { RouteBase, LocalHook, InputSchema, RouteSchema, SingletonBase, BaseMacro, Elysia as Elysia$1 } from 'elysia';
3
2
 
4
3
  type RemoveLastChar<T extends string> = T extends `${infer V}/` ? V : T;
5
4
  type RoutesWithPrefix<Routes extends RouteBase, Prefix extends string> = {
@@ -34,6 +33,7 @@ declare function autoload(options?: IAutoloadOptions): Promise<Elysia$1<string,
34
33
  }, {
35
34
  schema: {};
36
35
  macro: {};
36
+ macroFn: {};
37
37
  }, {}, {
38
38
  derive: {};
39
39
  resolve: {};
@@ -44,4 +44,4 @@ declare function autoload(options?: IAutoloadOptions): Promise<Elysia$1<string,
44
44
  schema: {};
45
45
  }>>;
46
46
 
47
- export { type ElysiaWithBaseUrl, type IAutoloadOptions, type ITypesOptions, autoload };
47
+ export { type ElysiaWithBaseUrl, type IAutoloadOptions, type ITypesOptions, type TSchemaHandler, autoload };
package/dist/index.js CHANGED
@@ -1,12 +1,7 @@
1
- // src/index.ts
2
- import fs from "node:fs";
3
- import path2 from "node:path";
4
- import {
5
- Elysia
6
- } from "elysia";
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { Elysia } from 'elysia';
7
4
 
8
- // src/utils.ts
9
- import path from "node:path";
10
5
  function getPath(dir) {
11
6
  if (path.isAbsolute(dir))
12
7
  return dir;
@@ -14,7 +9,7 @@ function getPath(dir) {
14
9
  return path.join(process.argv[1], "..", dir);
15
10
  return path.join(process.cwd(), process.argv[1], "..", dir);
16
11
  }
17
- function transformToUrl(path3) {
12
+ function transformToUrl(path2) {
18
13
  const replacements = [
19
14
  // Clean the url extensions
20
15
  { regex: /\.(ts|tsx|js|jsx|mjs|cjs)$/u, replacement: "" },
@@ -36,14 +31,14 @@ function transformToUrl(path3) {
36
31
  // remove index from end of path
37
32
  { regex: /\/?index$/, replacement: "" }
38
33
  ];
39
- let url = path3;
34
+ let url = path2;
40
35
  for (const { regex, replacement } of replacements) {
41
36
  url = url.replace(regex, replacement);
42
37
  }
43
38
  return url;
44
39
  }
45
- function getParamsCount(path3) {
46
- return path3.match(/\[(.*?)\]/gu)?.length || 0;
40
+ function getParamsCount(path2) {
41
+ return path2.match(/\[(.*?)\]/gu)?.length || 0;
47
42
  }
48
43
  function sortByNestedParams(routes) {
49
44
  return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
@@ -53,30 +48,28 @@ function fixSlashes(prefix) {
53
48
  return prefix;
54
49
  return prefix.slice(0, -1);
55
50
  }
56
- function addRelativeIfNotDot(path3) {
57
- if (path3.at(0) !== ".")
58
- return `./${path3}`;
59
- return path3;
51
+ function addRelativeIfNotDot(path2) {
52
+ if (path2.at(0) !== ".")
53
+ return `./${path2}`;
54
+ return path2;
60
55
  }
61
56
 
62
- // src/index.ts
63
- var DIR_ROUTES_DEFAULT = "./routes";
64
- var TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
65
- var TYPES_TYPENAME_DEFAULT = "Routes";
66
- var TYPES_OBJECT_DEFAULT = {
57
+ const DIR_ROUTES_DEFAULT = "./routes";
58
+ const TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
59
+ const TYPES_TYPENAME_DEFAULT = "Routes";
60
+ const TYPES_OBJECT_DEFAULT = {
67
61
  output: [TYPES_OUTPUT_DEFAULT],
68
62
  typeName: TYPES_TYPENAME_DEFAULT
69
63
  };
70
64
  async function autoload(options = {}) {
71
- const fileSources = {};
72
65
  const { pattern, prefix, schema } = options;
73
66
  const dir = options.dir ?? DIR_ROUTES_DEFAULT;
74
- const types = options.types && options.types !== true ? {
67
+ const types = options.types ? options.types !== true ? {
75
68
  ...TYPES_OBJECT_DEFAULT,
76
69
  ...options.types,
77
70
  // This code allows you to omit the output data or specify it as an string[] or string.
78
71
  output: !options.types.output ? [TYPES_OUTPUT_DEFAULT] : Array.isArray(options.types.output) ? options.types.output : [options.types.output]
79
- } : TYPES_OBJECT_DEFAULT;
72
+ } : TYPES_OBJECT_DEFAULT : false;
80
73
  const directoryPath = getPath(dir);
81
74
  if (!fs.existsSync(directoryPath))
82
75
  throw new Error(`Directory ${directoryPath} doesn't exists`);
@@ -100,7 +93,7 @@ async function autoload(options = {}) {
100
93
  );
101
94
  const paths = [];
102
95
  for await (const filePath of sortByNestedParams(files)) {
103
- const fullPath = path2.join(directoryPath, filePath);
96
+ const fullPath = path.join(directoryPath, filePath);
104
97
  const file = await import(fullPath);
105
98
  if (!file.default)
106
99
  throw new Error(`${filePath} doesn't provide default export`);
@@ -115,8 +108,8 @@ async function autoload(options = {}) {
115
108
  const outputAbsolutePath = getPath(outputPath);
116
109
  const imports = paths.map(
117
110
  (x, index) => `import type Route${index} from "${addRelativeIfNotDot(
118
- path2.relative(
119
- path2.dirname(outputAbsolutePath),
111
+ path.relative(
112
+ path.dirname(outputAbsolutePath),
120
113
  directoryPath + x.replace(".ts", "").replace(".tsx", "")
121
114
  ).replace(/\\/gu, "/")
122
115
  )}";`
@@ -138,6 +131,5 @@ async function autoload(options = {}) {
138
131
  }
139
132
  return plugin;
140
133
  }
141
- export {
142
- autoload
143
- };
134
+
135
+ export { autoload };
package/package.json CHANGED
@@ -1,12 +1,17 @@
1
1
  {
2
2
  "name": "elysia-autoload",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "author": "kravetsone",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
- "main": "./dist/index.cjs",
8
7
  "module": "./dist/index.js",
9
- "description": "Plugin for Elysia which autoload all routes in directory and code-generate types for Eden",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "description": "Plugin for Elysia which autoload all routes in directory and code-generate types for Eden with Bun.build support",
10
15
  "homepage": "https://github.com/kravetsone/elysia-autoload",
11
16
  "keywords": [
12
17
  "bun",
@@ -22,7 +27,7 @@
22
27
  "codegeneration"
23
28
  ],
24
29
  "scripts": {
25
- "prepublishOnly": "bun test && bunx tsup && rm -f ./dist/index.d.сts",
30
+ "prepublishOnly": "bun test && bunx pkgroll",
26
31
  "lint": "bunx @biomejs/biome check src",
27
32
  "lint:fix": "bun lint --apply",
28
33
  "prepare": "bunx husky"
@@ -31,16 +36,15 @@
31
36
  "dist"
32
37
  ],
33
38
  "devDependencies": {
34
- "@biomejs/biome": "1.6.3",
35
- "@elysiajs/eden": "^1.0.14",
36
- "@elysiajs/swagger": "^1.0.5",
37
- "@types/bun": "^1.1.3",
38
- "elysia": "^1.0.23",
39
- "husky": "^9.0.11",
40
- "tsup": "^8.0.2",
41
- "typescript": "^5.4.5"
39
+ "@biomejs/biome": "1.8.3",
40
+ "@elysiajs/eden": "^1.1.0",
41
+ "@elysiajs/swagger": "^1.1.0",
42
+ "@types/bun": "^1.1.6",
43
+ "elysia": "^1.1.2",
44
+ "pkgroll": "^2.1.1",
45
+ "typescript": "^5.5.3"
42
46
  },
43
47
  "peerDependencies": {
44
- "elysia": "^1.0.0"
48
+ "elysia": "^1.1.0"
45
49
  }
46
50
  }
package/dist/index.cjs DELETED
@@ -1,176 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
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
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- autoload: () => autoload
34
- });
35
- module.exports = __toCommonJS(src_exports);
36
- var import_node_fs = __toESM(require("fs"), 1);
37
- var import_node_path2 = __toESM(require("path"), 1);
38
- var import_elysia = require("elysia");
39
-
40
- // src/utils.ts
41
- var import_node_path = __toESM(require("path"), 1);
42
- function getPath(dir) {
43
- if (import_node_path.default.isAbsolute(dir))
44
- return dir;
45
- if (import_node_path.default.isAbsolute(process.argv[1]))
46
- return import_node_path.default.join(process.argv[1], "..", dir);
47
- return import_node_path.default.join(process.cwd(), process.argv[1], "..", dir);
48
- }
49
- function transformToUrl(path3) {
50
- const replacements = [
51
- // Clean the url extensions
52
- { regex: /\.(ts|tsx|js|jsx|mjs|cjs)$/u, replacement: "" },
53
- // Fix windows slashes
54
- { regex: /\\/gu, replacement: "/" },
55
- // Handle wild card based routes - users/[...id]/profile.ts -> users/*/profile
56
- { regex: /\[\.\.\..*\]/gu, replacement: "*" },
57
- // Handle generic square bracket based routes - users/[id]/index.ts -> users/:id
58
- {
59
- regex: /\[(.*?)\]/gu,
60
- replacement: (_, match) => `:${match}`
61
- },
62
- // Handle the case when multiple parameters are present in one file
63
- // users / [id] - [name].ts to users /: id -:name and users / [id] - [name] / [age].ts to users /: id -: name /: age
64
- { regex: /\]-\[/gu, replacement: "-:" },
65
- { regex: /\]\//gu, replacement: "/" },
66
- { regex: /\[/gu, replacement: "" },
67
- { regex: /\]/gu, replacement: "" },
68
- // remove index from end of path
69
- { regex: /\/?index$/, replacement: "" }
70
- ];
71
- let url = path3;
72
- for (const { regex, replacement } of replacements) {
73
- url = url.replace(regex, replacement);
74
- }
75
- return url;
76
- }
77
- function getParamsCount(path3) {
78
- return path3.match(/\[(.*?)\]/gu)?.length || 0;
79
- }
80
- function sortByNestedParams(routes) {
81
- return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
82
- }
83
- function fixSlashes(prefix) {
84
- if (!prefix?.endsWith("/"))
85
- return prefix;
86
- return prefix.slice(0, -1);
87
- }
88
- function addRelativeIfNotDot(path3) {
89
- if (path3.at(0) !== ".")
90
- return `./${path3}`;
91
- return path3;
92
- }
93
-
94
- // src/index.ts
95
- var DIR_ROUTES_DEFAULT = "./routes";
96
- var TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
97
- var TYPES_TYPENAME_DEFAULT = "Routes";
98
- var TYPES_OBJECT_DEFAULT = {
99
- output: [TYPES_OUTPUT_DEFAULT],
100
- typeName: TYPES_TYPENAME_DEFAULT
101
- };
102
- async function autoload(options = {}) {
103
- const fileSources = {};
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);
113
- if (!import_node_fs.default.existsSync(directoryPath))
114
- throw new Error(`Directory ${directoryPath} doesn't exists`);
115
- if (!import_node_fs.default.statSync(directoryPath).isDirectory())
116
- throw new Error(`${directoryPath} isn't a directory`);
117
- const plugin = new import_elysia.Elysia({
118
- name: "elysia-autoload",
119
- prefix: fixSlashes(prefix),
120
- seed: {
121
- pattern,
122
- dir,
123
- prefix,
124
- types
125
- }
126
- });
127
- const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
128
- const files = await Array.fromAsync(
129
- glob.scan({
130
- cwd: directoryPath
131
- })
132
- );
133
- const paths = [];
134
- for await (const filePath of sortByNestedParams(files)) {
135
- const fullPath = import_node_path2.default.join(directoryPath, filePath);
136
- const file = await import(fullPath);
137
- if (!file.default)
138
- throw new Error(`${filePath} doesn't provide default export`);
139
- const url = transformToUrl(filePath);
140
- const groupOptions = schema ? schema({ path: filePath, url }) : {};
141
- plugin.group(url, groupOptions, file.default);
142
- if (types)
143
- paths.push(fullPath.replace(directoryPath, ""));
144
- }
145
- if (types) {
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
- );
156
- await Bun.write(
157
- outputAbsolutePath,
158
- [
159
- `import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
160
- imports.join("\n"),
161
- "",
162
- !types.useExport ? "declare global {" : "",
163
- ` export type ${types.typeName} = ${paths.map(
164
- (x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ?? "") + transformToUrl(x) || "/"}", ReturnType<typeof Route${index}>>`
165
- ).join("\n & ")}`,
166
- !types.useExport ? "}" : ""
167
- ].join("\n")
168
- );
169
- }
170
- }
171
- return plugin;
172
- }
173
- // Annotate the CommonJS export names for ESM import in node:
174
- 0 && (module.exports = {
175
- autoload
176
- });
package/dist/index.d.cts DELETED
@@ -1,47 +0,0 @@
1
- import Elysia, { RouteBase, Elysia as Elysia$1, LocalHook, InputSchema, RouteSchema, SingletonBase } from 'elysia';
2
- import { BaseMacro } from 'elysia/dist/types';
3
-
4
- type RemoveLastChar<T extends string> = T extends `${infer V}/` ? V : T;
5
- type RoutesWithPrefix<Routes extends RouteBase, Prefix extends string> = {
6
- [K in keyof Routes as `${Prefix}${RemoveLastChar<K & string>}`]: Routes[K];
7
- };
8
- 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;
9
-
10
- type TSchemaHandler = ({ path, url, }: {
11
- path: string;
12
- url: string;
13
- }) => LocalHook<InputSchema, RouteSchema, SingletonBase, Record<string, Error>, BaseMacro, "">;
14
- interface ITypesOptions {
15
- output?: string | string[];
16
- typeName?: string;
17
- useExport?: boolean;
18
- }
19
- interface IAutoloadOptions {
20
- pattern?: string;
21
- dir?: string;
22
- prefix?: string;
23
- schema?: TSchemaHandler;
24
- types?: ITypesOptions | true;
25
- }
26
- declare function autoload(options?: IAutoloadOptions): Promise<Elysia$1<string, false, {
27
- decorator: {};
28
- store: {};
29
- derive: {};
30
- resolve: {};
31
- }, {
32
- type: {};
33
- error: {};
34
- }, {
35
- schema: {};
36
- macro: {};
37
- }, {}, {
38
- derive: {};
39
- resolve: {};
40
- schema: {};
41
- }, {
42
- derive: {};
43
- resolve: {};
44
- schema: {};
45
- }>>;
46
-
47
- export { type ElysiaWithBaseUrl, type IAutoloadOptions, type ITypesOptions, autoload };