Skip to content

Commit 2b8ac7b

Browse files
committed
thjs-63: * fix client app crashing
1 parent b023aaa commit 2b8ac7b

File tree

7 files changed

+44
-48
lines changed

7 files changed

+44
-48
lines changed

client/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ VITE_API_SERVER=http://localhost:3001
66
VITE_SOCKET_SERVER=ws://localhost:3001
77
VITE_API_PATH=/api
88
VITE_SOCKET_PATH=/socket.io
9-
VITE_PORT=3000
10-
VITE_HOST=localhost
9+
VITE_APP_PORT=3000
10+
VITE_APP_HOST=localhost

client/src/packages/http/http-api.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Http implements HttpApi {
3030
contentType = ContentType.JSON,
3131
query
3232
} = options;
33-
const headers = await this.#getHeaders({
33+
const headers = this.#getHeaders({
3434
hasAuth,
3535
contentType
3636
});
@@ -40,31 +40,31 @@ class Http implements HttpApi {
4040
headers,
4141
body: payload
4242
})
43-
.then(void this.#checkStatus)
44-
.then<T>(void this.#parseJSON)
45-
.catch(void this.#throwError);
43+
.then(this.#checkStatus)
44+
.then<T>(this.#parseJSON)
45+
.catch(this.#throwError);
4646
}
4747

48-
async #getHeaders({
48+
#getHeaders({
4949
hasAuth,
5050
contentType
51-
}: Pick<HttpOptions, 'hasAuth' | 'contentType'>): Promise<Headers> {
51+
}: Pick<HttpOptions, 'hasAuth' | 'contentType'>): Headers {
5252
const headers = new Headers();
5353

5454
if (contentType) {
5555
headers.append(HttpHeader.CONTENT_TYPE, contentType);
5656
}
5757

5858
if (hasAuth) {
59-
const token = await this.#storageApi.get(StorageKey.TOKEN);
59+
const token = this.#storageApi.get(StorageKey.TOKEN);
6060

6161
headers.append(HttpHeader.AUTHORIZATION, `Bearer ${token}`);
6262
}
6363

6464
return headers;
6565
}
6666

67-
async #checkStatus(response: Response): Promise<Response> {
67+
#checkStatus = async (response: Response): Promise<Response> => {
6868
if (!response.ok) {
6969
const parsedException = (await response.json().catch(() => ({
7070
message: response.statusText
@@ -77,26 +77,26 @@ class Http implements HttpApi {
7777
}
7878

7979
return response;
80-
}
80+
};
8181

82-
#getUrl<T extends Record<string, unknown>>(
82+
#getUrl = <T extends Record<string, unknown>>(
8383
url: string,
8484
query: T | undefined
85-
): string {
85+
): string => {
8686
if (query) {
8787
return `${url}?${getStringifiedQuery(query)}`;
8888
}
8989

9090
return url;
91-
}
91+
};
9292

93-
#parseJSON<T>(response: Response): Promise<T> {
93+
#parseJSON = <T>(response: Response): Promise<T> => {
9494
return response.json() as Promise<T>;
95-
}
95+
};
9696

97-
#throwError(error: Error): never {
97+
#throwError = (error: Error): never => {
9898
throw error;
99-
}
99+
};
100100
}
101101

102102
export { Http };

client/src/packages/storage/libs/types/storage-api.type.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { type StorageKey } from '~/libs/enums/enums.js';
22
import { type ValueOf } from '~/libs/types/types.js';
33

44
type StorageApi = {
5-
set(key: ValueOf<typeof StorageKey>, value: string): Promise<void>;
6-
get<R = string>(key: ValueOf<typeof StorageKey>): Promise<R | null>;
7-
drop(key: ValueOf<typeof StorageKey>): Promise<void>;
8-
has(key: ValueOf<typeof StorageKey>): Promise<boolean>;
9-
clear(): Promise<void>;
5+
set(key: ValueOf<typeof StorageKey>, value: string): void;
6+
get<R = string>(key: ValueOf<typeof StorageKey>): R | null;
7+
drop(key: ValueOf<typeof StorageKey>): void;
8+
has(key: ValueOf<typeof StorageKey>): boolean;
9+
clear(): void;
1010
};
1111

1212
export { type StorageApi };

client/src/packages/storage/storage-api.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@ class Storage implements StorageApi {
1414
this.#storage = storage;
1515
}
1616

17-
public set(key: ValueOf<typeof StorageKey>, value: string): Promise<void> {
17+
public set(key: ValueOf<typeof StorageKey>, value: string): void {
1818
this.#storage.setItem(key as string, value);
19-
20-
return Promise.resolve();
2119
}
2220

23-
public get<R = string>(key: ValueOf<typeof StorageKey>): Promise<R | null> {
24-
return Promise.resolve(this.#storage.getItem(key as string) as R);
21+
public get<R = string>(key: ValueOf<typeof StorageKey>): R | null {
22+
return this.#storage.getItem(key as string) as R;
2523
}
2624

27-
public drop(key: ValueOf<typeof StorageKey>): Promise<void> {
25+
public drop(key: ValueOf<typeof StorageKey>): void {
2826
this.#storage.removeItem(key as string);
29-
30-
return Promise.resolve();
3127
}
3228

33-
public async has(key: ValueOf<typeof StorageKey>): Promise<boolean> {
34-
const value = await this.get(key);
29+
public has(key: ValueOf<typeof StorageKey>): boolean {
30+
const value = this.get(key);
3531

3632
return Boolean(value);
3733
}

client/src/slices/profile/actions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const login = createAsyncThunk<
2121
>(ActionType.LOG_IN, async (request, { extra: { authApi, storageApi } }) => {
2222
const { user, token } = await authApi.login(request);
2323

24-
void storageApi.set(StorageKey.TOKEN, token);
24+
storageApi.set(StorageKey.TOKEN, token);
2525

2626
return user;
2727
});
@@ -33,15 +33,15 @@ const register = createAsyncThunk<
3333
>(ActionType.REGISTER, async (request, { extra: { authApi, storageApi } }) => {
3434
const { user, token } = await authApi.registration(request);
3535

36-
void storageApi.set(StorageKey.TOKEN, token);
36+
storageApi.set(StorageKey.TOKEN, token);
3737

3838
return user;
3939
});
4040

4141
const logout = createAsyncThunk<null, undefined, AsyncThunkConfig>(
4242
ActionType.LOG_OUT,
4343
(_request, { extra: { storageApi } }) => {
44-
void storageApi.drop(StorageKey.TOKEN);
44+
storageApi.drop(StorageKey.TOKEN);
4545

4646
return null;
4747
}

client/vite.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const viteReactPlugin = reactPlugin as unknown as (
99
const config = ({ mode }: ConfigEnv): ReturnType<typeof defineConfig> => {
1010
// import.meta.env doesn't exist at this moment
1111
const {
12-
VITE_PORT,
13-
VITE_HOST,
12+
VITE_APP_PORT,
13+
VITE_APP_HOST,
1414
VITE_API_PATH,
1515
VITE_API_SERVER,
1616
VITE_SOCKET_SERVER,
@@ -22,8 +22,8 @@ const config = ({ mode }: ConfigEnv): ReturnType<typeof defineConfig> => {
2222
outDir: 'build'
2323
},
2424
server: {
25-
host: VITE_HOST as string,
26-
port: Number(VITE_PORT),
25+
host: VITE_APP_HOST as string,
26+
port: Number(VITE_APP_PORT),
2727
proxy: {
2828
[VITE_API_PATH as string]: VITE_API_SERVER as string,
2929
[VITE_SOCKET_PATH as string]: {

server/src/libs/packages/http/http.service.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Http implements HttpService {
2121
url: string,
2222
options?: HttpLoadOptions<T>
2323
): Promise<K> | never {
24-
const { method = HttpMethod.GET, data, headers } = options ?? {};
24+
const { data, headers, method = HttpMethod.GET } = options ?? {};
2525

2626
return this.#instance
2727
.request<T, K>({
@@ -30,20 +30,20 @@ class Http implements HttpService {
3030
headers: headers as NonNullable<typeof headers>,
3131
data
3232
})
33-
.then(void this.#getData)
34-
.catch(void this.#catchError);
33+
.then(this.#getData)
34+
.catch(this.#catchError);
3535
}
3636

37-
#getData<T>(response: AxiosResponse<T>): AxiosResponse<T>['data'] {
38-
return response.data;
39-
}
37+
#getData = <T>(response: T): AxiosResponse<T>['data'] => {
38+
return (response as AxiosResponse<T>).data;
39+
};
4040

41-
#catchError<T = unknown>(error: AxiosError): never {
41+
#catchError = <T = unknown>(error: AxiosError): never => {
4242
const { response } = error;
4343
const { data } = response as AxiosResponse<T>;
4444

4545
throw new Error((data as Error).toString());
46-
}
46+
};
4747
}
4848

4949
export { Http };

0 commit comments

Comments
 (0)