swallowkit 1.0.0-beta.6 → 1.0.0-beta.60
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.
- package/README.ja.md +86 -177
- package/README.md +86 -178
- package/dist/__tests__/fixtures.d.ts +12 -0
- package/dist/__tests__/fixtures.d.ts.map +1 -1
- package/dist/__tests__/fixtures.js +101 -0
- package/dist/__tests__/fixtures.js.map +1 -1
- package/dist/cli/commands/add-auth.d.ts +15 -0
- package/dist/cli/commands/add-auth.d.ts.map +1 -0
- package/dist/cli/commands/add-auth.js +790 -0
- package/dist/cli/commands/add-auth.js.map +1 -0
- package/dist/cli/commands/add-connector.d.ts +20 -0
- package/dist/cli/commands/add-connector.d.ts.map +1 -0
- package/dist/cli/commands/add-connector.js +163 -0
- package/dist/cli/commands/add-connector.js.map +1 -0
- package/dist/cli/commands/cosmos-worktree-isolation.d.ts +25 -0
- package/dist/cli/commands/cosmos-worktree-isolation.d.ts.map +1 -0
- package/dist/cli/commands/cosmos-worktree-isolation.js +124 -0
- package/dist/cli/commands/cosmos-worktree-isolation.js.map +1 -0
- package/dist/cli/commands/create-model.d.ts +1 -4
- package/dist/cli/commands/create-model.d.ts.map +1 -1
- package/dist/cli/commands/create-model.js +19 -81
- package/dist/cli/commands/create-model.js.map +1 -1
- package/dist/cli/commands/dev-seeds.d.ts +62 -0
- package/dist/cli/commands/dev-seeds.d.ts.map +1 -0
- package/dist/cli/commands/dev-seeds.js +474 -0
- package/dist/cli/commands/dev-seeds.js.map +1 -0
- package/dist/cli/commands/dev.d.ts +38 -0
- package/dist/cli/commands/dev.d.ts.map +1 -1
- package/dist/cli/commands/dev.js +733 -155
- package/dist/cli/commands/dev.js.map +1 -1
- package/dist/cli/commands/index.d.ts +1 -0
- package/dist/cli/commands/index.d.ts.map +1 -1
- package/dist/cli/commands/index.js +3 -1
- package/dist/cli/commands/index.js.map +1 -1
- package/dist/cli/commands/init.d.ts +78 -0
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/init.js +1453 -898
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/cli/commands/scaffold.d.ts +24 -0
- package/dist/cli/commands/scaffold.d.ts.map +1 -1
- package/dist/cli/commands/scaffold.js +473 -90
- package/dist/cli/commands/scaffold.js.map +1 -1
- package/dist/cli/index.d.ts +5 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +333 -55
- package/dist/cli/index.js.map +1 -1
- package/dist/core/config.d.ts +14 -3
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +179 -8
- package/dist/core/config.js.map +1 -1
- package/dist/core/mock/connector-mock-server.d.ts +101 -0
- package/dist/core/mock/connector-mock-server.d.ts.map +1 -0
- package/dist/core/mock/connector-mock-server.js +480 -0
- package/dist/core/mock/connector-mock-server.js.map +1 -0
- package/dist/core/mock/zod-mock-generator.d.ts +14 -0
- package/dist/core/mock/zod-mock-generator.d.ts.map +1 -0
- package/dist/core/mock/zod-mock-generator.js +163 -0
- package/dist/core/mock/zod-mock-generator.js.map +1 -0
- package/dist/core/operations/auth-operations.d.ts +61 -0
- package/dist/core/operations/auth-operations.d.ts.map +1 -0
- package/dist/core/operations/auth-operations.js +272 -0
- package/dist/core/operations/auth-operations.js.map +1 -0
- package/dist/core/operations/create-model.d.ts +15 -0
- package/dist/core/operations/create-model.d.ts.map +1 -0
- package/dist/core/operations/create-model.js +185 -0
- package/dist/core/operations/create-model.js.map +1 -0
- package/dist/core/operations/file-session.d.ts +56 -0
- package/dist/core/operations/file-session.d.ts.map +1 -0
- package/dist/core/operations/file-session.js +203 -0
- package/dist/core/operations/file-session.js.map +1 -0
- package/dist/core/operations/provision-operations.d.ts +52 -0
- package/dist/core/operations/provision-operations.d.ts.map +1 -0
- package/dist/core/operations/provision-operations.js +211 -0
- package/dist/core/operations/provision-operations.js.map +1 -0
- package/dist/core/operations/runtime.d.ts +37 -0
- package/dist/core/operations/runtime.d.ts.map +1 -0
- package/dist/core/operations/runtime.js +248 -0
- package/dist/core/operations/runtime.js.map +1 -0
- package/dist/core/operations/scaffold-apply.d.ts +21 -0
- package/dist/core/operations/scaffold-apply.d.ts.map +1 -0
- package/dist/core/operations/scaffold-apply.js +57 -0
- package/dist/core/operations/scaffold-apply.js.map +1 -0
- package/dist/core/operations/scaffold-machine.d.ts +16 -0
- package/dist/core/operations/scaffold-machine.d.ts.map +1 -0
- package/dist/core/operations/scaffold-machine.js +51 -0
- package/dist/core/operations/scaffold-machine.js.map +1 -0
- package/dist/core/operations/scaffold-plan.d.ts +43 -0
- package/dist/core/operations/scaffold-plan.d.ts.map +1 -0
- package/dist/core/operations/scaffold-plan.js +187 -0
- package/dist/core/operations/scaffold-plan.js.map +1 -0
- package/dist/core/project/agent-instructions.d.ts +29 -0
- package/dist/core/project/agent-instructions.d.ts.map +1 -0
- package/dist/core/project/agent-instructions.js +159 -0
- package/dist/core/project/agent-instructions.js.map +1 -0
- package/dist/core/project/artifacts.d.ts +58 -0
- package/dist/core/project/artifacts.d.ts.map +1 -0
- package/dist/core/project/artifacts.js +179 -0
- package/dist/core/project/artifacts.js.map +1 -0
- package/dist/core/project/boundaries.d.ts +50 -0
- package/dist/core/project/boundaries.d.ts.map +1 -0
- package/dist/core/project/boundaries.js +117 -0
- package/dist/core/project/boundaries.js.map +1 -0
- package/dist/core/project/capabilities.d.ts +58 -0
- package/dist/core/project/capabilities.d.ts.map +1 -0
- package/dist/core/project/capabilities.js +135 -0
- package/dist/core/project/capabilities.js.map +1 -0
- package/dist/core/project/drift.d.ts +29 -0
- package/dist/core/project/drift.d.ts.map +1 -0
- package/dist/core/project/drift.js +188 -0
- package/dist/core/project/drift.js.map +1 -0
- package/dist/core/project/infra.d.ts +49 -0
- package/dist/core/project/infra.d.ts.map +1 -0
- package/dist/core/project/infra.js +167 -0
- package/dist/core/project/infra.js.map +1 -0
- package/dist/core/project/manifest.d.ts +92 -0
- package/dist/core/project/manifest.d.ts.map +1 -0
- package/dist/core/project/manifest.js +321 -0
- package/dist/core/project/manifest.js.map +1 -0
- package/dist/core/project/state.d.ts +15 -0
- package/dist/core/project/state.d.ts.map +1 -0
- package/dist/core/project/state.js +106 -0
- package/dist/core/project/state.js.map +1 -0
- package/dist/core/project/validation.d.ts +20 -0
- package/dist/core/project/validation.d.ts.map +1 -0
- package/dist/core/project/validation.js +274 -0
- package/dist/core/project/validation.js.map +1 -0
- package/dist/core/project/workflows.d.ts +43 -0
- package/dist/core/project/workflows.d.ts.map +1 -0
- package/dist/core/project/workflows.js +299 -0
- package/dist/core/project/workflows.js.map +1 -0
- package/dist/core/scaffold/auth-generator.d.ts +63 -0
- package/dist/core/scaffold/auth-generator.d.ts.map +1 -0
- package/dist/core/scaffold/auth-generator.js +1774 -0
- package/dist/core/scaffold/auth-generator.js.map +1 -0
- package/dist/core/scaffold/connector-functions-generator.d.ts +41 -0
- package/dist/core/scaffold/connector-functions-generator.d.ts.map +1 -0
- package/dist/core/scaffold/connector-functions-generator.js +1029 -0
- package/dist/core/scaffold/connector-functions-generator.js.map +1 -0
- package/dist/core/scaffold/functions-generator.d.ts +7 -1
- package/dist/core/scaffold/functions-generator.d.ts.map +1 -1
- package/dist/core/scaffold/functions-generator.js +771 -69
- package/dist/core/scaffold/functions-generator.js.map +1 -1
- package/dist/core/scaffold/model-parser.d.ts +21 -1
- package/dist/core/scaffold/model-parser.d.ts.map +1 -1
- package/dist/core/scaffold/model-parser.js +243 -43
- package/dist/core/scaffold/model-parser.js.map +1 -1
- package/dist/core/scaffold/native-schema-generator.d.ts +19 -0
- package/dist/core/scaffold/native-schema-generator.d.ts.map +1 -0
- package/dist/core/scaffold/native-schema-generator.js +753 -0
- package/dist/core/scaffold/native-schema-generator.js.map +1 -0
- package/dist/core/scaffold/nextjs-generator.d.ts +8 -0
- package/dist/core/scaffold/nextjs-generator.d.ts.map +1 -1
- package/dist/core/scaffold/nextjs-generator.js +153 -9
- package/dist/core/scaffold/nextjs-generator.js.map +1 -1
- package/dist/core/scaffold/openapi-generator.d.ts +3 -0
- package/dist/core/scaffold/openapi-generator.d.ts.map +1 -0
- package/dist/core/scaffold/openapi-generator.js +190 -0
- package/dist/core/scaffold/openapi-generator.js.map +1 -0
- package/dist/core/scaffold/ui-generator.d.ts +17 -4
- package/dist/core/scaffold/ui-generator.d.ts.map +1 -1
- package/dist/core/scaffold/ui-generator.js +174 -34
- package/dist/core/scaffold/ui-generator.js.map +1 -1
- package/dist/core/verify/index.d.ts +69 -0
- package/dist/core/verify/index.d.ts.map +1 -0
- package/dist/core/verify/index.js +412 -0
- package/dist/core/verify/index.js.map +1 -0
- package/dist/database/base-model.d.ts +3 -3
- package/dist/database/base-model.js +3 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/machine/contracts.d.ts +24 -0
- package/dist/machine/contracts.d.ts.map +1 -0
- package/dist/machine/contracts.js +3 -0
- package/dist/machine/contracts.js.map +1 -0
- package/dist/machine/errors.d.ts +14 -0
- package/dist/machine/errors.d.ts.map +1 -0
- package/dist/machine/errors.js +42 -0
- package/dist/machine/errors.js.map +1 -0
- package/dist/machine/index.d.ts +3 -0
- package/dist/machine/index.d.ts.map +1 -0
- package/dist/machine/index.js +566 -0
- package/dist/machine/index.js.map +1 -0
- package/dist/mcp/index.d.ts +25 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +425 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/types/index.d.ts +109 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils/package-manager.d.ts +5 -2
- package/dist/utils/package-manager.d.ts.map +1 -1
- package/dist/utils/package-manager.js +30 -9
- package/dist/utils/package-manager.js.map +1 -1
- package/dist/utils/python-uv.d.ts +21 -0
- package/dist/utils/python-uv.d.ts.map +1 -0
- package/dist/utils/python-uv.js +111 -0
- package/dist/utils/python-uv.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +49 -0
- package/dist/version.js.map +1 -0
- package/package.json +9 -5
- package/src/__tests__/__snapshots__/functions-generator.test.ts.snap +1139 -0
- package/src/__tests__/__snapshots__/nextjs-generator.test.ts.snap +206 -0
- package/src/__tests__/__snapshots__/ui-generator.test.ts.snap +532 -0
- package/src/__tests__/agent-instructions.test.ts +237 -0
- package/src/__tests__/agent-loop-phase34.test.ts +513 -0
- package/src/__tests__/auth.test.ts +770 -0
- package/src/__tests__/bff-swa-auth.test.ts +275 -0
- package/src/__tests__/config.test.ts +295 -0
- package/src/__tests__/connector-functions-generator.test.ts +288 -0
- package/src/__tests__/connector-mock-server.test.ts +439 -0
- package/src/__tests__/connector-model-bff.test.ts +162 -0
- package/src/__tests__/cosmos-worktree-isolation.test.ts +149 -0
- package/src/__tests__/dev-seeds.test.ts +236 -0
- package/src/__tests__/dev.test.ts +296 -0
- package/src/__tests__/file-session.test.ts +98 -0
- package/src/__tests__/fixtures.ts +184 -0
- package/src/__tests__/functions-generator.test.ts +237 -0
- package/src/__tests__/init.test.ts +411 -0
- package/src/__tests__/jest-globals.d.ts +8 -0
- package/src/__tests__/machine.test.ts +371 -0
- package/src/__tests__/mcp.test.ts +253 -0
- package/src/__tests__/model-parser.test.ts +81 -0
- package/src/__tests__/multi-auth.test.ts +160 -0
- package/src/__tests__/nextjs-generator.test.ts +105 -0
- package/src/__tests__/openapi-generator.test.ts +43 -0
- package/src/__tests__/package-manager.test.ts +227 -0
- package/src/__tests__/plan-apply.test.ts +499 -0
- package/src/__tests__/python-uv.test.ts +48 -0
- package/src/__tests__/scaffold.test.ts +302 -0
- package/src/__tests__/string-utils.test.ts +75 -0
- package/src/__tests__/tsconfig.json +15 -0
- package/src/__tests__/ui-generator.test.ts +248 -0
- package/src/__tests__/zod-mock-generator.test.ts +132 -0
- package/src/cli/commands/add-auth.ts +853 -0
- package/src/cli/commands/add-connector.ts +158 -0
- package/src/cli/commands/cosmos-worktree-isolation.ts +130 -0
- package/src/cli/commands/create-model.ts +62 -0
- package/src/cli/commands/dev-seeds.ts +629 -0
- package/src/cli/commands/dev.ts +1252 -0
- package/src/cli/commands/index.ts +9 -0
- package/src/cli/commands/init.ts +3541 -0
- package/src/cli/commands/provision.ts +193 -0
- package/src/cli/commands/scaffold.ts +1081 -0
- package/src/cli/index.ts +350 -0
- package/src/core/config.ts +380 -0
- package/src/core/mock/connector-mock-server.ts +555 -0
- package/src/core/mock/zod-mock-generator.ts +205 -0
- package/src/core/operations/auth-operations.ts +360 -0
- package/src/core/operations/create-model.ts +191 -0
- package/src/core/operations/file-session.ts +222 -0
- package/src/core/operations/provision-operations.ts +297 -0
- package/src/core/operations/runtime.ts +258 -0
- package/src/core/operations/scaffold-apply.ts +106 -0
- package/src/core/operations/scaffold-machine.ts +77 -0
- package/src/core/operations/scaffold-plan.ts +215 -0
- package/src/core/project/agent-instructions.ts +167 -0
- package/src/core/project/agent-templates/AGENTS.md +502 -0
- package/src/core/project/agent-templates/CLAUDE.md +31 -0
- package/src/core/project/agent-templates/azure-functions.instructions.md +22 -0
- package/src/core/project/agent-templates/bff-routes.instructions.md +18 -0
- package/src/core/project/agent-templates/copilot-instructions.md +27 -0
- package/src/core/project/agent-templates/shared-models.instructions.md +72 -0
- package/src/core/project/artifacts.ts +205 -0
- package/src/core/project/boundaries.ts +170 -0
- package/src/core/project/capabilities.ts +193 -0
- package/src/core/project/drift.ts +202 -0
- package/src/core/project/infra.ts +196 -0
- package/src/core/project/manifest.ts +402 -0
- package/src/core/project/state.ts +77 -0
- package/src/core/project/validation.ts +295 -0
- package/src/core/project/workflows.ts +295 -0
- package/src/core/scaffold/auth-generator.ts +1824 -0
- package/src/core/scaffold/connector-functions-generator.ts +1133 -0
- package/src/core/scaffold/functions-generator.ts +972 -0
- package/src/core/scaffold/model-parser.ts +854 -0
- package/src/core/scaffold/native-schema-generator.ts +904 -0
- package/src/core/scaffold/nextjs-generator.ts +382 -0
- package/src/core/scaffold/openapi-generator.ts +212 -0
- package/src/core/scaffold/ui-generator.ts +1100 -0
- package/src/core/verify/index.ts +468 -0
- package/src/database/base-model.ts +184 -0
- package/src/database/client.ts +140 -0
- package/src/database/repository.ts +104 -0
- package/src/database/runtime-check.ts +25 -0
- package/src/index.ts +27 -0
- package/src/machine/contracts.ts +33 -0
- package/src/machine/errors.ts +45 -0
- package/src/machine/index.ts +690 -0
- package/src/mcp/index.ts +423 -0
- package/src/types/index.ts +182 -0
- package/src/utils/package-manager.ts +246 -0
- package/src/utils/python-uv.ts +96 -0
- package/src/version.ts +13 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js BFF API Routes コード生成
|
|
3
|
+
* Azure Functions を呼び出す BFF パターンの API routes を生成
|
|
4
|
+
* callFunction ヘルパー方式
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { ModelInfo, toCamelCase } from "./model-parser";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* BFF callFunction ヘルパー (lib/api/call-function.ts) のコードを生成
|
|
11
|
+
*/
|
|
12
|
+
export function generateBFFCallFunction(): string {
|
|
13
|
+
return `import { NextResponse } from 'next/server';
|
|
14
|
+
import { z } from 'zod/v4';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* SwallowKit BFF Call Function Helper
|
|
18
|
+
* Azure Functions を呼び出す汎用ヘルパー
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // シンプルな GET
|
|
22
|
+
* return callFunction({ method: 'GET', path: '/api/todo', responseSchema: z.array(TodoSchema) });
|
|
23
|
+
*
|
|
24
|
+
* // バリデーション付き POST
|
|
25
|
+
* return callFunction({ method: 'POST', path: '/api/todo', body, inputSchema: InputSchema, responseSchema: TodoSchema, successStatus: 201 });
|
|
26
|
+
*
|
|
27
|
+
* // カスタムビジネスロジック関数の呼び出し
|
|
28
|
+
* return callFunction({ method: 'POST', path: '/api/todo/archive', body: { ids } });
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
function getFunctionsBaseUrl(): string {
|
|
32
|
+
return process.env.BACKEND_FUNCTIONS_BASE_URL || process.env.FUNCTIONS_BASE_URL || 'http://localhost:7071';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isLocalFunctionsUrl(url: string): boolean {
|
|
36
|
+
return url.startsWith('http://localhost:') || url.startsWith('http://127.0.0.1:');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function getFunctionsKeyHeaders(functionsBaseUrl: string): Record<string, string> {
|
|
40
|
+
const key = process.env.BACKEND_FUNCTIONS_KEY;
|
|
41
|
+
if (key) return { 'x-functions-key': key };
|
|
42
|
+
if (isLocalFunctionsUrl(functionsBaseUrl)) return {};
|
|
43
|
+
throw new Error('BACKEND_FUNCTIONS_KEY is required for non-local Functions calls');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface CallFunctionConfig<TInput = unknown, TOutput = unknown> {
|
|
47
|
+
/** HTTP メソッド */
|
|
48
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
49
|
+
/** Azure Functions のパス (例: '/api/todo', '/api/todo/123') */
|
|
50
|
+
path: string;
|
|
51
|
+
/** リクエストボディ (POST/PUT 用) */
|
|
52
|
+
body?: unknown;
|
|
53
|
+
/** 入力バリデーション用 Zod スキーマ (省略時はバリデーションなし) */
|
|
54
|
+
inputSchema?: z.ZodSchema<TInput>;
|
|
55
|
+
/** 出力バリデーション用 Zod スキーマ (省略時はそのまま返す) */
|
|
56
|
+
responseSchema?: z.ZodSchema<TOutput>;
|
|
57
|
+
/** 成功時の HTTP ステータスコード (デフォルト: 200) */
|
|
58
|
+
successStatus?: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function callFunction<TInput = unknown, TOutput = unknown>(
|
|
62
|
+
config: CallFunctionConfig<TInput, TOutput>
|
|
63
|
+
): Promise<NextResponse> {
|
|
64
|
+
const { method, path, body, inputSchema, responseSchema, successStatus = 200 } = config;
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
// 入力バリデーション
|
|
68
|
+
let validatedBody = body;
|
|
69
|
+
if (inputSchema && body !== undefined) {
|
|
70
|
+
const result = inputSchema.safeParse(body);
|
|
71
|
+
if (!result.success) {
|
|
72
|
+
console.error('[BFF] Validation failed:', result.error.issues);
|
|
73
|
+
return NextResponse.json(
|
|
74
|
+
{ error: 'Validation failed', details: result.error.issues },
|
|
75
|
+
{ status: 400 }
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
validatedBody = result.data;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Azure Functions を呼び出し
|
|
82
|
+
const functionsBaseUrl = getFunctionsBaseUrl();
|
|
83
|
+
const url = functionsBaseUrl + path;
|
|
84
|
+
const keyHeaders = getFunctionsKeyHeaders(functionsBaseUrl);
|
|
85
|
+
console.log(\`[BFF] \${method} \${url}\`);
|
|
86
|
+
|
|
87
|
+
const response = await fetch(url, {
|
|
88
|
+
method,
|
|
89
|
+
headers: { 'Content-Type': 'application/json', ...keyHeaders },
|
|
90
|
+
body: validatedBody !== undefined ? JSON.stringify(validatedBody) : undefined,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
console.log('[BFF] Functions response status:', response.status);
|
|
94
|
+
|
|
95
|
+
// エラーレスポンスの転送
|
|
96
|
+
if (!response.ok) {
|
|
97
|
+
const text = await response.text();
|
|
98
|
+
console.error('[BFF] Functions error:', { status: response.status, body: text });
|
|
99
|
+
|
|
100
|
+
let errorMessage = 'Request failed';
|
|
101
|
+
try {
|
|
102
|
+
const error = JSON.parse(text);
|
|
103
|
+
errorMessage = error.error || errorMessage;
|
|
104
|
+
} catch {
|
|
105
|
+
errorMessage = text || errorMessage;
|
|
106
|
+
}
|
|
107
|
+
return NextResponse.json({ error: errorMessage }, { status: response.status });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// DELETE 204 の場合はボディなし
|
|
111
|
+
if (response.status === 204 || method === 'DELETE') {
|
|
112
|
+
return new NextResponse(null, { status: 204 });
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// レスポンスの取得と出力バリデーション
|
|
116
|
+
const data = await response.json();
|
|
117
|
+
|
|
118
|
+
if (responseSchema) {
|
|
119
|
+
const validated = responseSchema.parse(data);
|
|
120
|
+
return NextResponse.json(validated, { status: successStatus });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return NextResponse.json(data, { status: successStatus });
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.error(\`[BFF] Error:\`, error);
|
|
126
|
+
return NextResponse.json(
|
|
127
|
+
{ error: error instanceof Error ? error.message : 'Internal server error' },
|
|
128
|
+
{ status: 500 }
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 明示的ハンドラー方式の BFF ルートファイルを生成
|
|
137
|
+
*/
|
|
138
|
+
export function generateCompactBFFRoutes(model: ModelInfo, sharedPackageName: string): {
|
|
139
|
+
listRoute: string;
|
|
140
|
+
detailRoute: string;
|
|
141
|
+
} {
|
|
142
|
+
const modelName = model.name;
|
|
143
|
+
const modelCamel = toCamelCase(modelName);
|
|
144
|
+
const schemaName = model.schemaName;
|
|
145
|
+
|
|
146
|
+
const listRoute = `import { NextRequest } from 'next/server';
|
|
147
|
+
import { callFunction } from '@/lib/api/call-function';
|
|
148
|
+
import { ${schemaName} } from '${sharedPackageName}';
|
|
149
|
+
import { z } from 'zod/v4';
|
|
150
|
+
|
|
151
|
+
const InputSchema = ${schemaName}.omit({ id: true, createdAt: true, updatedAt: true });
|
|
152
|
+
|
|
153
|
+
// GET /api/${modelCamel} - 一覧取得
|
|
154
|
+
export async function GET() {
|
|
155
|
+
return callFunction({
|
|
156
|
+
method: 'GET',
|
|
157
|
+
path: '/api/${modelCamel}',
|
|
158
|
+
responseSchema: z.array(${schemaName}),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// POST /api/${modelCamel} - 新規作成
|
|
163
|
+
export async function POST(request: NextRequest) {
|
|
164
|
+
const body = await request.json();
|
|
165
|
+
return callFunction({
|
|
166
|
+
method: 'POST',
|
|
167
|
+
path: '/api/${modelCamel}',
|
|
168
|
+
body,
|
|
169
|
+
inputSchema: InputSchema,
|
|
170
|
+
responseSchema: ${schemaName},
|
|
171
|
+
successStatus: 201,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
`;
|
|
175
|
+
|
|
176
|
+
const detailRoute = `import { NextRequest } from 'next/server';
|
|
177
|
+
import { callFunction } from '@/lib/api/call-function';
|
|
178
|
+
import { ${schemaName} } from '${sharedPackageName}';
|
|
179
|
+
|
|
180
|
+
const InputSchema = ${schemaName}.omit({ id: true, createdAt: true, updatedAt: true });
|
|
181
|
+
|
|
182
|
+
// GET /api/${modelCamel}/{id} - 詳細取得
|
|
183
|
+
export async function GET(
|
|
184
|
+
_request: NextRequest,
|
|
185
|
+
{ params }: { params: Promise<{ id: string }> }
|
|
186
|
+
) {
|
|
187
|
+
const { id } = await params;
|
|
188
|
+
return callFunction({
|
|
189
|
+
method: 'GET',
|
|
190
|
+
path: \`/api/${modelCamel}/\${id}\`,
|
|
191
|
+
responseSchema: ${schemaName},
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// PUT /api/${modelCamel}/{id} - 更新
|
|
196
|
+
export async function PUT(
|
|
197
|
+
request: NextRequest,
|
|
198
|
+
{ params }: { params: Promise<{ id: string }> }
|
|
199
|
+
) {
|
|
200
|
+
const { id } = await params;
|
|
201
|
+
const body = await request.json();
|
|
202
|
+
return callFunction({
|
|
203
|
+
method: 'PUT',
|
|
204
|
+
path: \`/api/${modelCamel}/\${id}\`,
|
|
205
|
+
body,
|
|
206
|
+
inputSchema: InputSchema,
|
|
207
|
+
responseSchema: ${schemaName},
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// DELETE /api/${modelCamel}/{id} - 削除
|
|
212
|
+
export async function DELETE(
|
|
213
|
+
_request: NextRequest,
|
|
214
|
+
{ params }: { params: Promise<{ id: string }> }
|
|
215
|
+
) {
|
|
216
|
+
const { id } = await params;
|
|
217
|
+
return callFunction({
|
|
218
|
+
method: 'DELETE',
|
|
219
|
+
path: \`/api/${modelCamel}/\${id}\`,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
`;
|
|
223
|
+
|
|
224
|
+
return {
|
|
225
|
+
listRoute,
|
|
226
|
+
detailRoute,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* コネクタ用 BFF ルートファイルを生成(操作制限に対応)
|
|
232
|
+
* 指定された operations のみハンドラーを生成する
|
|
233
|
+
*/
|
|
234
|
+
export function generateConnectorBFFRoutes(
|
|
235
|
+
model: ModelInfo,
|
|
236
|
+
sharedPackageName: string,
|
|
237
|
+
operations: readonly string[]
|
|
238
|
+
): {
|
|
239
|
+
listRoute: string;
|
|
240
|
+
detailRoute: string;
|
|
241
|
+
} {
|
|
242
|
+
const modelName = model.name;
|
|
243
|
+
const modelCamel = toCamelCase(modelName);
|
|
244
|
+
const schemaName = model.schemaName;
|
|
245
|
+
const ops = new Set(operations);
|
|
246
|
+
|
|
247
|
+
const hasGetAll = ops.has("getAll");
|
|
248
|
+
const hasCreate = ops.has("create");
|
|
249
|
+
const hasGetById = ops.has("getById");
|
|
250
|
+
const hasUpdate = ops.has("update");
|
|
251
|
+
const hasDelete = ops.has("delete");
|
|
252
|
+
|
|
253
|
+
// --- List route ---
|
|
254
|
+
let listImports = `import { callFunction } from '@/lib/api/call-function';
|
|
255
|
+
import { ${schemaName} } from '${sharedPackageName}';
|
|
256
|
+
import { z } from 'zod/v4';`;
|
|
257
|
+
|
|
258
|
+
if (hasCreate) {
|
|
259
|
+
listImports = `import { NextRequest } from 'next/server';
|
|
260
|
+
${listImports}
|
|
261
|
+
|
|
262
|
+
const InputSchema = ${schemaName}.omit({ id: true, createdAt: true, updatedAt: true });`;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
let listHandlers = "";
|
|
266
|
+
if (hasGetAll) {
|
|
267
|
+
listHandlers += `
|
|
268
|
+
// GET /api/${modelCamel} - 一覧取得
|
|
269
|
+
export async function GET() {
|
|
270
|
+
return callFunction({
|
|
271
|
+
method: 'GET',
|
|
272
|
+
path: '/api/${modelCamel}',
|
|
273
|
+
responseSchema: z.array(${schemaName}),
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
`;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (hasCreate) {
|
|
280
|
+
listHandlers += `
|
|
281
|
+
// POST /api/${modelCamel} - 新規作成
|
|
282
|
+
export async function POST(request: NextRequest) {
|
|
283
|
+
const body = await request.json();
|
|
284
|
+
return callFunction({
|
|
285
|
+
method: 'POST',
|
|
286
|
+
path: '/api/${modelCamel}',
|
|
287
|
+
body,
|
|
288
|
+
inputSchema: InputSchema,
|
|
289
|
+
responseSchema: ${schemaName},
|
|
290
|
+
successStatus: 201,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
`;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const listRoute = `${listImports}
|
|
297
|
+
${listHandlers}`;
|
|
298
|
+
|
|
299
|
+
// --- Detail route ---
|
|
300
|
+
const needDetailImport = hasGetById || hasUpdate || hasDelete;
|
|
301
|
+
let detailImports = "";
|
|
302
|
+
let detailHandlers = "";
|
|
303
|
+
|
|
304
|
+
if (needDetailImport) {
|
|
305
|
+
const needNextRequest = hasUpdate;
|
|
306
|
+
detailImports = needNextRequest
|
|
307
|
+
? `import { NextRequest } from 'next/server';
|
|
308
|
+
import { callFunction } from '@/lib/api/call-function';
|
|
309
|
+
import { ${schemaName} } from '${sharedPackageName}';`
|
|
310
|
+
: `import { NextRequest } from 'next/server';
|
|
311
|
+
import { callFunction } from '@/lib/api/call-function';
|
|
312
|
+
import { ${schemaName} } from '${sharedPackageName}';`;
|
|
313
|
+
|
|
314
|
+
if (hasUpdate) {
|
|
315
|
+
detailImports += `\n\nconst InputSchema = ${schemaName}.omit({ id: true, createdAt: true, updatedAt: true });`;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (hasGetById) {
|
|
319
|
+
detailHandlers += `
|
|
320
|
+
// GET /api/${modelCamel}/{id} - 詳細取得
|
|
321
|
+
export async function GET(
|
|
322
|
+
_request: NextRequest,
|
|
323
|
+
{ params }: { params: Promise<{ id: string }> }
|
|
324
|
+
) {
|
|
325
|
+
const { id } = await params;
|
|
326
|
+
return callFunction({
|
|
327
|
+
method: 'GET',
|
|
328
|
+
path: \`/api/${modelCamel}/\${id}\`,
|
|
329
|
+
responseSchema: ${schemaName},
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (hasUpdate) {
|
|
336
|
+
detailHandlers += `
|
|
337
|
+
// PUT /api/${modelCamel}/{id} - 更新
|
|
338
|
+
export async function PUT(
|
|
339
|
+
request: NextRequest,
|
|
340
|
+
{ params }: { params: Promise<{ id: string }> }
|
|
341
|
+
) {
|
|
342
|
+
const { id } = await params;
|
|
343
|
+
const body = await request.json();
|
|
344
|
+
return callFunction({
|
|
345
|
+
method: 'PUT',
|
|
346
|
+
path: \`/api/${modelCamel}/\${id}\`,
|
|
347
|
+
body,
|
|
348
|
+
inputSchema: InputSchema,
|
|
349
|
+
responseSchema: ${schemaName},
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
`;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (hasDelete) {
|
|
356
|
+
detailHandlers += `
|
|
357
|
+
// DELETE /api/${modelCamel}/{id} - 削除
|
|
358
|
+
export async function DELETE(
|
|
359
|
+
_request: NextRequest,
|
|
360
|
+
{ params }: { params: Promise<{ id: string }> }
|
|
361
|
+
) {
|
|
362
|
+
const { id } = await params;
|
|
363
|
+
return callFunction({
|
|
364
|
+
method: 'DELETE',
|
|
365
|
+
path: \`/api/${modelCamel}/\${id}\`,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
`;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const detailRoute = needDetailImport
|
|
373
|
+
? `${detailImports}
|
|
374
|
+
${detailHandlers}`
|
|
375
|
+
: `// No detail operations defined for this connector model
|
|
376
|
+
`;
|
|
377
|
+
|
|
378
|
+
return {
|
|
379
|
+
listRoute,
|
|
380
|
+
detailRoute,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { FieldInfo, ModelInfo, toCamelCase } from "./model-parser";
|
|
2
|
+
|
|
3
|
+
type OpenApiSchema =
|
|
4
|
+
| {
|
|
5
|
+
type?: string;
|
|
6
|
+
format?: string;
|
|
7
|
+
enum?: string[];
|
|
8
|
+
items?: OpenApiSchema;
|
|
9
|
+
properties?: Record<string, OpenApiSchema>;
|
|
10
|
+
required?: string[];
|
|
11
|
+
additionalProperties?: boolean;
|
|
12
|
+
nullable?: boolean;
|
|
13
|
+
$ref?: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function toScalarSchema(field: FieldInfo): OpenApiSchema {
|
|
17
|
+
if (field.enumValues?.length) {
|
|
18
|
+
return {
|
|
19
|
+
type: "string",
|
|
20
|
+
enum: field.enumValues,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
switch (field.type) {
|
|
25
|
+
case "string":
|
|
26
|
+
return {
|
|
27
|
+
type: "string",
|
|
28
|
+
format: field.name.toLowerCase().endsWith("at") ? "date-time" : undefined,
|
|
29
|
+
};
|
|
30
|
+
case "date":
|
|
31
|
+
return {
|
|
32
|
+
type: "string",
|
|
33
|
+
format: "date-time",
|
|
34
|
+
};
|
|
35
|
+
case "number":
|
|
36
|
+
return {
|
|
37
|
+
type: "number",
|
|
38
|
+
};
|
|
39
|
+
case "boolean":
|
|
40
|
+
return {
|
|
41
|
+
type: "boolean",
|
|
42
|
+
};
|
|
43
|
+
default:
|
|
44
|
+
return {
|
|
45
|
+
type: "object",
|
|
46
|
+
additionalProperties: true,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function toFieldSchema(field: FieldInfo): OpenApiSchema {
|
|
52
|
+
const schema = field.isNestedSchema && field.nestedModelName
|
|
53
|
+
? {
|
|
54
|
+
$ref: `#/components/schemas/${field.nestedModelName}`,
|
|
55
|
+
}
|
|
56
|
+
: toScalarSchema(field);
|
|
57
|
+
|
|
58
|
+
if (field.isArray) {
|
|
59
|
+
return {
|
|
60
|
+
type: "array",
|
|
61
|
+
items: schema,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return schema;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function createComponentSchema(model: ModelInfo): OpenApiSchema {
|
|
69
|
+
const properties: Record<string, OpenApiSchema> = {};
|
|
70
|
+
const required: string[] = [];
|
|
71
|
+
|
|
72
|
+
for (const field of model.fields) {
|
|
73
|
+
properties[field.name] = toFieldSchema(field);
|
|
74
|
+
if (!field.isOptional) {
|
|
75
|
+
required.push(field.name);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
type: "object",
|
|
81
|
+
properties,
|
|
82
|
+
required,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function generateOpenApiDocument(models: ModelInfo[], rootModel: ModelInfo): string {
|
|
87
|
+
const rootModelRoute = toCamelCase(rootModel.name);
|
|
88
|
+
const components = Object.fromEntries(
|
|
89
|
+
models.map((model) => [model.name, createComponentSchema(model)])
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const document = {
|
|
93
|
+
openapi: "3.0.3",
|
|
94
|
+
info: {
|
|
95
|
+
title: `${rootModel.name} API`,
|
|
96
|
+
version: "1.0.0",
|
|
97
|
+
description: "Generated from SwallowKit Zod model metadata.",
|
|
98
|
+
},
|
|
99
|
+
paths: {
|
|
100
|
+
[`/api/${rootModelRoute}`]: {
|
|
101
|
+
get: {
|
|
102
|
+
operationId: `${rootModelRoute}GetAll`,
|
|
103
|
+
responses: {
|
|
104
|
+
"200": {
|
|
105
|
+
description: "Successful response",
|
|
106
|
+
content: {
|
|
107
|
+
"application/json": {
|
|
108
|
+
schema: {
|
|
109
|
+
type: "array",
|
|
110
|
+
items: {
|
|
111
|
+
$ref: `#/components/schemas/${rootModel.name}`,
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
post: {
|
|
120
|
+
operationId: `${rootModelRoute}Create`,
|
|
121
|
+
requestBody: {
|
|
122
|
+
required: true,
|
|
123
|
+
content: {
|
|
124
|
+
"application/json": {
|
|
125
|
+
schema: {
|
|
126
|
+
$ref: `#/components/schemas/${rootModel.name}`,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
responses: {
|
|
132
|
+
"201": {
|
|
133
|
+
description: "Created response",
|
|
134
|
+
content: {
|
|
135
|
+
"application/json": {
|
|
136
|
+
schema: {
|
|
137
|
+
$ref: `#/components/schemas/${rootModel.name}`,
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
[`/api/${rootModelRoute}/{id}`]: {
|
|
146
|
+
parameters: [
|
|
147
|
+
{
|
|
148
|
+
name: "id",
|
|
149
|
+
in: "path",
|
|
150
|
+
required: true,
|
|
151
|
+
schema: {
|
|
152
|
+
type: "string",
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
],
|
|
156
|
+
get: {
|
|
157
|
+
operationId: `${rootModelRoute}GetById`,
|
|
158
|
+
responses: {
|
|
159
|
+
"200": {
|
|
160
|
+
description: "Successful response",
|
|
161
|
+
content: {
|
|
162
|
+
"application/json": {
|
|
163
|
+
schema: {
|
|
164
|
+
$ref: `#/components/schemas/${rootModel.name}`,
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
put: {
|
|
172
|
+
operationId: `${rootModelRoute}Update`,
|
|
173
|
+
requestBody: {
|
|
174
|
+
required: true,
|
|
175
|
+
content: {
|
|
176
|
+
"application/json": {
|
|
177
|
+
schema: {
|
|
178
|
+
$ref: `#/components/schemas/${rootModel.name}`,
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
responses: {
|
|
184
|
+
"200": {
|
|
185
|
+
description: "Updated response",
|
|
186
|
+
content: {
|
|
187
|
+
"application/json": {
|
|
188
|
+
schema: {
|
|
189
|
+
$ref: `#/components/schemas/${rootModel.name}`,
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
delete: {
|
|
197
|
+
operationId: `${rootModelRoute}Delete`,
|
|
198
|
+
responses: {
|
|
199
|
+
"204": {
|
|
200
|
+
description: "Deleted response",
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
components: {
|
|
207
|
+
schemas: components,
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
return JSON.stringify(document, null, 2);
|
|
212
|
+
}
|