Skip to content
Merged
Changes from 2 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
55 changes: 55 additions & 0 deletions src/api/imageApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import api from './api';
import { AxiosError } from 'axios';
import type { LinkInfo } from './shopApi';

interface ErrorMessage {
message: string;
}

interface ImageRequest {
name: string;
}

interface ImageResponse {
item: {
url: string; // ✅ 쿼리 스트링 포함된 presigned S3 PUT URL
};
links: LinkInfo[];
}

// Presigned URL 발급 요청
export const getPresignedUrl = async (filename: string): Promise<string> => {
try {
const body: ImageRequest = { name: filename };
const response = await api.post<ImageResponse>('/images', body);
Copy link
Contributor

Choose a reason for hiding this comment

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

💬 이렇게 될 경우 굳이 ImageRequest type을 선언하지 않아도 될 거 같습니다! 한 줄로 합쳐도 될 것 같고요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

오 그렇네요! 변경하겠습니다!

return response.data.item.url;
} catch (error) {
const axiosError = error as AxiosError<ErrorMessage>; // 에러 타입 명시
if (axiosError.response) {
throw new Error(axiosError.response.data.message);
} else {
throw new Error('서버에 연결할 수 없습니다. 인터넷 연결을 확인해주세요.');
}
}
};

// S3 업로드
export const uploadImageToS3 = async (
uploadUrl: string,
file: File,
): Promise<void> => {
try {
await api.put(uploadUrl, file, {
headers: {
'Content-Type': file.type,
},
});
} catch (error) {
const axiosError = error as AxiosError<ErrorMessage>; // 에러 타입 명시
if (axiosError.response) {
throw new Error(axiosError.response.data.message);
} else {
throw new Error('서버에 연결할 수 없습니다. 인터넷 연결을 확인해주세요.');
}
}
};