@stacksjs/router 0.61.24 → 0.63.0

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/src/utils.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { ok } from '@stacksjs/error-handling'
2
- import { route } from './router'
3
2
  import { path } from '@stacksjs/path'
4
-
5
- import { lowercase } from '@stacksjs/strings'
3
+ import { existsSync } from '@stacksjs/storage'
4
+ import { camelCase } from '@stacksjs/strings'
5
+ import { route } from './router'
6
6
 
7
7
  export async function listRoutes() {
8
8
  const routeLists = await route.getRoutes()
@@ -15,48 +15,98 @@ export async function listRoutes() {
15
15
  export function extractModelFromAction(action: string): string {
16
16
  let model = ''
17
17
 
18
- if (action.includes("StoreOrmAction")) {
19
- const match = action.match(/\/([A-Z][a-z]+)StoreOrmAction/);
18
+ if (action.includes('IndexOrmAction')) {
19
+ const match = action.match(/\/([A-Z][a-z]+)IndexOrmAction/)
20
+
21
+ const modelString = match ? match[1] : ''
22
+
23
+ model = modelString as string
24
+ }
25
+
26
+ if (action.includes('StoreOrmAction')) {
27
+ const match = action.match(/\/([A-Z][a-z]+)StoreOrmAction/)
28
+
29
+ const modelString = match ? match[1] : ''
20
30
 
21
- const modelString = match ? match[1] : '';
22
-
23
31
  model = modelString as string
24
32
  }
25
33
 
26
- if (action.includes("ShowOrmAction")) {
27
- const match = action.match(/\/([A-Z][a-z]+)ShowOrmAction/);
28
- const modelString = match ? match[1] : '';
29
-
34
+ if (action.includes('ShowOrmAction')) {
35
+ const match = action.match(/\/([A-Z][a-z]+)ShowOrmAction/)
36
+ const modelString = match ? match[1] : ''
37
+
30
38
  model = modelString as string
31
39
  }
32
40
 
33
- if (action.includes("UpdateOrmAction")) {
34
- const match = action.match(/\/([A-Z][a-z]+)UpdateOrmAction/);
35
- const modelString = match ? match[1] : '';
36
-
41
+ if (action.includes('UpdateOrmAction')) {
42
+ const match = action.match(/\/([A-Z][a-z]+)UpdateOrmAction/)
43
+ const modelString = match ? match[1] : ''
44
+
37
45
  model = modelString as string
38
46
  }
39
47
 
40
- if (action.includes("DestroyOrmAction")) {
41
- const match = action.match(/\/([A-Z][a-z]+)DestroyOrmAction/);
42
- const modelString = match ? match[1] : '';
43
-
48
+ if (action.includes('DestroyOrmAction')) {
49
+ const match = action.match(/\/([A-Z][a-z]+)DestroyOrmAction/)
50
+ const modelString = match ? match[1] : ''
51
+
44
52
  model = modelString as string
45
53
  }
46
54
 
47
55
  return model
48
56
  }
49
57
 
58
+ export function extractDynamicAction(action: string): string | undefined {
59
+ const regex = /Actions\/(.*?)Action/
60
+
61
+ const match = action.match(regex)
62
+
63
+ return match ? match[1] : ''
64
+ }
65
+
50
66
  export async function extractModelRequest(action: string) {
51
67
  const extractedModel = extractModelFromAction(action)
52
68
 
53
- const lowerCaseModel = lowercase(extractedModel)
69
+ const lowerCaseModel = camelCase(extractedModel)
54
70
 
55
- const requestPath = path.projectStoragePath(`framework/requests/${extractedModel}Request.ts`)
71
+ const requestPath = path.frameworkPath(`requests/${extractedModel}Request.ts`)
56
72
 
57
73
  const requestInstance = await import(requestPath)
58
74
 
59
75
  const requestIndex = `${lowerCaseModel}Request`
60
-
76
+
61
77
  return requestInstance[requestIndex]
62
- }
78
+ }
79
+
80
+ export async function findRequestInstance(requestInstance: string) {
81
+ const frameworkDirectory = path.projectStoragePath('framework/requests')
82
+
83
+ const filePath = path.join(frameworkDirectory, `${requestInstance}.ts`)
84
+
85
+ const pathExists = await existsSync(filePath)
86
+
87
+ // Check if the directory exists
88
+ if (pathExists) {
89
+ const requestInstance = await import(filePath)
90
+
91
+ return requestInstance.request
92
+ }
93
+
94
+ const defaultRequestPath = path.projectStoragePath('framework/core/router/src/request.ts')
95
+
96
+ const fileExists = await existsSync(defaultRequestPath)
97
+
98
+ if (fileExists) {
99
+ const requestInstance = await import(defaultRequestPath)
100
+
101
+ return requestInstance.request
102
+ }
103
+
104
+ return null
105
+ }
106
+
107
+ export async function extractDefaultRequest(action: string) {
108
+ const requestPath = path.frameworkPath(`core/router/src/request.ts`)
109
+ const requestInstance = await import(requestPath)
110
+
111
+ return requestInstance.request
112
+ }