rajt 0.0.53 → 0.0.55
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/dev.ts +3 -0
- package/src/http.ts +57 -24
- package/src/scripts/cache-routes.ts +10 -1
- package/src/utils/environment.ts +21 -7
- package/src/utils/version-sha.ts +19 -0
package/package.json
CHANGED
package/src/dev.ts
CHANGED
|
@@ -6,6 +6,9 @@ import { registerHandler, registerMiddleware } from './register'
|
|
|
6
6
|
import { Ability } from './auth'
|
|
7
7
|
import { getAvailablePort } from './utils/port'
|
|
8
8
|
import jsonImport from './utils/json-import'
|
|
9
|
+
import { setEnv, detectEnvironment } from './utils/environment'
|
|
10
|
+
|
|
11
|
+
setEnv(detectEnvironment())
|
|
9
12
|
|
|
10
13
|
config({ path: '../../.env.dev' })
|
|
11
14
|
|
package/src/http.ts
CHANGED
|
@@ -4,48 +4,81 @@ import Response from './response'
|
|
|
4
4
|
import { Ability, Auth as Gate } from './auth'
|
|
5
5
|
import mergeMiddleware from './utils/merge-middleware'
|
|
6
6
|
|
|
7
|
-
function method(method: string,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
function method(method: string, ...args: any[]): void | ClassDecorator {
|
|
8
|
+
if (args.length === 1 && typeof args[0] === 'function')
|
|
9
|
+
return _method(method, '/', args[0])
|
|
10
|
+
|
|
11
|
+
const path = typeof args[0] === 'string' ? args[0] : '/'
|
|
12
|
+
return (target: Function) => _method(method, path, target)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function _method(method: string, path = '/', target: Function | any) {
|
|
16
|
+
target.m = method
|
|
17
|
+
target.p = path
|
|
18
|
+
target.mw = []
|
|
13
19
|
}
|
|
14
20
|
|
|
15
|
-
export function Get(
|
|
16
|
-
|
|
21
|
+
export function Get(): ClassDecorator
|
|
22
|
+
export function Get(target: Function): void
|
|
23
|
+
export function Get(path: string): ClassDecorator
|
|
24
|
+
export function Get(...args: any[]): void | ClassDecorator {
|
|
25
|
+
return method('get', ...args)
|
|
17
26
|
}
|
|
18
27
|
|
|
19
|
-
export function Post(
|
|
20
|
-
|
|
28
|
+
export function Post(): ClassDecorator
|
|
29
|
+
export function Post(target: Function): void
|
|
30
|
+
export function Post(path: string): ClassDecorator
|
|
31
|
+
export function Post(...args: any[]): void | ClassDecorator {
|
|
32
|
+
return method('post', ...args)
|
|
21
33
|
}
|
|
22
34
|
|
|
23
|
-
export function Put(
|
|
24
|
-
|
|
35
|
+
export function Put(): ClassDecorator
|
|
36
|
+
export function Put(target: Function): void
|
|
37
|
+
export function Put(path: string): ClassDecorator
|
|
38
|
+
export function Put(...args: any[]): void | ClassDecorator {
|
|
39
|
+
return method('put', ...args)
|
|
25
40
|
}
|
|
26
41
|
|
|
27
|
-
export function Patch(
|
|
28
|
-
|
|
42
|
+
export function Patch(): ClassDecorator
|
|
43
|
+
export function Patch(target: Function): void
|
|
44
|
+
export function Patch(path: string): ClassDecorator
|
|
45
|
+
export function Patch(...args: any[]): void | ClassDecorator {
|
|
46
|
+
return method('patch', ...args)
|
|
29
47
|
}
|
|
30
48
|
|
|
31
|
-
export function Delete(
|
|
32
|
-
|
|
49
|
+
export function Delete(): ClassDecorator
|
|
50
|
+
export function Delete(target: Function): void
|
|
51
|
+
export function Delete(path: string): ClassDecorator
|
|
52
|
+
export function Delete(...args: any[]): void | ClassDecorator {
|
|
53
|
+
return method('delete', ...args)
|
|
33
54
|
}
|
|
34
55
|
|
|
35
|
-
export function Head(
|
|
36
|
-
|
|
56
|
+
export function Head(): ClassDecorator
|
|
57
|
+
export function Head(target: Function): void
|
|
58
|
+
export function Head(path: string): ClassDecorator
|
|
59
|
+
export function Head(...args: any[]): void | ClassDecorator {
|
|
60
|
+
return method('head', ...args)
|
|
37
61
|
}
|
|
38
62
|
|
|
39
|
-
export function Options(
|
|
40
|
-
|
|
63
|
+
export function Options(): ClassDecorator
|
|
64
|
+
export function Options(target: Function): void
|
|
65
|
+
export function Options(path: string): ClassDecorator
|
|
66
|
+
export function Options(...args: any[]): void | ClassDecorator {
|
|
67
|
+
return method('options', ...args)
|
|
41
68
|
}
|
|
42
69
|
|
|
43
|
-
export function Connect(
|
|
44
|
-
|
|
70
|
+
export function Connect(): ClassDecorator
|
|
71
|
+
export function Connect(target: Function): void
|
|
72
|
+
export function Connect(path: string): ClassDecorator
|
|
73
|
+
export function Connect(...args: any[]): void | ClassDecorator {
|
|
74
|
+
return method('connect', ...args)
|
|
45
75
|
}
|
|
46
76
|
|
|
47
|
-
export function Trace(
|
|
48
|
-
|
|
77
|
+
export function Trace(): ClassDecorator
|
|
78
|
+
export function Trace(target: Function): void
|
|
79
|
+
export function Trace(path: string): ClassDecorator
|
|
80
|
+
export function Trace(...args: any[]): void | ClassDecorator {
|
|
81
|
+
return method('trace', ...args)
|
|
49
82
|
}
|
|
50
83
|
|
|
51
84
|
export function Middleware(...handlers: MiddlewareType[]) {
|
|
@@ -2,8 +2,15 @@ import { existsSync, writeFileSync } from 'node:fs'
|
|
|
2
2
|
import { config } from 'dotenv'
|
|
3
3
|
import { getRoutes, getMiddlewares } from '../routes'
|
|
4
4
|
import ensureDir from '../utils/ensuredir'
|
|
5
|
+
import versionSHA from '../utils/version-sha'
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
const env = Object.entries(
|
|
8
|
+
config({ path: '../../.env.prod' })?.parsed || {}
|
|
9
|
+
).filter(([key, val]) => key?.toLowerCase().indexOf('aws') != 0) // prevent AWS credentials
|
|
10
|
+
|
|
11
|
+
const version = versionSHA('../../.git')
|
|
12
|
+
env.push(['VERSION_SHA', process.env['VERSION_SHA'] = version])
|
|
13
|
+
env.push(['VERSION_HASH', process.env['VERSION_HASH'] = version?.substring(0, 7)])
|
|
7
14
|
|
|
8
15
|
async function cacheRoutes() {
|
|
9
16
|
const rolePath = '../../roles.json'
|
|
@@ -16,6 +23,8 @@ async function cacheRoutes() {
|
|
|
16
23
|
const iPath = '../../tmp/import-routes.mjs'
|
|
17
24
|
ensureDir(iPath)
|
|
18
25
|
writeFileSync(iPath, `// AUTO-GENERATED FILE - DO NOT EDIT
|
|
26
|
+
${env.map(([key, val]) => `process.env.${key} = ${JSON.stringify(val)}`).join('\n')}
|
|
27
|
+
|
|
19
28
|
import { registerHandler, registerMiddleware } from '../node_modules/rajt/src/register'
|
|
20
29
|
|
|
21
30
|
${routes.map(r => `import ${r.name} from '../${normalizePath(r.file)}'`).join('\n')}
|
package/src/utils/environment.ts
CHANGED
|
@@ -1,19 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
const prd = Symbol('prd')
|
|
2
|
+
const dev = Symbol('dev')
|
|
3
|
+
|
|
4
|
+
let env = prd
|
|
5
|
+
|
|
6
|
+
export const getEnv = () => env
|
|
7
|
+
export const setEnv = (e: symbol) => env = e
|
|
8
|
+
|
|
9
|
+
export function detectEnvironment() {
|
|
2
10
|
try {
|
|
3
11
|
if (
|
|
4
|
-
process.env?.npm_lifecycle_event
|
|
12
|
+
process.env?.npm_lifecycle_event == 'dev'
|
|
5
13
|
|| process.env?.AWS_SAM_LOCAL
|
|
6
|
-
|| process?.argv?.includes('--dev')
|
|
14
|
+
// || process?.argv?.includes('--dev')
|
|
15
|
+
|| process?.argv?.some(arg => ['--dev', '--development', '--watch'].includes(arg))
|
|
7
16
|
|| process?.execArgv?.includes('--watch')
|
|
8
17
|
|| import.meta.url?.includes('localhost')
|
|
9
18
|
) {
|
|
10
|
-
return
|
|
19
|
+
return dev
|
|
11
20
|
}
|
|
12
21
|
} catch (e) { }
|
|
13
22
|
|
|
14
|
-
return
|
|
23
|
+
return prd
|
|
15
24
|
}
|
|
16
25
|
|
|
17
|
-
export const
|
|
18
|
-
|
|
26
|
+
export const isEnv = (e: symbol) => env == e
|
|
27
|
+
|
|
28
|
+
export const isDev = () => env == dev
|
|
29
|
+
export const isProd = () => env == prd
|
|
30
|
+
|
|
31
|
+
export const isDevelopment = isDev
|
|
32
|
+
export const isProduction = isProd
|
|
19
33
|
export const isPrd = isProd
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
export default function getLastCommitHash(path: string = '.git') {
|
|
5
|
+
try {
|
|
6
|
+
const gitDir = join(process.cwd(), path)
|
|
7
|
+
const headPath = join(gitDir, 'HEAD')
|
|
8
|
+
|
|
9
|
+
let headContent = readFileSync(headPath, 'utf8').trim()
|
|
10
|
+
|
|
11
|
+
if (headContent.startsWith('ref:'))
|
|
12
|
+
headContent = readFileSync(join(gitDir, headContent.substring(5)), 'utf8').trim()
|
|
13
|
+
|
|
14
|
+
return headContent
|
|
15
|
+
} catch (e) {
|
|
16
|
+
console.error('Error reading HEAD file: ', e?.message)
|
|
17
|
+
return null
|
|
18
|
+
}
|
|
19
|
+
}
|