Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚚 login api 를 package 로 분리 #127

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/CODEOWNDERS
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

apps/friends-react-native/ @woohm402
apps/snutt-ev-webview/ @ars-ki-00
apps/snutt-webclient/ @woohm402
apps/snutt-webclient/ @woohm402
10 changes: 9 additions & 1 deletion apps/friends-react-native/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module.exports = {
extends: ['@react-native', 'plugin:prettier/recommended'],
extends: [
'@react-native',
'plugin:prettier/recommended',
'plugin:react-hooks/recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@tanstack/eslint-plugin-query/recommended',
],
parser: '@typescript-eslint/parser',
root: true,
rules: {
'react/react-in-jsx-scope': 'off',
},
Expand Down
1 change: 1 addition & 0 deletions apps/friends-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "28.5.0",
"eslint-plugin-prettier": "5.1.3",
"eslint-plugin-react-hooks": "4.6.2",
"jest": "29.7.0",
"metro-react-native-babel-preset": "0.77.0",
"prettier": "3.2.5",
Expand Down
1 change: 1 addition & 0 deletions apps/snutt-webclient/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"react-dom": "18.3.1",
"react-facebook-login": "4.1.1",
"react-router-dom": "6.23.1",
"@sf/snutt-api": "*",
"styled-components": "6.1.11"
},
"devDependencies": {
Expand Down
33 changes: 31 additions & 2 deletions apps/snutt-webclient/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { implSnuttApi } from '@sf/snutt-api';
import { QueryCache, QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { getTruffleClient } from '@wafflestudio/truffle-browser';
Expand Down Expand Up @@ -165,7 +166,8 @@ const getUnauthorizedServices = (ENV: { API_BASE_URL: string; API_KEY: string })
baseURL: ENV.API_BASE_URL,
headers: { 'x-access-apikey': ENV.API_KEY },
});
const authRepository = getAuthRepository({ httpClient });

const authRepository = getAuthRepository({ httpClient, snuttApi: getSnuttApi(ENV) });
const feedbackRepository = getFeedbackRepository({ httpClient });
const userRepository = getUserRepository({ httpClient });
const authService = getAuthService({ repositories: [authRepository, userRepository] });
Expand All @@ -189,7 +191,7 @@ const getAuthorizedServices = (
});

const userRepository = getUserRepository({ httpClient });
const authRepository = getAuthRepository({ httpClient });
const authRepository = getAuthRepository({ httpClient, snuttApi: getSnuttApi(ENV) });
const timetableRepository = getTimetableRepository({ httpClient });
const semesterRepository = getSemesterRepository({ httpClient });
const searchRepository = getSearchRepository({ httpClient });
Expand Down Expand Up @@ -222,3 +224,30 @@ const getAuthorizedServices = (
userService,
};
};

const getSnuttApi = (ENV: { API_BASE_URL: string; API_KEY: string }) =>
implSnuttApi({
httpClient: {
call: async <R,>(_: {
method: string;
path: string;
body?: Record<string, unknown>;
headers?: Record<string, string>;
}) => {
const response = await fetch(`${ENV.API_BASE_URL}${_.path}`, {
method: _.method,
headers: _.headers,
...(!!_.body ? { body: JSON.stringify(_.body) } : {}),
});

const responseBody = await response.json().catch(() => null);

if (response.ok) {
return responseBody as R;
} else {
throw responseBody;
}
},
},
apiKey: ENV.API_KEY,
});
12 changes: 10 additions & 2 deletions apps/snutt-webclient/src/repositories/authRepository.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type SnuttApi } from '@sf/snutt-api';

import { type HttpClient } from '@/clients/HttpClient';
import type { SignInResponse } from '@/entities/auth';

Expand All @@ -15,9 +17,15 @@ export interface AuthRepository {
resetPassword(body: { user_id: string; password: string }): Promise<{ message: 'ok' }>;
}

export const getAuthRepository = ({ httpClient }: { httpClient: HttpClient }): AuthRepository => {
export const getAuthRepository = ({
httpClient,
snuttApi,
}: {
httpClient: HttpClient;
snuttApi: SnuttApi;
}): AuthRepository => {
return {
signInWithIdPassword: async (body) => (await httpClient.post<SignInResponse>(`/v1/auth/login_local`, body)).data,
signInWithIdPassword: (body) => snuttApi['POST /v1/login/local'](body),
signInWithFacebook: async (body) => (await httpClient.post<SignInResponse>(`/auth/login_fb`, body)).data,
signUpWithIdPassword: async (body) =>
(await httpClient.post<{ message: 'ok'; token: string; user_id: string }>(`/v1/auth/register_local`, body)).data,
Expand Down
3 changes: 1 addition & 2 deletions apps/snutt-webclient/src/usecases/authService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { type SignInResponse } from '@/entities/auth';
import { type AuthRepository } from '@/repositories/authRepository';
import { type UserRepository } from '@/repositories/userRepository';

Expand All @@ -7,7 +6,7 @@ export interface AuthService {
changePassword(body: { old_password: string; new_password: string }): Promise<{ token: string }>;
signIn(
params: { type: 'LOCAL'; id: string; password: string } | { type: 'FACEBOOK'; fb_id: string; fb_token: string },
): Promise<SignInResponse>;
): Promise<{ token: string }>;
signUp(body: { id: string; password: string }): Promise<{ message: 'ok'; token: string; user_id: string }>;
closeAccount(): Promise<{ message: 'ok' }>;
findIdByEmail(body: { email: string }): Promise<{ message: 'ok' }>;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"turbo": "1.13.3"
},
"workspaces": [
"apps/*"
"apps/*",
"packages/*"
]
}
5 changes: 5 additions & 0 deletions packages/snutt-api/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
parser: '@typescript-eslint/parser',
root: true,
extends: ['plugin:prettier/recommended', 'plugin:@typescript-eslint/recommended'],
};
8 changes: 8 additions & 0 deletions packages/snutt-api/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"printWidth": 120,
"trailingComma": "all",
"tabWidth": 2,
"semi": true,
"arrowParens": "always",
"singleQuote": true
}
18 changes: 18 additions & 0 deletions packages/snutt-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@sf/snutt-api",
"version": "0.0.0",
"types": "src/index.ts",
"main": "src/index.ts",
"scripts": {
"lint": "eslint ./src",
"tsc": "tsc"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "7.9.0",
"@typescript-eslint/parser": "7.9.0",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.1.3",
"typescript": "5.4.5"
}
}
19 changes: 19 additions & 0 deletions packages/snutt-api/src/apis/post-v1-auth-login-local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { InternalClient } from '../httpClient';

type Request = {
id: string;
password: string;
};

type Response = {
user_id: string;
token: string;
message: string;
};

export const postV1AuthLoginLocal = (httpClient: InternalClient) => (req: Request) =>
httpClient.call<Response>({
method: 'post',
path: '/v1/auth/login_local',
body: { id: req.id, password: req.password },
});
3 changes: 3 additions & 0 deletions packages/snutt-api/src/httpClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type InternalClient = {
call: <R>(_: { method: string; path: string; body?: Record<string, unknown>; token?: string }) => Promise<R>;
};
36 changes: 36 additions & 0 deletions packages/snutt-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @link https://snu4t-api-dev.wafflestudio.com/webjars/swagger-ui/index.html
*/

import { postV1AuthLoginLocal } from './apis/post-v1-auth-login-local';
import { InternalClient } from './httpClient';

export const implSnuttApi = ({ httpClient, apiKey }: { httpClient: SnuttBackendHttpClient; apiKey: string }) =>
apis({
call: async <R>(_: { method: string; path: string; body?: Record<string, unknown>; token?: string }) =>
httpClient.call<R>({
method: _.method,
path: _.path,
body: _.body,
headers: {
'content-type': 'application/json;charset=UTF-8',
...(_.token ? { 'x-access-token': _.token } : {}),
'x-access-apikey': apiKey,
},
}),
});

export type SnuttApi = ReturnType<typeof implSnuttApi>;

type SnuttBackendHttpClient = {
call: <R>(_: {
method: string;
path: string;
body?: Record<string, unknown>;
headers?: Record<string, string>;
}) => Promise<R>;
};

const apis = (client: InternalClient) => ({
'POST /v1/login/local': postV1AuthLoginLocal(client),
});
14 changes: 14 additions & 0 deletions packages/snutt-api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ESNext",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"strict": true,
"module": "ESNext",
"moduleResolution": "Node",
"noEmit": true
},
"include": ["src"]
}
Loading