rajt 0.0.17 → 0.0.19

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/src/routes.ts CHANGED
@@ -1,27 +1,31 @@
1
- import fs from 'node:fs'
2
- import path from 'node:path'
1
+ import { existsSync, readdirSync, statSync } from 'node:fs'
2
+ import { dirname, join, resolve } from 'node:path'
3
3
  import { fileURLToPath } from 'node:url'
4
4
  import { Route } from './types'
5
5
  import { registerHandler } from './register'
6
6
  import { isAnonFn } from './utils/func'
7
7
 
8
8
  const __filename = fileURLToPath(import.meta.url)
9
- const __dirname = path.dirname(__filename)
9
+ const __dirname = dirname(__filename)
10
10
 
11
- export default async function getRoutes(all: boolean = false, baseDir: string = 'actions'): Promise<Route[]> {
11
+ export async function getRoutes(
12
+ all: boolean = false,
13
+ dirs: string[] = ['actions', 'features']
14
+ ): Promise<Route[]> {
12
15
  const routes: Route[] = []
13
16
 
14
- const walk = async (dir: string, middlewares: Function[] = []): Promise<void> => {
15
- const files = fs.readdirSync(dir)
17
+ const walk = async (dir: string, baseDir: string, middlewares: Function[] = []): Promise<void> => {
18
+ if (!existsSync(dir)) return
19
+ const files = readdirSync(dir)
16
20
 
17
21
  for (const file of files) {
18
- const fullPath = path.join(dir, file)
19
- const stat = fs.statSync(fullPath)
22
+ const fullPath = join(dir, file)
23
+ const stat = statSync(fullPath)
20
24
 
21
25
  if (stat.isDirectory()) {
22
- const indexFile = path.join(fullPath, 'index.ts')
26
+ const indexFile = join(fullPath, 'index.ts')
23
27
 
24
- if (fs.existsSync(indexFile)) {
28
+ if (existsSync(indexFile)) {
25
29
  const mod = await import(indexFile)
26
30
  const group = mod.default
27
31
  registerHandler(group.name, group)
@@ -37,11 +41,12 @@ export default async function getRoutes(all: boolean = false, baseDir: string =
37
41
  })
38
42
  }
39
43
 
40
- await walk(fullPath, middlewares)
44
+ await walk(fullPath, baseDir, middlewares)
41
45
  } else if (file.endsWith('.ts')) {
42
46
  const mod = await import(fullPath)
43
47
  const handle = mod.default
44
48
 
49
+ if (handle?.gmw) return
45
50
  if (handle?.m) {
46
51
  registerHandler(handle.name, handle)
47
52
 
@@ -58,6 +63,57 @@ export default async function getRoutes(all: boolean = false, baseDir: string =
58
63
  }
59
64
  }
60
65
 
61
- await walk(path.resolve(__dirname, '../../..', baseDir))
66
+ await Promise.all(dirs.map(dir => walk(resolve(__dirname, '../../..', dir), dir)))
62
67
  return routes
63
68
  }
69
+
70
+ export async function getMiddlewares(
71
+ dirs: string[] = ['middlewares']
72
+ ): Promise<Route[]> {
73
+ const mw: Route[] = []
74
+
75
+ const walk = async (dir: string, baseDir: string, middlewares: Function[] = []): Promise<void> => {
76
+ if (!existsSync(dir)) return
77
+ const files = readdirSync(dir)
78
+
79
+ for (const file of files) {
80
+ const fullPath = join(dir, file)
81
+ const stat = statSync(fullPath)
82
+
83
+ if (stat.isDirectory()) {
84
+ const indexFile = join(fullPath, 'index.ts')
85
+
86
+ if (existsSync(indexFile)) {
87
+ const mod = await import(indexFile)
88
+ const group = mod.default
89
+
90
+ if (group?.gmw) {
91
+ // @ts-ignore
92
+ mw.push({
93
+ name: group.name.replace(/\.ts$/, ''),
94
+ file: baseDir + fullPath.split(baseDir)[1],
95
+ handle: group,
96
+ })
97
+ }
98
+ }
99
+
100
+ await walk(fullPath, baseDir, middlewares)
101
+ } else if (file.endsWith('.ts')) {
102
+ const mod = await import(fullPath)
103
+ const handle = mod.default
104
+
105
+ if (handle?.gmw) {
106
+ // @ts-ignore
107
+ mw.push({
108
+ name: handle.name.replace(/\.ts$/, ''),
109
+ file: baseDir + fullPath.split(baseDir)[1],
110
+ handle,
111
+ })
112
+ }
113
+ }
114
+ }
115
+ }
116
+
117
+ await Promise.all(dirs.map(dir => walk(resolve(__dirname, '../../..', dir), dir)))
118
+ return mw
119
+ }
@@ -1,9 +1,17 @@
1
- import { writeFileSync } from 'node:fs'
2
- import getRoutes from '../routes'
1
+ import { existsSync, writeFileSync } from 'node:fs'
2
+ import { config } from 'dotenv'
3
+ import { getRoutes, getMiddlewares } from '../routes'
3
4
  import ensureDir from '../utils/ensuredir'
4
5
 
6
+ config({ path: '../../.env.dev' })
7
+
5
8
  async function cacheRoutes() {
9
+ const rolePath = '../../roles.json'
10
+ if (!existsSync(rolePath))
11
+ writeFileSync(rolePath, '{}')
12
+
6
13
  const routes = await getRoutes(true)
14
+ const middlewares = await getMiddlewares()
7
15
 
8
16
  const iPath = '../../tmp/import-routes.mjs'
9
17
  ensureDir(iPath)
@@ -11,6 +19,7 @@ async function cacheRoutes() {
11
19
  import { registerHandler } from '../node_modules/rajt/src/register'
12
20
 
13
21
  ${routes.map(r => `import ${r.name} from '../${normalizePath(r.file)}'`).join('\n')}
22
+ ${middlewares.map(r => `import ${r.name} from '../${normalizePath(r.file)}'`).join('\n')}
14
23
 
15
24
  try {
16
25
  const handlers = {${routes.map(r => r.name).join()}}
@@ -0,0 +1,16 @@
1
+ import { readFileSync } from 'node:fs'
2
+ import { dirname, join } from 'node:path'
3
+ import { fileURLToPath } from 'node:url'
4
+
5
+ export default function jsonImport<T = any>(filePath: string, defaultValue: T = {} as T): T {
6
+ const __filename = fileURLToPath(import.meta.url)
7
+ const __dirname = dirname(__filename)
8
+
9
+ try {
10
+ const fullPath = join(__dirname, filePath)
11
+ const fileContent = readFileSync(fullPath, 'utf-8')
12
+ return JSON.parse(fileContent) as T
13
+ } catch (error) {
14
+ return defaultValue
15
+ }
16
+ }
@@ -0,0 +1,9 @@
1
+ import type { MiddlewareHandler } from 'hono'
2
+ import { MiddlewareType } from '../middleware'
3
+ import { resolveMiddleware } from './resolve'
4
+
5
+ export default function mergeMiddleware(target: Function | any, ...handlers: MiddlewareType[]) {
6
+ const existingMiddlewares: MiddlewareHandler[] = target?.mw || []
7
+ const allMiddlewares = [...existingMiddlewares, ...handlers.flat().map(handler => resolveMiddleware(handler))]
8
+ target.mw = allMiddlewares
9
+ }
@@ -1,6 +1,7 @@
1
1
  import Action, { ActionType } from '../action'
2
+ import { MiddlewareType } from '../middleware'
2
3
 
3
- export default function resolve(obj: ActionType) {
4
+ export function resolve(obj: ActionType) {
4
5
  if (typeof obj === 'function' && obj?.length === 2)
5
6
  return [obj]
6
7
 
@@ -16,3 +17,24 @@ export default function resolve(obj: ActionType) {
16
17
 
17
18
  throw new Error('Invalid action')
18
19
  }
20
+
21
+ export function resolveMiddleware(obj: MiddlewareType) {
22
+ if (typeof obj === 'function' && obj.length === 2)
23
+ return obj
24
+
25
+ if (obj?.handle)
26
+ return obj.handle
27
+
28
+ if (obj.prototype?.handle)
29
+ return (new obj()).handle
30
+
31
+ // if (obj instanceof BaseMiddleware)
32
+ // return obj.handle
33
+
34
+ // if (BaseMiddleware.isPrototypeOf(obj)) {
35
+ // const instance = new (obj as new () => BaseMiddleware)()
36
+ // return instance.handle
37
+ // }
38
+
39
+ throw new Error('Invalid middleware provided. Must be a Hono middleware function or MiddlewareClass instance/constructor')
40
+ }