mcpField
F
firebase-functions

firebase-functions

github.com/bumgeunsong开发945 次安装1 周前更新
代码仓库

关于 firebase-functions

firebase-functions 是面向 Firebase Functions 项目的开发模式技能,涵盖 TypeScript 函数组织、错误处理和构建测试流程。它适合维护 Firestore 触发函数及相关后端代码的开发者。

主要功能

  1. 01展示函数目录组织方式
  2. 02提供 Firestore 触发函数示例
  3. 03说明错误处理模式
  4. 04列出构建与 Jest 测试命令

适用场景

  1. 01规划 Firebase Functions 项目结构
  2. 02编写 Firestore 文档创建触发函数
  3. 03处理函数中的异步操作错误
  4. 04构建并运行函数测试

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/bumgeunsong/daily-writing-friends --skill firebase-functions

安装后在 Claude Code 中输入/firebase-functions确认已加载。

详细介绍

Firebase Functions 模式

目录结构

functions/
├── src/
│   ├── index.ts              # Function exports
│   ├── admin.ts              # Firebase Admin SDK init
│   ├── backfill/             # Data migration scripts
│   ├── commentSuggestion/    # AI comment features
│   ├── commentings/          # Comment activity tracking
│   ├── notifications/        # Push notification functions
│   ├── postings/             # Post activity tracking
│   ├── replyings/            # Reply activity tracking
│   └── shared/               # Shared utilities

函数结构

import { onDocumentCreated } from 'firebase-functions/v2/firestore';
import admin from '../admin';
import { Post } from '../types/Post';

export const createPosting = onDocumentCreated(
  'boards/{boardId}/posts/{postId}',
  async (event) => {
    const postData = event.data?.data() as Post;
    const { boardId, postId } = event.params;

    if (!postData) {
      console.error('No post data found.');
      return null;
    }

    try {
      await admin.firestore()
        .collection('users')
        .doc(postData.authorId)
        .collection('postings')
        .add(postingData);
      console.log(`Created posting for user ${postData.authorId}`);
    } catch (error) {
      console.error('Error writing posting:', error);
    }

    return null;
  }
);

错误处理

不要抛出错误,而应让函数正常完成:

try {
  await admin.firestore().collection('...').add(data);
  console.log(`Successfully created ${resourceType}`);
} catch (error) {
  console.error(`Error creating ${resourceType}:`, error);
  // Don't throw - function should complete
}

return null;

构建与测试

cd functions && npm install   # Install deps
cd functions && npm run build # Compile TypeScript
cd functions && npm test      # Run Jest tests

评论

支持 Markdown