Azure FunctionsをTypeScriptで作る


$ mkdir sample001
$ cd sample001

datakuminoMacBook-Air@takumi ~/Documents/Azure/work/ts/functions/sample001$ func init
Select a number for worker runtime:
1. dotnet
2. dotnet (isolated process)
3. node
4. python
5. powershell
6. custom
Choose option: 3
Select a number for language:
1. javascript
2. typescript
Choose option: 2
Writing .funcignore
Writing package.json
Writing tsconfig.json
Writing .gitignore
Writing host.json
Writing local.settings.json
Writing /Users/takumi/Documents/Azure/work/ts/functions/sample001/.vscode/extensions.json

odatakuminoMacBook-Air@takumi ~/Documents/Azure/work/ts/functions/sample001$ ls
host.json               local.settings.json     package.json            tsconfig.json

odatakuminoMacBook-Air@takumi ~/Documents/Azure/work/ts/functions/sample001$ npm install && npm update

10を入力するとHTTPトリガーを作れる。その後、Function nameを入力する。

odatakuminoMacBook-Air@takumi ~/Documents/Azure/work/ts/functions/sample001$ func new
Select a number for template:
1. Azure Blob Storage trigger
2. Azure Cosmos DB trigger
3. Durable Functions activity
4. Durable Functions entity
5. Durable Functions Entity HTTP starter
6. Durable Functions HTTP starter
7. Durable Functions orchestrator
8. Azure Event Grid trigger
9. Azure Event Hub trigger
10. HTTP trigger
11. IoT Hub (Event Hub)
12. Kafka output
13. Kafka trigger
14. Azure Queue Storage trigger
15. RabbitMQ trigger
16. SendGrid
17. Azure Service Bus Queue trigger
18. Azure Service Bus Topic trigger
19. SignalR negotiate HTTP trigger
20. Timer trigger
Choose option: 10
HTTP trigger
Function name: [HttpTrigger] SampleTrigger
Writing /Users/takumi/Documents/Azure/work/ts/functions/sample001/SampleTrigger/index.ts
Writing /Users/takumi/Documents/Azure/work/ts/functions/sample001/SampleTrigger/function.json
The function "SampleTrigger" was created successfully from the "HTTP trigger" template.

func startして実際に動かしてみる。

odatakuminoMacBook-Air@takumi ~/Documents/Azure/work/ts/functions/sample001$ func start

Azure Functions Core Tools
Core Tools Version:       4.0.4544 Commit hash: N/A  (64-bit)
Function Runtime Version:

[2022-07-14T04:41:44.925Z] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
[2022-07-14T04:41:44.954Z] The 'SampleTrigger' function is in error: Invalid script file name configuration. The 'scriptFile' property is set to a file that does not exist.
For detailed output, run func with --verbose flag.

func startだけだとdist作成されないっぽい。なのでnpm run buildしてdistを作る。

odatakuminoMacBook-Air@takumi ~/Documents/Azure/work/ts/functions/sample001$ npm run build

> build
> tsc

node_modules/@azure/functions/index.d.ts:220:16 - error TS2580: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

220         value: Buffer;

Found 1 error in node_modules/@azure/functions/index.d.ts:220

Cannot find name ‘Buffer’. Do you need to install type definitions for node?というエラーが出てるので、これを以下実行する。

npm install –save @types/node

odatakuminoMacBook-Air@takumi ~/Documents/Azure/work/ts/functions/sample001$ npm install --save @types/node

added 1 package, and audited 4 packages in 475ms

found 0 vulnerabilities

npm run buildもうまくいった。

npm run buildした後にdistができる。
func startをその後にすると動きます。