nodejs-development
Payload CMS、Vue.js(TypeScript)、および TypeScript 全般のベストプラクティスを網羅した Node.js 開発ガイドラインです。バックエンドからフロントエンドまで一貫した開発規約を参照したい場面で活用できます。
description の原文を見る
Node.js development guidelines covering Payload CMS, Vue.js with TypeScript, and general TypeScript best practices
SKILL.md 本文
Node.js 開発ガイドライン
TypeScript を使用した Node.js 開発の専門家です。様々なフレームワークとパターンをカバーしています。
Payload CMS with Next.js
プロジェクトセットアップ
project/
├── src/
│ ├── app/ # Next.js app directory
│ ├── collections/ # Payload collections
│ ├── blocks/ # Custom blocks
│ ├── fields/ # Custom fields
│ └── access/ # Access control functions
├── payload.config.ts # Payload configuration
└── next.config.js # Next.js configuration
コレクション定義
import { CollectionConfig } from 'payload/types';
const Posts: CollectionConfig = {
slug: 'posts',
admin: {
useAsTitle: 'title',
},
access: {
read: () => true,
create: ({ req: { user } }) => Boolean(user),
update: ({ req: { user } }) => Boolean(user),
delete: ({ req: { user } }) => Boolean(user),
},
fields: [
{
name: 'title',
type: 'text',
required: true,
},
{
name: 'content',
type: 'richText',
},
{
name: 'publishedDate',
type: 'date',
},
],
};
export default Posts;
API ルート
// app/api/posts/route.ts
import { getPayloadClient } from '@/lib/payload';
export async function GET() {
const payload = await getPayloadClient();
const posts = await payload.find({
collection: 'posts',
limit: 10,
});
return Response.json(posts);
}
Vue.js with TypeScript
コンポーネント構造
<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
interface User {
id: number;
name: string;
email: string;
}
const props = defineProps<{
userId: number;
}>();
const emit = defineEmits<{
(e: 'update', user: User): void;
}>();
const user = ref<User | null>(null);
const isLoading = ref(false);
const displayName = computed(() => user.value?.name ?? 'Unknown');
async function fetchUser() {
isLoading.value = true;
try {
const response = await fetch(`/api/users/${props.userId}`);
user.value = await response.json();
} finally {
isLoading.value = false;
}
}
onMounted(fetchUser);
</script>
<template>
<div v-if="isLoading">Loading...</div>
<div v-else-if="user">
<h2>{{ displayName }}</h2>
<p>{{ user.email }}</p>
</div>
</template>
コンポーザブル
// composables/useApi.ts
import { ref } from 'vue';
export function useApi<T>(url: string) {
const data = ref<T | null>(null);
const error = ref<Error | null>(null);
const isLoading = ref(false);
async function execute() {
isLoading.value = true;
error.value = null;
try {
const response = await fetch(url);
if (!response.ok) throw new Error('Request failed');
data.value = await response.json();
} catch (e) {
error.value = e as Error;
} finally {
isLoading.value = false;
}
}
return { data, error, isLoading, execute };
}
Pinia ストア
// stores/user.ts
import { defineStore } from 'pinia';
interface UserState {
user: User | null;
isAuthenticated: boolean;
}
export const useUserStore = defineStore('user', {
state: (): UserState => ({
user: null,
isAuthenticated: false,
}),
getters: {
displayName: (state) => state.user?.name ?? 'Guest',
},
actions: {
async login(credentials: LoginCredentials) {
const user = await authService.login(credentials);
this.user = user;
this.isAuthenticated = true;
},
logout() {
this.user = null;
this.isAuthenticated = false;
},
},
});
TypeScript with Zod
スキーマ定義
import { z } from 'zod';
const UserSchema = z.object({
id: z.number(),
name: z.string().min(1),
email: z.string().email(),
role: z.enum(['user', 'admin']),
createdAt: z.date(),
});
type User = z.infer<typeof UserSchema>;
const CreateUserSchema = UserSchema.omit({ id: true, createdAt: true });
type CreateUserInput = z.infer<typeof CreateUserSchema>;
バリデーション
function createUser(input: unknown): User {
const validatedInput = CreateUserSchema.parse(input);
// Input is now typed as CreateUserInput
return {
...validatedInput,
id: generateId(),
createdAt: new Date(),
};
}
// Safe parsing (doesn't throw)
function validateUser(input: unknown) {
const result = UserSchema.safeParse(input);
if (!result.success) {
console.error(result.error.issues);
return null;
}
return result.data;
}
API バリデーションミドルウェア
import { z } from 'zod';
import { Request, Response, NextFunction } from 'express';
function validate<T extends z.ZodSchema>(schema: T) {
return (req: Request, res: Response, next: NextFunction) => {
const result = schema.safeParse(req.body);
if (!result.success) {
return res.status(400).json({ errors: result.error.issues });
}
req.body = result.data;
next();
};
}
// Usage
app.post('/users', validate(CreateUserSchema), createUserHandler);
一般的なベストプラクティス
エラーハンドリング
- カスタムエラークラスを使用
- グローバルエラーハンドラを実装
- コンテキスト情報とともにエラーをログ
- 適切なステータスコードを返却
非同期パターン
- async/await を一貫して使用
- Promise の拒否を処理
- 適切なタイムアウトを実装
- 並列処理には Promise.all を使用
テスト
- ユーティリティのユニットテストを記述
- API の統合テストを実装
- テストフィクスチャを使用
- 外部依存関係をモック
ライセンス: Apache-2.0(寛容ライセンスのため全文を引用しています) · 原本リポジトリ
詳細情報
- 作者
- mindrally
- リポジトリ
- mindrally/skills
- ライセンス
- Apache-2.0
- 最終更新
- 不明
Source: https://github.com/mindrally/skills / ライセンス: Apache-2.0
関連スキル
doubt-driven-development
重要な判断はすべて、本番環境への展開前に新しい視点から対抗的レビューを実施します。速度より正確性が重要な場合、不慣れなコードを扱う場合、本番環境・セキュリティに関わるロジック・取り消し不可の操作など影響度が高い場合、または後でバグを修正するよりも今検証する方が効率的な場合に活用してください。
apprun-skills
TypeScriptを使用したAppRunアプリケーションのMVU設計に関する総合的なガイダンスが得られます。コンポーネントパターン、イベントハンドリング、状態管理(非同期ジェネレータを含む)、パラメータと保護機能を備えたルーティング・ナビゲーション、vistestを使用したテストに対応しています。AppRunコンポーネントの設計・レビュー、ルートの配線、状態フローの管理、AppRunテストの作成時に活用してください。
desloppify
コードベースのヘルスチェックと技術負債の追跡ツールです。コード品質、技術負債、デッドコード、大規模ファイル、ゴッドクラス、重複関数、コードスメル、命名規則の問題、インポートサイクル、結合度の問題についてユーザーが質問した場合に使用してください。また、ヘルススコアの確認、次の改善項目の提案、クリーンアップ計画の作成をリクエストされた際にも対応します。29言語に対応しています。
debugging-and-error-recovery
テストが失敗したり、ビルドが壊れたり、動作が期待と異なったり、予期しないエラーが発生したりした場合に、体系的な根本原因デバッグをガイドします。推測ではなく、根本原因を見つけて修正するための体系的なアプローチが必要な場合に使用してください。
test-driven-development
テスト駆動開発により実装を進めます。ロジックの実装、バグの修正、動作の変更など、あらゆる場面で活用できます。コードが正常に動作することを証明する必要がある場合、バグ報告を受けた場合、既存機能を修正する予定がある場合に使用してください。
incremental-implementation
変更を段階的に実施します。複数のファイルに影響する機能や変更を実装する場合に使用してください。大量のコードを一度に書こうとしている場合や、タスクが一度では完結できないほど大きい場合に活用します。