Skip to content

Commit

Permalink
httpclient 인스턴스 서버별로 생성으로 변경 (#363)
Browse files Browse the repository at this point in the history
* fix(lib-api): httpclient폴더 이동, 서버별 인스턴스 생성

* fix(lib-api): httpclient 수정 반영

* feat: cSpell 예외 추가

* fix(lib-api): httpclient -> http-client 파일명 변경
  • Loading branch information
MINJE-98 authored Aug 27, 2022
1 parent ea72203 commit a7bb051
Show file tree
Hide file tree
Showing 27 changed files with 350 additions and 155 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["httpclient"]
}
42 changes: 33 additions & 9 deletions lib/api/auth/authRepository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
import { AxiosInstance } from "axios";
import { AxiosRequestConfig, AxiosResponse } from "axios";

import { AuthApi, Configuration } from "@til-log.lab/tilog-api";
import {
AuthApi,
GetAccessTokenUsingRefreshTokenResponse,
} from "@til-log.lab/tilog-api";

export default class AuthRepository extends AuthApi {
constructor(
axios?: AxiosInstance,
basePath?: string,
configuration?: Configuration
) {
super(configuration, basePath, axios);
import ExceptionInterface from "@Library/api/exception/interface";
import RepositoryConfig from "@Library/api/interface/RepositoryConfig";

export default class AuthRepository {
protected authApi: AuthApi;
constructor(repositoryConfig: RepositoryConfig) {
this.authApi = new AuthApi(
repositoryConfig.configuration,
repositoryConfig.basePath,
repositoryConfig.axios
);
}
deleteRefreshToken(
options?: AxiosRequestConfig
): Promise<AxiosResponse<void, ExceptionInterface>> {
return this.authApi.usersAuthControllerDeleteRefreshToken(options);
}

getAccessTokenUsingRefreshToken(
userAgent?: string,
options?: AxiosRequestConfig
): Promise<
AxiosResponse<GetAccessTokenUsingRefreshTokenResponse, ExceptionInterface>
> {
return this.authApi.usersAuthControllerGetAccessTokenUsingRefreshToken(
userAgent,
options
);
}
}
14 changes: 8 additions & 6 deletions lib/api/auth/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class AuthService {
deleteRefreshToken(
options?: AxiosRequestConfig<ExceptionInterface>
): Promise<AxiosResponse<void, ExceptionInterface>> {
return this.authRepository.usersAuthControllerDeleteRefreshToken({
return this.authRepository.deleteRefreshToken({
...options,
withCredentials: true,
});
Expand All @@ -24,11 +24,13 @@ export default class AuthService {
userAgent?: string,
options?: AxiosRequestConfig<ExceptionInterface>
): Promise<void> {
const { data } =
await this.authRepository.usersAuthControllerGetAccessTokenUsingRefreshToken(
userAgent,
{ ...options, withCredentials: true }
);
const { data } = await this.authRepository.getAccessTokenUsingRefreshToken(
userAgent,
{
...options,
withCredentials: true,
}
);
this.axios.defaults.headers.common.Authorization = `bearer ${data.accessToken}`;
}
}
7 changes: 3 additions & 4 deletions lib/api/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { TILOG_API } from "@Library/constants/environment";
import AuthRepository from "@Library/api/auth/authRepository";
import AuthService from "@Library/api/auth/authService";
import httpClient from "@Library/api/httpClient";
import { tilogApi } from "@Library/api/http-client";

const authRepository = new AuthRepository(httpClient.http, TILOG_API);
const authService = new AuthService(authRepository, httpClient.http);
const authRepository = new AuthRepository({ axios: tilogApi.http });
const authService = new AuthService(authRepository, tilogApi.http);

export default authService;
4 changes: 2 additions & 2 deletions lib/api/auth/validateTokenDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable no-param-reassign */

import http from "@Library/api";
import httpClient from "@Library/api/httpClient";
import { tilogApi } from "@Library/api/http-client";
import getAccessTokenToAxiosHeader from "@Library/api/utility/getAccessTokenToAxiosHeader";
import isTokenExpired from "@Library/api/utility/isTokenExpired";

Expand All @@ -14,7 +14,7 @@ export default function validateToken() {
) => {
const originMethod = descriptor.value;
descriptor.value = async function (...args: unknown[]) {
const accessToken = getAccessTokenToAxiosHeader(httpClient.http);
const accessToken = getAccessTokenToAxiosHeader(tilogApi.http);

if (!accessToken || isTokenExpired(accessToken)) {
await http.authService.getAccessTokenUsingRefreshToken();
Expand Down
45 changes: 36 additions & 9 deletions lib/api/category/categoryRepository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
import { AxiosInstance } from "axios";
import { AxiosRequestConfig, AxiosResponse } from "axios";

import { CategoryApi, Configuration } from "@til-log.lab/tilog-api";
import {
CategoryApi,
GetCategoriesResponseDto,
GetUserCategoriesResponseDto,
} from "@til-log.lab/tilog-api";

export default class CategoryRepository extends CategoryApi {
constructor(
axios?: AxiosInstance,
basePath?: string,
configuration?: Configuration
) {
super(configuration, basePath, axios);
import ExceptionInterface from "@Library/api/exception/interface";
import RepositoryConfig from "@Library/api/interface/RepositoryConfig";

export default class CategoryRepository {
protected categoryApi: CategoryApi;
constructor(repositoryConfig: RepositoryConfig) {
this.categoryApi = new CategoryApi(
repositoryConfig.configuration,
repositoryConfig.basePath,
repositoryConfig.axios
);
}
getCategories(
categoryName?: string,
options?: AxiosRequestConfig
): Promise<AxiosResponse<GetCategoriesResponseDto, ExceptionInterface>> {
return this.categoryApi.categoriesControllerGetCategories(
categoryName,
options
);
}

getUsersCategories(
userId: number,
options?: AxiosRequestConfig
): Promise<AxiosResponse<GetUserCategoriesResponseDto, ExceptionInterface>> {
return this.categoryApi.categoriesControllerGetUsersCategories(
userId,
options
);
}
}
27 changes: 6 additions & 21 deletions lib/api/category/categoryService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import { AxiosRequestConfig, AxiosResponse } from "axios";

import CategoryRepository from "@Library/api/category/categoryRepository";

Expand All @@ -11,27 +11,18 @@ import GetCategoryListInterface from "@Library/api/category/interface/GetCategor
import ExceptionInterface from "@Library/api/exception/interface";

export default class CategoryService {
constructor(
private readonly categoryRepository: CategoryRepository,
private readonly axios: AxiosInstance
) {}
constructor(private readonly categoryRepository: CategoryRepository) {}
getCategories(
categoryName?: string,
options?: AxiosRequestConfig
): Promise<AxiosResponse<GetCategoriesResponseDto, ExceptionInterface>> {
return this.categoryRepository.categoriesControllerGetCategories(
categoryName,
options
);
return this.categoryRepository.getCategories(categoryName, options);
}
getUsersCategories(
userId: number,
options?: AxiosRequestConfig
): Promise<AxiosResponse<GetUserCategoriesResponseDto, ExceptionInterface>> {
return this.categoryRepository.categoriesControllerGetUsersCategories(
userId,
options
);
return this.categoryRepository.getUsersCategories(userId, options);
}

getCategoryList({
Expand All @@ -44,14 +35,8 @@ export default class CategoryService {
>
> {
if (userId) {
return this.categoryRepository.categoriesControllerGetUsersCategories(
userId,
options
);
return this.categoryRepository.getUsersCategories(userId, options);
}
return this.categoryRepository.categoriesControllerGetCategories(
undefined,
options
);
return this.categoryRepository.getCategories(undefined, options);
}
}
10 changes: 3 additions & 7 deletions lib/api/category/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { TILOG_API } from "@Library/constants/environment";
import CategoryRepository from "@Library/api/category/categoryRepository";
import CategoryService from "@Library/api/category/categoryService";
import httpClient from "@Library/api/httpClient";
import { tilogApi } from "@Library/api/http-client";

const categoryRepository = new CategoryRepository(httpClient.http, TILOG_API);
const categoryService = new CategoryService(
categoryRepository,
httpClient.http
);
const categoryRepository = new CategoryRepository({ axios: tilogApi.http });
const categoryService = new CategoryService(categoryRepository);

export default categoryService;
4 changes: 1 addition & 3 deletions lib/api/httpClient.ts → lib/api/http-client/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import RequestError from "@Library/api/exception/requestError";
import ResponseError from "@Library/api/exception/responseError";
import UnknownError from "@Library/api/exception/unknownError";

class HttpClient {
export default class HttpClient {
public http: AxiosInstance;

constructor(config: AxiosRequestConfig) {
Expand Down Expand Up @@ -34,5 +34,3 @@ class HttpClient {
return Promise.reject(new UnknownError());
};
}

export default new HttpClient({});
9 changes: 9 additions & 0 deletions lib/api/http-client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import HttpClient from "@Library/api/http-client/HttpClient";

const tilogApi = new HttpClient({
baseURL: process.env.TILOG_API,
});

const githubApi = new HttpClient({});

export { tilogApi, githubApi };
9 changes: 9 additions & 0 deletions lib/api/interface/RepositoryConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AxiosInstance } from "axios";

import { Configuration } from "@til-log.lab/tilog-api";

export default interface RepositoryConfig {
configuration?: Configuration;
basePath?: string;
axios?: AxiosInstance;
}
68 changes: 59 additions & 9 deletions lib/api/post/comment/CommentRepository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,63 @@
import { AxiosInstance } from "axios";
import { AxiosRequestConfig, AxiosResponse } from "axios";

import { CommentApi, Configuration } from "@til-log.lab/tilog-api";
import {
CommentApi,
CreateCommentsRequestBodyDto,
DeleteCommentRequestDto,
GetCommentsResponseDto,
UpdateCommentRequestDto,
} from "@til-log.lab/tilog-api";

export default class CommentRepository extends CommentApi {
constructor(
axios?: AxiosInstance,
basePath?: string,
configuration?: Configuration
) {
super(configuration, basePath, axios);
import RepositoryConfig from "@Library/api/interface/RepositoryConfig";

export default class CommentRepository {
protected commentApi: CommentApi;
constructor(repositoryConfig: RepositoryConfig) {
this.commentApi = new CommentApi(
repositoryConfig.configuration,
repositoryConfig.basePath,
repositoryConfig.axios
);
}
createComment(
createCommentsRequestBodyDto: CreateCommentsRequestBodyDto,
options?: AxiosRequestConfig
): Promise<AxiosResponse<void, any>> {
return this.commentApi.commentsControllerCreateComment(
createCommentsRequestBodyDto,
options
);
}

deleteComment(
deleteCommentRequestDto: DeleteCommentRequestDto,
options?: AxiosRequestConfig
): Promise<AxiosResponse<void, any>> {
return this.commentApi.commentsControllerDeleteComment(
deleteCommentRequestDto,
options
);
}

getComments(
postId: string,
replyTo?: string,
options?: AxiosRequestConfig
): Promise<AxiosResponse<GetCommentsResponseDto, any>> {
return this.commentApi.commentsControllerGetComments(
postId,
replyTo,
options
);
}

updateComment(
updateCommentRequestDto: UpdateCommentRequestDto,
options?: AxiosRequestConfig
): Promise<AxiosResponse<void, any>> {
return this.commentApi.commentsControllerUpdateComment(
updateCommentRequestDto,
options
);
}
}
12 changes: 4 additions & 8 deletions lib/api/post/comment/CommentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class CommentService {
createCommentsRequestBodyDto: CreateCommentsRequestBodyDto,
options?: AxiosRequestConfig
): Promise<AxiosResponse<void, ExceptionInterface>> {
return this.commentRepository.commentsControllerCreateComment(
return this.commentRepository.createComment(
createCommentsRequestBodyDto,
options
);
Expand All @@ -31,7 +31,7 @@ export default class CommentService {
deleteCommentRequestDto: DeleteCommentRequestDto,
options?: AxiosRequestConfig
): Promise<AxiosResponse<void, ExceptionInterface>> {
return this.commentRepository.commentsControllerDeleteComment(
return this.commentRepository.deleteComment(
deleteCommentRequestDto,
options
);
Expand All @@ -42,19 +42,15 @@ export default class CommentService {
replyTo?: string,
options?: AxiosRequestConfig
): Promise<AxiosResponse<GetCommentsResponseDto, ExceptionInterface>> {
return this.commentRepository.commentsControllerGetComments(
postId,
replyTo,
options
);
return this.commentRepository.getComments(postId, replyTo, options);
}

@validateToken()
updateComment(
updateCommentRequestDto: UpdateCommentRequestDto,
options?: AxiosRequestConfig
): Promise<AxiosResponse<void>> {
return this.commentRepository.commentsControllerUpdateComment(
return this.commentRepository.updateComment(
updateCommentRequestDto,
options
);
Expand Down
9 changes: 4 additions & 5 deletions lib/api/post/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { TILOG_API } from "@Library/constants/environment";
import httpClient from "@Library/api/httpClient";
import { tilogApi } from "@Library/api/http-client";
import CommentRepository from "@Library/api/post/comment/CommentRepository";
import CommentService from "@Library/api/post/comment/CommentService";
import PostLikeRepository from "@Library/api/post/like/postLikeRepository";
import PostLikeService from "@Library/api/post/like/postLikeService";
import PostRepository from "@Library/api/post/postRepository";
import PostService from "@Library/api/post/postService";

const postRepository = new PostRepository(httpClient.http, TILOG_API);
const postLikeRepository = new PostLikeRepository(httpClient.http, TILOG_API);
const commentRepository = new CommentRepository(httpClient.http, TILOG_API);
const postRepository = new PostRepository({ axios: tilogApi.http });
const postLikeRepository = new PostLikeRepository({ axios: tilogApi.http });
const commentRepository = new CommentRepository({ axios: tilogApi.http });

const postService = new PostService(postRepository);
const postLikeService = new PostLikeService(postLikeRepository);
Expand Down
Loading

0 comments on commit a7bb051

Please sign in to comment.