Skip to content
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
Empty file removed src/api/.gitkeep
Empty file.
40 changes: 40 additions & 0 deletions src/api/make-api-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export default async function makeApiRequest<T, R = string>(
method: "GET" | "POST" | "PUT" | "DELETE",
endpoint: string,
options: {
token: string | null;
data?: T;
responseType?: "json" | "text";
},
): Promise<R> {
const { token, data, responseType = "json" } = options;

if (!token) {
throw new Error("로그인이 필요합니다.");
}

const headers: HeadersInit = {
Authorization: `Bearer ${token}`,
...(data && { "Content-Type": "application/json" }),
};

const config: RequestInit = {
method,
headers,
...(data && { body: JSON.stringify(data) }),
};

const response = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}${endpoint}`,
config,
);

if (!response.ok) {
const errorData = await response.text();
throw new Error(
errorData || `요청 실패: ${response.status} ${response.statusText}`,
);
}

return responseType === "json" ? response.json() : response.text();
}
281 changes: 44 additions & 237 deletions src/api/transaction/index.ts
Original file line number Diff line number Diff line change
@@ -1,195 +1,68 @@
import {
BuyMarketPriceResponse,
CancelData,
LimitPriceOrderHistory,
MarketPriceHistoryResponse,
ModifyTradeFormData,
SellMarketPriceResponse,
TradeAtLimitPriceFormDataType,
TradeAtMarketPriceFormDataType,
} from "@/app/search/[id]/types";
TradeHistory,
TradeLimitPriceResponse,
} from "@/types/transaction";

import makeApiRequest from "../make-api-request";

// 현재가 매수
export async function buyAtMarketPrice({
token,
data,
}: TradeAtMarketPriceFormDataType): Promise<string> {
if (token === null) {
throw new Error();
}
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/account/buy`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
},
);

if (!res.ok) {
throw new Error(`Failed to post transaction data: ${res.status}`);
}

return await res.json();
} catch {
throw new Error();
}
}: TradeAtMarketPriceFormDataType): Promise<BuyMarketPriceResponse> {
return makeApiRequest("POST", "/api/account/buy", { token, data });
}

// 지정가 매수
export async function buyAtLimitPrice({
// 현재가 매도
export async function sellAtMarketPrice({
token,
data,
}: TradeAtLimitPriceFormDataType): Promise<string> {
if (token === null) {
throw new Error();
}
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/account/order/buy`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
},
);

if (!res.ok) {
throw new Error(`Failed to post transaction data: ${res.status}`);
}

return await res.json();
} catch {
throw new Error();
}
}: TradeAtMarketPriceFormDataType): Promise<SellMarketPriceResponse> {
return makeApiRequest("POST", "/api/account/sell", { token, data });
}

// 현재가 매도
export async function sellAtMarketPrice({
// 지정가 매수
export async function buyAtLimitPrice({
token,
data,
}: TradeAtMarketPriceFormDataType): Promise<string> {
if (token === null) {
throw new Error();
}
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/account/sell`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
},
);

if (!res.ok) {
throw new Error(`Failed to post transaction data: ${res.status}`);
}

return await res.json();
} catch {
throw new Error();
}
}: TradeAtLimitPriceFormDataType): Promise<TradeLimitPriceResponse> {
return makeApiRequest("POST", "/api/account/order/buy", { token, data });
}

// 지정가 매도
export async function sellAtLimitPrice({
token,
data,
}: TradeAtLimitPriceFormDataType): Promise<string> {
if (token === null) {
throw new Error();
}
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/account/order/sell`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
},
);

if (!res.ok) {
throw new Error(`Failed to post transaction data: ${res.status}`);
}

return await res.json();
} catch {
throw new Error();
}
}: TradeAtLimitPriceFormDataType): Promise<TradeLimitPriceResponse> {
return makeApiRequest("POST", "/api/account/order/sell", { token, data });
}

// 체결내역 조회
// 정정취소 화면의 매수/매도 체결내역 조회
export async function getHistory(
token: string,
stockName: string,
): Promise<string> {
if (token === null) {
throw new Error();
}
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/account/${stockName}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
},
);

if (!res.ok) {
throw new Error(`Failed to post transaction data: ${res.status}`);
}

return await res.json();
} catch {
throw new Error();
}
}

export interface OrderHistory {
OrderId: number;
buyPrice: number;
remainCount: number;
stockCount: number;
stockName: string;
type: string;
): Promise<MarketPriceHistoryResponse[]> {
return makeApiRequest("GET", `/api/account/${stockName}`, {
token,
});
}

// 지정가 매수/매도 내역
export async function getTrade(
token: string | null,
stockName: string,
): Promise<OrderHistory[]> {
if (token === null) {
throw new Error();
}

const response = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/account/orders/${stockName}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
},
);

if (!response.ok) {
throw new Error("Failed to fetch stocks");
}

return response.json();
): Promise<LimitPriceOrderHistory[]> {
return makeApiRequest("GET", `/api/account/orders/${stockName}`, {
token,
});
}

// 지정가 정정
Expand All @@ -198,96 +71,30 @@ export async function modifyTrade({
orderId,
data,
}: ModifyTradeFormData): Promise<string> {
if (token === null || orderId === undefined) {
throw new Error();
}
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/account/order/${orderId}/modify`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
},
);

if (!res.ok) {
throw new Error(`Failed to post transaction data: ${res.status}`);
}

return await res.text();
} catch {
throw new Error();
}
return makeApiRequest("PUT", `/api/account/order/${orderId}/modify`, {
token,
data,
responseType: "text",
});
}

interface CancelData {
token: string | null;
orderId: string;
}
// 지정가 취소
export async function cancelTrade({
token,
orderId,
}: CancelData): Promise<string> {
if (token === null) {
throw new Error();
}
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/account/order/${orderId}/cancel`,
{
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
},
);

if (!res.ok) {
throw new Error(`Failed to post transaction data: ${res.status}`);
}

return await res.text();
} catch {
throw new Error();
}
}

export interface TradeHistory {
id: number;
buyPrice: number;
remainCount: number;
stockCount: number;
stockName: string;
buyOrder: string;
return makeApiRequest("DELETE", `/api/account/order/${orderId}/cancel`, {
token,
responseType: "text",
});
}

// 매수/매도 체결내역
// 체결내역
export async function getTradeHistory(
token: string | null,
stockName: string,
): Promise<TradeHistory[]> {
if (token === null) {
throw new Error();
}

const response = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/account/accounts/save/${stockName}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
},
);

if (!response.ok) {
throw new Error("Failed to fetch stocks");
}

return response.json();
return makeApiRequest("GET", `/api/account/accounts/save/${stockName}`, {
token,
});
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { OrderHistory } from "@/api/transaction";
import CheckButton from "@/components/common/check-button";
import { LimitPriceOrderHistory } from "@/types/transaction";
import cn from "@/utils/cn";
import { getKoreanPrice } from "@/utils/price";

interface EditTableBodyProps {
data: OrderHistory;
data: LimitPriceOrderHistory;
isChecked: boolean;
toggleSelection: (orderId: string) => void;
}
Expand Down
Loading
Loading