Skip to content

Commit d9ddc8a

Browse files
authored
Merge pull request #311 from prgrms-web-devcourse-final-project/feat/idempotencykey
feat: 포스팅 헤더 idempotencykey 추가
2 parents 39b0e90 + c60089d commit d9ddc8a

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/apis/emotionRecord.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ interface EmotionRecordsParams {
1717
}
1818

1919
// 감정 기록 포스팅
20-
export const postEmotionRecord = async (emotionRecord: EmotionRecordRequest) => {
21-
const { data } = await axiosInstance.post(`/emotion`, emotionRecord);
20+
export const postEmotionRecord = async (
21+
emotionRecord: EmotionRecordRequest,
22+
idempotencyKey: string,
23+
) => {
24+
const { data } = await axiosInstance.post(`/emotion`, emotionRecord, {
25+
headers: {
26+
'Idempotency-Key': idempotencyKey,
27+
},
28+
});
2229
return data;
2330
};
2431

src/hooks/post/usePostSubmit.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { postEmotionRecord, putEmotionRecord } from '@/apis/emotionRecord';
22
import { useMutation } from '@tanstack/react-query';
3+
import { useRef } from 'react';
34

45
interface RequestDataType {
56
spotifyId: string;
@@ -24,11 +25,13 @@ const usePostSubmit = ({
2425
onSuccess,
2526
onError,
2627
}: UsePostSubmitProps) => {
28+
const idempotencyKeyRef = useRef(crypto.randomUUID()); // idempotency key 생성 (새 글 작성 시 중복 방지)
2729
// ✅ useMutation 설정 (작성, 수정)
2830
const { mutate, isPending, isSuccess, isError } = useMutation({
2931
mutationFn: async (requestData: RequestDataType) => {
3032
if (mode === 'edit') return putEmotionRecord(Number(postId), requestData); // 수정 모드
31-
if (mode === 'create') return postEmotionRecord(requestData); // 새 글 작성 모드
33+
console.log('idempotencyKeyRef', idempotencyKeyRef);
34+
if (mode === 'create') return postEmotionRecord(requestData, idempotencyKeyRef.current); // 새 글 작성 모드
3235
},
3336
onSuccess,
3437
onError,

0 commit comments

Comments
 (0)