← スキル一覧に戻る

api-layer
by BumgeunSong
Writing App
⭐ 6🍴 0📅 2026年1月17日
SKILL.md
name: api-layer description: Use when creating or modifying API functions in */api/ directories. Enforces Firestore patterns and data fetching conventions.
API Layer Patterns
File Location
All API functions go in [feature]/api/:
src/
├── post/
│ └── api/
│ ├── post.ts # CRUD operations
│ └── postQueries.ts # React Query hooks
Function Structure
import { collection, addDoc, query, where, getDocs } from 'firebase/firestore';
import { db } from '@/firebase';
import type { Post } from '../model/Post';
export async function createPost(
boardId: string,
postData: Omit<Post, 'id' | 'createdAt'>,
): Promise<Post> {
const postsRef = collection(db, 'boards', boardId, 'posts');
const docRef = await addDoc(postsRef, {
...postData,
createdAt: new Date(),
});
return { ...postData, id: docRef.id, createdAt: new Date() };
}
Firestore Best Practices
Batch Writes for Related Operations
import { writeBatch, doc } from 'firebase/firestore';
const batch = writeBatch(db);
batch.set(doc(db, 'posts', postId), postData);
batch.update(doc(db, 'users', userId), { postCount: increment(1) });
await batch.commit();
Collection Paths
users/{userId}
users/{userId}/postings/{postingId}
users/{userId}/commentings/{commentingId}
boards/{boardId}/posts/{postId}
boards/{boardId}/posts/{postId}/comments/{commentId}
boards/{boardId}/posts/{postId}/comments/{commentId}/replies/{replyId}
Optimistic Updates with React Query
const mutation = useMutation({
mutationFn: createPost,
onMutate: async (newPost) => {
await queryClient.cancelQueries({ queryKey: ['posts'] });
const previous = queryClient.getQueryData(['posts']);
queryClient.setQueryData(['posts'], (old) => [...old, newPost]);
return { previous };
},
onError: (err, newPost, context) => {
queryClient.setQueryData(['posts'], context.previous);
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ['posts'] });
},
});
Quick Reference
| Pattern | When |
|---|---|
addDoc | Create with auto-ID |
setDoc | Create/overwrite with known ID |
updateDoc | Partial update |
writeBatch | Multiple related writes |
| Listeners | Real-time features |
スコア
総合スコア
50/100
リポジトリの品質指標に基づく評価
✓SKILL.md
SKILL.mdファイルが含まれている
+20
○LICENSE
ライセンスが設定されている
0/10
○説明文
100文字以上の説明がある
0/10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
レビュー
💬
レビュー機能は近日公開予定です