1- import { collection , addDoc , serverTimestamp } from 'firebase/firestore' ;
1+ import {
2+ doc ,
3+ getDoc ,
4+ collection ,
5+ addDoc ,
6+ serverTimestamp ,
7+ Timestamp ,
8+ } from 'firebase/firestore' ;
29import { db } from '@/lib/firebase' ;
310
11+ export type PostData = {
12+ title : string ;
13+ content : string ;
14+ createdAt : Timestamp | null ;
15+ updatedAt : Timestamp | null ;
16+ } ;
17+
18+ export type Post = PostData & { id : string } ;
19+
420export async function createPost ( uid : string , content : string , title ?: string ) {
521 const postsCol = collection ( db , 'users' , uid , 'posts' ) ;
622 const docRef = await addDoc ( postsCol , {
@@ -11,3 +27,42 @@ export async function createPost(uid: string, content: string, title?: string) {
1127 } ) ;
1228 return docRef . id ;
1329}
30+
31+ /** 런타임 최소 검증(안전하게 any 제거) */
32+ function isRecord ( v : unknown ) : v is Record < string , unknown > {
33+ return typeof v === 'object' && v !== null ;
34+ }
35+
36+ function parsePostData ( raw : unknown ) : PostData | null {
37+ if ( ! isRecord ( raw ) ) return null ;
38+
39+ const content = raw . content ;
40+ if ( typeof content !== 'string' ) return null ; // content는 필수
41+
42+ const title = typeof raw . title === 'string' ? raw . title : '' ;
43+
44+ const createdAt = raw . createdAt instanceof Timestamp ? raw . createdAt : null ;
45+
46+ const updatedAt = raw . updatedAt instanceof Timestamp ? raw . updatedAt : null ;
47+
48+ return { title, content, createdAt, updatedAt } ;
49+ }
50+
51+ export async function fetchMyPost (
52+ uid : string | null | undefined ,
53+ postId : string | null | undefined
54+ ) : Promise < Post | null > {
55+ // ✅ 여기서 uid/postId 실물 확인
56+ console . log ( '[fetchMyPost] path =' , { uid, postId } ) ;
57+
58+ if ( ! uid || ! postId ) return null ;
59+
60+ const ref = doc ( db , 'users' , uid , 'posts' , postId ) ;
61+ const snap = await getDoc ( ref ) ;
62+ if ( ! snap . exists ( ) ) return null ;
63+
64+ const parsed = parsePostData ( snap . data ( ) ) ;
65+ if ( ! parsed ) return null ;
66+
67+ return { id : snap . id , ...parsed } ;
68+ }
0 commit comments