@stacksjs/router 0.61.22 → 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/dist/index.js +184915 -76269
- package/package.json +5 -5
- package/src/request.ts +59 -3
- package/src/router.ts +12 -9
- package/src/server.ts +245 -54
- package/src/utils.ts +57 -22
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 {
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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 =
|
|
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
|
+
}
|