rajt 0.0.52 → 0.0.54

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
@@ -11,10 +11,10 @@
11
11
 
12
12
  ## Packages
13
13
 
14
- | Package | Version (click for changelogs) |
15
- | ----------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------- |
16
- | [rajt](https://github.com/attla/rajt) | [![rajt version](https://img.shields.io/npm/v/rajt.svg?label=%20)](https://github.com/attla/rajt/CHANGELOG.md) |
17
- | [create-rajt](https://github.com/attla/create-rajt) | [![create-rajt version](https://img.shields.io/npm/v/create-rajt.svg?label=%20)](https://github.com/attla/create-rajt/CHANGELOG.md) |
14
+ | Package | Version |
15
+ | --------------------------------------------------- | :----------------------------------------------------------------------------- |
16
+ | [rajt](https://github.com/attla/rajt) | ![rajt version](https://img.shields.io/npm/v/rajt.svg?label=%20) |
17
+ | [create-rajt](https://github.com/attla/create-rajt) | ![create-rajt version](https://img.shields.io/npm/v/create-rajt.svg?label=%20) |
18
18
 
19
19
  ## License
20
20
 
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.52",
4
+ "version": "0.0.54",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
package/src/http.ts CHANGED
@@ -4,32 +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, path = '/') {
8
- return function (target: any) {
9
- target.m = method
10
- target.p = path
11
- target.mw = []
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 = []
19
+ }
20
+
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)
26
+ }
27
+
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)
33
+ }
34
+
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)
40
+ }
41
+
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)
13
47
  }
14
48
 
15
- export function Get(path = '/') {
16
- return method('get', path)
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)
17
54
  }
18
55
 
19
- export function Post(path = '/') {
20
- return method('post', path)
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)
21
61
  }
22
62
 
23
- export function Put(path = '/') {
24
- return method('put', path)
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)
25
68
  }
26
69
 
27
- export function Patch(path = '/') {
28
- return method('patch', path)
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)
29
75
  }
30
76
 
31
- export function Delete(path = '/') {
32
- return method('delete', path)
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)
33
82
  }
34
83
 
35
84
  export function Middleware(...handlers: MiddlewareType[]) {
package/src/routes.ts CHANGED
@@ -36,7 +36,6 @@ const walk = async (dir: string, baseDir: string, fn: Function, parentMw: string
36
36
  }
37
37
  }
38
38
 
39
-
40
39
  export async function getRoutes(
41
40
  dirs: string[] = ['actions', 'features']
42
41
  ): Promise<Route[]> {
@@ -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
- config({ path: '../../.env.dev' })
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')}
@@ -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
+ }