rajt 0.0.18 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rajt",
3
3
  "description": "A serverless bundler layer, fully typed for AWS Lambda (Node.js and LLRT) and Cloudflare Workers.",
4
- "version": "0.0.18",
4
+ "version": "0.0.19",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
package/src/auth/token.ts CHANGED
@@ -7,14 +7,7 @@ export class Token {
7
7
  static #prefix: string = 'bearer'
8
8
 
9
9
  static fromRequest(c: Context) {
10
- // const token = this.fromHeader(c.req)
11
- // Mock user
12
- const token = this.create(c.req, {
13
- name: 'Nicolau',
14
- email: 'nicolau@zunq.com',
15
- role: 'user',
16
- }).get()
17
-
10
+ const token = this.fromHeader(c.req)
18
11
  return token ? this.parse(c.req, token) : null
19
12
  }
20
13
 
package/src/dev.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { config } from 'dotenv'
2
2
  import { serve } from '@hono/node-server'
3
3
  import createApp from './create-app'
4
- import getRoutes from './routes'
4
+ import { getRoutes, getMiddlewares } from './routes'
5
5
  import { Ability } from './auth'
6
6
  import { getAvailablePort } from './utils/port'
7
7
  import jsonImport from './utils/json-import'
@@ -9,6 +9,7 @@ import jsonImport from './utils/json-import'
9
9
  config({ path: '../../.env.dev' })
10
10
 
11
11
  const routes = await getRoutes()
12
+ const middlewares = await getMiddlewares()
12
13
  Ability.fromRoutes(routes)
13
14
  Ability.roles = jsonImport('../../../../.rolefile')
14
15
 
package/src/routes.ts CHANGED
@@ -8,9 +8,9 @@ import { isAnonFn } from './utils/func'
8
8
  const __filename = fileURLToPath(import.meta.url)
9
9
  const __dirname = dirname(__filename)
10
10
 
11
- export default async function getRoutes(
11
+ export async function getRoutes(
12
12
  all: boolean = false,
13
- dirs: string[] = ['actions', 'features', 'errors', 'middlewares']
13
+ dirs: string[] = ['actions', 'features']
14
14
  ): Promise<Route[]> {
15
15
  const routes: Route[] = []
16
16
 
@@ -66,3 +66,54 @@ export default async function getRoutes(
66
66
  await Promise.all(dirs.map(dir => walk(resolve(__dirname, '../../..', dir), dir)))
67
67
  return routes
68
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,6 +1,6 @@
1
1
  import { existsSync, writeFileSync } from 'node:fs'
2
2
  import { config } from 'dotenv'
3
- import getRoutes from '../routes'
3
+ import { getRoutes, getMiddlewares } from '../routes'
4
4
  import ensureDir from '../utils/ensuredir'
5
5
 
6
6
  config({ path: '../../.env.dev' })
@@ -11,6 +11,7 @@ async function cacheRoutes() {
11
11
  writeFileSync(rolePath, '{}')
12
12
 
13
13
  const routes = await getRoutes(true)
14
+ const middlewares = await getMiddlewares()
14
15
 
15
16
  const iPath = '../../tmp/import-routes.mjs'
16
17
  ensureDir(iPath)
@@ -18,6 +19,7 @@ async function cacheRoutes() {
18
19
  import { registerHandler } from '../node_modules/rajt/src/register'
19
20
 
20
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')}
21
23
 
22
24
  try {
23
25
  const handlers = {${routes.map(r => r.name).join()}}