rajt 0.0.25 → 0.0.26

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
@@ -18,4 +18,4 @@
18
18
 
19
19
  ## License
20
20
 
21
- This package is licensed under the [MIT license](https://github.com/attla/rajt/blob/main/LICENSE) © [Zunq](https://zunq.com).
21
+ This package is licensed under the [MIT license](https://github.com/attla/rajt/blob/main/LICENSE) © [Zunq](https://zunq.com)
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.25",
4
+ "version": "0.0.26",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
@@ -49,6 +49,12 @@
49
49
  "tsx": "^4.19.3",
50
50
  "typescript": "^5.8.3"
51
51
  },
52
+ "devDependencies": {
53
+ "bun-types": "^1.2.14"
54
+ },
55
+ "engines": {
56
+ "node": ">=18.0.0"
57
+ },
52
58
  "resolutions": {
53
59
  "@smithy/types": "^4.3.0"
54
60
  },
package/src/dev.ts CHANGED
@@ -17,7 +17,7 @@ const middlewares = await getMiddlewares()
17
17
  middlewares.forEach(mw => registerMiddleware(mw.handle))
18
18
 
19
19
  Ability.fromRoutes(routes)
20
- Ability.roles = jsonImport('../../../../.rolefile')
20
+ Ability.roles = jsonImport('../../../../roles.json')
21
21
 
22
22
  const fetch = createApp({ routes }).fetch
23
23
 
package/src/enum.ts ADDED
@@ -0,0 +1,75 @@
1
+ type EnumType<T> = T extends string[]
2
+ ? { [K in T[number]]: K }
3
+ : T extends Record<string, string | number>
4
+ ? T
5
+ : never
6
+
7
+ type EnumValue<T> = T extends string[]
8
+ ? T[number]
9
+ : T extends Record<string, infer V>
10
+ ? V
11
+ : never
12
+
13
+ type EnumStatic<T> = {
14
+ has(search: string | number): boolean
15
+ key(search: string | number): string | null
16
+ value(search: string | number): EnumValue<T> | null
17
+ readonly keys: string[]
18
+ readonly values: Array<EnumValue<T>>
19
+ toObject(): EnumType<T>
20
+ } & EnumType<T>
21
+
22
+ export default function Enum<T extends string[] | Record<string, string | number>>(definition: T): EnumStatic<T> {
23
+ const enumObj = Array.isArray(definition)
24
+ ? Object.fromEntries(definition.map(key => [key, key])) as EnumType<T>
25
+ : definition as EnumType<T>
26
+
27
+ const keyToValueMap = new Map<string | number, string | number>()
28
+ const valueToKeyMap = new Map<string | number, string>()
29
+
30
+ Object.entries(enumObj).forEach(([key, value]) => {
31
+ keyToValueMap.set(key, value)
32
+ valueToKeyMap.set(String(value), key)
33
+ if (typeof value === 'number') valueToKeyMap.set(value.toString(), key)
34
+ })
35
+
36
+ const GeneratedEnum = class {
37
+ static has(search: string | number): boolean {
38
+ return keyToValueMap.has(search as string) || valueToKeyMap.has(String(search))
39
+ }
40
+
41
+ static key(search: string | number): string | null {
42
+ if (keyToValueMap.has(search as string))
43
+ return search as string
44
+
45
+ return valueToKeyMap.get(String(search)) || null
46
+ }
47
+
48
+ static value(search: string | number): EnumValue<T> | null {
49
+ if (keyToValueMap.has(search as string))
50
+ return keyToValueMap.get(search as string) as EnumValue<T>
51
+
52
+ return valueToKeyMap.has(String(search))
53
+ ? keyToValueMap.get(valueToKeyMap.get(String(search))!) as EnumValue<T>
54
+ : null
55
+ }
56
+
57
+ static get keys(): string[] {
58
+ return Object.keys(enumObj)
59
+ }
60
+
61
+ static get values(): Array<EnumValue<T>> {
62
+ return Object.values(enumObj) as Array<EnumValue<T>>
63
+ }
64
+
65
+ static toObject(): EnumType<T> {
66
+ return { ...enumObj }
67
+ }
68
+ }
69
+
70
+ Object.entries(enumObj).forEach(([key, value]) => {
71
+ (GeneratedEnum as any)[key] = value
72
+ })
73
+
74
+ return GeneratedEnum as unknown as EnumStatic<T>
75
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { default as Action } from './action'
2
2
  export { default as Middleware } from './middleware'
3
3
  export { default as JsonResponse } from './response'
4
+ export { default as Enum } from './enum'