Skip to content

Commit

Permalink
feat(ctx): improve js doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Mar 2, 2024
1 parent e31f26a commit 12a7d59
Show file tree
Hide file tree
Showing 20 changed files with 357 additions and 165 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "4.23.0",
"version": "4.24.0",
"description": "The Athenna Http server. Built on top of fastify.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
186 changes: 145 additions & 41 deletions src/context/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export class Request {
/**
* Get the request id.
*
* @example 12345
* @example
* ```ts
* console.log(request.id) // '12345'
* ```
*/
public get id(): string {
return this.request.id
Expand All @@ -33,7 +36,10 @@ export class Request {
/**
* Get the request ip.
*
* @example 192.168.0.1
* @example
* ```ts
* console.log(request.ip) // '192.168.0.1'
* ```
*/
public get ip(): string {
return this.request.ip
Expand All @@ -42,16 +48,46 @@ export class Request {
/**
* Get the request hostname.
*
* @example localhost
* @example
* ```ts
* console.log(request.hostname) // 'localhost'
* ```
*/
public get hostname(): string {
return this.request.hostname
}

/**
* Get the server port.
*
* @example
* ```ts
* console.log(request.port) // 3000
* ```
*/
public get port(): number {
return this.getAddressInfo().port
}

/**
* Get the http version.
*
* @example
* ```ts
* console.log(request.version) // 1
* ```
*/
public get version(): string {
return this.request.raw.httpVersion
}

/**
* Get the request protocol.
*
* @example http
* @example
* ```ts
* console.log(request.protocol) // 'http'
* ```
*/
public get protocol(): 'http' | 'https' {
return this.request.protocol
Expand All @@ -60,7 +96,10 @@ export class Request {
/**
* Get the request method.
*
* @example GET
* @example
* ```ts
* console.log(request.method) // 'GET'
* ```
*/
public get method(): string {
return this.request.method
Expand All @@ -69,7 +108,10 @@ export class Request {
/**
* Get the base url from request.
*
* @example /users/1
* @example
* ```ts
* console.log(request.baseUrl) // '/users/1'
* ```
*/
public get baseUrl(): string {
return this.request.url.split('?')[0]
Expand All @@ -78,7 +120,10 @@ export class Request {
/**
* Get the base url with host and port info from request.
*
* @example http://localhost:3030/users/1
* @example
* ```ts
* console.log(request.baseHostUrl) // 'http://localhost:3030/users/1'
* ```
*/
public get baseHostUrl(): string {
return this.getHostUrlFor(this.baseUrl)
Expand All @@ -87,7 +132,10 @@ export class Request {
/**
* Get the route url from request.
*
* @example /users/:id
* @example
* ```ts
* console.log(request.routeUrl) // '/users/:id'
* ```
*/
public get routeUrl(): string {
return this.request.routeOptions.url
Expand All @@ -96,7 +144,10 @@ export class Request {
/**
* Get the route url with host and port info from request.
*
* @example http://localhost:3030/users/:id
* @example
* ```ts
* console.log(request.routeHostUrl) // 'http://localhost:3030/users/:id'
* ```
*/
public get routeHostUrl(): string {
return this.getHostUrlFor(this.routeUrl)
Expand All @@ -105,7 +156,10 @@ export class Request {
/**
* Get the original url from request.
*
* @example /users/1?query=true
* @example
* ```ts
* console.log(request.originalUrl) // '/users/1?query=true'
* ```
*/
public get originalUrl(): string {
return this.request.url
Expand All @@ -114,77 +168,135 @@ export class Request {
/**
* Get the original url with host and port info from request.
*
* @example /users/1?query=true
* @example
* ```ts
* console.log(request.originalHostUrl) // 'http://localhost:3000/users/1?query=true'
* ```
*/
public get originalHostUrl(): string {
return this.getHostUrlFor(this.originalUrl)
}

/**
* Get all body from request.
*
* @example
* ```ts
* const { name, email } = request.body
* ```
*/
public get body(): any | any[] {
return this.request.body || {}
}

/**
* Get all params from request.
*
* @example
* ```ts
* const { id } = request.params
* ```
*/
public get params(): any {
return this.request.params || {}
}

/**
* Get all queries from request.
*
* @example
* ```ts
* const { page, limit } = request.queries
* ```
*/
public get queries(): any {
return this.request.query || {}
}

/**
* Get all headers from request.
*
* @example
* ```ts
* const { accept } = request.headers
* ```
*/
public get headers(): any {
return this.request.headers || {}
}

/**
* Get the server port.
* Get a value from the request params or return
* the default value.
*
* @example
* ```ts
* const id = request.param('id', '1')
* ```
*/
public get port(): number {
return this.getAddressInfo().port
public param(param: string, defaultValue?: any): any {
return Json.get(this.params, param, defaultValue)
}

/**
* Get the http version.
* Get a value from the request query param or return
* the default value.
*
* @example
* ```ts
* const page = request.query('page', '1')
* ```
*/
public get version(): string {
return this.request.raw.httpVersion
public query(query: string, defaultValue?: any): any {
return Json.get(this.queries, query, defaultValue)
}

/**
* Get a value from the request params or the default value.
* Get a value from the request header or return
* the default value.
*
* @example
* ```ts
* const accept = request.header('accept', 'application/json')
* ```
*/
public param(param: string, defaultValue?: any): any {
return this.params[param] || defaultValue
public header(header: string, defaultValue?: any): any {
return Json.get(this.headers, header, defaultValue)
}

/**
* Get a value from the request query param or the default value.
* Get a value from the request body or return
* the default value.
*
* @example
* ```ts
* const name = request.input('name', 'lenon')
* ```
*/
public query(query: string, defaultValue?: any): any {
return this.queries[query] || defaultValue
public input(key: string, defaultValue?: any): any {
return this.payload(key, defaultValue)
}

/**
* Get a value from the request header or the default value.
* Get a value from the request body or return
* the default value.
*
* @example
* ```ts
* const name = request.payload('name', 'lenon')
* ```
*/
public header(header: string, defaultValue?: any): any {
return this.headers[header] || defaultValue
public payload(key: string, defaultValue?: any) {
return Json.get(this.body, key, defaultValue)
}

/**
* Get only the selected values from the request body.
*
* @example
* ```ts
* const body = request.only(['name', 'email'])
* ```
*/
public only(keys: string[]): any {
const body = {}
Expand All @@ -201,7 +313,13 @@ export class Request {
}

/**
* Get all the values from the request body except the selected ones.
* Get all the values from the request body except the
* selected ones.
*
* @example
* ```ts
* const body = request.except(['name'])
* ```
*/
public except(keys: string[]): any {
const body = {}
Expand All @@ -217,20 +335,6 @@ export class Request {
return body
}

/**
* Get a value from the request body or the default value.
*/
public input(key: string, defaultValue?: any): any {
return this.payload(key, defaultValue)
}

/**
* Get a value from the request body or the default value.
*/
public payload(key: string, defaultValue?: any) {
return Json.get(this.body, key, defaultValue)
}

/**
* Add the hostname and port to the url.
*/
Expand Down
Loading

0 comments on commit 12a7d59

Please sign in to comment.