Skip to content

Commit 9ec1220

Browse files
chore: wip
1 parent 107ef7b commit 9ec1220

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

storage/framework/core/router/src/router.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,7 @@ export class Router implements RouterInterface {
303303
// if fails, return validation error
304304

305305
const requestInstance = await extractModelRequest(modulePath)
306-
307-
console.log(requestInstance)
308-
306+
309307
return await actionModule.default.handle(requestInstance)
310308
}
311309

storage/framework/core/router/src/server.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import process from 'node:process'
22
import { log } from '@stacksjs/logging'
3-
import { extname } from '@stacksjs/path'
3+
import { extname, path } from '@stacksjs/path'
4+
import { glob } from '@stacksjs/storage'
45
import type { Route, RouteParam, StatusCode } from '@stacksjs/types'
56
import { route } from '.'
67
import { middlewares } from './middleware'
78
import { request as RequestParam } from './request'
9+
import { getModelName } from '@stacksjs/orm'
10+
import { lowercase } from '@stacksjs/strings'
811

912
interface ServeOptions {
1013
host?: string
@@ -71,8 +74,8 @@ export async function serverResponse(req: Request) {
7174

7275
const routeParams = extractDynamicSegments(foundRoute.uri, url.pathname)
7376

74-
addRouteQuery(url)
75-
addRouteParam(routeParams)
77+
await addRouteQuery(url)
78+
await addRouteParam(routeParams)
7679

7780
await executeMiddleware(foundRoute)
7881

@@ -146,13 +149,43 @@ function noCache(response: Response) {
146149
return response
147150
}
148151

149-
function addRouteQuery(url: URL): void {
150-
if (!isObjectNotEmpty(url.searchParams)) RequestParam.addQuery(url)
152+
async function addRouteQuery(url: URL) {
153+
const modelFiles = glob.sync(path.userModelsPath('*.ts'));
151154

152-
// requestInstance.extractParamsFromRoute(route.uri, url.pathname)
155+
for (const modelFile of modelFiles) {
156+
const model = (await import(modelFile)).default;
157+
const modelName = getModelName(model, modelFile);
158+
const modelNameLower = `${lowercase(modelName)}Request`;
159+
const requestPath = path.projectStoragePath(`framework/requests/${modelName}Request.ts`);
160+
const requestImport = await import(requestPath);
161+
const requestInstance = requestImport[modelNameLower];
162+
163+
if (requestInstance && !isObjectNotEmpty(url.searchParams)) {
164+
requestInstance.addQuery(url);
165+
}
166+
}
167+
168+
if (!isObjectNotEmpty(url.searchParams)) {
169+
RequestParam.addQuery(url);
170+
}
153171
}
154172

155-
function addRouteParam(param: RouteParam): void {
173+
async function addRouteParam(param: RouteParam): Promise<void> {
174+
const modelFiles = glob.sync(path.userModelsPath('*.ts'));
175+
176+
for (const modelFile of modelFiles) {
177+
const model = (await import(modelFile)).default;
178+
const modelName = getModelName(model, modelFile);
179+
const modelNameLower = `${lowercase(modelName)}Request`;
180+
const requestPath = path.projectStoragePath(`framework/requests/${modelName}Request.ts`);
181+
const requestImport = await import(requestPath);
182+
const requestInstance = requestImport[modelNameLower];
183+
184+
if (requestInstance) {
185+
requestInstance.addParam(param);
186+
}
187+
}
188+
156189
RequestParam.addParam(param)
157190
}
158191

storage/framework/requests/UserRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ public password = ''
2222
}
2323
}
2424

25-
export const userRequest = new UserRequest()
25+
export const userRequest = new UserRequest()
2626

0 commit comments

Comments
 (0)