0.1.61 • Published 7 months ago

ts-api-decorators-azure-function v0.1.61

Weekly downloads
2
License
Apache-2.0
Repository
github
Last release
7 months ago

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 the tsc build and is the compiled version of src/myApi.ts. The contents of the functions 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:

TypeTriggerInputOutput
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,
	) {
		// ...
	}
}
0.1.58

7 months ago

0.1.59

7 months ago

0.1.60

7 months ago

0.1.61

7 months ago

0.1.57

1 year ago

0.1.52

1 year ago

0.1.53

1 year ago

0.1.54

1 year ago

0.1.55

1 year ago

0.1.56

1 year ago

0.1.50

1 year ago

0.1.51

1 year ago

0.1.49

1 year ago

0.1.46

2 years ago

0.1.47

2 years ago

0.1.48

2 years ago

0.1.45

2 years ago

0.1.41

2 years ago

0.1.42

2 years ago

0.1.43

2 years ago

0.1.44

2 years ago

0.1.40

2 years ago

0.2.7

2 years ago

0.2.1

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.0

3 years ago

0.1.38

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago