elysia-autoload 0.1.5 → 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,162 +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
- ### 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
- ```
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
@@ -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)
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
@@ -35,7 +37,7 @@ function transformToUrl(path) {
35
37
  for (const { regex, replacement } of replacements) {
36
38
  url = url.replace(regex, replacement);
37
39
  }
38
- return url.length ? url : "/";
40
+ return url;
39
41
  }
40
42
  exports.transformToUrl = transformToUrl;
41
43
  function getParamsCount(path) {
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "elysia-autoload",
3
- "version": "0.1.5",
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",
@@ -22,19 +23,19 @@
22
23
  "prepublishOnly": "bun test && rm -rf dist && tsc",
23
24
  "lint": "bunx @biomejs/biome check src",
24
25
  "lint:fix": "bun lint --apply",
25
- "prepare": "bunx husky install"
26
+ "prepare": "husky"
26
27
  },
27
28
  "files": [
28
29
  "dist"
29
30
  ],
30
31
  "devDependencies": {
31
- "@biomejs/biome": "1.5.3",
32
+ "@biomejs/biome": "1.6.0",
32
33
  "@elysiajs/eden": "^0.8.1",
33
- "@elysiajs/swagger": "^0.8.4",
34
- "@types/bun": "^1.0.4",
35
- "elysia": "^0.8.10",
36
- "typescript": "^5.3.3",
37
- "husky": "^8.0.0"
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"