elysia-autoload 0.1.7 → 0.1.9

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
@@ -2,6 +2,8 @@
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)
4
4
 
5
+ **Currently, Eden types generation is broken!!**
6
+
5
7
  ## Installation
6
8
 
7
9
  ### Start new project with [create-elysiajs](https://github.com/kravetsone/create-elysiajs)
@@ -102,7 +104,7 @@ const app = new Elysia()
102
104
  output: "./routes.ts",
103
105
  typeName: "Routes",
104
106
  }, // or pass true for use default params
105
- }),
107
+ })
106
108
  )
107
109
  .listen(3000);
108
110
 
@@ -152,7 +154,7 @@ const app = new Elysia()
152
154
  },
153
155
  };
154
156
  },
155
- }),
157
+ })
156
158
  )
157
159
  .use(swagger());
158
160
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import Elysia from "elysia";
1
+ import { Elysia } from "elysia";
2
2
  type TSchemaHandler = ({ path, url, }: {
3
3
  path: string;
4
4
  url: string;
@@ -15,13 +15,24 @@ 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<string, {
19
- request: {};
18
+ export declare function autoload(options?: IAutoloadOptions): () => Promise<Elysia<string, false, {
19
+ decorator: {};
20
20
  store: {};
21
21
  derive: {};
22
22
  resolve: {};
23
23
  }, {
24
24
  type: {};
25
25
  error: {};
26
- }, {}, {}, {}, false>>;
26
+ }, {
27
+ schema: {};
28
+ macro: {};
29
+ }, {}, {
30
+ derive: {};
31
+ resolve: {};
32
+ schema: {};
33
+ }, {
34
+ derive: {};
35
+ resolve: {};
36
+ schema: {};
37
+ }>>;
27
38
  export * from "./types";
package/dist/index.js CHANGED
@@ -21,66 +21,64 @@ 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";
24
- async function autoload({ pattern, dir, prefix, schema, types, } = {}) {
25
- const directoryPath = (0, utils_1.getPath)(dir || "./routes");
26
- if (!(0, node_fs_1.existsSync)(directoryPath))
27
- throw new Error(`Directory ${directoryPath} doesn't exists`);
28
- if (!(0, node_fs_1.statSync)(directoryPath).isDirectory())
29
- throw new Error(`${directoryPath} isn't a directory`);
30
- const app = new elysia_1.default({
31
- name: "elysia-autoload",
32
- prefix: prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix,
33
- seed: {
34
- pattern,
35
- dir,
36
- prefix,
37
- types,
38
- },
39
- });
40
- const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
41
- const files = await Array.fromAsync(glob.scan({
42
- cwd: directoryPath,
43
- }));
44
- const paths = [];
45
- for await (const path of (0, utils_1.sortByNestedParams)(files)) {
46
- const fullPath = (0, node_path_1.join)(directoryPath, path);
47
- const file = await Promise.resolve(`${(0, node_path_1.join)(directoryPath, path)}`).then(s => require(s));
48
- if (!file.default)
49
- throw new Error(`${path} don't provide export default`);
50
- const url = (0, utils_1.transformToUrl)(path);
51
- const groupOptions = schema ? schema({ path, url }) : {};
52
- // Типы свойства "body" несовместимы.
53
- // Тип "string | TSchema | undefined" не может быть назначен для типа "TSchema | undefined".
54
- // Тип "string" не может быть назначен для типа "TSchema".ts(2345)
55
- app.group(url,
56
- // @ts-expect-error why....
57
- groupOptions, file.default);
58
- if (types)
59
- paths.push(fullPath.replace(directoryPath, ""));
60
- }
61
- if (types) {
62
- const imports = paths.map((x, index) => `import type Route${index} from "${(directoryPath + x.replace(".ts", "").replace(".tsx", "")).replace(/\\/gu, "/")}";`);
63
- for await (const outputPath of types === true || !types.output
64
- ? [TYPES_OUTPUT_DEFAULT]
65
- : Array.isArray(types.output)
66
- ? types.output
67
- : [types.output]) {
68
- await Bun.write((0, utils_1.getPath)(outputPath), [
69
- `import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
70
- imports.join("\n"),
71
- "",
72
- types === true || !types.useExport ? "declare global {" : "",
73
- ` export type ${types === true || !types.typeName
74
- ? TYPES_TYPENAME_DEFAULT
75
- : types.typeName} = ${paths
76
- .map((x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ??
77
- "") + (0, utils_1.transformToUrl)(x) || "/"}", ReturnType<typeof Route${index}>>`)
78
- .join("\n & ")}`,
79
- types === true || !types.useExport ? "}" : "",
80
- ].join("\n"));
24
+ function autoload(options = {}) {
25
+ return async () => {
26
+ const { pattern, dir, prefix, schema, types } = options;
27
+ const directoryPath = (0, utils_1.getPath)(dir || "./routes");
28
+ if (!(0, node_fs_1.existsSync)(directoryPath))
29
+ throw new Error(`Directory ${directoryPath} doesn't exists`);
30
+ if (!(0, node_fs_1.statSync)(directoryPath).isDirectory())
31
+ throw new Error(`${directoryPath} isn't a directory`);
32
+ const plugin = new elysia_1.Elysia({
33
+ name: "elysia-autoload",
34
+ prefix: (0, utils_1.fixSlashes)(prefix),
35
+ seed: {
36
+ pattern,
37
+ dir,
38
+ prefix,
39
+ types,
40
+ },
41
+ });
42
+ const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
43
+ const files = await Array.fromAsync(glob.scan({
44
+ cwd: directoryPath,
45
+ }));
46
+ const paths = [];
47
+ for await (const path of (0, utils_1.sortByNestedParams)(files)) {
48
+ const fullPath = (0, node_path_1.join)(directoryPath, path);
49
+ const file = await Promise.resolve(`${fullPath}`).then(s => require(s));
50
+ if (!file.default)
51
+ throw new Error(`${path} doesn't provide default export`);
52
+ const url = (0, utils_1.transformToUrl)(path);
53
+ const groupOptions = schema ? schema({ path, url }) : {};
54
+ plugin.group(url, groupOptions, file.default);
55
+ if (types)
56
+ paths.push(fullPath.replace(directoryPath, ""));
81
57
  }
82
- }
83
- return app;
58
+ if (types) {
59
+ const imports = paths.map((x, index) => `import type Route${index} from "${(directoryPath + x.replace(".ts", "").replace(".tsx", "")).replace(/\\/gu, "/")}";`);
60
+ for await (const outputPath of types === true || !types.output
61
+ ? [TYPES_OUTPUT_DEFAULT]
62
+ : Array.isArray(types.output)
63
+ ? types.output
64
+ : [types.output]) {
65
+ await Bun.write((0, utils_1.getPath)(outputPath), [
66
+ `import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
67
+ imports.join("\n"),
68
+ "",
69
+ types === true || !types.useExport ? "declare global {" : "",
70
+ ` export type ${types === true || !types.typeName
71
+ ? TYPES_TYPENAME_DEFAULT
72
+ : types.typeName} = ${paths
73
+ .map((x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ??
74
+ "") + (0, utils_1.transformToUrl)(x) || "/"}", ReturnType<typeof Route${index}>>`)
75
+ .join("\n & ")}`,
76
+ types === true || !types.useExport ? "}" : "",
77
+ ].join("\n"));
78
+ }
79
+ }
80
+ return plugin;
81
+ };
84
82
  }
85
83
  exports.autoload = autoload;
86
84
  __exportStar(require("./types"), exports);
package/dist/types.d.ts CHANGED
@@ -4,5 +4,5 @@ type RemoveLastChar<T extends string> = T extends `${infer V}/` ? V : T;
4
4
  type RoutesWithPrefix<Routes extends RouteBase, Prefix extends string> = {
5
5
  [K in keyof Routes as `${Prefix}${RemoveLastChar<K & string>}`]: Routes[K];
6
6
  };
7
- export type ElysiaWithBaseUrl<BaseUrl extends string, ElysiaType extends Elysia> = ElysiaType extends Elysia<infer BasePath, infer Decorators, infer Definitions, infer ParentSchema, infer Macro, infer Routes, infer Scoped> ? Elysia<BasePath, Decorators, Definitions, ParentSchema, Macro, RoutesWithPrefix<Routes, BaseUrl>, Scoped> : never;
7
+ export 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;
8
8
  export {};
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export declare function getPath(dir: string): string;
2
2
  export declare function transformToUrl(path: string): string;
3
3
  export declare function sortByNestedParams(routes: string[]): string[];
4
+ export declare function fixSlashes(prefix?: string): string | undefined;
package/dist/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sortByNestedParams = exports.transformToUrl = exports.getPath = void 0;
3
+ exports.fixSlashes = exports.sortByNestedParams = exports.transformToUrl = exports.getPath = void 0;
4
4
  const node_path_1 = require("node:path");
5
5
  function getPath(dir) {
6
6
  if ((0, node_path_1.isAbsolute)(dir))
@@ -49,3 +49,9 @@ function sortByNestedParams(routes) {
49
49
  return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
50
50
  }
51
51
  exports.sortByNestedParams = sortByNestedParams;
52
+ function fixSlashes(prefix) {
53
+ if (!prefix?.endsWith("/"))
54
+ return prefix;
55
+ return prefix.slice(0, -1);
56
+ }
57
+ exports.fixSlashes = fixSlashes;
package/package.json CHANGED
@@ -1,43 +1,43 @@
1
- {
2
- "name": "elysia-autoload",
3
- "version": "0.1.7",
4
- "author": "kravetsone",
5
- "type": "commonjs",
6
- "main": "dist/index.js",
7
- "description": "Plugin for Elysia which autoload all routes in directory and code-generate types for Eden",
8
- "homepage": "https://github.com/kravetsone/elysia-autoload",
9
- "keywords": [
10
- "bun",
11
- "elysia",
12
- "autoimports",
13
- "autoload",
14
- "nextjs",
15
- "filerouter",
16
- "autoroutes",
17
- "eden",
18
- "treaty",
19
- "trpc",
20
- "codegeneration"
21
- ],
22
- "scripts": {
23
- "prepublishOnly": "bun test && rm -rf dist && tsc",
24
- "lint": "bunx @biomejs/biome check src",
25
- "lint:fix": "bun lint --apply",
26
- "prepare": "husky"
27
- },
28
- "files": [
29
- "dist"
30
- ],
31
- "devDependencies": {
32
- "@biomejs/biome": "1.6.0",
33
- "@elysiajs/eden": "^0.8.1",
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"
39
- },
40
- "peerDependencies": {
41
- "elysia": "^0.8.0"
42
- }
43
- }
1
+ {
2
+ "name": "elysia-autoload",
3
+ "version": "0.1.9",
4
+ "author": "kravetsone",
5
+ "type": "commonjs",
6
+ "main": "dist/index.js",
7
+ "description": "Plugin for Elysia which autoload all routes in directory and code-generate types for Eden",
8
+ "homepage": "https://github.com/kravetsone/elysia-autoload",
9
+ "keywords": [
10
+ "bun",
11
+ "elysia",
12
+ "autoimports",
13
+ "autoload",
14
+ "nextjs",
15
+ "filerouter",
16
+ "autoroutes",
17
+ "eden",
18
+ "treaty",
19
+ "trpc",
20
+ "codegeneration"
21
+ ],
22
+ "scripts": {
23
+ "prepublishOnly": "bun test && rm -rf dist && tsc",
24
+ "lint": "bunx @biomejs/biome check src",
25
+ "lint:fix": "bun lint --apply",
26
+ "prepare": "husky"
27
+ },
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "devDependencies": {
32
+ "@biomejs/biome": "1.6.1",
33
+ "@elysiajs/eden": "^1.0.4",
34
+ "@elysiajs/swagger": "^1.0.2",
35
+ "@types/bun": "^1.0.8",
36
+ "elysia": "^1.0.5",
37
+ "typescript": "^5.4.2",
38
+ "husky": "^9.0.11"
39
+ },
40
+ "peerDependencies": {
41
+ "elysia": "^1.0.0"
42
+ }
43
+ }