Skip to content

Commit

Permalink
Merge pull request #110 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(client): add new methods
  • Loading branch information
jlenon7 authored Feb 4, 2024
2 parents cf741f6 + 71a925a commit 0699d58
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 63 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "4.29.0",
"version": "4.30.0",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
154 changes: 99 additions & 55 deletions src/helpers/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ import { Json } from '#src/helpers/Json'

import type { Store } from 'keyv'
import type { ClientHttp2Session } from 'http2'
import type {
Body,
Query,
Request,
Response,
RetryStrategyCallback
} from '#src/types'
import type { Request, Response, RetryStrategyCallback } from '#src/types'

import type {
Hooks,
Expand Down Expand Up @@ -404,37 +398,60 @@ export class HttpClientBuilder {
}

/**
* Set the request body.
* Set a key value to the request body.
*/
public body(body: Body): HttpClientBuilder {
if (Is.Object(body)) {
this.options.json = body
public body(key: any, value?: any): HttpClientBuilder {
if (!this.options.body) {
this.options.body = {} as any
}

if (Is.Undefined(value)) {
this.options.body = key

return this
}

this.options.body = body as any
this.options.body[key] = value

return this
}

/**
* Set the request form.
* Set a key value to the request body.
*/
public input(key: string, value: any): HttpClientBuilder {
return this.body(key, value)
}

/**
* Execute the given closure only when the value is
* truthy.
*/
public form(form: any | Record<string, any>): HttpClientBuilder {
this.options.form = form
public when<T = any>(
value: T,
closure: (builder?: HttpClientBuilder, value?: T) => any
): HttpClientBuilder {
if (value) {
closure(this, value)
}

return this
}

/**
* Set a header at the request.
*/
public header(key: string, value: string): HttpClientBuilder {
public header(key: any, value?: any): HttpClientBuilder {
if (!this.options.headers) {
this.options.headers = {}
}

if (Is.Undefined(value)) {
this.options.headers = key

return this
}

this.options.headers[key] = value

return this
Expand All @@ -444,7 +461,7 @@ export class HttpClientBuilder {
* Set a header at the request only if is not already
* defined.
*/
public safeHeader(key: string, value: string): HttpClientBuilder {
public safeHeader(key: string, value: any): HttpClientBuilder {
if (!this.options.headers) {
this.options.headers = {}
}
Expand All @@ -453,24 +470,22 @@ export class HttpClientBuilder {
return this
}

this.options.headers[key] = value

return this
return this.header(key, value)
}

/**
* Remove a header from the request.
*/
public removeHeader(key: string): HttpClientBuilder {
if (!this.options.headers) {
this.options.headers = {}
return this
}

if (!this.options.headers[key]) {
return this
}

delete this.options.headers[key]
this.options.headers = Json.omit(this.options.headers, [key])

return this
}
Expand Down Expand Up @@ -595,17 +610,58 @@ export class HttpClientBuilder {
* //=> 'key=a&key=b'
* ```
*/
public searchParams(value: Query): HttpClientBuilder {
this.options.searchParams = value
public searchParams(key: any, value?: any): HttpClientBuilder {
if (!this.options.searchParams) {
this.options.searchParams = {}
}

if (Is.Undefined(value)) {
this.options.searchParams = key

return this
}

this.options.searchParams[key] = value

return this
}

/**
* Alias for the searchParams method.
*/
public queryParams(value: Query): HttpClientBuilder {
return this.searchParams(value)
public query(key: any, value?: any): HttpClientBuilder {
return this.searchParams(key, value)
}

/**
* Set a query at the request only if is not already
* defined.
*/
public safeQuery(key: string, value: any): HttpClientBuilder {
if (this.options.searchParams[key]) {
return this
}

return this.searchParams(key, value)
}

/**
* Remove a query from the request.
*/
public removeQuery(key: string): HttpClientBuilder {
if (!this.options.searchParams) {
return this
}

if (!this.options.searchParams[key]) {
return this
}

this.options.searchParams = Json.omit(this.options.searchParams as any, [
key
])

return this
}

/**
Expand Down Expand Up @@ -1066,7 +1122,14 @@ export class HttpClientBuilder {
* Execute the request using all the options defined.
*/
public request<T = any>(options: Request = {}): Response<T> {
return got<T>({ ...this.options, ...options } as any)
options = { ...this.options, ...options }

if (options.body && Is.Object(options.body)) {
options.json = Json.copy(options.body)
options.body = undefined
}

return got<T>(options as any)
}

/**
Expand All @@ -1081,34 +1144,27 @@ export class HttpClientBuilder {
/**
* Make a POST request.
*/
public post<T = any>(url?: string | URL, body?: Body, options: Request = {}) {
public post<T = any>(url?: string | URL, options: Request = {}) {
return this.method('POST')
.url(url || options.url || this.options.url)
.body(body || options.body || this.options.body || {})
.request<T>(options)
}

/**
* Make a PUT request.
*/
public put<T = any>(url?: string | URL, body?: Body, options: Request = {}) {
public put<T = any>(url?: string | URL, options: Request = {}) {
return this.method('PUT')
.url(url || options.url || this.options.url)
.body(body || options.body || this.options.body || {})
.request<T>(options)
}

/**
* Make a PATCH request.
*/
public patch<T = any>(
url?: string | URL,
body?: Response,
options: Request = {}
) {
public patch<T = any>(url?: string | URL, options: Request = {}) {
return this.method('PATCH')
.url(url || options.url || this.options.url)
.body(body || options.body || this.options.body || {})
.request<T>(options)
}

Expand Down Expand Up @@ -1168,34 +1224,22 @@ export class HttpClient {
/**
* Make a POST request.
*/
public static post<T = any>(
url?: string | URL,
body?: Body,
options?: Request
) {
return this._builder.post<T>(url, body, options)
public static post<T = any>(url?: string | URL, options?: Request) {
return this._builder.post<T>(url, options)
}

/**
* Make a PUT request.
*/
public static put<T = any>(
url?: string | URL,
body?: Body,
options?: Request
) {
return this._builder.put<T>(url, body, options)
public static put<T = any>(url?: string | URL, options?: Request) {
return this._builder.put<T>(url, options)
}

/**
* Make a PATCH request.
*/
public static patch<T = any>(
url?: string | URL,
body?: Body,
options?: Request
) {
return this._builder.patch<T>(url, body, options)
public static patch<T = any>(url?: string | URL, options?: Request) {
return this._builder.patch<T>(url, options)
}

/**
Expand Down
24 changes: 23 additions & 1 deletion src/types/http-client/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,33 @@
* file that was distributed with this source code.
*/

import { Readable } from 'node:stream'
import type { Except } from '#src/types'
import type { OptionsInit, ResponseType } from 'got'
import type { FormDataLike } from 'form-data-encoder'

export type Request = Except<OptionsInit, 'responseType'> &
export type Request = Except<OptionsInit, 'body' | 'responseType'> &
Partial<{
get body():
| string
| Record<string, any>
| Buffer
| Readable
| Generator
| AsyncGenerator
| FormDataLike
| undefined
set body(
value:
| string
| Record<string, any>
| Buffer
| Readable
| Generator
| AsyncGenerator
| FormDataLike
| undefined
)
get responseType(): ResponseType | string
set responseType(value: ResponseType | string)
}>
Loading

0 comments on commit 0699d58

Please sign in to comment.