@stacksjs/router 0.61.24 → 0.62.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 { glob } 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,83 @@ 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('StoreOrmAction')) {
19
+ const match = action.match(/\/([A-Z][a-z]+)StoreOrmAction/)
20
+
21
+ const modelString = match ? match[1] : ''
20
22
 
21
- const modelString = match ? match[1] : '';
22
-
23
23
  model = modelString as string
24
24
  }
25
25
 
26
- if (action.includes("ShowOrmAction")) {
27
- const match = action.match(/\/([A-Z][a-z]+)ShowOrmAction/);
28
- const modelString = match ? match[1] : '';
29
-
26
+ if (action.includes('ShowOrmAction')) {
27
+ const match = action.match(/\/([A-Z][a-z]+)ShowOrmAction/)
28
+ const modelString = match ? match[1] : ''
29
+
30
30
  model = modelString as string
31
31
  }
32
32
 
33
- if (action.includes("UpdateOrmAction")) {
34
- const match = action.match(/\/([A-Z][a-z]+)UpdateOrmAction/);
35
- const modelString = match ? match[1] : '';
36
-
33
+ if (action.includes('UpdateOrmAction')) {
34
+ const match = action.match(/\/([A-Z][a-z]+)UpdateOrmAction/)
35
+ const modelString = match ? match[1] : ''
36
+
37
37
  model = modelString as string
38
38
  }
39
39
 
40
- if (action.includes("DestroyOrmAction")) {
41
- const match = action.match(/\/([A-Z][a-z]+)DestroyOrmAction/);
42
- const modelString = match ? match[1] : '';
43
-
40
+ if (action.includes('DestroyOrmAction')) {
41
+ const match = action.match(/\/([A-Z][a-z]+)DestroyOrmAction/)
42
+ const modelString = match ? match[1] : ''
43
+
44
44
  model = modelString as string
45
45
  }
46
46
 
47
47
  return model
48
48
  }
49
49
 
50
+ export function extractDynamicAction(action: string): string | undefined {
51
+ const regex = /Actions\/(.*?)Action/
52
+
53
+ const match = action.match(regex)
54
+
55
+ return match ? match[1] : ''
56
+ }
57
+
50
58
  export async function extractModelRequest(action: string) {
51
59
  const extractedModel = extractModelFromAction(action)
52
60
 
53
- const lowerCaseModel = lowercase(extractedModel)
61
+ const lowerCaseModel = camelCase(extractedModel)
62
+
63
+ const requestPath = path.projectStoragePath(`framework/requests/${extractedModel}Request.ts`)
64
+
65
+ const requestInstance = await import(requestPath)
66
+
67
+ const requestIndex = `${lowerCaseModel}Request`
68
+
69
+ return requestInstance[requestIndex]
70
+ }
71
+
72
+ export async function extractDynamicRequest(action: string) {
73
+ const extractedModel = extractDynamicAction(action) || ''
74
+
75
+ const lowerCaseModel = camelCase(extractedModel)
76
+
77
+ const modelFiles = glob.sync(path.userModelsPath('*.ts'))
78
+
79
+ const fileToCheck = `${extractedModel}.ts`
80
+
81
+ const fileExists = modelFiles.some((file) => file.split('/').pop() === fileToCheck)
82
+
83
+ if (!fileExists) {
84
+ const requestPath = path.projectStoragePath(`framework/core/router/src/request.ts`)
85
+ const requestInstance = await import(requestPath)
86
+
87
+ return requestInstance.request
88
+ }
54
89
 
55
90
  const requestPath = path.projectStoragePath(`framework/requests/${extractedModel}Request.ts`)
56
91
 
57
92
  const requestInstance = await import(requestPath)
58
93
 
59
94
  const requestIndex = `${lowerCaseModel}Request`
60
-
95
+
61
96
  return requestInstance[requestIndex]
62
- }
97
+ }