swallowkit 1.0.0-beta.45 → 1.0.0-beta.47

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 (52) hide show
  1. package/dist/cli/commands/add-auth.d.ts +2 -0
  2. package/dist/cli/commands/add-auth.d.ts.map +1 -1
  3. package/dist/cli/commands/add-auth.js +214 -16
  4. package/dist/cli/commands/add-auth.js.map +1 -1
  5. package/dist/cli/commands/dev.d.ts +4 -0
  6. package/dist/cli/commands/dev.d.ts.map +1 -1
  7. package/dist/cli/commands/dev.js +100 -12
  8. package/dist/cli/commands/dev.js.map +1 -1
  9. package/dist/cli/commands/init.d.ts +2 -0
  10. package/dist/cli/commands/init.d.ts.map +1 -1
  11. package/dist/cli/commands/init.js +74 -30
  12. package/dist/cli/commands/init.js.map +1 -1
  13. package/dist/cli/commands/scaffold.d.ts.map +1 -1
  14. package/dist/cli/commands/scaffold.js +37 -5
  15. package/dist/cli/commands/scaffold.js.map +1 -1
  16. package/dist/cli/index.d.ts.map +1 -1
  17. package/dist/cli/index.js +3 -1
  18. package/dist/cli/index.js.map +1 -1
  19. package/dist/core/scaffold/auth-generator.d.ts +20 -2
  20. package/dist/core/scaffold/auth-generator.d.ts.map +1 -1
  21. package/dist/core/scaffold/auth-generator.js +265 -12
  22. package/dist/core/scaffold/auth-generator.js.map +1 -1
  23. package/dist/core/scaffold/connector-functions-generator.d.ts +3 -3
  24. package/dist/core/scaffold/connector-functions-generator.d.ts.map +1 -1
  25. package/dist/core/scaffold/connector-functions-generator.js +19 -19
  26. package/dist/core/scaffold/connector-functions-generator.js.map +1 -1
  27. package/dist/core/scaffold/functions-generator.d.ts +2 -2
  28. package/dist/core/scaffold/functions-generator.d.ts.map +1 -1
  29. package/dist/core/scaffold/functions-generator.js +16 -16
  30. package/dist/core/scaffold/functions-generator.js.map +1 -1
  31. package/dist/core/scaffold/nextjs-generator.d.ts.map +1 -1
  32. package/dist/core/scaffold/nextjs-generator.js +13 -1
  33. package/dist/core/scaffold/nextjs-generator.js.map +1 -1
  34. package/dist/types/index.d.ts +1 -1
  35. package/dist/types/index.d.ts.map +1 -1
  36. package/package.json +1 -1
  37. package/src/__tests__/__snapshots__/functions-generator.test.ts.snap +20 -20
  38. package/src/__tests__/__snapshots__/nextjs-generator.test.ts.snap +13 -1
  39. package/src/__tests__/auth.test.ts +103 -0
  40. package/src/__tests__/dev.test.ts +27 -0
  41. package/src/__tests__/init.test.ts +27 -0
  42. package/src/__tests__/nextjs-generator.test.ts +8 -0
  43. package/src/cli/commands/add-auth.ts +239 -16
  44. package/src/cli/commands/dev.ts +111 -14
  45. package/src/cli/commands/init.ts +76 -30
  46. package/src/cli/commands/scaffold.ts +37 -5
  47. package/src/cli/index.ts +3 -1
  48. package/src/core/scaffold/auth-generator.ts +267 -13
  49. package/src/core/scaffold/connector-functions-generator.ts +22 -19
  50. package/src/core/scaffold/functions-generator.ts +17 -17
  51. package/src/core/scaffold/nextjs-generator.ts +13 -1
  52. package/src/types/index.ts +1 -1
@@ -5,14 +5,14 @@
5
5
  */
6
6
 
7
7
  import { ModelInfo, toCamelCase, toKebabCase } from "./model-parser";
8
- import { ModelAuthPolicy } from "../../types";
8
+ import { AuthProvider, ModelAuthPolicy } from "../../types";
9
9
  import { generateAuthImportTS, generateAuthGuardTS, generateAuthGuardCSharp, generateAuthGuardPython } from "./auth-generator";
10
10
 
11
11
  /**
12
12
  * Azure Functions エンティティファイルを生成(インラインハンドラー方式)
13
13
  * 各ハンドラーがベタ書きされており、ビジネスロジックの追加・変更が容易
14
14
  */
15
- export function generateCompactAzureFunctionsCRUD(model: ModelInfo, sharedPackageName: string, authPolicy?: ModelAuthPolicy): string {
15
+ export function generateCompactAzureFunctionsCRUD(model: ModelInfo, sharedPackageName: string, authPolicy?: ModelAuthPolicy, authProvider?: AuthProvider): string {
16
16
  const modelName = model.name;
17
17
  const modelCamel = toCamelCase(modelName);
18
18
  const schemaName = model.schemaName;
@@ -23,8 +23,8 @@ export function generateCompactAzureFunctionsCRUD(model: ModelInfo, sharedPackag
23
23
 
24
24
  const hasAuth = !!authPolicy;
25
25
  const authImport = hasAuth ? `\n${generateAuthImportTS()}\n` : '';
26
- const readGuard = hasAuth ? `\n${generateAuthGuardTS(authPolicy!, 'read')}\n` : '';
27
- const writeGuard = hasAuth ? `\n${generateAuthGuardTS(authPolicy!, 'write')}\n` : '';
26
+ const readGuard = hasAuth ? `\n${generateAuthGuardTS(authPolicy!, 'read', authProvider)}\n` : '';
27
+ const writeGuard = hasAuth ? `\n${generateAuthGuardTS(authPolicy!, 'write', authProvider)}\n` : '';
28
28
  const authCatchBlock = hasAuth
29
29
  ? ` const authErr = handleAuthError(error);
30
30
  if (authErr) return authErr;\n`
@@ -69,7 +69,7 @@ const containerName = '${modelName.endsWith('s') ? modelName : modelName + 's'}'
69
69
  app.http('${modelCamel}-get-all', {
70
70
  methods: ['GET'],
71
71
  route: '${modelCamel}',
72
- authLevel: 'anonymous',
72
+ authLevel: 'function',
73
73
  extraInputs: [
74
74
  {
75
75
  type: 'cosmosDB',
@@ -105,7 +105,7 @@ ${getByIdHandler}
105
105
  app.http('${modelCamel}-create', {
106
106
  methods: ['POST'],
107
107
  route: '${modelCamel}',
108
- authLevel: 'anonymous',
108
+ authLevel: 'function',
109
109
  extraOutputs: [
110
110
  {
111
111
  type: 'cosmosDB',
@@ -158,7 +158,7 @@ function generateGetByIdWithBinding(modelCamel: string, schemaName: string, read
158
158
  app.http('${modelCamel}-get-by-id', {
159
159
  methods: ['GET'],
160
160
  route: '${modelCamel}/{id}',
161
- authLevel: 'anonymous',
161
+ authLevel: 'function',
162
162
  extraInputs: [
163
163
  {
164
164
  type: 'cosmosDB',
@@ -193,7 +193,7 @@ function generateGetByIdWithSdk(modelCamel: string, schemaName: string, readGuar
193
193
  app.http('${modelCamel}-get-by-id', {
194
194
  methods: ['GET'],
195
195
  route: '${modelCamel}/{id}',
196
- authLevel: 'anonymous',
196
+ authLevel: 'function',
197
197
  handler: async (request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
198
198
  try {${readGuard}
199
199
  const id = request.params.id;
@@ -229,7 +229,7 @@ function generateUpdateWithBinding(modelCamel: string, schemaName: string, write
229
229
  app.http('${modelCamel}-update', {
230
230
  methods: ['PUT'],
231
231
  route: '${modelCamel}/{id}',
232
- authLevel: 'anonymous',
232
+ authLevel: 'function',
233
233
  extraInputs: [
234
234
  {
235
235
  type: 'cosmosDB',
@@ -294,7 +294,7 @@ function generateUpdateWithSdk(modelCamel: string, schemaName: string, partition
294
294
  app.http('${modelCamel}-update', {
295
295
  methods: ['PUT'],
296
296
  route: '${modelCamel}/{id}',
297
- authLevel: 'anonymous',
297
+ authLevel: 'function',
298
298
  handler: async (request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
299
299
  try {${writeGuard}
300
300
  const id = request.params.id;
@@ -349,7 +349,7 @@ function generateDeleteIdPartition(modelCamel: string, writeGuard: string, authC
349
349
  app.http('${modelCamel}-delete', {
350
350
  methods: ['DELETE'],
351
351
  route: '${modelCamel}/{id}',
352
- authLevel: 'anonymous',
352
+ authLevel: 'function',
353
353
  handler: async (request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
354
354
  try {${writeGuard}
355
355
  const id = request.params.id;
@@ -379,7 +379,7 @@ function generateDeleteCustomPartition(modelCamel: string, partitionKeyField: st
379
379
  app.http('${modelCamel}-delete', {
380
380
  methods: ['DELETE'],
381
381
  route: '${modelCamel}/{id}',
382
- authLevel: 'anonymous',
382
+ authLevel: 'function',
383
383
  handler: async (request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
384
384
  try {${writeGuard}
385
385
  const id = request.params.id;
@@ -596,7 +596,7 @@ ${readItemMethod}
596
596
 
597
597
  [Function("${modelCamel}GetAll")]
598
598
  public async Task<HttpResponseData> GetAll(
599
- [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "${modelCamel}")] HttpRequestData request)
599
+ [HttpTrigger(AuthorizationLevel.Function, "get", Route = "${modelCamel}")] HttpRequestData request)
600
600
  {
601
601
  try
602
602
  {${readGuard}
@@ -637,7 +637,7 @@ ${readItemMethod}
637
637
 
638
638
  [Function("${modelCamel}GetById")]
639
639
  public async Task<HttpResponseData> GetById(
640
- [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "${modelCamel}/{id}")] HttpRequestData request,
640
+ [HttpTrigger(AuthorizationLevel.Function, "get", Route = "${modelCamel}/{id}")] HttpRequestData request,
641
641
  string id)
642
642
  {
643
643
  try
@@ -660,7 +660,7 @@ ${readItemMethod}
660
660
 
661
661
  [Function("${modelCamel}Create")]
662
662
  public async Task<HttpResponseData> Create(
663
- [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "${modelCamel}")] HttpRequestData request)
663
+ [HttpTrigger(AuthorizationLevel.Function, "post", Route = "${modelCamel}")] HttpRequestData request)
664
664
  {
665
665
  try
666
666
  {${writeGuard}
@@ -695,7 +695,7 @@ ${readItemMethod}
695
695
 
696
696
  [Function("${modelCamel}Update")]
697
697
  public async Task<HttpResponseData> Update(
698
- [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "${modelCamel}/{id}")] HttpRequestData request,
698
+ [HttpTrigger(AuthorizationLevel.Function, "put", Route = "${modelCamel}/{id}")] HttpRequestData request,
699
699
  string id)
700
700
  {
701
701
  try
@@ -740,7 +740,7 @@ ${readItemMethod}
740
740
 
741
741
  [Function("${modelCamel}Delete")]
742
742
  public async Task<HttpResponseData> Delete(
743
- [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "${modelCamel}/{id}")] HttpRequestData request,
743
+ [HttpTrigger(AuthorizationLevel.Function, "delete", Route = "${modelCamel}/{id}")] HttpRequestData request,
744
744
  string id)
745
745
  {
746
746
  try
@@ -32,6 +32,17 @@ function getFunctionsBaseUrl(): string {
32
32
  return process.env.BACKEND_FUNCTIONS_BASE_URL || process.env.FUNCTIONS_BASE_URL || 'http://localhost:7071';
33
33
  }
34
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
+
35
46
  interface CallFunctionConfig<TInput = any, TOutput = any> {
36
47
  /** HTTP メソッド */
37
48
  method: 'GET' | 'POST' | 'PUT' | 'DELETE';
@@ -70,11 +81,12 @@ export async function callFunction<TInput = any, TOutput = any>(
70
81
  // Azure Functions を呼び出し
71
82
  const functionsBaseUrl = getFunctionsBaseUrl();
72
83
  const url = functionsBaseUrl + path;
84
+ const keyHeaders = getFunctionsKeyHeaders(functionsBaseUrl);
73
85
  console.log(\`[BFF] \${method} \${url}\`);
74
86
 
75
87
  const response = await fetch(url, {
76
88
  method,
77
- headers: { 'Content-Type': 'application/json' },
89
+ headers: { 'Content-Type': 'application/json', ...keyHeaders },
78
90
  body: validatedBody !== undefined ? JSON.stringify(validatedBody) : undefined,
79
91
  });
80
92
 
@@ -73,7 +73,7 @@ export interface ApiModelConnectorConfig {
73
73
  export type ModelConnectorConfig = RdbModelConnectorConfig | ApiModelConnectorConfig;
74
74
 
75
75
  // 認証プロバイダー種別
76
- export type AuthProvider = "custom-jwt" | "swa" | "swa-custom" | "none";
76
+ export type AuthProvider = "custom-jwt" | "swa" | "external-token" | "swa-custom" | "none";
77
77
 
78
78
  // custom-jwt プロバイダー設定
79
79
  export interface CustomJwtConfig {