Skip to content

Commit

Permalink
feat(the-camp): TheCampSession 타입으로 교체
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteKiwi committed Mar 1, 2022
1 parent d00f792 commit ef9c084
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
coveragePathIgnorePatterns: ['<rootDir>/index.ts'],
moduleNameMapper: {
'^@core/(.*)$': '<rootDir>/core/$1',
'^@common/(.*)$': '<rootDir>/common/$1',
},
coverageDirectory: '../coverage',
testEnvironment: 'node',
Expand Down
1 change: 1 addition & 0 deletions src/common/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './the-camp.session';
3 changes: 3 additions & 0 deletions src/common/types/the-camp.session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Session } from '@core/http';

export type TheCampSession = Session<{ IUID: string }>;
3 changes: 2 additions & 1 deletion src/core/http/session.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Cookie } from './cookie';

export interface Session {
export interface Session<T = undefined> {
cookies: Cookie[];
extra?: T;
}
5 changes: 3 additions & 2 deletions src/services/the-camp/requesters/login/login.requester.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Parameter, Session } from '@core/http';
import { TheCampSession } from '@common/types';
import { Parameter } from '@core/http';
import { Credential } from '@core/types';
import axios, { AxiosRequestConfig } from 'axios';

Expand All @@ -7,7 +8,7 @@ import { parseLogin } from './parse-login';
export class LoginRequester {
constructor(private readonly parse = parseLogin) {}

async request(credential: Credential): Promise<Session> {
async request(credential: Credential): Promise<TheCampSession> {
const response = await axios.post(
'https://www.thecamp.or.kr/login/loginA.do',
this.createPayload(credential),
Expand Down
18 changes: 13 additions & 5 deletions src/services/the-camp/requesters/login/parse-login.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Cookie, Session } from '@core/http';
import { TheCampSession } from '@common/types';
import { Cookie } from '@core/http';
import { AxiosResponse } from 'axios';

export function parseLogin(response: AxiosResponse): Session {
export function parseLogin(response: AxiosResponse): TheCampSession {
assertsResponse(response);

const cookies: Cookie[] = [];
const setCookie = response.headers['set-cookie'];
// TODO: asserts

if (!setCookie?.length) {
throw new Error('');
Expand All @@ -17,7 +17,7 @@ export function parseLogin(response: AxiosResponse): Session {
cookies.push({ key, value });
}

return { cookies };
return { cookies, extra: { IUID: extractIUIDFromCookies(cookies) } };
}

function assertsResponse(response: any) {
Expand All @@ -29,6 +29,14 @@ function assertsResponse(response: any) {

if (response.data.resultCd !== '0000')
throw new Error(
response.data?.resultMsg || '알 수 없는 오류가 발생하였습니다',
response.data?.resultMsg || '알 수 없는 오류가 발생하였습니다.',
);
}

function extractIUIDFromCookies(cookies: Cookie[]): string {
const IUID = cookies.find((cookie) => cookie.key === 'iuid')?.value;
if (!IUID) {
throw new Error('알 수 없는 오류가 발생하였습니다.');
}
return IUID;
}
4 changes: 2 additions & 2 deletions src/services/the-camp/the-camp.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Session } from '@core/http';
import { TheCampSession } from '@common/types';
import { Credential } from '@core/types';

import { loginRequester as _loginRequester } from './requesters';

export class TheCampService {
constructor(private readonly loginRequester = _loginRequester) {}

async login(credential: Credential): Promise<Session> {
async login(credential: Credential): Promise<TheCampSession> {
return await this.loginRequester.request(credential);
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"noImplicitAny": true,
"noUnusedLocals": true,
"paths": {
"@core/*": ["./src/core/*"]
"@core/*": ["./src/core/*"],
"@common/*": ["./src/common/*"]
}
}
}

0 comments on commit ef9c084

Please sign in to comment.