Skip to content

Commit

Permalink
feat: add error handler, enviroment checking and logs to kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Apr 7, 2022
1 parent ce8e8b5 commit 00e5aaa
Show file tree
Hide file tree
Showing 15 changed files with 552 additions and 57 deletions.
138 changes: 136 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "1.1.4",
"version": "1.1.5",
"description": "The Athenna Http server. Built on top of fastify",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -160,6 +160,7 @@
"dependencies": {
"@athenna/config": "1.0.6",
"@athenna/ioc": "1.1.1",
"@athenna/logger": "1.1.2",
"@secjs/utils": "1.8.2",
"fastify": "3.27.4",
"reflect-metadata": "0.1.13",
Expand Down
119 changes: 100 additions & 19 deletions src/Context/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,81 +8,162 @@
*/

import { Is } from '@secjs/utils'
import { Env } from '@athenna/config'
import { Config } from '@athenna/config'
import { FastifyRequest } from 'fastify'
import { removeSlash } from '../Utils/removeSlash'
import { RequestContract } from '../Contracts/Context/RequestContract'

export class Request implements RequestContract {
private request: FastifyRequest
private readonly request: FastifyRequest

/**
* Create a new instance of Request.
*
* @return Request
*/
constructor(request: FastifyRequest) {
this.request = request
}

/**
* Get the request ip.
*
* @return string
*/
get ip(): string {
return this.request.ip
}

/**
* Get the request method.
*
* @return string
*/
get method(): string {
return this.request.method
}

/**
* Get the host url from request.
*
* @return string
*/
get hostUrl(): string {
const url = this.request.url
const port = Env('PORT', '1335')
let host = Env('APP_DOMAIN', `http://localhost:${port}`)
const port = Config.get('http.port', 1335)
let host = Config.get('http.domain', `http://localhost:${port}`) as string

if (!Is.Ip(host)) host = host.replace(`:${port}`, '')
if (!Is.Ip(host) && !host.includes('localhost')) {
host = host.replace(`:${port}`, '')
}

return removeSlash(`${host}${url}`) as string
}

/**
* Get the base request url.
*
* @return string
*/
get baseUrl(): string {
return this.request.url.split('?')[0]
}

/**
* Get the original request url.
*
* @return string
*/
get originalUrl(): string {
return this.request.url
}

get body(): Record<string, any> {
/**
* Get all body from request.
*
* @return any
*/
get body(): any {
return this.request.body as Record<string, any>
}

get params(): Record<string, string> {
return this.request.params as Record<string, string>
/**
* Get all params from request.
*
* @return any
*/
get params(): any {
return this.request.params as any
}

get queries(): Record<string, string> {
return this.request.query as Record<string, string>
/**
* Get all queries from request.
*
* @return any
*/
get queries(): any {
return this.request.query as any
}

get headers(): Record<string, string> {
return this.request.headers as Record<string, string>
/**
* Get all headers from request.
*
* @return any
*/
get headers(): any {
return this.request.headers as any
}

param(param: string, defaultValue?: string) {
const params = this.request.params as Record<string, string>
/**
* Get a value from the request params or the default value.
*
* @return any
*/
param(param: string, defaultValue?: string): any {
const params = this.request.params as any

return params[param] || defaultValue
}

query(query: string, defaultValue?: string) {
const queries = this.request.query as Record<string, string>
/**
* Get a value from the request query param or the default value.
*
* @return any
*/
query(query: string, defaultValue?: string): any {
const queries = this.request.query as any

return queries[query] || defaultValue
}

header(header: string, defaultValue?: string) {
const headers = this.request.headers as Record<string, string>
/**
* Get a value from the request header or the default value.
*
* @return any
*/
header(header: string, defaultValue?: string): any {
const headers = this.request.headers as any

return headers[header] || defaultValue
}

payload(payload: string, defaultValue?: string) {
/**
* Get a value from the request body or the default value.
*
* @return any
*/
payload(payload: string, defaultValue?: string): any {
const body = this.request.body as Record<string, any>

return body[payload] || defaultValue
}

/**
* Get the default fastify request object.
*
* @return FastifyRequest
*/
getFastifyRequest(): FastifyRequest {
return this.request
}
}
Loading

0 comments on commit 00e5aaa

Please sign in to comment.