swallowkit 0.4.0-beta.3 → 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/LICENSE +21 -21
  2. package/README.ja.md +254 -183
  3. package/README.md +311 -184
  4. package/dist/__tests__/fixtures.d.ts +14 -0
  5. package/dist/__tests__/fixtures.d.ts.map +1 -0
  6. package/dist/__tests__/fixtures.js +85 -0
  7. package/dist/__tests__/fixtures.js.map +1 -0
  8. package/dist/cli/commands/create-model.d.ts.map +1 -1
  9. package/dist/cli/commands/create-model.js +16 -15
  10. package/dist/cli/commands/create-model.js.map +1 -1
  11. package/dist/cli/commands/dev-seeds.d.ts +35 -0
  12. package/dist/cli/commands/dev-seeds.d.ts.map +1 -0
  13. package/dist/cli/commands/dev-seeds.js +292 -0
  14. package/dist/cli/commands/dev-seeds.js.map +1 -0
  15. package/dist/cli/commands/dev.d.ts +8 -0
  16. package/dist/cli/commands/dev.d.ts.map +1 -1
  17. package/dist/cli/commands/dev.js +308 -87
  18. package/dist/cli/commands/dev.js.map +1 -1
  19. package/dist/cli/commands/index.d.ts +1 -0
  20. package/dist/cli/commands/index.d.ts.map +1 -1
  21. package/dist/cli/commands/index.js +3 -1
  22. package/dist/cli/commands/index.js.map +1 -1
  23. package/dist/cli/commands/init.d.ts +13 -0
  24. package/dist/cli/commands/init.d.ts.map +1 -1
  25. package/dist/cli/commands/init.js +2639 -1708
  26. package/dist/cli/commands/init.js.map +1 -1
  27. package/dist/cli/commands/scaffold.d.ts +3 -0
  28. package/dist/cli/commands/scaffold.d.ts.map +1 -1
  29. package/dist/cli/commands/scaffold.js +283 -118
  30. package/dist/cli/commands/scaffold.js.map +1 -1
  31. package/dist/cli/index.js +17 -1
  32. package/dist/cli/index.js.map +1 -1
  33. package/dist/core/config.d.ts +2 -1
  34. package/dist/core/config.d.ts.map +1 -1
  35. package/dist/core/config.js +31 -1
  36. package/dist/core/config.js.map +1 -1
  37. package/dist/core/scaffold/functions-generator.d.ts +5 -0
  38. package/dist/core/scaffold/functions-generator.d.ts.map +1 -1
  39. package/dist/core/scaffold/functions-generator.js +649 -211
  40. package/dist/core/scaffold/functions-generator.js.map +1 -1
  41. package/dist/core/scaffold/model-parser.d.ts +1 -1
  42. package/dist/core/scaffold/model-parser.d.ts.map +1 -1
  43. package/dist/core/scaffold/model-parser.js +105 -101
  44. package/dist/core/scaffold/model-parser.js.map +1 -1
  45. package/dist/core/scaffold/nextjs-generator.js +181 -181
  46. package/dist/core/scaffold/openapi-generator.d.ts +3 -0
  47. package/dist/core/scaffold/openapi-generator.d.ts.map +1 -0
  48. package/dist/core/scaffold/openapi-generator.js +190 -0
  49. package/dist/core/scaffold/openapi-generator.js.map +1 -0
  50. package/dist/core/scaffold/ui-generator.js +656 -656
  51. package/dist/database/base-model.d.ts +3 -3
  52. package/dist/database/base-model.js +3 -3
  53. package/dist/index.d.ts +2 -2
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +2 -1
  56. package/dist/index.js.map +1 -1
  57. package/dist/types/index.d.ts +4 -0
  58. package/dist/types/index.d.ts.map +1 -1
  59. package/dist/utils/package-manager.d.ts +109 -0
  60. package/dist/utils/package-manager.d.ts.map +1 -0
  61. package/dist/utils/package-manager.js +215 -0
  62. package/dist/utils/package-manager.js.map +1 -0
  63. package/package.json +81 -73
  64. package/src/__tests__/__snapshots__/functions-generator.test.ts.snap +445 -0
  65. package/src/__tests__/__snapshots__/nextjs-generator.test.ts.snap +194 -0
  66. package/src/__tests__/__snapshots__/ui-generator.test.ts.snap +524 -0
  67. package/src/__tests__/config.test.ts +122 -0
  68. package/src/__tests__/dev-seeds.test.ts +112 -0
  69. package/src/__tests__/dev.test.ts +42 -0
  70. package/src/__tests__/fixtures.ts +83 -0
  71. package/src/__tests__/functions-generator.test.ts +101 -0
  72. package/src/__tests__/init.test.ts +80 -0
  73. package/src/__tests__/nextjs-generator.test.ts +97 -0
  74. package/src/__tests__/openapi-generator.test.ts +43 -0
  75. package/src/__tests__/package-manager.test.ts +189 -0
  76. package/src/__tests__/scaffold.test.ts +39 -0
  77. package/src/__tests__/string-utils.test.ts +75 -0
  78. package/src/__tests__/ui-generator.test.ts +144 -0
  79. package/src/cli/commands/create-model.ts +141 -0
  80. package/src/cli/commands/dev-seeds.ts +358 -0
  81. package/src/cli/commands/dev.ts +805 -0
  82. package/src/cli/commands/index.ts +9 -0
  83. package/src/cli/commands/init.ts +3370 -0
  84. package/src/cli/commands/provision.ts +193 -0
  85. package/src/cli/commands/scaffold.ts +786 -0
  86. package/src/cli/index.ts +74 -0
  87. package/src/core/config.ts +244 -0
  88. package/src/core/scaffold/functions-generator.ts +674 -0
  89. package/src/core/scaffold/model-parser.ts +627 -0
  90. package/src/core/scaffold/nextjs-generator.ts +217 -0
  91. package/src/core/scaffold/openapi-generator.ts +212 -0
  92. package/src/core/scaffold/ui-generator.ts +945 -0
  93. package/src/database/base-model.ts +184 -0
  94. package/src/database/client.ts +140 -0
  95. package/src/database/repository.ts +104 -0
  96. package/src/database/runtime-check.ts +25 -0
  97. package/src/index.ts +27 -0
  98. package/src/types/index.ts +45 -0
  99. package/src/utils/package-manager.ts +229 -0
  100. package/dist/cli/commands/build.d.ts +0 -6
  101. package/dist/cli/commands/build.d.ts.map +0 -1
  102. package/dist/cli/commands/build.js +0 -177
  103. package/dist/cli/commands/build.js.map +0 -1
  104. package/dist/cli/commands/deploy.d.ts +0 -3
  105. package/dist/cli/commands/deploy.d.ts.map +0 -1
  106. package/dist/cli/commands/deploy.js +0 -147
  107. package/dist/cli/commands/deploy.js.map +0 -1
  108. package/dist/cli/commands/setup.d.ts +0 -6
  109. package/dist/cli/commands/setup.d.ts.map +0 -1
  110. package/dist/cli/commands/setup.js +0 -254
  111. package/dist/cli/commands/setup.js.map +0 -1
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Takumasa Hirabayashi
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Takumasa Hirabayashi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.ja.md CHANGED
@@ -1,183 +1,254 @@
1
- # SwallowKit
2
-
3
- [![npm version](https://img.shields.io/npm/v/swallowkit.svg)](https://www.npmjs.com/package/swallowkit)
4
- [![npm downloads](https://img.shields.io/npm/dm/swallowkit.svg)](https://www.npmjs.com/package/swallowkit)
5
- [![license](https://img.shields.io/npm/l/swallowkit.svg)](./LICENSE)
6
-
7
- [English](./README.md) | 日本語
8
-
9
- **Azure 上の Next.js アプリケーション向けの型安全なスキーマ駆動開発ツールキット**
10
-
11
- SwallowKit は、Zod スキーマを通じてエンドツーエンドの型安全性を維持しながら、外部の Azure Functions バックエンドを持つフルスタック Next.js アプリケーションを Static Web Apps で構築するための統合キットです。
12
-
13
- Next.js の API ルートを BFF(Backend For Frontend)としての使用のみに制限し、ビジネスロジックは独立した Azure Functions にオフロードすることでクライアント・サーバー間の明確な分離を提供します。
14
-
15
- Zod スキーマから自動的に CRUD 操作を生成する Scaffold 機能を備え、一貫した型定義で Next.js (Azure Static Web Apps)、Azure Functions、Cosmos DB を組み合わせた構成で、CRUD コードの自動生成から本番デプロイ・CI/CD までをサポートします。
16
-
17
- > **注意**: このプロジェクトは活発に開発中です。将来のバージョンでAPIが変更される可能性があります。
18
-
19
- ## ✨ 主な特徴
20
-
21
- - **🔄 Zod スキーマ共有** - フロントエンド、BFF、Azure Functions、Cosmos DB で同じスキーマを使用
22
- - **⚡ CRUD コード生成** - `swallowkit scaffold` で Azure Functions + Next.js コードを自動生成
23
- - **🛡️ 完全な型安全性** - クライアントからデータベースまでエンドツーエンド TypeScript
24
- - **🎯 BFF パターン** - Next.js API Routes BFF レイヤーとして機能、自動検証・リソース名推論
25
- - **☁️ Azure 最適化** - Static Web Apps + Functions + Cosmos DB で最小コスト構成
26
- - **🚀 簡単デプロイ** - Bicep IaC + CI/CD ワークフローを自動生成
27
-
28
-
29
- ## 📚 ドキュメント
30
-
31
- - **[CLI リファレンス](./docs/cli-reference.ja.md)** - 全コマンドの詳細
32
- - **[Scaffold ガイド](./docs/scaffold-guide.ja.md)** - CRUD コード生成
33
- - **[Zod スキーマ共有ガイド](./docs/zod-schema-sharing-guide.ja.md)** - スキーマ設計
34
- - **[デプロイガイド](./docs/deployment-guide.ja.md)** - Azure へのデプロイ
35
-
36
- ## 🚀 クイックスタート
37
-
38
- ### 1. プロジェクト作成
39
-
40
- ```bash
41
- npx swallowkit init my-app
42
- cd my-app
43
- ```
44
-
45
- ### 2. モデルの作成
46
-
47
- ```bash
48
- npx swallowkit create-model todo
49
- ```
50
-
51
- これにより `lib/models/todo.ts` が生成されます。必要なフィールドを追加してカスタマイズ:
52
-
53
- ```typescript
54
- // lib/models/todo.ts
55
- import { z } from 'zod';
56
-
57
- export const todoSchema = z.object({
58
- id: z.string(),
59
- text: z.string().min(1).max(200),
60
- completed: z.boolean().default(false),
61
- createdAt: z.string().optional(),
62
- updatedAt: z.string().optional(),
63
- });
64
-
65
- export type Todo = z.infer<typeof todoSchema>;
66
- ```
67
-
68
- ### 3. CRUD コード生成
69
-
70
- ```bash
71
- npx swallowkit scaffold lib/models/todo.ts
72
- ```
73
-
74
- これで以下が自動生成されます:
75
- - Azure Functions (CRUD エンドポイント + Cosmos DB バインディング)
76
- - ✅ Next.js BFF API Routes (自動検証・リソース名推論)
77
- - React コンポーネント (型安全なフォーム)
78
-
79
- ### 4. 開発サーバー起動
80
-
81
- ```bash
82
- npx swallowkit dev
83
- ```
84
-
85
- - Next.js: http://localhost:3000
86
- - Azure Functions: http://localhost:7071
87
-
88
- ### 5. フロントエンドから使用
89
-
90
- ```typescript
91
- import { api } from '@/lib/api/backend';
92
- import type { Todo } from '@/lib/models/todo';
93
-
94
- // 全件取得 - BFFエンドポイントを呼び出し
95
- const todos = await api.get<Todo[]>('/api/todos');
96
-
97
- // 作成 - バックエンドで検証
98
- const created = await api.post<Todo>('/api/todos', {
99
- text: '牛乳を買う',
100
- completed: false
101
- });
102
-
103
- // 更新 - バックエンドで検証
104
- const updated = await api.put<Todo>('/api/todos/123', { completed: true });
105
-
106
- // 削除
107
- await api.delete('/api/todos/123');
108
- ```
109
-
110
- ## 🏗️ アーキテクチャ
111
-
112
- ```
113
- ┌─────────────────────────────────────────────────────────────┐
114
- │ Frontend (React) │
115
- │ - Client Components │
116
- │ - Server Components (SSR) │
117
- └──────────────────────────┬───────────────────────────────────┘
118
- │ api.post('/api/todos', data)
119
-
120
- ┌─────────────────────────────────────────────────────────────┐
121
- │ BFF Layer (Next.js API Routes) │
122
- │ - Auto Schema Validation (Zod) │
123
- │ - Error Handling │
124
- └──────────────────────────┬───────────────────────────────────┘
125
- │ HTTP Request
126
-
127
- ┌─────────────────────────────────────────────────────────────┐
128
- │ Azure Functions (Backend)
129
- │ - HTTP Triggers (CRUD) │
130
- │ - Zod Validation (Re-check) │
131
- │ - Business Logic │
132
- │ - Cosmos DB Bindings │
133
- └──────────────────────────┬───────────────────────────────────┘
134
-
135
-
136
- ┌─────────────────────────────────────────────────────────────┐
137
- │ Azure Cosmos DB │
138
- │ - NoSQL Database │
139
- │ - Zod Schema Validation │
140
- └─────────────────────────────────────────────────────────────┘
141
- ```
142
-
143
- **重要なパターン:**
144
- - **BFF (Backend For Frontend)**: Next.js API Routes が Azure Functions へのプロキシ
145
- - **共有スキーマ**: Zod スキーマをフロントエンド・BFF・Functions・DB で共有
146
- - **型安全性**: Zod から TypeScript 型を自動推論
147
- - **マネージド ID**: サービス間の安全な接続(接続文字列不要)
148
-
149
- ## 📦 前提条件
150
-
151
- - Node.js 22.x
152
- - Azure Cosmos DB Emulator (ローカル開発用)
153
- - [公式ドキュメント (vNext 推奨)](https://learn.microsoft.com/ja-jp/azure/cosmos-db/emulator-linux)
154
- - Windows: [ダウンロード](https://aka.ms/cosmosdb-emulator)
155
- - Docker: `docker run -p 8081:8081 mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator`
156
-
157
- ## 🚀 Azure へのデプロイ
158
-
159
- Next.js アプリを standalone モードで Azure Static Web Apps にデプロイし、バックエンド操作のために独立した Azure Functions に接続します。
160
-
161
- ```bash
162
- # 1. リソースをプロビジョニング (Bicep IaC)
163
- npx swallowkit provision --resource-group my-app-rg --location japaneast
164
-
165
- # 2. CI/CD シークレットを設定 (詳細はデプロイガイド参照)
166
-
167
- # 3. コードをプッシュして自動デプロイ
168
- git push origin main
169
- ```
170
-
171
- 詳細は **[デプロイガイド](./docs/deployment-guide.ja.md)** を参照してください。
172
-
173
- ## ライセンス
174
-
175
- MIT
176
-
177
- ## 🔗 関連リンク
178
-
179
- - [Azure Static Web Apps](https://learn.microsoft.com/ja-jp/azure/static-web-apps/)
180
- - [Azure Functions](https://learn.microsoft.com/ja-jp/azure/azure-functions/)
181
- - [Azure Cosmos DB](https://learn.microsoft.com/ja-jp/azure/cosmos-db/)
182
- - [Next.js](https://nextjs.org/)
183
- - [Zod](https://zod.dev/)
1
+ # SwallowKit
2
+
3
+ [![npm version](https://img.shields.io/npm/v/swallowkit.svg)](https://www.npmjs.com/package/swallowkit)
4
+ [![npm downloads](https://img.shields.io/npm/dm/swallowkit.svg)](https://www.npmjs.com/package/swallowkit)
5
+ [![VS Code Marketplace](https://img.shields.io/visual-studio-marketplace/v/himanago.swallowkit-vscode?label=VS%20Code%20Extension)](https://marketplace.visualstudio.com/items?itemName=himanago.swallowkit-vscode)
6
+ [![license](https://img.shields.io/npm/l/swallowkit.svg)](./LICENSE)
7
+
8
+ [English](./README.md) | 日本語
9
+
10
+ **Azure 上の Next.js アプリケーション向けの型安全なスキーマ駆動開発ツールキット**
11
+
12
+ SwallowKit は、Zod スキーマを通じてエンドツーエンドの型安全性を維持しながら、外部の Azure Functions バックエンドを持つフルスタック Next.js アプリケーションを Static Web Apps で構築するための統合キットです。
13
+
14
+ Next.js の API ルートを BFF(Backend For Frontend)としての使用のみに制限し、ビジネスロジックは独立した Azure Functions にオフロードすることでクライアント・サーバー間の明確な分離を提供します。
15
+
16
+ Zod スキーマから自動的に CRUD 操作を生成する Scaffold 機能を備え、一貫した型定義で Next.js (Azure Static Web Apps)、Azure Functions、Cosmos DB を組み合わせた構成で、CRUD コードの自動生成から本番デプロイ・CI/CD までをサポートします。
17
+
18
+ > **注意**: このプロジェクトは活発に開発中です。将来のバージョンでAPIが変更される可能性があります。
19
+
20
+ ## ✨ 主な特徴
21
+
22
+ - **🔄 Zod スキーマ共有** - フロントエンド、BFF、Azure Functions、Cosmos DB をまたいで Zod を唯一のソースとして維持
23
+ - **⚡ CRUD コード生成** - `swallowkit scaffold` で Azure Functions + Next.js コードを自動生成
24
+ - **🌐 Functions バックエンド多言語対応** - `init` 時に Azure Functions の言語として TypeScript、C#、Python を選択可能
25
+ - **🧬 OpenAPI スキーマブリッジ** - C#/Python バックエンドでは `scaffold` Zod から OpenAPI を出力し、各言語向けスキーマ資産を生成
26
+ - **🛡️ 契約安全性** - 共有 Zod または OpenAPI 由来のモデルにより、フロント/BFF とバックエンドの契約を整合
27
+ - **🎯 BFF パターン** - Next.js API Routes が BFF レイヤーとして機能、自動検証・リソース名推論
28
+ - **☁️ Azure 最適化** - Static Web Apps + Functions + Cosmos DB で最小コスト構成
29
+ - **🚀 簡単デプロイ** - Bicep IaC + CI/CD ワークフローを自動生成
30
+ - **🤖 AI フレンドリー** - 自動生成される指示ファイル(`AGENTS.md`、`CLAUDE.md`、`.github/copilot-instructions.md`)とレイヤー別ルールにより、GitHub Copilot・Claude Code・OpenAI Codex がプロジェクト規約に従ってコードを生成
31
+ - **🧩 [VS Code 拡張機能](https://marketplace.visualstudio.com/items?itemName=himanago.swallowkit-vscode)** - init/scaffold/dev の GUI ウィザード、モデルファイル右クリックでスキャフォールド、開発サーバーステータスバー、TypeScript スニペット
32
+
33
+
34
+ ## 📚 ドキュメント
35
+
36
+ **[SwallowKit ドキュメント](https://himanago.github.io/swallowkit/ja/)** で完全なドキュメントをご覧ください([English](https://himanago.github.io/swallowkit/en/) もあります)。
37
+
38
+ - **[CLI リファレンス](https://himanago.github.io/swallowkit/ja/cli-reference)** - 全コマンドの詳細
39
+ - **[Scaffold ガイド](https://himanago.github.io/swallowkit/ja/scaffold-guide)** - CRUD コード生成
40
+ - **[Zod スキーマ共有ガイド](https://himanago.github.io/swallowkit/ja/zod-schema-sharing-guide)** - スキーマ設計
41
+ - **[デプロイガイド](https://himanago.github.io/swallowkit/ja/deployment-guide)** - Azure へのデプロイ
42
+
43
+ ## 🚀 クイックスタート
44
+
45
+ ### 1. プロジェクト作成
46
+
47
+ ```bash
48
+ npx swallowkit init my-app
49
+ # or
50
+ pnpm dlx swallowkit init my-app
51
+ cd my-app
52
+ ```
53
+
54
+ 対話プロンプトで CI/CD プロバイダー、Azure Functions のバックエンド言語、Cosmos DB モード、ネットワーク設定を選択します。フラグで直接指定するとプロンプトをスキップできます:
55
+
56
+ ```bash
57
+ # 非対話モード(VS Code 拡張機能や自動化に便利)
58
+ npx swallowkit init my-app --cicd github --backend-language python --cosmos-db-mode serverless --vnet outbound
59
+ ```
60
+
61
+ | フラグ | 値 | 説明 |
62
+ |------|-----|------|
63
+ | `--cicd <provider>` | `github`, `azure`, `skip` | CI/CD プロバイダー |
64
+ | `--backend-language <language>` | `typescript`, `csharp`, `python` | Azure Functions のバックエンド言語 |
65
+ | `--cosmos-db-mode <mode>` | `freetier`, `serverless` | Cosmos DB 課金モード |
66
+ | `--vnet <option>` | `outbound`, `none` | ネットワークセキュリティ |
67
+
68
+ フラグを省略すると、その項目は対話プロンプトで質問されます。
69
+
70
+ ### 2. モデルの作成
71
+
72
+ 複数のモデルをまとめて作成できます:
73
+
74
+ ```bash
75
+ npx swallowkit create-model category todo
76
+ # or
77
+ pnpm dlx swallowkit create-model category todo
78
+ ```
79
+
80
+ これにより `shared/models/category.ts` と `shared/models/todo.ts` が生成されます。必要なフィールドを追加してカスタマイズ:
81
+
82
+ ```typescript
83
+ // shared/models/category.ts
84
+ import { z } from 'zod';
85
+
86
+ export const category = z.object({
87
+ id: z.string(),
88
+ name: z.string().min(1).max(50),
89
+ createdAt: z.string().optional(),
90
+ updatedAt: z.string().optional(),
91
+ });
92
+
93
+ export type Category = z.infer<typeof category>;
94
+ ```
95
+
96
+ 親子関係を表現するには、ID 参照ではなく**ネスト型**で記述します:
97
+
98
+ ```typescript
99
+ // shared/models/todo.ts
100
+ import { z } from 'zod';
101
+ import { category } from './category';
102
+
103
+ export const todo = z.object({
104
+ id: z.string(),
105
+ text: z.string().min(1).max(200),
106
+ completed: z.boolean().default(false),
107
+ category: category.optional(), // ネストオブジェクト(categoryId ではない)
108
+ createdAt: z.string().optional(),
109
+ updatedAt: z.string().optional(),
110
+ });
111
+
112
+ export type Todo = z.infer<typeof todo>;
113
+ ```
114
+
115
+ > **Tip**: ネスト型を使うことで型安全性が保たれ、関連データがドキュメント内にまとまって保存されます。これは Cosmos DB のドキュメントモデルに自然な形です。
116
+
117
+ ### 3. CRUD コード生成
118
+
119
+ ```bash
120
+ npx swallowkit scaffold shared/models/todo.ts
121
+ # or
122
+ pnpm dlx swallowkit scaffold shared/models/todo.ts
123
+ ```
124
+
125
+ これで以下が自動生成されます:
126
+ - ✅ Azure Functions (CRUD エンドポイント + Cosmos DB バインディング)
127
+ - ✅ Next.js BFF API Routes (自動検証・リソース名推論)
128
+ - React コンポーネント (型安全なフォーム)
129
+
130
+ `init` `csharp` または `python` を選んだ場合、`swallowkit scaffold` はあわせて `functions/openapi/` に OpenAPI ドキュメントを出力し、`functions/generated/` に各言語向けスキーマ資産を生成します。
131
+
132
+ ### 4. 開発サーバー起動
133
+
134
+ ```bash
135
+ npx swallowkit dev
136
+ # or
137
+ pnpm dlx swallowkit dev
138
+ ```
139
+
140
+ - Next.js: http://localhost:3000
141
+ - Azure Functions: http://localhost:7071
142
+
143
+ ### 5. フロントエンドから使用
144
+
145
+ ```typescript
146
+ import { api } from '@/lib/api/backend';
147
+ import type { Todo } from '@/shared/models/todo';
148
+
149
+ // 全件取得 - BFFエンドポイントを呼び出し
150
+ const todos = await api.get<Todo[]>('/api/todos');
151
+
152
+ // 作成 - バックエンドで検証
153
+ const created = await api.post<Todo>('/api/todos', {
154
+ text: '牛乳を買う',
155
+ completed: false
156
+ });
157
+
158
+ // 更新 - バックエンドで検証
159
+ const updated = await api.put<Todo>('/api/todos/123', { completed: true });
160
+
161
+ // 削除
162
+ await api.delete('/api/todos/123');
163
+ ```
164
+
165
+ ## 🏗️ アーキテクチャ
166
+
167
+ ```
168
+ ┌─────────────────────────────────────────────────────────────┐
169
+ │ Frontend (React) │
170
+ │ - Client Components │
171
+ │ - Server Components (SSR)
172
+ └──────────────────────────┬───────────────────────────────────┘
173
+ │ api.post('/api/todos', data)
174
+
175
+ ┌─────────────────────────────────────────────────────────────┐
176
+ │ BFF Layer (Next.js API Routes) │
177
+ │ - Auto Schema Validation (Zod) │
178
+ │ - Error Handling │
179
+ └──────────────────────────┬───────────────────────────────────┘
180
+ HTTP Request
181
+
182
+ ┌─────────────────────────────────────────────────────────────┐
183
+ │ Azure Functions (Backend)
184
+ │ - HTTP Triggers (CRUD) │
185
+ │ - Zod Validation (Re-check) │
186
+ │ - Business Logic │
187
+ │ - Cosmos DB Bindings │
188
+ └──────────────────────────┬───────────────────────────────────┘
189
+
190
+
191
+ ┌─────────────────────────────────────────────────────────────┐
192
+ │ Azure Cosmos DB │
193
+ │ - NoSQL Database │
194
+ │ - Zod Schema Validation │
195
+ └─────────────────────────────────────────────────────────────┘
196
+ ```
197
+
198
+ **重要なパターン:**
199
+ - **BFF (Backend For Frontend)**: Next.js API Routes が Azure Functions へのプロキシ
200
+ - **共有スキーマ**: `shared/models/` の Zod スキーマを唯一のソースとして扱う
201
+ - **C#/Python 向け OpenAPI ブリッジ**: TypeScript 以外の Functions は `functions/generated/` の生成資産を利用
202
+ - **契約安全性**: 共有 Zod または生成モデルで BFF とバックエンドの整合を保つ
203
+ - **マネージド ID**: サービス間の安全な接続(接続文字列不要)
204
+
205
+ ## 📦 前提条件
206
+
207
+ - Node.js 22.x
208
+ - **pnpm**(推奨): `corepack enable` を実行するか、`npm install -g pnpm` でインストール
209
+ - npm でも動作します — SwallowKit はパッケージマネージャーを自動検出します(pnpm がインストール済みなら常に pnpm を優先)
210
+ - Azure Cosmos DB Emulator (ローカル開発用)
211
+ - [公式ドキュメント (vNext 推奨)](https://learn.microsoft.com/ja-jp/azure/cosmos-db/emulator-linux)
212
+ - Windows: [ダウンロード](https://aka.ms/cosmosdb-emulator)
213
+ - Docker: `docker run -p 8081:8081 mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator`
214
+
215
+ ## 🚀 Azure へのデプロイ
216
+
217
+ Next.js アプリを standalone モードで Azure Static Web Apps にデプロイし、バックエンド操作のために独立した Azure Functions に接続します。
218
+
219
+ **1. リソースをプロビジョニング (Bicep IaC)**
220
+
221
+ ```bash
222
+ npx swallowkit provision --resource-group my-app-rg --location japaneast
223
+ # or
224
+ pnpm dlx swallowkit provision --resource-group my-app-rg --location japaneast
225
+ ```
226
+
227
+ プロビジョニング完了後、CI/CD に必要なシークレット値がターミナルに表示されます。コピーしておいてください。
228
+
229
+ **2. コードをプッシュ**
230
+
231
+ ```bash
232
+ git push origin main
233
+ ```
234
+
235
+ **3. 自動実行された CI/CD をキャンセル** — シークレット未登録のため失敗します。
236
+
237
+ **4. 表示された値をシークレットに登録** — GitHub (Settings → Secrets) または Azure DevOps (Pipelines → Library) に登録します。
238
+
239
+ **5. CI/CD ワークフローを手動で再実行します。**
240
+
241
+ 詳細は **[デプロイガイド](https://himanago.github.io/swallowkit/ja/deployment-guide)** を参照してください。
242
+
243
+ ## ライセンス
244
+
245
+ MIT
246
+
247
+ ## 🔗 関連リンク
248
+
249
+ - [SwallowKit VS Code 拡張機能](https://marketplace.visualstudio.com/items?itemName=himanago.swallowkit-vscode)
250
+ - [Azure Static Web Apps](https://learn.microsoft.com/ja-jp/azure/static-web-apps/)
251
+ - [Azure Functions](https://learn.microsoft.com/ja-jp/azure/azure-functions/)
252
+ - [Azure Cosmos DB](https://learn.microsoft.com/ja-jp/azure/cosmos-db/)
253
+ - [Next.js](https://nextjs.org/)
254
+ - [Zod](https://zod.dev/)