Skip to content

Comments

✨[Feat] block 생성 api 호출하는 함수 생성 #111#116

Merged
lumpenop merged 1 commit intodevelopfrom
feature/theme-fix
Oct 22, 2024
Merged

✨[Feat] block 생성 api 호출하는 함수 생성 #111#116
lumpenop merged 1 commit intodevelopfrom
feature/theme-fix

Conversation

@lumpenop
Copy link
Collaborator

@lumpenop lumpenop commented Oct 22, 2024

작업사항

  • block post api 호출하는 함수 생성

미리보기, 사용방법 및 결과물

생성한 함수

export const postBlock = async (
  path: string,
  params: { [index: string]: string | number },
  router?: AppRouterInstance,
) => {
  const token = sessionStorage.getItem("token");
  if (!token) {
    if (router) router.push("/login");
    return;
  }
  params["sequence"] = (await getSequence(token)) + 1;

  try {
    const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}${path}`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${token}`,
      },
      body: JSON.stringify(params),
    });
    const responseJson = await response.json();
    if (response.ok) {
      console.log("성공");
      return responseJson;
    } else {
      const { status } = response;
      const { message } = responseJson;
      if (status === 500) {
        alert("서버 에러");
      }
      console.log(`Error: ${status}, Message: ${message || "Unknown error"}`);
    }
  } catch (error) {
    throw new Error(error instanceof Error ? error.message : "알 수 없는 에러");
  }
};

사용

const addImageBlock = () => {
    const params = {
      type: 4,
      title,
      url: connectingUrl,
      imgUrl: selectedImageUrl,
    };
    postBlock("/api/link/add", params, router).then((res) => {
      if (res) console.log(res);
    });
  };

기타

논의 후 사용하는 편이 더 불편하다 하시면 삭제할 예정입니다

작성일

2024.10.22

@lumpenop lumpenop added ✨ Feature [작업 유형] 새로운 기능 개발을 위한 이슈나 PR ✅ Done [작업 상황] 작업이 완료된 이슈 또는 머지된 PR labels Oct 22, 2024
@lumpenop lumpenop added this to the 3rd Week (개선) milestone Oct 22, 2024
@lumpenop lumpenop self-assigned this Oct 22, 2024
@lumpenop lumpenop changed the title ✨[Feat] block post api 호출하는 함수 생성 #111 ✨[Feat] block 생성 api 호출하는 함수 생성 #111 Oct 22, 2024
Copy link
Collaborator

@s-ja s-ja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인했습니다! 고생하셨어요 :)

@lumpenop lumpenop merged commit 453a162 into develop Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✅ Done [작업 상황] 작업이 완료된 이슈 또는 머지된 PR ✨ Feature [작업 유형] 새로운 기능 개발을 위한 이슈나 PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants