Skip to content

Commit

Permalink
http 관련 모듈 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
giwonn committed Jan 21, 2024
1 parent 064c7a0 commit 1b19834
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 3 deletions.
35 changes: 32 additions & 3 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"ioredis": "^5.3.2",
"morgan": "^1.10.0",
"nest-winston": "^1.9.4",
"qs": "^6.11.2",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"uuid": "^9.0.1",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import { SuccessResponseInterceptor } from '@/common/interceptor/success-respons
import { ErrorExceptionFilter } from '@/common/filter/error-exception.filter';
import { FailExceptionFilter } from '@/common/filter/fail-exception.filter';
import { RedisCacheModule } from '@/client/redis-cache/redis-cache.module';
import { HttpModule } from '@/client/http/http.module';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
HttpModule,
LoggerModule,
RedisJwtModule,
RedisCacheModule,
Expand Down
57 changes: 57 additions & 0 deletions src/client/http/fetch/fetch.client.ts
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;
}
}
25 changes: 25 additions & 0 deletions src/client/http/fetch/fetch.service.ts
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');
}
}
6 changes: 6 additions & 0 deletions src/client/http/http-client.interface.ts
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>;
}
9 changes: 9 additions & 0 deletions src/client/http/http-client.service.ts
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;
}
10 changes: 10 additions & 0 deletions src/client/http/http.module.ts
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 {}

0 comments on commit 1b19834

Please sign in to comment.