azure-functions-decorators-typescript v1.0.0-alpha.2
Azure Functions TypeScript Decorators
Branch | Support level | Node.js Versions |
---|---|---|
main | Preview | 18 (preview) |
Install
npm install azure-functions-decorators-typescript
Usage
This library contains definitions for TypeScript decorators that makes registering and defining Azure Functions easier than ever! Building upon the new Node.js programming model (v4 of @azure/functions
), this library extends that framework to allow users to register functions using decorators!
This library is just a personal hackathon project, so there is no clear plan on when or if this may become officially public. That being said, you can try out the limited functionality that exists now and give your feedback for improvements!
Prerequisites
- Node.js v18+
- TypeScript v4+
Setup
For a ready-to-run app, clone this repo: https://github.com/hossam-nasr/func-node-decorators-prototype and fpllow the instructions in the README.
If you are creating your own app, the instructions you need to follow to run an app with v4 of @azure/functions
still apply to this library. See more detailed information here: https://github.com/Azure/azure-functions-nodejs-library/wiki/Azure-Functions-Node.js-Framework-v4. Namely, take note of these important points:
- Make sure you reference this preview npm package in your package.json:
"func-cli-nodejs-v4": "4.0.4764"
. This is a preview build of the func CLI that contains tooling support for running the new framework, which this library relies on. - If you are using an extension bundle, you must be on version 3.15.0 or greater, e.g. by specifying
[3.15.0, 4.0.0]
in yourhost.json
file. - You must set the
AzureWebJobsStorage
setting to a valid value in yourlocal.settings.json
file, either to a connection string of a valid Azure storagea account, or toUseDevelopmentStorage=true
and use the local storage emulator.
Define your functions using decorators!
You can now define your functions using TypeScript decorators! Below are a few examples:
Simple HTTP trigger function:
src/HttpTrigger1/index.ts
:
module.exports = async function(context, req) {
context.log("HTTP function processed request");
const name = req.query.name
|| req.body
|| 'world';
context.res = {
body: `Hello, ${name}!`
};
};
src/HttpTrigger1/function.json
:
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
src/index.ts
:
import {
azureFunction,
http
} from 'azure-functions-decorators-typescript'
class FunctionApp {
@azureFunction()
async httpTrigger1(context, @http() request) {
context.log("Http function processed request");
const name = request.query.get('name')
|| (await request.text())
|| 'world';
return {
body: `Hello, ${name}`,
};
}
}
export default FunctionApp
Setting an extra blob input:
src/MyFunction/index.ts
:
module.exports = async function(context, req) {
context.log("HTTP function processed request");
const name = context.bindings.blobInput;
context.res = {
body: `Hello, ${name}!`
};
};
src/MyFunction/function.json
:
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "blob",
"name": "blobInput",
"direction": "in",
"path": "helloworld/{name}"
"connection": "storage_conn"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
src/index.ts
:
import {
azureFunction,
http,
blobInput
} from 'azure-functions-decorators-typescript'
class FunctionApp {
@azureFunction()
async httpTrigger1(
context,
@http() request,
@blobInput('helloworld/{name}', 'storage_conn') blobInput
) {
context.log("Http function processed request");
const name = blobInput;
return {
body: `Hello, ${name}`,
};
}
}
export default FunctionApp
Setting an extra queue output:
src/MyFunction/index.ts
:
module.exports = async function(context, req) {
context.log("HTTP function processed request");
const name = req.query.name
|| req.body
|| 'world';
context.bindings.queueOut = name;
context.res = {
body: `Hello, ${name}!`
};
};
src/MyFunction/function.json
:
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "queue",
"name": "queueOut",
"direction": "output",
"queueName": "helloworld"
"connection": "storage_conn"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
src/index.ts
:
import {
azureFunction,
http,
queueOutput
} from 'azure-functions-decorators-typescript'
class FunctionApp {
@azureFunction()
async httpTrigger1(
context,
@http() request,
@queueOutput('helloworld', 'storage_conn') queueOut
) {
context.log("Http function processed request");
const name = request.query.get('name')
|| (await request.text())
|| 'world';
queueOut.set(name);
return {
body: `Hello, ${name}`,
};
}
}
export default FunctionApp
Supported triggers and bindings
As this is just a hackathon project at the moment, most triggers and bindings do not have their own custom decorators. Below is a list of the available triggers and bindings and their supported state. This list will be updated when/if more triggers and bindings are added. The list of all available triggers and bindings was retrieved from the Microsoft docs here
Type | Trigger | Input | Output |
---|---|---|---|
HTTP | ✅ | ❌ | |
Timer | ✅ | ||
Storage blob | ✅ | ✅ | ✅ |
Storage queue | ✅ | ✅ | |
CosmosDB | ✅ | ❌ | ❌ |
Azure SQL | ❌ | ❌ | |
Dapr | ❌ | ❌ | ❌ |
Event Grid | ❌ | ❌ | |
Event Hubs | ❌ | ❌ | |
IoT Hub | ❌ | ||
Kafka | ❌ | ❌ | |
Mobile Apps | ❌ | ❌ | |
Notification Hubs | ❌ | ||
RabbitMQ | ❌ | ❌ | |
SendGrid | ❌ | ||
Service Bus | ❌ | ❌ | |
SignalR | ❌ | ❌ | ❌ |
Table storage | ❌ | ❌ | |
Twilio | ❌ |
Generic triggers, inputs, and outputs
Even for the triggers and bindings that are not explicitly supported in this library, they can still be registered using the generic @trigger
, @input
, @output
and @returns
decorators. Below is an example of a function that uses all 4 of these to illustrate how they work:
import { HttpResponse, InvocationContext, output as AzFuncOutput } from '@azure/functions';
import { azureFunction, input, output, returns, trigger } from 'azure-functions-decorators-typescript';
class FunctionApp {
@azureFunction()
@returns(AzFuncOutput.http({}))
async copyBlob1(
context: InvocationContext,
@trigger('queueTrigger', { queueName: 'copyblobqueue', connection: 'storage_APPSETTING' }) queueItem: unknown,
@input('blob', { path: 'helloworld/{queueTrigger}', connection: 'storage_APPSETTING' }) blobInput: unknown,
@output('blob', { path: 'helloworld/{queueTrigger}-copy', connection: 'storage_APPSETTING' }) blobOutput: any
): Promise<HttpResponse> {
context.log('Storage queue function processes work item: ', queueItem);
blobOutput.set(blobInput);
return {
body: `Successfully copied ${queueItem}`,
};
}
}
export default FunctionApp;
2 years ago
2 years ago