Skip to content

[Webpubsub] Add types for input/output/trigger #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
StorageQueueFunctionOptions,
TimerFunctionOptions,
WarmupFunctionOptions,
WebPubSubFunctionOptions,
} from '@azure/functions';
import { FunctionCallback } from '@azure/functions-core';
import { toCoreFunctionMetadata } from './converters/toCoreFunctionMetadata';
Expand Down Expand Up @@ -135,6 +136,10 @@ export function sql(name: string, options: SqlFunctionOptions): void {
generic(name, convertToGenericOptions(options, trigger.sql));
}

export function webPubSub(name: string, options: WebPubSubFunctionOptions): void {
generic(name, convertToGenericOptions(options, trigger.webPubSub));
}

export function generic(name: string, options: GenericFunctionOptions): void {
if (!hasSetModel) {
setProgrammingModel();
Expand Down
18 changes: 18 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
StorageBlobInputOptions,
TableInput,
TableInputOptions,
WebPubSubConnectionInput,
WebPubSubConnectionInputOptions,
WebPubSubContextInput,
WebPubSubContextInputOptions,
} from '@azure/functions';
import { addBindingName } from './addBindingName';

Expand Down Expand Up @@ -43,6 +47,20 @@ export function sql(options: SqlInputOptions): SqlInput {
});
}

export function webPubSubConnection(options: WebPubSubConnectionInputOptions): WebPubSubConnectionInput {
return addInputBindingName({
...options,
type: 'webPubSubConnection',
});
}

export function webPubSubContext(options: WebPubSubContextInputOptions): WebPubSubContextInput {
return addInputBindingName({
...options,
type: 'webPubSubContext',
});
}

export function generic(options: GenericInputOptions): FunctionInput {
return addInputBindingName(options);
}
Expand Down
9 changes: 9 additions & 0 deletions src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
StorageQueueOutputOptions,
TableOutput,
TableOutputOptions,
WebPubSubOutput,
WebPubSubOutputOptions,
} from '@azure/functions';
import { addBindingName } from './addBindingName';

Expand Down Expand Up @@ -97,6 +99,13 @@ export function sql(options: SqlOutputOptions): SqlOutput {
});
}

export function webPubSub(options: WebPubSubOutputOptions): WebPubSubOutput {
return addOutputBindingName({
...options,
type: 'webPubSub',
});
}

export function generic(options: GenericOutputOptions): FunctionOutput {
return addOutputBindingName(options);
}
Expand Down
9 changes: 9 additions & 0 deletions src/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
TimerTriggerOptions,
WarmupTrigger,
WarmupTriggerOptions,
WebPubSubTrigger,
WebPubSubTriggerOptions,
} from '@azure/functions';
import { addBindingName } from './addBindingName';

Expand Down Expand Up @@ -108,6 +110,13 @@ export function sql(options: SqlTriggerOptions): SqlTrigger {
});
}

export function webPubSub(options: WebPubSubTriggerOptions): WebPubSubTrigger {
return addTriggerBindingName({
...options,
type: 'webPubSubTrigger',
});
}

export function generic(options: GenericTriggerOptions): FunctionTrigger {
return addTriggerBindingName(options);
}
Expand Down
8 changes: 8 additions & 0 deletions types/InvocationContext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ServiceBusQueueOutput, ServiceBusTopicOutput } from './serviceBus';
import { SqlInput, SqlOutput } from './sql';
import { StorageBlobInput, StorageBlobOutput, StorageQueueOutput } from './storage';
import { TableInput, TableOutput } from './table';
import { WebPubSubOutput } from './webpubsub';

/**
* Contains metadata and helper methods specific to this invocation
Expand Down Expand Up @@ -216,6 +217,13 @@ export interface InvocationContextExtraOutputs {
*/
set(output: EventGridOutput, events: EventGridPartialEvent | EventGridPartialEvent[]): void;

/**
* Set a secondary Web PubSub output for this invocation
* @output the configuration object for this Web PubSub output
* @message the output message(s) value
*/
set(output: WebPubSubOutput, messages: unknown): void;

/**
* Set a secondary generic output for this invocation
* @outputOrName the configuration object or name for this output
Expand Down
8 changes: 8 additions & 0 deletions types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SqlFunctionOptions } from './sql';
import { StorageBlobFunctionOptions, StorageQueueFunctionOptions } from './storage';
import { TimerFunctionOptions } from './timer';
import { WarmupFunctionOptions } from './warmup';
import { WebPubSubFunctionOptions } from './webpubsub';

/**
* Optional method to configure the behavior of your app.
Expand Down Expand Up @@ -180,4 +181,11 @@ export function sql(name: string, options: SqlFunctionOptions): void;
*/
export function generic(name: string, options: GenericFunctionOptions): void;

/**
* Registers a WebPubSub function in your app that will be triggered by WebPubSub events
* @param name The name of the function. The name must be unique within your app and will mostly be used for your own tracking purposes
* @param options Configuration options describing the inputs, outputs, and handler for this function
*/
export function webPubSub(name: string, options: WebPubSubFunctionOptions): void;

export * as hook from './hooks/registerHook';
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export * from './table';
export * from './timer';
export * as trigger from './trigger';
export * from './warmup';
export * from './webpubsub';

/**
* Void if no `return` output is registered
Expand Down
16 changes: 16 additions & 0 deletions types/input.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { FunctionInput } from './index';
import { SqlInput, SqlInputOptions } from './sql';
import { StorageBlobInput, StorageBlobInputOptions } from './storage';
import { TableInput, TableInputOptions } from './table';
import {
WebPubSubConnectionInput,
WebPubSubConnectionInputOptions,
WebPubSubContextInput,
WebPubSubContextInputOptions,
} from './webpubsub';

/**
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-storage-blob-input?pivots=programming-language-javascript)
Expand All @@ -28,6 +34,16 @@ export function cosmosDB(options: CosmosDBInputOptions): CosmosDBInput;
*/
export function sql(options: SqlInputOptions): SqlInput;

/**
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-web-pubsub-input?pivots=programming-language-javascript)
*/
export function webPubSubConnection(options: WebPubSubConnectionInputOptions): WebPubSubConnectionInput;

/**
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-web-pubsub-input?pivots=programming-language-javascript)
*/
export function webPubSubContext(options: WebPubSubContextInputOptions): WebPubSubContextInput;

/**
* A generic option that can be used for any input type
* Use this method if your desired input type does not already have its own method
Expand Down
6 changes: 6 additions & 0 deletions types/output.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { SqlOutput, SqlOutputOptions } from './sql';
import { StorageBlobOutput, StorageBlobOutputOptions, StorageQueueOutput, StorageQueueOutputOptions } from './storage';
import { TableOutput, TableOutputOptions } from './table';
import { WebPubSubOutput, WebPubSubOutputOptions } from './webpubsub';

/**
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-http-webhook-output?&pivots=programming-language-javascript)
Expand Down Expand Up @@ -67,6 +68,11 @@ export function cosmosDB(options: CosmosDBOutputOptions): CosmosDBOutput;
*/
export function sql(options: SqlOutputOptions): SqlOutput;

/**
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-web-pubsub-output?pivots=programming-language-javascript)
*/
export function webPubSub(options: WebPubSubOutputOptions): WebPubSubOutput;

/**
* A generic option that can be used for any output type
* Use this method if your desired output type does not already have its own method
Expand Down
7 changes: 6 additions & 1 deletion types/trigger.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from './storage';
import { TimerTrigger, TimerTriggerOptions } from './timer';
import { WarmupTrigger, WarmupTriggerOptions } from './warmup';

import { WebPubSubTrigger, WebPubSubTriggerOptions } from './webpubsub';
/**
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-http-webhook-trigger?&pivots=programming-language-javascript)
*/
Expand Down Expand Up @@ -78,6 +78,11 @@ export function warmup(options: WarmupTriggerOptions): WarmupTrigger;
*/
export function sql(options: SqlTriggerOptions): SqlTrigger;

/**
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-web-pubsub-trigger?pivots=programming-language-javascript)
*/
export function webPubSub(options: WebPubSubTriggerOptions): WebPubSubTrigger;

/**
* A generic option that can be used for any trigger type
* Use this method if your desired trigger type does not already have its own method
Expand Down
124 changes: 124 additions & 0 deletions types/webpubsub.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.

import { FunctionInput, FunctionOptions, FunctionOutput, FunctionResult, FunctionTrigger } from './index';
import { InvocationContext } from './InvocationContext';

export type WebPubSubHandler = (message: unknown, context: InvocationContext) => FunctionResult;

export interface WebPubSubFunctionOptions extends WebPubSubTriggerOptions, Partial<FunctionOptions> {
handler: WebPubSubHandler;

trigger?: WebPubSubTrigger;
}

export interface WebPubSubTriggerOptions {
/**
* Required - The variable name used in function code for the parameter that receives the event data
*/
name: string;

/**
* Required - The name of the hub to which the function is bound
*/
hub: string;

/**
* Required - The type of event to which the function should respond
* Must be either 'user' or 'system'
*/
eventType: 'user' | 'system';

/**
* Required - The name of the event to which the function should respond
* For system event type: 'connect', 'connected', or 'disconnected'
* For user-defined subprotocols: 'message'
* For system supported subprotocol json.webpubsub.azure.v1: user-defined event name
*/
eventName: string;

/**
* Optional - Specifies which client protocol can trigger the Web PubSub trigger functions
* Default is 'all'
*/
clientProtocols?: 'all' | 'webPubSub' | 'mqtt';

/**
* Optional - The name of an app setting or setting collection that specifies the upstream Azure Web PubSub service
* Used for signature validation
* Defaults to "WebPubSubConnectionString" if not specified
* Set to null to disable validation
*/
connection?: string | null;
}

export type WebPubSubTrigger = FunctionTrigger & WebPubSubTriggerOptions;

export interface WebPubSubConnectionInputOptions {
/**
* Required - Variable name used in function code for input connection binding object.
*/
name: string;

/**
* Required - The name of the Web PubSub hub for the function to be triggered.
* Can be set in the attribute (higher priority) or in app settings as a global value.
*/
hub: string;

/**
* Optional - The value of the user identifier claim to be set in the access key token.
*/
userId?: string;

/**
* Optional - The client protocol type.
* Valid values are 'default' and 'mqtt'.
* For MQTT clients, you must set it to 'mqtt'.
* For other clients, you can omit the property or set it to 'default'.
*/
clientProtocol?: 'default' | 'mqtt';

/**
* Optional - The name of the app setting that contains the Web PubSub Service connection string.
* Defaults to "WebPubSubConnectionString".
*/
connection?: string;
}
export type WebPubSubConnectionInput = FunctionInput & WebPubSubConnectionInputOptions;

export interface WebPubSubContextInputOptions {
/**
* Required - Variable name used in function code for input Web PubSub request.
*/
name: string;

/**
* Optional - The name of an app settings or setting collection that specifies the upstream Azure Web PubSub service.
* The value is used for Abuse Protection and Signature validation.
* The value is auto resolved with "WebPubSubConnectionString" by default.
* Null means the validation isn't needed and always succeeds.
*/
connection?: string;
}
export type WebPubSubContextInput = FunctionInput & WebPubSubContextInputOptions;

export interface WebPubSubOutputOptions {
/**
* Required - Variable name used in function code for output binding object.
*/
name: string;

/**
* Required - The name of the hub to which the function is bound.
* Can be set in the attribute (higher priority) or in app settings as a global value.
*/
hub: string;

/**
* Optional - The name of the app setting that contains the Web PubSub Service connection string.
* Defaults to "WebPubSubConnectionString".
*/
connection?: string;
}
export type WebPubSubOutput = FunctionOutput & WebPubSubOutputOptions;
Loading