elysia-autoload 0.1.4 → 0.1.6

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,147 +1,162 @@
1
- # elysia-autoload
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)
4
-
5
- ## Installation
6
-
7
- ```bash
8
- bun install elysia-autoload
9
- ```
10
-
11
- ## Usage
12
-
13
- ## Register the plugin
14
-
15
- ```ts
16
- import { Elysia } from "elysia";
17
- import { autoload } from "elysia-autoload";
18
-
19
- const app = new Elysia().use(autoload()).listen(3000);
20
-
21
- export type ElysiaApp = typeof app;
22
- ```
23
-
24
- ## Create route
25
-
26
- ```ts
27
- // routes/index.ts
28
- import type { ElysiaApp } from "app";
29
-
30
- export default (app: ElysiaApp) => app.get("/", { hello: "world" });
31
- ```
32
-
33
- ### Directory structure
34
-
35
- Guide how `elysia-autoload` match routes
36
-
37
- ```
38
- ├── app.ts
39
- ├── routes
40
- ├── index.ts // index routes
41
- ├── posts
42
- ├── index.ts
43
- └── [id].ts // dynamic params
44
- ├── likes
45
- ├── [...].ts
46
- └── users.ts
47
- └── package.json
48
- ```
49
-
50
- - /routes/index.ts /
51
- - /routes/posts/index.ts → /posts
52
- - /routes/posts/[id].ts → /posts/:id
53
- - /routes/users.ts /users
54
- - /routes/likes/[...].ts → /likes/\*
55
-
56
- ## Options
57
-
58
- | Key | Type | Default | Description |
59
- | -------- | ------------------------------------------ | ---------------------------------- | ----------------------------------------------------------------------------------- |
60
- | pattern? | string | "\*\*\/\*.{ts,tsx,js,jsx,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 \| string[] | "./routes-types.ts" | Type code-generation output. It can be an array |
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)
114
-
115
- ### Usage of schema handler
116
-
117
- ```ts
118
- import swagger from "@elysiajs/swagger";
119
- import Elysia from "elysia";
120
- import { autoload } from "elysia-autoload";
121
-
122
- const app = new Elysia()
123
- .use(
124
- autoload({
125
- schema: ({ path, url }) => {
126
- const tag = url.split("/").at(1)!;
127
-
128
- return {
129
- beforeHandle: ({ request }) => {
130
- console.log(request.url);
131
- },
132
- detail: {
133
- description: `Route autoloaded from ${path}`,
134
- tags: [tag],
135
- },
136
- };
137
- },
138
- }),
139
- )
140
- .use(swagger());
141
-
142
- export type ElysiaApp = typeof app;
143
-
144
- app.listen(3001, console.log);
145
- ```
146
-
147
- ### Thanks [https://github.com/wobsoriano/elysia-autoroutes](elysia-autoroutes) for some ideas
1
+ # elysia-autoload
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)
4
+
5
+ ## Installation
6
+
7
+ ### Start new project with [create-elysiajs](https://github.com/kravetsone/create-elysiajs)
8
+
9
+ ```bash
10
+ bun create elysiajs <directory-name>
11
+ ```
12
+
13
+ and select `Autoload` in plugins
14
+
15
+ ### Manual
16
+
17
+ ```bash
18
+ bun install elysia-autoload
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ## Register the plugin
24
+
25
+ ```ts
26
+ import { Elysia } from "elysia";
27
+ import { autoload } from "elysia-autoload";
28
+
29
+ const app = new Elysia().use(autoload()).listen(3000);
30
+
31
+ export type ElysiaApp = typeof app;
32
+ ```
33
+
34
+ ## Create route
35
+
36
+ ```ts
37
+ // routes/index.ts
38
+ import type { ElysiaApp } from "app";
39
+
40
+ export default (app: ElysiaApp) => app.get("/", { hello: "world" });
41
+ ```
42
+
43
+ ### Directory structure
44
+
45
+ Guide how `elysia-autoload` match routes
46
+
47
+ ```
48
+ ├── app.ts
49
+ ├── routes
50
+ ├── index.ts // index routes
51
+ ├── posts
52
+ ├── index.ts
53
+ └── [id].ts // dynamic params
54
+ ├── likes
55
+ └── [...].ts // wildcard
56
+ ├── domains
57
+ ├── @[...] // wildcard with @ prefix
58
+ └──index.ts
59
+ ├── frontend
60
+ └──index.tsx // usage of tsx extension
61
+ └── users.ts
62
+ └── package.json
63
+ ```
64
+
65
+ - /routes/index.ts → /
66
+ - /routes/posts/index.ts /posts
67
+ - /routes/posts/[id].ts → /posts/:id
68
+ - /routes/users.ts /users
69
+ - /routes/likes/[...].ts /likes/\*
70
+ - /routes/domains/@[...]/index.ts /domains/@\*
71
+ - /routes/frontend/index.tsx → /frontend
72
+
73
+ ## Options
74
+
75
+ | Key | Type | Default | Description |
76
+ | -------- | ------------------------------------------ | ---------------------------------- | ----------------------------------------------------------------------------------- |
77
+ | pattern? | string | "\*\*\/\*.{ts,tsx,js,jsx,mjs,cjs}" | [Glob patterns](<https://en.wikipedia.org/wiki/Glob_(programming)>) |
78
+ | dir? | string | "./routes" | The folder where routes are located |
79
+ | prefix? | string | | Prefix for routes |
80
+ | types? | boolean \| [Types Options](#types-options) | false | Options to configure type code-generation. if boolean - enables/disables generation |
81
+ | schema? | Function | | Handler for providing routes guard schema |
82
+
83
+ ### Types Options
84
+
85
+ | Key | Type | Default | Description |
86
+ | ---------- | ------------------ | ------------------- | --------------------------------------------------------------------------------------- |
87
+ | output? | string \| string[] | "./routes-types.ts" | Type code-generation output. It can be an array |
88
+ | typeName? | string | "Routes" | Name for code-generated global type for [Eden](https://elysiajs.com/eden/overview.html) |
89
+ | useExport? | boolean | false | Use export instead of global type |
90
+
91
+ ### Usage of types code-generation for [Eden](https://elysiajs.com/eden/overview.html)
92
+
93
+ ```ts
94
+ // app.ts
95
+ import { Elysia } from "elysia";
96
+ import { autoload } from "elysia-autoload";
97
+
98
+ const app = new Elysia()
99
+ .use(
100
+ autoload({
101
+ types: {
102
+ output: "./routes.ts",
103
+ typeName: "Routes",
104
+ }, // or pass true for use default params
105
+ }),
106
+ )
107
+ .listen(3000);
108
+
109
+ export type ElysiaApp = typeof app;
110
+ ```
111
+
112
+ ```ts
113
+ // client.ts
114
+
115
+ import { edenTreaty } from "@elysiajs/eden";
116
+
117
+ // Routes are a global type so you don't need to import it.
118
+
119
+ const app = edenTreaty<Routes>("http://localhost:3002");
120
+
121
+ const { data } = await app.test["some-path-param"].get({
122
+ $query: {
123
+ key: 2,
124
+ },
125
+ });
126
+
127
+ console.log(data);
128
+ ```
129
+
130
+ Example of app with types code-generation you can see in [example](https://github.com/kravetsone/elysia-autoload/tree/main/example)
131
+
132
+ ### Usage of schema handler
133
+
134
+ ```ts
135
+ import swagger from "@elysiajs/swagger";
136
+ import Elysia from "elysia";
137
+ import { autoload } from "elysia-autoload";
138
+
139
+ const app = new Elysia()
140
+ .use(
141
+ autoload({
142
+ schema: ({ path, url }) => {
143
+ const tag = url.split("/").at(1)!;
144
+
145
+ return {
146
+ beforeHandle: ({ request }) => {
147
+ console.log(request.url);
148
+ },
149
+ detail: {
150
+ description: `Route autoloaded from ${path}`,
151
+ tags: [tag],
152
+ },
153
+ };
154
+ },
155
+ }),
156
+ )
157
+ .use(swagger());
158
+
159
+ export type ElysiaApp = typeof app;
160
+
161
+ app.listen(3001, console.log);
162
+ ```
package/dist/index.d.ts CHANGED
@@ -15,7 +15,7 @@ export interface IAutoloadOptions {
15
15
  schema?: TSchemaHandler;
16
16
  types?: ITypesOptions | true;
17
17
  }
18
- export declare function autoload({ pattern, dir, prefix, schema, types, }?: IAutoloadOptions): Promise<Elysia<"", {
18
+ export declare function autoload({ pattern, dir, prefix, schema, types, }?: IAutoloadOptions): Promise<Elysia<string, {
19
19
  request: {};
20
20
  store: {};
21
21
  derive: {};
package/dist/index.js CHANGED
@@ -15,9 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.autoload = void 0;
18
- const elysia_1 = require("elysia");
19
18
  const node_fs_1 = require("node:fs");
20
19
  const node_path_1 = require("node:path");
20
+ const elysia_1 = require("elysia");
21
21
  const utils_1 = require("./utils");
22
22
  const TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
23
23
  const TYPES_TYPENAME_DEFAULT = "Routes";
@@ -29,6 +29,7 @@ async function autoload({ pattern, dir, prefix, schema, types, } = {}) {
29
29
  throw new Error(`${directoryPath} isn't a directory`);
30
30
  const app = new elysia_1.default({
31
31
  name: "elysia-autoload",
32
+ prefix: prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix,
32
33
  seed: {
33
34
  pattern,
34
35
  dir,
@@ -51,13 +52,14 @@ async function autoload({ pattern, dir, prefix, schema, types, } = {}) {
51
52
  // Типы свойства "body" несовместимы.
52
53
  // Тип "string | TSchema | undefined" не может быть назначен для типа "TSchema | undefined".
53
54
  // Тип "string" не может быть назначен для типа "TSchema".ts(2345)
55
+ app.group(url,
54
56
  // @ts-expect-error why....
55
- app.group((prefix ?? "") + url, groupOptions, file.default);
57
+ groupOptions, file.default);
56
58
  if (types)
57
59
  paths.push(fullPath.replace(directoryPath, ""));
58
60
  }
59
61
  if (types) {
60
- const imports = paths.map((x, index) => `import Route${index} from "${directoryPath + x.replace(".ts", "").replace(".tsx", "")}";`);
62
+ const imports = paths.map((x, index) => `import type Route${index} from "${(directoryPath + x.replace(".ts", "").replace(".tsx", "")).replace(/\\/gu, "/")}";`);
61
63
  for await (const outputPath of types === true || !types.output
62
64
  ? [TYPES_OUTPUT_DEFAULT]
63
65
  : Array.isArray(types.output)
@@ -67,9 +69,7 @@ async function autoload({ pattern, dir, prefix, schema, types, } = {}) {
67
69
  `import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
68
70
  imports.join("\n"),
69
71
  "",
70
- types === true || !types.useExport
71
- ? "declare global {"
72
- : "",
72
+ types === true || !types.useExport ? "declare global {" : "",
73
73
  ` export type ${types === true || !types.typeName
74
74
  ? TYPES_TYPENAME_DEFAULT
75
75
  : types.typeName} = ${paths
package/dist/types.d.ts CHANGED
File without changes
package/dist/types.js CHANGED
File without changes
package/dist/utils.d.ts CHANGED
File without changes
package/dist/utils.js CHANGED
@@ -15,6 +15,8 @@ function transformToUrl(path) {
15
15
  const replacements = [
16
16
  // Clean the url extensions
17
17
  { regex: /\.(ts|tsx|js|jsx|mjs|cjs)$/u, replacement: "" },
18
+ // Fix windows slashes
19
+ { regex: /\\/gu, replacement: "/" },
18
20
  // Handle wild card based routes - users/[...id]/profile.ts -> users/*/profile
19
21
  { regex: /\[\.\.\..*\]/gu, replacement: "*" },
20
22
  // Handle generic square bracket based routes - users/[id]/index.ts -> users/:id
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "elysia-autoload",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "author": "kravetsone",
5
+ "type": "commonjs",
5
6
  "main": "dist/index.js",
6
7
  "description": "Plugin for Elysia which autoload all routes in directory and code-generate types for Eden",
7
8
  "homepage": "https://github.com/kravetsone/elysia-autoload",
@@ -19,22 +20,22 @@
19
20
  "codegeneration"
20
21
  ],
21
22
  "scripts": {
22
- "prepublishOnly": "tsc",
23
- "lint": "eslint \"{src,example}/**/*.{ts,tsx,js,mjs,cjs}\"",
24
- "lint:fix": "eslint \"{src,example}/**/*.{ts,tsx,js,mjs,cjs}\" --fix"
23
+ "prepublishOnly": "bun test && rm -rf dist && tsc",
24
+ "lint": "bunx @biomejs/biome check src",
25
+ "lint:fix": "bun lint --apply",
26
+ "prepare": "husky"
25
27
  },
26
28
  "files": [
27
29
  "dist"
28
30
  ],
29
31
  "devDependencies": {
32
+ "@biomejs/biome": "1.6.0",
30
33
  "@elysiajs/eden": "^0.8.1",
31
- "@elysiajs/swagger": "^0.8.4",
32
- "bun-types": "latest",
33
- "elysia": "^0.8.10",
34
- "eslint": "^8.56.0",
35
- "eslint-kit": "^10.7.0",
36
- "prettier": "^3.2.4",
37
- "typescript": "^5.3.3"
34
+ "@elysiajs/swagger": "^0.8.5",
35
+ "@types/bun": "^1.0.8",
36
+ "elysia": "^0.8.17",
37
+ "typescript": "^5.4.2",
38
+ "husky": "^9.0.11"
38
39
  },
39
40
  "peerDependencies": {
40
41
  "elysia": "^0.8.0"