ts-api-decorators-azure-function
v0.1.61
Published
API Decorators for typescript and Azure Functions
Downloads
73
Readme
Azure Function API Decorators
This library allows you to use Typescript API Decorators with Azure Functions. By doing so you:
- Remove the strict requirements Azure Functions have on the layout of your source directory, and dependency on having just the right config files in just the right place
- Generate your Azure Functions config on build based on typed declarations in your code
- All binding inputs/outputs have type declarations and are checked at compile time
- Benefit from the core tsapi library for your HTTP bindings, including compile and runtime type checking
Installation
This library performs preprocessing on APIs during the typescript compilation step. See Configuring Transformers for how to set this up. If you want to get started faster, check out the simple example in examples/azure/simple.
Usage (Defining an API)
APIs are defined as methods on a class:
import { Api, ApiGetMethod } from 'ts-api-decorators-azure-function';
@Api
class MyApi {
@ApiGetMethod('/hello')
greet() {
return 'Hello World!';
}
}
This defines an API that exposes a single GET
handler at /hello
that returns the string Hello World!
. To call the API, build your project and then use tsapi
to generate the Azure Function binding files:
tsc
npx tsapi azfunc-generate . functions
This command will output the functions definitions to a new folder called functions
(the last argument). The resulting directory structure will look like this:
dist/
myApi.js
functions/hello/
function.json
index.js
src/
myApi.ts
tsconfig.json
The
dist
folder above was generated by thetsc
build and is the compiled version ofsrc/myApi.ts
. The contents of thefunctions
directory are completely autogenerated.
To start your Azure Function server, run the below using the Azure Functions Core Tools:
cd functions
func start
For complete documentation on ts-api-decorators
functionality, see the README at the root of the repo. Continue reading for Azure Functions-specific features and examples.
Examples
See the examples directory for the following examples:
Non-Http Triggers and Bindings
You can also define functions that use non-HTTP triggers and input/output bindings. The table below shows the list of supported triggers and bindings:
| Type | Trigger | Input | Output | | ---- | :------: | :---: | :----: | | Blob storage |✅|✅|✅| | Cosmos DB |❌|❌|❌| | Event Grid |❌| | | | Event Hubs |❌| |❌| | HTTP & webhooks |✅| |✅| | IoT Hub |❌| |❌| | Microsoft GraphExcel tables | |❌|❌| | Microsoft GraphOneDrive files | |❌|❌| | Microsoft GraphOutlook email | | |❌| | Microsoft Graphevents |❌|❌|❌| | Microsoft GraphAuth tokens | |❌| | | Mobile Apps | |❌|❌| | Notification Hubs || |❌| | Queue storage |❌| |❌| | SendGrid || |❌| | Service Bus |❌| |❌| | SignalR ||❌|❌| | Table storage | |❌|❌| | Timer |✅| | | | Twilio | | |❌|
Access Azure Context Functionality
You can access the Azure Context object using the @AzureApiContextParam
decorator.
import { Api, ApiGetMethod, AzureApiContextParam } from 'ts-api-decorators-azure-function';
import { Context } from "@azure/functions";
@Api
class MyApi {
@ApiGetMethod('/hello')
greet(
@AzureApiContextParam() context: Context,
) {
// ...
}
}