Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
glennmichael123 committed Jun 20, 2024
1 parent a84817a commit c582b27
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app/Actions/SubscriberEmailAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export default new Action({
async handle(request: SubscriberEmailRequestType) {
const email = request.get('email')

await request.validate()

const model = await SubscriberEmail.create({ email })

return model
Expand Down
12 changes: 6 additions & 6 deletions storage/framework/core/router/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export class Request implements RequestInstance {
return Request.instance
}

public addQuery(url: URL, params: any): void {
if (Object.keys(url.searchParams).length === 0) {
this.query = params
} else {
this.query = Object.fromEntries(url.searchParams)
}
public addQuery(url: URL): void {
this.query = Object.fromEntries(url.searchParams)
}

public addBodies(params: any): void {
this.query = params
}

public addParam(param: RouteParam): void {
Expand Down
37 changes: 31 additions & 6 deletions storage/framework/core/router/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ export async function serverResponse(req: Request, body: string) {

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

await addRouteQuery(url, body)
if (!body) {
await addRouteQuery(url)
} else {
await addBody(body)
}

await addRouteParam(routeParams)
await addHeaders(req.headers)

Expand Down Expand Up @@ -214,7 +219,9 @@ async function execute(foundRoute: Route, req: Request, { statusCode }: Options)

if (isObject(foundCallback) && foundCallback.status) {
if (foundCallback.status === 422) {
// biome-ignore lint/performance/noDelete: <explanation>
delete foundCallback.status

return await new Response(JSON.stringify(foundCallback), {
headers: {
'Content-Type': 'json',
Expand All @@ -229,6 +236,7 @@ async function execute(foundRoute: Route, req: Request, { statusCode }: Options)
if (isObject(foundCallback) && foundCallback.status) {
if (foundCallback.status === 401) {
delete foundCallback.status

return await new Response(JSON.stringify(foundCallback), {
headers: {
'Content-Type': 'json',
Expand Down Expand Up @@ -283,7 +291,7 @@ function noCache(response: Response) {
return response
}

async function addRouteQuery(url: URL, body: string) {
async function addRouteQuery(url: URL) {
const modelFiles = glob.sync(path.userModelsPath('*.ts'))

for (const modelFile of modelFiles) {
Expand All @@ -294,14 +302,31 @@ async function addRouteQuery(url: URL, body: string) {
const requestImport = await import(requestPath)
const requestInstance = requestImport[modelNameLower]

if (requestInstance && !isObjectNotEmpty(url.searchParams)) {
requestInstance.addQuery(url, JSON.parse(body))
if (requestInstance) {
requestInstance.addQuery(url)
}
}

if (!isObjectNotEmpty(url.searchParams)) {
RequestParam.addQuery(url, JSON.parse(body))
RequestParam.addQuery(url)
}

async function addBody(params: any) {
const modelFiles = glob.sync(path.userModelsPath('*.ts'))

for (const modelFile of modelFiles) {
const model = (await import(modelFile)).default
const modelName = getModelName(model, modelFile)
const modelNameLower = `${camelCase(modelName)}Request`
const requestPath = path.projectStoragePath(`framework/requests/${modelName}Request.ts`)
const requestImport = await import(requestPath)
const requestInstance = requestImport[modelNameLower]

if (requestInstance) {
requestInstance.addBodies(JSON.parse(params))
}
}

RequestParam.addBodies(JSON.parse(params))
}

async function addRouteParam(param: RouteParam): Promise<void> {
Expand Down
4 changes: 3 additions & 1 deletion storage/framework/core/types/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ interface RequestData {
type RouteParams = { [key: string]: string } | null

export interface RequestInstance {
addQuery(url: URL, params: any): void
addQuery(url: URL): void

addBodies(params: any): void

addParam(param: RouteParams): void

Expand Down

0 comments on commit c582b27

Please sign in to comment.