Skip to content

Commit

Permalink
Merge branch 'v2' into async-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
César Alberca committed Dec 2, 2021
2 parents 15e6541 + cd63e90 commit 7a7f1f7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
28 changes: 20 additions & 8 deletions packages/utils/src/http-client/http-client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { HttpError } from './http-error'
import { HttpParams } from './http-params'
import { HttpStatusCode } from './http-status-code'

type Url = string

export interface HttpResponse<Result> {
result: Result
status: HttpStatusCode
headers: Headers
options: RequestInit
}

export type BeforeHook = (request: Request, options: Options) => void
export type AfterHook = (response: Response & { result: unknown }, options: Options) => void

Expand Down Expand Up @@ -42,29 +50,33 @@ export class HttpClient {

private constructor(private readonly options: Options) {}

async get<Result>(url: Url, httpParams?: HttpParams): Promise<Result> {
async get<Result>(url: Url, httpParams?: HttpParams): Promise<HttpResponse<Result>> {
return this.sendRequest(url, { method: 'GET' }, httpParams)
}

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

async put<Result, Body>(url: string, body: Body, httpParams?: HttpParams): Promise<Result> {
async put<Result, Body>(url: string, body: Body, httpParams?: HttpParams): Promise<HttpResponse<Result>> {
return this.sendRequest(url, { method: 'PUT', body: this.getParsedBody(body) }, httpParams)
}

async delete<Result>(url: string, httpParams?: HttpParams): Promise<Result> {
async delete<Result>(url: string, httpParams?: HttpParams): Promise<HttpResponse<Result>> {
return this.sendRequest(url, { method: 'DELETE' }, httpParams)
}

private async sendRequest<Result>(url: string, options: RequestInit, httpParams?: HttpParams): Promise<Result> {
private async sendRequest<Result>(
url: string,
options: RequestInit,
httpParams?: HttpParams
): Promise<HttpResponse<Result>> {
const request = this.getRequest(url, httpParams)
this.options.hooks.before.forEach(hook => hook(request, this.options))
const response = await fetch(request, { ...this.options.defaults, ...options })
const result = await response.json()
this.options.hooks.after.forEach(hook => hook({ ...response, result }, this.options))
return this.getResponse(response)
return this.getResponse(response, options)
}

private getRequest(url: string, httpParams: HttpParams | undefined) {
Expand All @@ -84,11 +96,11 @@ export class HttpClient {
return JSON.stringify(body)
}

private async getResponse<Result>(response: Response) {
private async getResponse<Result>(response: Response, options: RequestInit): Promise<HttpResponse<Result>> {
if (!response.ok) {
throw new HttpError({ name: response.status.toString(), message: response.statusText })
}
const json = await response.json()
return json as Result
return { result: json as Result, headers: response.headers, options, status: response.status }
}
}
65 changes: 65 additions & 0 deletions packages/utils/src/http-client/http-status-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export enum HttpStatusCode {
Continue = 100,
SwitchingProtocols = 101,
Processing = 102,
EarlyHints = 103,
Ok = 200,
Created = 201,
Accepted = 202,
NonAuthoritativeInformation = 203,
NoContent = 204,
ResetContent = 205,
PartialContent = 206,
MultiStatus = 207,
AlreadyReported = 208,
ImUsed = 226,
MultipleChoices = 300,
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
UseProxy = 305,
Unused = 306,
TemporaryRedirect = 307,
PermanentRedirect = 308,
BadRequest = 400,
Unauthorized = 401,
PaymentRequired = 402,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
ProxyAuthenticationRequired = 407,
RequestTimeout = 408,
Conflict = 409,
Gone = 410,
LengthRequired = 411,
PreconditionFailed = 412,
PayloadTooLarge = 413,
UriTooLong = 414,
UnsupportedMediaType = 415,
RangeNotSatisfiable = 416,
ExpectationFailed = 417,
ImATeapot = 418,
MisdirectedRequest = 421,
UnprocessableEntity = 422,
Locked = 423,
FailedDependency = 424,
TooEarly = 425,
UpgradeRequired = 426,
PreconditionRequired = 428,
TooManyRequests = 429,
RequestHeaderFieldsTooLarge = 431,
UnavailableForLegalReasons = 451,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
HttpVersionNotSupported = 505,
VariantAlsoNegotiates = 506,
InsufficientStorage = 507,
LoopDetected = 508,
NotExtended = 510,
NetworkAuthenticationRequired = 511
}

0 comments on commit 7a7f1f7

Please sign in to comment.