불필요 파일 삭제
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
/** HTTP 실패 응답의 상태와 본문을 보존해 기능별 오류 안내에 전달한다. */
|
||||
export class ApiRequestError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly status: number,
|
||||
public readonly body: unknown,
|
||||
) {
|
||||
super(message);
|
||||
this.name = "ApiRequestError";
|
||||
}
|
||||
}
|
||||
|
||||
type HttpTransportOptions = {
|
||||
baseUrl: string;
|
||||
headers?: HeadersInit;
|
||||
onUnauthorized?: () => void;
|
||||
};
|
||||
|
||||
/** JSON 오류는 서버 메시지를 사용하고 HTML·빈 오류는 HTTP 상태로 설명한다. */
|
||||
const requestError = (response: Response, body: unknown) => {
|
||||
const bodyMessage =
|
||||
typeof body === "object" && body !== null
|
||||
? ((body as { message?: unknown }).message ??
|
||||
(body as { error?: { message?: unknown } }).error?.message)
|
||||
: undefined;
|
||||
return new ApiRequestError(
|
||||
typeof bodyMessage === "string" && bodyMessage.trim()
|
||||
? bodyMessage
|
||||
: `API request failed: ${response.status} ${response.statusText}`.trim(),
|
||||
response.status,
|
||||
body,
|
||||
);
|
||||
};
|
||||
|
||||
/** HTTP 실패에서는 파싱 실패로 원래 상태를 가리지 않고 원문을 보관한다. */
|
||||
const readErrorBody = async (response: Response): Promise<unknown> => {
|
||||
const text = await response.text().catch(() => "");
|
||||
if (!text.trim()) return null;
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
};
|
||||
|
||||
/** 정상 JSON의 빈 본문은 null이며, 비어 있지 않은 잘못된 JSON은 오류다. */
|
||||
const readJsonBody = async (response: Response, url: string): Promise<any> => {
|
||||
const text = await response.text();
|
||||
if (!text.trim()) return null;
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch {
|
||||
throw new ApiRequestError(
|
||||
`JSON 응답이 아닌 데이터를 받았습니다: ${url}`,
|
||||
response.status,
|
||||
text,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* URL·헤더·HTTP 성공 여부를 한곳에서 처리한다.
|
||||
* 인증 정책은 호출자가 주입하므로 shared가 entities 또는 app을 역참조하지 않는다.
|
||||
* 래퍼 해제와 raw 응답은 명시적으로 구분하며 필드 이름을 자동 변환하지 않는다.
|
||||
*/
|
||||
export const createHttpTransport = (options: HttpTransportOptions) => {
|
||||
const resolveUrl = (url: string) =>
|
||||
url.startsWith("/") ? url : `${options.baseUrl}/${url}`;
|
||||
|
||||
/** 요청 헤더 형식(객체·Headers·튜플)에 관계없이 사이트 헤더를 보존한다. */
|
||||
const request = async (url: string, init: RequestInit): Promise<Response> => {
|
||||
const headers = new Headers(init.headers);
|
||||
new Headers(options.headers).forEach((value, key) =>
|
||||
headers.set(key, value),
|
||||
);
|
||||
const response = await fetch(resolveUrl(url), { ...init, headers });
|
||||
if (response.status === 401) options.onUnauthorized?.();
|
||||
if (!response.ok) {
|
||||
throw requestError(response, await readErrorBody(response));
|
||||
}
|
||||
return response;
|
||||
};
|
||||
|
||||
/** GET/POST/PUT가 같은 상태 판정·빈 본문·JSON 파싱 규칙을 공유한다. */
|
||||
const requestJson = async (url: string, init: RequestInit) =>
|
||||
readJsonBody(await request(url, init), url);
|
||||
|
||||
/** JSON 쓰기 요청의 직렬화와 Content-Type을 통일한다. */
|
||||
const jsonInit = (method: string, data: object): RequestInit => ({
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
return {
|
||||
/** 기존 공통 래퍼 조회 계약을 유지한다. */
|
||||
getData: async (url: string): Promise<any> => {
|
||||
const body = await requestJson(url, { method: "GET" });
|
||||
return body === null ? null : body.data;
|
||||
},
|
||||
/** 등록 실패는 reject하며 정상 공통 래퍼의 data만 반환한다. */
|
||||
postData: async (url: string, data: object): Promise<any> => {
|
||||
const body = await requestJson(url, jsonInit("POST", data));
|
||||
return body === null ? null : body.data;
|
||||
},
|
||||
/** 기존 PUT의 래퍼·raw 호환을 유지한다. */
|
||||
putData: async (url: string, data: object): Promise<any> => {
|
||||
const body = await requestJson(url, jsonInit("PUT", data));
|
||||
return typeof body === "object" && body !== null && "data" in body
|
||||
? (body.data ?? null)
|
||||
: body;
|
||||
},
|
||||
/** GeoJSON 등 원문 JSON과 AbortSignal을 그대로 유지한다. */
|
||||
getRawJson: (url: string, init: RequestInit = {}): Promise<any> =>
|
||||
requestJson(url, { ...init, method: "GET" }),
|
||||
/** 성공 응답의 래퍼 유무를 기능 adapter가 결정하도록 원문을 반환한다. */
|
||||
postRawJson: (url: string, data: object): Promise<any> =>
|
||||
requestJson(url, jsonInit("POST", data)),
|
||||
/** 204=true, 그 외 성공=읽지 않은 Response인 기존 소비자 계약을 유지한다. */
|
||||
deleteData: async (
|
||||
url: string,
|
||||
data?: object,
|
||||
): Promise<true | Response> => {
|
||||
const response = await request(url, {
|
||||
method: "DELETE",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: data ? JSON.stringify(data) : undefined,
|
||||
});
|
||||
return response.status === 204 ? true : response;
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -1 +1,3 @@
|
||||
export { client } from "./client";
|
||||
/** SN 내부 HTTP 기반의 공개 진입점. 인증·화면 정책은 호출자가 조합한다. */
|
||||
export { ApiRequestError, createHttpTransport } from "./http-transport";
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import { useStore } from "zustand";
|
||||
import { useContext } from "react";
|
||||
import type { DataStore } from "../store/data-store";
|
||||
import { StoreContext } from "../providers/store-provider";
|
||||
|
||||
export const useDataStore = <T>(selector: (store: DataStore) => T): T => {
|
||||
const storeContext = useContext(StoreContext);
|
||||
|
||||
if (!storeContext) {
|
||||
throw new Error("useDataStore must be used within StoreProvider");
|
||||
}
|
||||
|
||||
return useStore(storeContext.dataStore, selector);
|
||||
};
|
||||
@@ -1,175 +0,0 @@
|
||||
import * as React from "react";
|
||||
import * as LabelPrimitive from "@radix-ui/react-label";
|
||||
import { Slot } from "@radix-ui/react-slot";
|
||||
import {
|
||||
Controller,
|
||||
FormProvider,
|
||||
useFormContext,
|
||||
type ControllerProps,
|
||||
type FieldPath,
|
||||
type FieldValues,
|
||||
} from "react-hook-form";
|
||||
import { cn, Label } from "@common/components";
|
||||
|
||||
const Form = FormProvider;
|
||||
|
||||
type FormFieldContextValue<
|
||||
TFieldValues extends FieldValues = FieldValues,
|
||||
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
||||
> = {
|
||||
name: TName;
|
||||
};
|
||||
|
||||
const FormFieldContext = React.createContext<FormFieldContextValue>(
|
||||
{} as FormFieldContextValue,
|
||||
);
|
||||
|
||||
const FormField = <
|
||||
TFieldValues extends FieldValues = FieldValues,
|
||||
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
||||
>({
|
||||
...props
|
||||
}: ControllerProps<TFieldValues, TName>) => {
|
||||
return (
|
||||
<FormFieldContext.Provider value={{ name: props.name }}>
|
||||
<Controller {...props} />
|
||||
</FormFieldContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
const useFormField = () => {
|
||||
const fieldContext = React.useContext(FormFieldContext);
|
||||
const itemContext = React.useContext(FormItemContext);
|
||||
const { getFieldState, formState } = useFormContext();
|
||||
|
||||
const fieldState = getFieldState(fieldContext.name, formState);
|
||||
|
||||
if (!fieldContext) {
|
||||
throw new Error("useFormField should be used within <FormField>");
|
||||
}
|
||||
|
||||
const { id } = itemContext;
|
||||
|
||||
return {
|
||||
id,
|
||||
name: fieldContext.name,
|
||||
formItemId: `${id}-form-item`,
|
||||
formDescriptionId: `${id}-form-item-description`,
|
||||
formMessageId: `${id}-form-item-message`,
|
||||
...fieldState,
|
||||
};
|
||||
};
|
||||
|
||||
type FormItemContextValue = {
|
||||
id: string;
|
||||
};
|
||||
|
||||
const FormItemContext = React.createContext<FormItemContextValue>(
|
||||
{} as FormItemContextValue,
|
||||
);
|
||||
|
||||
const FormItem = React.forwardRef<
|
||||
HTMLDivElement,
|
||||
React.HTMLAttributes<HTMLDivElement>
|
||||
>(({ className, ...props }, ref) => {
|
||||
const id = React.useId();
|
||||
|
||||
return (
|
||||
<FormItemContext.Provider value={{ id }}>
|
||||
<div ref={ref} className={cn("space-y-2", className)} {...props} />
|
||||
</FormItemContext.Provider>
|
||||
);
|
||||
});
|
||||
FormItem.displayName = "FormItem";
|
||||
|
||||
const FormLabel = React.forwardRef<
|
||||
React.ElementRef<typeof LabelPrimitive.Root>,
|
||||
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root>
|
||||
>(({ className, ...props }, ref) => {
|
||||
const { error, formItemId } = useFormField();
|
||||
|
||||
return (
|
||||
<Label
|
||||
ref={ref}
|
||||
className={cn(error && "text-destructive", className)}
|
||||
htmlFor={formItemId}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
});
|
||||
FormLabel.displayName = "FormLabel";
|
||||
|
||||
const FormControl = React.forwardRef<
|
||||
React.ElementRef<typeof Slot>,
|
||||
React.ComponentPropsWithoutRef<typeof Slot>
|
||||
>(({ ...props }, ref) => {
|
||||
const { error, formItemId, formDescriptionId, formMessageId } =
|
||||
useFormField();
|
||||
|
||||
return (
|
||||
<Slot
|
||||
ref={ref}
|
||||
id={formItemId}
|
||||
aria-describedby={
|
||||
!error
|
||||
? `${formDescriptionId}`
|
||||
: `${formDescriptionId} ${formMessageId}`
|
||||
}
|
||||
aria-invalid={!!error}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
});
|
||||
FormControl.displayName = "FormControl";
|
||||
|
||||
const FormDescription = React.forwardRef<
|
||||
HTMLParagraphElement,
|
||||
React.HTMLAttributes<HTMLParagraphElement>
|
||||
>(({ className, ...props }, ref) => {
|
||||
const { formDescriptionId } = useFormField();
|
||||
|
||||
return (
|
||||
<p
|
||||
ref={ref}
|
||||
id={formDescriptionId}
|
||||
className={cn("text-[0.8rem] text-muted-foreground", className)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
});
|
||||
FormDescription.displayName = "FormDescription";
|
||||
|
||||
const FormMessage = React.forwardRef<
|
||||
HTMLParagraphElement,
|
||||
React.HTMLAttributes<HTMLParagraphElement>
|
||||
>(({ className, children, ...props }, ref) => {
|
||||
const { error, formMessageId } = useFormField();
|
||||
const body = error ? String(error?.message ?? "") : children;
|
||||
|
||||
if (!body) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<p
|
||||
ref={ref}
|
||||
id={formMessageId}
|
||||
className={cn("text-[1rem] font-medium text-destructive", className)}
|
||||
{...props}
|
||||
>
|
||||
{body}
|
||||
</p>
|
||||
);
|
||||
});
|
||||
FormMessage.displayName = "FormMessage";
|
||||
|
||||
export {
|
||||
useFormField,
|
||||
Form,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormControl,
|
||||
FormDescription,
|
||||
FormMessage,
|
||||
FormField,
|
||||
};
|
||||
@@ -1,13 +0,0 @@
|
||||
import { useMenuStore } from "../lib/use-menu-store";
|
||||
|
||||
function MapLayout({ children }: { children: React.ReactNode }) {
|
||||
const currentMenuInfo = useMenuStore((state) => state.currentMenuInfo);
|
||||
|
||||
return (
|
||||
<div className="h-auto min-h-full bg-container pb-2">
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export { MapLayout };
|
||||
Reference in New Issue
Block a user