rajt 0.0.103 → 0.0.104
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 +1 -1
- package/src/auth/ability.ts +26 -6
- package/src/auth/middlewares.ts +2 -7
- package/src/cli/utils.ts +1 -1
- package/src/config.ts +2 -75
- package/src/routes.ts +1 -2
package/package.json
CHANGED
package/src/auth/ability.ts
CHANGED
|
@@ -1,21 +1,41 @@
|
|
|
1
|
-
import { Routes } from '../types'
|
|
1
|
+
import type { IRequest, Routes } from '../types'
|
|
2
2
|
import { Roles, Abilities } from './types'
|
|
3
|
+
import { verbAlias } from '../http'
|
|
3
4
|
|
|
4
5
|
export class Ability {
|
|
5
6
|
static #roles: Roles = {}
|
|
6
7
|
static #abilities: Abilities = []
|
|
8
|
+
static #actions: Record<string, string> = {}
|
|
7
9
|
|
|
8
10
|
static empty() {
|
|
9
11
|
this.#roles = {}
|
|
10
12
|
this.#abilities = []
|
|
11
13
|
}
|
|
12
14
|
|
|
13
|
-
static fromRoutes(
|
|
14
|
-
if (!
|
|
15
|
+
static fromRoutes(routes: Routes) {
|
|
16
|
+
if (!routes?.length) return
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
const actions: Record<string, string> = {}
|
|
19
|
+
const abilities = new Set()
|
|
20
|
+
for (const route of routes) {
|
|
21
|
+
const isArr = Array.isArray(route)
|
|
22
|
+
const name = isArr ? route[3] : route.name
|
|
23
|
+
if (!name || abilities.has(name)) continue
|
|
24
|
+
|
|
25
|
+
abilities.add(name)
|
|
26
|
+
actions[(isArr ? route[1] : route.path) +'$'+ verbAlias[isArr ? route[0] : route.method]] = name
|
|
27
|
+
const formatted = this.format(name)
|
|
28
|
+
|
|
29
|
+
if (formatted)
|
|
30
|
+
this.#abilities.push(formatted)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
this.#actions = actions
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static fromRequest(req: IRequest) {
|
|
37
|
+
const ability = this.#actions[`${req.routePath}$` + verbAlias[req.method.toLowerCase()]]
|
|
38
|
+
return ability ? this.format(ability) : ''
|
|
19
39
|
}
|
|
20
40
|
|
|
21
41
|
static fromAction(target: any): string {
|
package/src/auth/middlewares.ts
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
import { Ability } from './ability'
|
|
2
2
|
import response from '../response'
|
|
3
3
|
import { GET_REQUEST } from '../request'
|
|
4
|
-
import
|
|
5
|
-
import { verbAlias } from '../http'
|
|
6
|
-
import type {
|
|
7
|
-
Context, Next,
|
|
8
|
-
IRequest,
|
|
9
|
-
} from '../types'
|
|
4
|
+
import type { Context, Next, IRequest } from '../types'
|
|
10
5
|
|
|
11
6
|
export async function Autorized(c: Context, next: Next) {
|
|
12
7
|
const req = c.get(GET_REQUEST as unknown as string) as IRequest
|
|
13
|
-
const ability = Ability.
|
|
8
|
+
const ability = Ability.fromRequest(req)
|
|
14
9
|
|
|
15
10
|
if (!req?.user || !ability || req.cant(ability))
|
|
16
11
|
return response.unauthorized()
|
package/src/cli/utils.ts
CHANGED
|
@@ -142,7 +142,7 @@ export const build = async (platform: Platform, env: string = 'prd') => {
|
|
|
142
142
|
const opts = {
|
|
143
143
|
entryPoints: [join(_rajt, `prod${platform}.ts`)],
|
|
144
144
|
bundle: true,
|
|
145
|
-
minify:
|
|
145
|
+
minify: false,
|
|
146
146
|
outfile: join(_root, dist +'/index.js'),
|
|
147
147
|
format: 'esm',
|
|
148
148
|
target: isCF ? 'es2022' : 'node20',
|
package/src/config.ts
CHANGED
|
@@ -1,77 +1,4 @@
|
|
|
1
1
|
import { DataBag } from 't0n'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
static {
|
|
7
|
-
this.#c = new DataBag()
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
static all<T = any>(): Record<string, T> {
|
|
11
|
-
return this.#c.all()
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
static has(key: string): boolean {
|
|
15
|
-
return this.#c.has(key)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
static add<T = any>(data: Record<string, T> = {}) {
|
|
19
|
-
this.#c.add(data)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
static replace<T = any>(data: Record<string, T> = {}) {
|
|
23
|
-
this.#c.replace(data)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
static get<T = any>(key: string, defaultValue?: T): T {
|
|
27
|
-
return this.#c.get(key, defaultValue)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
static set<T = any>(key: string, value: T) {
|
|
31
|
-
this.#c.set(key, value)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
static remove(key: string) {
|
|
35
|
-
this.#c.remove(key)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
static clear() {
|
|
39
|
-
this.#c.clear()
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// @ts-ignore
|
|
43
|
-
static get length(): number {
|
|
44
|
-
return this.#c.length
|
|
45
|
-
}
|
|
46
|
-
static get size(): number {
|
|
47
|
-
return this.#c.length
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
static keys(): string[] {
|
|
51
|
-
return this.#c.keys()
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
static values<T = any>(): T[] {
|
|
55
|
-
return this.#c.values()
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
static entries<T = any>(): [string, T][] {
|
|
59
|
-
return this.#c.entries()
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
static toArray<T = any>(): [string, T][] {
|
|
63
|
-
return this.#c.entries()
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
static jsonSerialize<T = any>(): Record<string, T> {
|
|
67
|
-
return this.#c.all()
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
static toJson(options: number = 0): string {
|
|
71
|
-
return this.#c.toJson(options)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
static [Symbol.iterator](): IterableIterator<[string, any]> {
|
|
75
|
-
return this.#c[Symbol.iterator]()
|
|
76
|
-
}
|
|
77
|
-
}
|
|
3
|
+
const Config = new DataBag()
|
|
4
|
+
export default Config
|
package/src/routes.ts
CHANGED
|
@@ -328,7 +328,7 @@ async function dependencyPath(lib: string) {
|
|
|
328
328
|
export async function cacheRoutes() {
|
|
329
329
|
const env = Object.entries(
|
|
330
330
|
config({ path: join(_root, '.env.prod') })?.parsed || {}
|
|
331
|
-
).filter(([key, val]) => key?.toLowerCase().startsWith('aws')) // prevent AWS credentials
|
|
331
|
+
).filter(([key, val]) => !key?.toLowerCase().startsWith('aws')) // prevent AWS credentials
|
|
332
332
|
|
|
333
333
|
const version = versionSHA('../../.git') // @ts-ignore
|
|
334
334
|
env.push(['VERSION_SHA', process.env['VERSION_SHA'] = version]) // @ts-ignore
|
|
@@ -387,7 +387,6 @@ import { registerHandler, registerMiddleware } from '${_rajtDir}/src/register'
|
|
|
387
387
|
${handlers.map(([file, name, _export]) => `\nimport ${_export ? `{ ${name} }` : name} from '${_rajtDir}/src/${file}'\nregisterHandler('${name}', ${name})`).join('\n')}
|
|
388
388
|
|
|
389
389
|
${Object.entries(openApi)?.length ? `registerHandler('RAJT_OPENAPI', ${stringifyToJS(openApi)})` : ''}
|
|
390
|
-
Config.set('routes', ${stringifyToJS(Object.fromEntries(routes.map(r => ([r.path+'$'+verbAlias[r.method], r.name]))))})
|
|
391
390
|
|
|
392
391
|
${routes.map(r => `import ${r.name} from '../${normalizeImportPath(r.file)}'`).join('\n')}
|
|
393
392
|
${middlewares.map(r => `import ${r.name} from '../${normalizeImportPath(r.file)}'`).join('\n')}
|