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

[Fix] fetcher 클래스에서 클라이언트, 서버 로직 둘 다 수용할 수 있도록 수정 #33

Merged
merged 6 commits into from
Aug 20, 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/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run_install: false

- name: Install dependencies
run: pnpm install
run: pnpm install --no-frozen-lockfile

- name: Run build
run: pnpm run build
9 changes: 1 addition & 8 deletions apps/admin/apis/auth/dashboardApi.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { fetcher } from "@wow-class/utils";
import { apiPath } from "constants/apiPath";
import { tags } from "constants/tags";
import { cookies } from "next/headers";
import type { DashboardApiResponseDto } from "types/dto/auth";
import type { DashboardApiResponseDto } from "types/dtos/auth";

export const dashboardApi = {
getDashboardInfo: async () => {
const cookieStore = cookies();
const accessToken = cookieStore.get("accessToken")?.value;

const response = await fetcher.get<DashboardApiResponseDto>(
apiPath.dashboard,
{
next: { tags: [tags.dashboard] },
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);

Expand Down
5 changes: 1 addition & 4 deletions apps/admin/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ const middleware = async (req: NextRequest) => {

return NextResponse.redirect(new URL("/auth", url));
}
const response = NextResponse.next();

response.headers.set("Authorization", `Bearer ${accessToken}`);

return response;
return NextResponse.next();
};

export default middleware;
8 changes: 0 additions & 8 deletions apps/client/apis/dashboardApi.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { fetcher } from "@wow-class/utils";
import { apiPath } from "constants/apiPath";
import { tags } from "constants/tags";
import { cookies } from "next/headers";
import type { DashboardApiResponseDto } from "types/dtos/auth";

export const dashboardApi = {
getDashboardInfo: async () => {
const cookieStore = cookies();
const accessToken = cookieStore.get("accessToken")?.value;

// NOTE: middleware에서 호출하기 위해서 별도로 헤더 주입
const response = await fetcher.get<DashboardApiResponseDto>(
apiPath.dashboard,
{
next: { tags: [tags.dashboard] },
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"clsx": "^2.1.1",
"wowds-icons": "^0.1.3",
"wowds-tokens": "^0.1.1",
"wowds-ui": "^0.1.8",
"wowds-icons": "^0.1.2"
"wowds-ui": "^0.1.8"
}
}
9 changes: 6 additions & 3 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@wow-class/eslint-config": "workspace:*",
"@wow-class/typescript-config": "workspace:*",
"jest": "^29.7.0",
"jest-fetch-mock": "^3.0.3",
"ts-jest": "^29.2.4",
"@wow-class/eslint-config": "workspace:*",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"ts-jest": "^29.2.4"
},
"dependencies": {
"next": "^14.2.5"
}
}
27 changes: 26 additions & 1 deletion packages/utils/src/fetcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ class Fetcher {
): Promise<ApiResponse<T>> {
options = await this.interceptRequest(options);

const fetchOptions: RequestInit = {
...options,
credentials: "include",
};

const fullUrl = this.baseUrl + url;

let response: ApiResponse = await fetch(fullUrl, options);
let response: ApiResponse = await fetch(fullUrl, fetchOptions);

await this.handleError(response);

Expand Down Expand Up @@ -157,6 +162,8 @@ class Fetcher {
}
}

const isClient = typeof window !== "undefined";

const fetcher = new Fetcher({
baseUrl:
process.env.NODE_ENV === "production"
Expand All @@ -165,4 +172,22 @@ const fetcher = new Fetcher({
defaultHeaders: { "Content-Type": "application/json" },
});

if (!isClient) {
fetcher.addRequestInterceptor(async (options) => {
const { cookies } = await import("next/headers");

const cookieStore = cookies();
const accessToken = cookieStore.get("accessToken")?.value;

if (accessToken) {
options.headers = {
...options.headers,
Authorization: `Bearer ${accessToken}`,
};
}

return options;
});
}
ghdtjgus76 marked this conversation as resolved.
Show resolved Hide resolved

export default fetcher;
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.