Skip to content

Commit

Permalink
feat: make body optional in put and post
Browse files Browse the repository at this point in the history
  • Loading branch information
César Alberca committed Jan 10, 2022
1 parent eaa774e commit 1674c2a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/utils/src/http-client/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ type Url = string
export interface HttpClientResponse<Result> {
result: Result
status: HttpStatusCode
headers: Headers
headers: HttpClientHeaders
options: RequestInit
}

export type HttpClientBeforeHook = (request: Request, options: HttpClientOptions) => void
export type HttpClientAfterHook = (response: Response & { result: unknown }, options: HttpClientOptions) => void
export type HttpClientHeaders = Record<string, string | number | boolean>

export interface HttpClientOptions {
baseUrl: Url
Expand Down Expand Up @@ -58,15 +59,15 @@ export class HttpClient {

async post<Body, Result = void>(
url: string,
body: Body,
body?: Body,
httpParams?: HttpParams
): Promise<HttpClientResponse<Result>> {
return this.sendRequest(url, { method: 'POST', body: this.getParsedBody(body) }, httpParams)
}

async put<Body, Result = void>(
url: string,
body: Body,
body?: Body,
httpParams?: HttpParams
): Promise<HttpClientResponse<Result>> {
return this.sendRequest(url, { method: 'PUT', body: this.getParsedBody(body) }, httpParams)
Expand Down Expand Up @@ -110,7 +111,14 @@ export class HttpClient {
if (!response.ok) {
throw new HttpError({ name: response.status.toString(), message: response.statusText })
}

const headers: HttpClientHeaders = {}

response.headers.forEach((value, key) => {
headers[key] = value
})

const json = await response.json()
return { result: json as Result, headers: response.headers, options, status: response.status }
return { result: json as Result, headers, options, status: response.status }
}
}

0 comments on commit 1674c2a

Please sign in to comment.