elysia-autoload 1.3.0 → 1.4.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 +5 -9
- package/dist/index.d.ts +10 -11
- package/dist/index.js +7 -12
- package/package.json +9 -10
package/README.md
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
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
|
-
**Currently, Eden types generation is broken!!** Feels free to send PR
|
6
|
-
|
7
5
|
## Installation
|
8
6
|
|
9
7
|
### Start new project with [create-elysiajs](https://github.com/kravetsone/create-elysiajs)
|
@@ -124,14 +122,14 @@ export type ElysiaApp = typeof app;
|
|
124
122
|
```ts
|
125
123
|
// client.ts
|
126
124
|
|
127
|
-
import {
|
125
|
+
import { treaty } from "@elysiajs/eden";
|
128
126
|
|
129
127
|
// Routes are a global type so you don't need to import it.
|
130
128
|
|
131
|
-
const app =
|
129
|
+
const app = treaty<Routes>("http://localhost:3002");
|
132
130
|
|
133
131
|
const { data } = await app.test["some-path-param"].get({
|
134
|
-
|
132
|
+
query: {
|
135
133
|
key: 2,
|
136
134
|
},
|
137
135
|
});
|
@@ -149,15 +147,13 @@ import type Route0 from "./routes/index";
|
|
149
147
|
import type Route1 from "./routes/test/[some]/index";
|
150
148
|
|
151
149
|
declare global {
|
152
|
-
export type Routes = ElysiaWithBaseUrl<"/api",
|
153
|
-
ElysiaWithBaseUrl<"/api/test/:some",
|
150
|
+
export type Routes = ElysiaWithBaseUrl<"/api", typeof Route0> &
|
151
|
+
ElysiaWithBaseUrl<"/api/test/:some", typeof Route1>;
|
154
152
|
}
|
155
153
|
```
|
156
154
|
|
157
155
|
Example of app with types code-generation you can see in [example](https://github.com/kravetsone/elysia-autoload/tree/main/example)
|
158
156
|
|
159
|
-
**Currently, Eden types generation is broken!!**
|
160
|
-
|
161
157
|
### [Bun build](https://bun.sh/docs/bundler) usage
|
162
158
|
|
163
159
|
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)!
|
package/dist/index.d.ts
CHANGED
@@ -1,23 +1,22 @@
|
|
1
|
-
import Elysia, { RouteBase, LocalHook, InputSchema,
|
1
|
+
import Elysia, { RouteBase, RouteSchema, AnyElysia, LocalHook, InputSchema, SingletonBase, BaseMacro, Elysia as Elysia$1 } from 'elysia';
|
2
2
|
|
3
3
|
type PathToObject<Path extends string, Type extends RouteBase> = Path extends `${infer Head}/${infer Rest}` ? Head extends "" ? PathToObject<Rest, Type> : {
|
4
4
|
[K in Head]: PathToObject<Rest, Type>;
|
5
5
|
} : {
|
6
6
|
[K in Path]: Type;
|
7
7
|
};
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
}>;
|
8
|
+
declare namespace ElysiaMatch {
|
9
|
+
type RouteEnd = Record<string, RouteSchema>;
|
10
|
+
type Fx = (...args: any[]) => AnyElysia;
|
11
|
+
type All = AnyElysia | Fx;
|
12
|
+
type Extract<T extends All> = T extends Fx ? ReturnType<T> : T;
|
13
|
+
}
|
15
14
|
type FlattenIndexRoutes<T> = T extends object ? {
|
16
|
-
[K in keyof T as K extends "index" ? T[K] extends
|
15
|
+
[K in keyof T as K extends "index" ? T[K] extends ElysiaMatch.RouteEnd ? never : K : K]: FlattenIndexRoutes<T[K]>;
|
17
16
|
} & (T extends {
|
18
17
|
index: infer I;
|
19
|
-
} ? I extends
|
20
|
-
type ElysiaWithBaseUrl<BaseUrl extends string, ElysiaType extends
|
18
|
+
} ? I extends ElysiaMatch.RouteEnd ? FlattenIndexRoutes<I> : T : T) : T;
|
19
|
+
type ElysiaWithBaseUrl<BaseUrl extends string, ElysiaType extends ElysiaMatch.All> = ElysiaMatch.Extract<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, FlattenIndexRoutes<PathToObject<BaseUrl, Routes>>, Ephemeral, Volatile> : never;
|
21
20
|
type SoftString<T extends string> = T | (string & {});
|
22
21
|
|
23
22
|
type SchemaHandler = ({ path, url, }: {
|
package/dist/index.js
CHANGED
@@ -3,8 +3,7 @@ import path from 'node:path';
|
|
3
3
|
import { Elysia } from 'elysia';
|
4
4
|
|
5
5
|
function getPath(dir) {
|
6
|
-
if (path.isAbsolute(dir))
|
7
|
-
return dir;
|
6
|
+
if (path.isAbsolute(dir)) return dir;
|
8
7
|
if (path.isAbsolute(process.argv[1]))
|
9
8
|
return path.join(process.argv[1], "..", dir);
|
10
9
|
return path.join(process.cwd(), process.argv[1], "..", dir);
|
@@ -48,13 +47,11 @@ function sortByNestedParams(routes) {
|
|
48
47
|
return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
|
49
48
|
}
|
50
49
|
function fixSlashes(prefix) {
|
51
|
-
if (!prefix?.endsWith("/"))
|
52
|
-
return prefix;
|
50
|
+
if (!prefix?.endsWith("/")) return prefix;
|
53
51
|
return prefix.slice(0, -1);
|
54
52
|
}
|
55
53
|
function addRelativeIfNotDot(path2) {
|
56
|
-
if (path2.at(0) !== ".")
|
57
|
-
return `./${path2}`;
|
54
|
+
if (path2.at(0) !== ".") return `./${path2}`;
|
58
55
|
return path2;
|
59
56
|
}
|
60
57
|
|
@@ -106,8 +103,7 @@ async function autoload(options = {}) {
|
|
106
103
|
const fullPath = path.join(directoryPath, filePath);
|
107
104
|
const file = await import(fullPath);
|
108
105
|
const importName = typeof getImportName === "string" ? getImportName : getImportName(file);
|
109
|
-
if (!file[importName] && options?.skipImportErrors)
|
110
|
-
continue;
|
106
|
+
if (!file[importName] && options?.skipImportErrors) continue;
|
111
107
|
if (!file[importName])
|
112
108
|
throw new Error(`${filePath} don't provide export ${importName}`);
|
113
109
|
const url = transformToUrl(filePath);
|
@@ -119,14 +115,13 @@ async function autoload(options = {}) {
|
|
119
115
|
plugin.group(url, groupOptions, (app) => app.use(importedValue()));
|
120
116
|
if (importedValue instanceof Elysia)
|
121
117
|
plugin.group(url, groupOptions, (app) => app.use(importedValue));
|
122
|
-
if (types)
|
123
|
-
paths.push(fullPath.replace(directoryPath, ""));
|
118
|
+
if (types) paths.push([fullPath.replace(directoryPath, ""), importName]);
|
124
119
|
}
|
125
120
|
if (types) {
|
126
121
|
for await (const outputPath of types.output) {
|
127
122
|
const outputAbsolutePath = getPath(outputPath);
|
128
123
|
const imports = paths.map(
|
129
|
-
(x, index) => `import type Route${index} from "${addRelativeIfNotDot(
|
124
|
+
([x, exportName], index) => `import type ${exportName === "default" ? `Route${index}` : `{ ${exportName} as Route${index} }`} from "${addRelativeIfNotDot(
|
130
125
|
path.relative(
|
131
126
|
path.dirname(outputAbsolutePath),
|
132
127
|
directoryPath + x.replace(".ts", "").replace(".tsx", "")
|
@@ -141,7 +136,7 @@ async function autoload(options = {}) {
|
|
141
136
|
"",
|
142
137
|
!types.useExport ? "declare global {" : "",
|
143
138
|
` export type ${types.typeName} = ${paths.map(
|
144
|
-
(x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ?? "") + transformToUrl(x) || "/"}",
|
139
|
+
([x], index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ?? "") + transformToUrl(x) || "/"}", typeof Route${index}>`
|
145
140
|
).join("\n & ")}`,
|
146
141
|
!types.useExport ? "}" : ""
|
147
142
|
].join("\n")
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "elysia-autoload",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.4.0",
|
4
4
|
"author": "kravetsone",
|
5
5
|
"type": "module",
|
6
6
|
"types": "./dist/index.d.ts",
|
@@ -29,18 +29,17 @@
|
|
29
29
|
"scripts": {
|
30
30
|
"prepublishOnly": "bun test && bunx pkgroll",
|
31
31
|
"lint": "bunx @biomejs/biome check src",
|
32
|
-
"lint:fix": "bun lint --apply"
|
33
|
-
"prepare": "bunx husky"
|
32
|
+
"lint:fix": "bun lint --apply"
|
34
33
|
},
|
35
34
|
"files": ["dist"],
|
36
35
|
"devDependencies": {
|
37
|
-
"@biomejs/biome": "1.
|
38
|
-
"@elysiajs/eden": "^1.1.
|
39
|
-
"@elysiajs/swagger": "^1.1.
|
40
|
-
"@types/bun": "^1.1.
|
41
|
-
"elysia": "^1.1.
|
42
|
-
"pkgroll": "^2.
|
43
|
-
"typescript": "^5.
|
36
|
+
"@biomejs/biome": "1.9.2",
|
37
|
+
"@elysiajs/eden": "^1.1.3",
|
38
|
+
"@elysiajs/swagger": "^1.1.1",
|
39
|
+
"@types/bun": "^1.1.9",
|
40
|
+
"elysia": "^1.1.13",
|
41
|
+
"pkgroll": "^2.5.0",
|
42
|
+
"typescript": "^5.6.2"
|
44
43
|
},
|
45
44
|
"peerDependencies": {
|
46
45
|
"elysia": "^1.1.0"
|