-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
142 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { HttpClient } from '@/client/http/http-client.interface'; | ||
import * as qs from 'qs'; | ||
|
||
export class FetchClient implements HttpClient { | ||
private readonly _url: string; | ||
private readonly _method: string; | ||
private readonly _defaultHeaders = {}; | ||
private _headers?: Record<string, string>; | ||
private _queryParams?: Record<string, any>; | ||
private _body: Record<string, any> = {}; | ||
|
||
constructor(url: string, method: string) { | ||
this._url = url; | ||
this._method = method; | ||
} | ||
|
||
headers(headers: Record<string, string>): this { | ||
this._headers = { ...headers }; | ||
return this; | ||
} | ||
queryParams(params: Record<string, any>): this { | ||
this._queryParams = { ...params }; | ||
return this; | ||
} | ||
|
||
body(body: Record<string, any>): this { | ||
this._body = { ...body }; | ||
return this; | ||
} | ||
|
||
async send<T>(): Promise<T> { | ||
return await fetch(this._getUri(), this._getOptions()).then((res) => | ||
res.json(), | ||
); | ||
} | ||
|
||
private _getUri() { | ||
return this._queryParams | ||
? `${this._url}?${qs.stringify(this._queryParams, { | ||
arrayFormat: 'repeat', | ||
})}` | ||
: this._url; | ||
} | ||
|
||
private _getOptions() { | ||
const options: Record<string, any> = { | ||
method: this._method, | ||
headers: this._headers && { ...this._defaultHeaders, ...this._headers }, | ||
}; | ||
|
||
if (this._method !== 'GET') { | ||
options.body = JSON.stringify(this._body); | ||
} | ||
|
||
return options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { HttpClient } from '@/client/http/http-client.interface'; | ||
import { HttpClientService } from '@/client/http/http-client.service'; | ||
import { FetchClient } from '@/client/http/fetch/fetch.client'; | ||
|
||
export class FetchService extends HttpClientService { | ||
override get(url: string): Omit<HttpClient, 'body'> { | ||
return new FetchClient(url, 'GET'); | ||
} | ||
|
||
override post(url: string): HttpClient { | ||
return new FetchClient(url, 'POST'); | ||
} | ||
|
||
override put(url: string): HttpClient { | ||
return new FetchClient(url, 'PUT'); | ||
} | ||
|
||
override delete(url: string): HttpClient { | ||
return new FetchClient(url, 'DELETE'); | ||
} | ||
|
||
override patch(url: string): HttpClient { | ||
return new FetchClient(url, 'PATCH'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface HttpClient { | ||
queryParams(params: Record<string, any>): this; | ||
headers(headers: Record<string, string>): this; | ||
body(body: Record<string, any>): this; | ||
send<T = any>(): Promise<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { HttpClient } from '@/client/http/http-client.interface'; | ||
|
||
export abstract class HttpClientService { | ||
abstract get(url: string): Omit<HttpClient, 'body'>; | ||
abstract post(url: string): HttpClient; | ||
abstract put(url: string): HttpClient; | ||
abstract delete(url: string): HttpClient; | ||
abstract patch(url: string): HttpClient; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Global, Module } from '@nestjs/common'; | ||
import { HttpClientService } from '@/client/http/http-client.service'; | ||
import { FetchService } from '@/client/http/fetch/fetch.service'; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [{ provide: HttpClientService, useClass: FetchService }], | ||
exports: [HttpClientService], | ||
}) | ||
export class HttpModule {} |