Skip to content

Commit af8fad6

Browse files
committed
refactor: 코멘트 반영 겸 자잘한 오류 해결
1 parent 372c957 commit af8fad6

File tree

11 files changed

+41
-33
lines changed

11 files changed

+41
-33
lines changed

.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ NEXT_PUBLIC_BASE_URL=<'server url here'>
44
NEXT_PUBLIC_VELOG_URL=https://velog.io
55
NEXT_PUBLIC_ABORT_MS=<'abort time(ms) for fetch here'>
66
SENTRY_AUTH_TOKEN=<'sentry auth token here'>
7+
NEXT_PUBLIC_CHANNELTALK_PLUGIN_KEY=<'channelTalk plugin key here'>
78
NEXT_PUBLIC_EVENT_LOG=<'Whether to send an event log here (true | false)'>
89
SENTRY_DSN=<'sentry dsn here'>

src/apis/dashboard.request.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
import { PostDetailDto, PostListDto, PostSummaryDto } from '@/types';
22
import { PATHS } from '@/constants';
3-
import { InitType, instance } from './instance.request';
3+
import { instance } from './instance.request';
44

55
type SortType = {
66
asc: boolean;
77
sort: string;
88
};
99

10-
export const postList = async (
11-
props: InitType<PostListDto>,
12-
sort: SortType,
13-
cursor?: string,
14-
) =>
10+
export const postList = async (sort: SortType, cursor?: string) =>
1511
await instance<null, PostListDto>(
1612
cursor
1713
? `${PATHS.POSTS}?cursor=${cursor}&asc=${sort.asc}&sort=${sort.sort}`
1814
: `${PATHS.POSTS}?asc=${sort.asc}&sort=${sort.sort}`,
19-
props,
2015
);
2116

22-
export const postSummary = async (props: InitType<PostSummaryDto>) =>
23-
await instance<null, PostSummaryDto>(PATHS.SUMMARY, props);
17+
export const postSummary = async () =>
18+
await instance<null, PostSummaryDto>(PATHS.SUMMARY);
2419

2520
export const postDetail = async (path: string, start: string, end: string) =>
2621
await instance<null, PostDetailDto>(

src/apis/instance.request.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,20 @@ export const instance = async <I, R>(
6060
init?: InitType<I>,
6161
error?: Record<string, Error>,
6262
): Promise<R> => {
63+
let cookieHeader = '';
64+
if (typeof window === 'undefined') {
65+
cookieHeader = (await import('next/headers')).cookies().toString();
66+
}
67+
6368
try {
6469
const data = await fetch('/api' + input, {
6570
...init,
71+
headers: cookieHeader
72+
? {
73+
...init?.headers,
74+
Cookie: cookieHeader,
75+
}
76+
: init?.headers,
6677
body: init?.body ? JSON.stringify(init.body) : undefined,
6778
signal: AbortSignal.timeout
6879
? AbortSignal.timeout(ABORT_MS)

src/apis/user.request.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NotFoundError } from '@/errors';
22
import { PATHS } from '@/constants';
33
import { LoginVo, UserDto } from '@/types';
4-
import { InitType, instance } from './instance.request';
4+
import { instance } from './instance.request';
55

66
export const login = async (body: LoginVo) =>
77
await instance(
@@ -15,8 +15,7 @@ export const login = async (body: LoginVo) =>
1515
},
1616
);
1717

18-
export const me = async (props: InitType<UserDto>) =>
19-
await instance<null, UserDto>(PATHS.ME, props);
18+
export const me = async () => await instance<null, UserDto>(PATHS.ME);
2019

2120
export const logout = async () =>
2221
await instance(PATHS.LOGOUT, { method: 'POST', body: undefined });

src/app/(with-tracker)/(auth-required)/layout.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ReactElement } from 'react';
22
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
3-
import { cookies } from 'next/headers';
43
import { Header } from '@/components';
54
import { PATHS } from '@/constants';
65
import { me } from '@/apis';
@@ -15,8 +14,7 @@ export default async function Layout({ children }: IProp) {
1514

1615
await client.prefetchQuery({
1716
queryKey: [PATHS.ME],
18-
queryFn: async () =>
19-
await me({ headers: { Cookie: cookies().toString() } }),
17+
queryFn: me,
2018
});
2119

2220
return (

src/app/(with-tracker)/(auth-required)/main/Content.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const Content = () => {
3030
queryKey: [PATHS.POSTS, [searchParams.asc, searchParams.sort]], // Query Key
3131
queryFn: async ({ pageParam = '' }) =>
3232
await postList(
33-
{},
3433
{ asc: searchParams.asc === 'true', sort: searchParams.sort || '' },
3534
pageParam,
3635
),
@@ -41,7 +40,7 @@ export const Content = () => {
4140

4241
const { data: summaries } = useQuery({
4342
queryKey: [PATHS.SUMMARY],
44-
queryFn: async () => await postSummary({}),
43+
queryFn: postSummary,
4544
});
4645

4746
useEffect(() => {

src/app/(with-tracker)/(auth-required)/main/page.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
22
import { Metadata } from 'next';
3-
import { cookies } from 'next/headers';
43
import { PATHS } from '@/constants';
54
import { postList, postSummary } from '@/apis';
65
import { getQueryClient } from '@/utils/queryUtil';
@@ -24,20 +23,16 @@ export default async function Page({ searchParams }: IProp) {
2423
await client.prefetchInfiniteQuery({
2524
queryKey: [PATHS.POSTS, [searchParams.asc, searchParams.sort]],
2625
queryFn: async () =>
27-
await postList(
28-
{ headers: { Cookie: cookies().toString() } },
29-
{
30-
asc: searchParams.asc === 'true',
31-
sort: searchParams.sort || '',
32-
},
33-
),
26+
await postList({
27+
asc: searchParams.asc === 'true',
28+
sort: searchParams.sort || '',
29+
}),
3430
initialPageParam: undefined,
3531
});
3632

3733
await client.prefetchQuery({
3834
queryKey: [PATHS.SUMMARY],
39-
queryFn: async () =>
40-
await postSummary({ headers: { Cookie: cookies().toString() } }),
35+
queryFn: postSummary,
4136
});
4237

4338
return (

src/components/auth-required/header/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,13 @@ export const Header = () => {
3737

3838
const { mutate: out } = useMutation({
3939
mutationFn: logout,
40-
onSuccess: () => {
41-
client.removeQueries();
42-
router.replace('/');
43-
},
40+
onMutate: () => router.replace('/'),
41+
onSuccess: () => client.removeQueries(),
4442
});
4543

4644
const { data: profiles } = useQuery({
4745
queryKey: [PATHS.ME],
48-
queryFn: async () => me({}),
46+
queryFn: me,
4947
});
5048

5149
useEffect(() => {

src/components/auth-required/main/Section/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import { COLORS, PATHS } from '@/constants';
77
import { Icon } from '@/components';
88
import { PostType, UserDto } from '@/types';
99
import { trackUserEvent, MessageEnum } from '@/utils/trackUtil';
10+
import { UserNameNotFound } from '@/errors';
1011
import { Graph } from './Graph';
1112

1213
export const Section = (p: PostType) => {
1314
const [open, setOpen] = useState(false);
1415
const client = useQueryClient();
1516

16-
const username = (client.getQueryData([PATHS.ME]) as UserDto)?.username;
17+
const { username } = client.getQueryData([PATHS.ME]) as UserDto;
18+
if (!username) {
19+
throw new UserNameNotFound();
20+
}
1721
const url = `${process.env.NEXT_PUBLIC_VELOG_URL}/@${username || ''}/${p.slug}`;
1822

1923
return (

src/errors/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './fetch.error';
2+
export * from './main.error';

0 commit comments

Comments
 (0)