elysia-autoload 1.1.0 → 1.2.1
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.d.ts +44 -11
- package/dist/index.js +18 -3
- package/package.json +46 -48
package/dist/index.d.ts
CHANGED
@@ -1,28 +1,61 @@
|
|
1
1
|
import Elysia, { RouteBase, LocalHook, InputSchema, RouteSchema, SingletonBase, BaseMacro, Elysia as Elysia$1 } from 'elysia';
|
2
2
|
|
3
|
-
type
|
4
|
-
|
5
|
-
|
3
|
+
type PathToObject<Path extends string, Type extends RouteBase> = Path extends `${infer Head}/${infer Rest}` ? Head extends "" ? PathToObject<Rest, Type> : {
|
4
|
+
[K in Head]: PathToObject<Rest, Type>;
|
5
|
+
} : {
|
6
|
+
[K in Path]: Type;
|
6
7
|
};
|
7
|
-
type
|
8
|
+
type RouteEndType = Record<string, {
|
9
|
+
body: any;
|
10
|
+
params: any;
|
11
|
+
query: any;
|
12
|
+
headers: any;
|
13
|
+
response: any;
|
14
|
+
}>;
|
15
|
+
type FlattenIndexRoutes<T> = T extends object ? {
|
16
|
+
[K in keyof T as K extends "index" ? T[K] extends RouteEndType ? never : K : K]: FlattenIndexRoutes<T[K]>;
|
17
|
+
} & (T extends {
|
18
|
+
index: infer I;
|
19
|
+
} ? I extends RouteEndType ? FlattenIndexRoutes<I> : {} : {}) : T;
|
20
|
+
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, FlattenIndexRoutes<PathToObject<BaseUrl, Routes>>, Ephemeral, Volatile> : never;
|
21
|
+
type SoftString<T extends string> = T | (string & {});
|
8
22
|
|
9
|
-
type
|
23
|
+
type SchemaHandler = ({ path, url, }: {
|
10
24
|
path: string;
|
11
25
|
url: string;
|
12
26
|
}) => LocalHook<InputSchema, RouteSchema, SingletonBase, Record<string, Error>, BaseMacro, "">;
|
13
|
-
interface
|
27
|
+
interface TypesOptions {
|
14
28
|
output?: string | string[];
|
15
29
|
typeName?: string;
|
16
30
|
useExport?: boolean;
|
17
31
|
}
|
18
|
-
interface
|
32
|
+
interface AutoloadOptions {
|
19
33
|
pattern?: string;
|
20
34
|
dir?: string;
|
21
35
|
prefix?: string;
|
22
|
-
schema?:
|
23
|
-
types?:
|
36
|
+
schema?: SchemaHandler;
|
37
|
+
types?: TypesOptions | true;
|
38
|
+
/**
|
39
|
+
* Throws an error if no matches are found.
|
40
|
+
* @default true
|
41
|
+
*/
|
42
|
+
failGlob?: boolean;
|
43
|
+
/**
|
44
|
+
* import a specific `export` from a file
|
45
|
+
* @example import first export
|
46
|
+
* ```ts
|
47
|
+
* import: (file) => Object.keys(file).at(0) || "default",
|
48
|
+
* ```
|
49
|
+
* @default "default"
|
50
|
+
*/
|
51
|
+
import?: SoftString<"default"> | ((file: any) => string);
|
52
|
+
/**
|
53
|
+
* Skip imports where needed `export` not defined
|
54
|
+
* @default false
|
55
|
+
*/
|
56
|
+
skipImportErrors?: boolean;
|
24
57
|
}
|
25
|
-
declare function autoload(options?:
|
58
|
+
declare function autoload(options?: AutoloadOptions): Promise<Elysia$1<string, false, {
|
26
59
|
decorator: {};
|
27
60
|
store: {};
|
28
61
|
derive: {};
|
@@ -44,4 +77,4 @@ declare function autoload(options?: IAutoloadOptions): Promise<Elysia$1<string,
|
|
44
77
|
schema: {};
|
45
78
|
}>>;
|
46
79
|
|
47
|
-
export { type ElysiaWithBaseUrl, type
|
80
|
+
export { type AutoloadOptions, type ElysiaWithBaseUrl, type SchemaHandler, type SoftString, type TypesOptions, autoload };
|
package/dist/index.js
CHANGED
@@ -63,6 +63,8 @@ const TYPES_OBJECT_DEFAULT = {
|
|
63
63
|
};
|
64
64
|
async function autoload(options = {}) {
|
65
65
|
const { pattern, prefix, schema } = options;
|
66
|
+
const failGlob = options.failGlob ?? true;
|
67
|
+
const getImportName = options?.import ?? "default";
|
66
68
|
const dir = options.dir ?? DIR_ROUTES_DEFAULT;
|
67
69
|
const types = options.types ? options.types !== true ? {
|
68
70
|
...TYPES_OBJECT_DEFAULT,
|
@@ -91,15 +93,28 @@ async function autoload(options = {}) {
|
|
91
93
|
cwd: directoryPath
|
92
94
|
})
|
93
95
|
);
|
96
|
+
if (failGlob && files.length === 0)
|
97
|
+
throw new Error(
|
98
|
+
`No matches found in ${directoryPath}. You can disable this error by setting the failGlob parameter to false in the options of autoload plugin`
|
99
|
+
);
|
94
100
|
const paths = [];
|
95
101
|
for await (const filePath of sortByNestedParams(files)) {
|
96
102
|
const fullPath = path.join(directoryPath, filePath);
|
97
103
|
const file = await import(fullPath);
|
98
|
-
|
99
|
-
|
104
|
+
const importName = typeof getImportName === "string" ? getImportName : getImportName(file);
|
105
|
+
if (!file[importName] && options?.skipImportErrors)
|
106
|
+
continue;
|
107
|
+
if (!file[importName])
|
108
|
+
throw new Error(`${filePath} don't provide export ${importName}`);
|
100
109
|
const url = transformToUrl(filePath);
|
101
110
|
const groupOptions = schema ? schema({ path: filePath, url }) : {};
|
102
|
-
|
111
|
+
const importedValue = file[importName];
|
112
|
+
if (typeof importedValue === "function" && importedValue.length)
|
113
|
+
plugin.group(url, groupOptions, importedValue);
|
114
|
+
if (typeof importedValue === "function" && !importedValue.length)
|
115
|
+
plugin.group(url, groupOptions, (app) => app.use(importedValue()));
|
116
|
+
if (importedValue instanceof Elysia)
|
117
|
+
plugin.group(url, groupOptions, (app) => app.use(importedValue));
|
103
118
|
if (types)
|
104
119
|
paths.push(fullPath.replace(directoryPath, ""));
|
105
120
|
}
|
package/package.json
CHANGED
@@ -1,50 +1,48 @@
|
|
1
1
|
{
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
"elysia": "^1.1.0"
|
49
|
-
}
|
2
|
+
"name": "elysia-autoload",
|
3
|
+
"version": "1.2.1",
|
4
|
+
"author": "kravetsone",
|
5
|
+
"type": "module",
|
6
|
+
"types": "./dist/index.d.ts",
|
7
|
+
"module": "./dist/index.js",
|
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",
|
15
|
+
"homepage": "https://github.com/kravetsone/elysia-autoload",
|
16
|
+
"keywords": [
|
17
|
+
"bun",
|
18
|
+
"elysia",
|
19
|
+
"autoimports",
|
20
|
+
"autoload",
|
21
|
+
"nextjs",
|
22
|
+
"filerouter",
|
23
|
+
"autoroutes",
|
24
|
+
"eden",
|
25
|
+
"treaty",
|
26
|
+
"trpc",
|
27
|
+
"codegeneration"
|
28
|
+
],
|
29
|
+
"scripts": {
|
30
|
+
"prepublishOnly": "bun test && bunx pkgroll",
|
31
|
+
"lint": "bunx @biomejs/biome check src",
|
32
|
+
"lint:fix": "bun lint --apply",
|
33
|
+
"prepare": "bunx husky"
|
34
|
+
},
|
35
|
+
"files": ["dist"],
|
36
|
+
"devDependencies": {
|
37
|
+
"@biomejs/biome": "1.8.3",
|
38
|
+
"@elysiajs/eden": "^1.1.0",
|
39
|
+
"@elysiajs/swagger": "^1.1.0",
|
40
|
+
"@types/bun": "^1.1.6",
|
41
|
+
"elysia": "^1.1.2",
|
42
|
+
"pkgroll": "^2.1.1",
|
43
|
+
"typescript": "^5.5.3"
|
44
|
+
},
|
45
|
+
"peerDependencies": {
|
46
|
+
"elysia": "^1.1.0"
|
47
|
+
}
|
50
48
|
}
|