Skip to content

Commit 9259cfc

Browse files
[Webpubsub] Add types for input/output/trigger (#311)
* Init: input/output/trigger * App trigger
1 parent d515a0a commit 9259cfc

11 files changed

+210
-1
lines changed

src/app.ts

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
StorageQueueFunctionOptions,
1919
TimerFunctionOptions,
2020
WarmupFunctionOptions,
21+
WebPubSubFunctionOptions,
2122
} from '@azure/functions';
2223
import { FunctionCallback } from '@azure/functions-core';
2324
import { toCoreFunctionMetadata } from './converters/toCoreFunctionMetadata';
@@ -135,6 +136,10 @@ export function sql(name: string, options: SqlFunctionOptions): void {
135136
generic(name, convertToGenericOptions(options, trigger.sql));
136137
}
137138

139+
export function webPubSub(name: string, options: WebPubSubFunctionOptions): void {
140+
generic(name, convertToGenericOptions(options, trigger.webPubSub));
141+
}
142+
138143
export function generic(name: string, options: GenericFunctionOptions): void {
139144
if (!hasSetModel) {
140145
setProgrammingModel();

src/input.ts

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import {
1212
StorageBlobInputOptions,
1313
TableInput,
1414
TableInputOptions,
15+
WebPubSubConnectionInput,
16+
WebPubSubConnectionInputOptions,
17+
WebPubSubContextInput,
18+
WebPubSubContextInputOptions,
1519
} from '@azure/functions';
1620
import { addBindingName } from './addBindingName';
1721

@@ -43,6 +47,20 @@ export function sql(options: SqlInputOptions): SqlInput {
4347
});
4448
}
4549

50+
export function webPubSubConnection(options: WebPubSubConnectionInputOptions): WebPubSubConnectionInput {
51+
return addInputBindingName({
52+
...options,
53+
type: 'webPubSubConnection',
54+
});
55+
}
56+
57+
export function webPubSubContext(options: WebPubSubContextInputOptions): WebPubSubContextInput {
58+
return addInputBindingName({
59+
...options,
60+
type: 'webPubSubContext',
61+
});
62+
}
63+
4664
export function generic(options: GenericInputOptions): FunctionInput {
4765
return addInputBindingName(options);
4866
}

src/output.ts

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
StorageQueueOutputOptions,
2525
TableOutput,
2626
TableOutputOptions,
27+
WebPubSubOutput,
28+
WebPubSubOutputOptions,
2729
} from '@azure/functions';
2830
import { addBindingName } from './addBindingName';
2931

@@ -97,6 +99,13 @@ export function sql(options: SqlOutputOptions): SqlOutput {
9799
});
98100
}
99101

102+
export function webPubSub(options: WebPubSubOutputOptions): WebPubSubOutput {
103+
return addOutputBindingName({
104+
...options,
105+
type: 'webPubSub',
106+
});
107+
}
108+
100109
export function generic(options: GenericOutputOptions): FunctionOutput {
101110
return addOutputBindingName(options);
102111
}

src/trigger.ts

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import {
2626
TimerTriggerOptions,
2727
WarmupTrigger,
2828
WarmupTriggerOptions,
29+
WebPubSubTrigger,
30+
WebPubSubTriggerOptions,
2931
} from '@azure/functions';
3032
import { addBindingName } from './addBindingName';
3133

@@ -108,6 +110,13 @@ export function sql(options: SqlTriggerOptions): SqlTrigger {
108110
});
109111
}
110112

113+
export function webPubSub(options: WebPubSubTriggerOptions): WebPubSubTrigger {
114+
return addTriggerBindingName({
115+
...options,
116+
type: 'webPubSubTrigger',
117+
});
118+
}
119+
111120
export function generic(options: GenericTriggerOptions): FunctionTrigger {
112121
return addTriggerBindingName(options);
113122
}

types/InvocationContext.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ServiceBusQueueOutput, ServiceBusTopicOutput } from './serviceBus';
1010
import { SqlInput, SqlOutput } from './sql';
1111
import { StorageBlobInput, StorageBlobOutput, StorageQueueOutput } from './storage';
1212
import { TableInput, TableOutput } from './table';
13+
import { WebPubSubOutput } from './webpubsub';
1314

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

220+
/**
221+
* Set a secondary Web PubSub output for this invocation
222+
* @output the configuration object for this Web PubSub output
223+
* @message the output message(s) value
224+
*/
225+
set(output: WebPubSubOutput, messages: unknown): void;
226+
219227
/**
220228
* Set a secondary generic output for this invocation
221229
* @outputOrName the configuration object or name for this output

types/app.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { SqlFunctionOptions } from './sql';
1212
import { StorageBlobFunctionOptions, StorageQueueFunctionOptions } from './storage';
1313
import { TimerFunctionOptions } from './timer';
1414
import { WarmupFunctionOptions } from './warmup';
15+
import { WebPubSubFunctionOptions } from './webpubsub';
1516

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

184+
/**
185+
* Registers a WebPubSub function in your app that will be triggered by WebPubSub events
186+
* @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
187+
* @param options Configuration options describing the inputs, outputs, and handler for this function
188+
*/
189+
export function webPubSub(name: string, options: WebPubSubFunctionOptions): void;
190+
183191
export * as hook from './hooks/registerHook';

types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export * from './table';
2626
export * from './timer';
2727
export * as trigger from './trigger';
2828
export * from './warmup';
29+
export * from './webpubsub';
2930

3031
/**
3132
* Void if no `return` output is registered

types/input.d.ts

+16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import { FunctionInput } from './index';
77
import { SqlInput, SqlInputOptions } from './sql';
88
import { StorageBlobInput, StorageBlobInputOptions } from './storage';
99
import { TableInput, TableInputOptions } from './table';
10+
import {
11+
WebPubSubConnectionInput,
12+
WebPubSubConnectionInputOptions,
13+
WebPubSubContextInput,
14+
WebPubSubContextInputOptions,
15+
} from './webpubsub';
1016

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

37+
/**
38+
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-web-pubsub-input?pivots=programming-language-javascript)
39+
*/
40+
export function webPubSubConnection(options: WebPubSubConnectionInputOptions): WebPubSubConnectionInput;
41+
42+
/**
43+
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-web-pubsub-input?pivots=programming-language-javascript)
44+
*/
45+
export function webPubSubContext(options: WebPubSubContextInputOptions): WebPubSubContextInput;
46+
3147
/**
3248
* A generic option that can be used for any input type
3349
* Use this method if your desired input type does not already have its own method

types/output.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { SqlOutput, SqlOutputOptions } from './sql';
1717
import { StorageBlobOutput, StorageBlobOutputOptions, StorageQueueOutput, StorageQueueOutputOptions } from './storage';
1818
import { TableOutput, TableOutputOptions } from './table';
19+
import { WebPubSubOutput, WebPubSubOutputOptions } from './webpubsub';
1920

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

71+
/**
72+
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-web-pubsub-output?pivots=programming-language-javascript)
73+
*/
74+
export function webPubSub(options: WebPubSubOutputOptions): WebPubSubOutput;
75+
7076
/**
7177
* A generic option that can be used for any output type
7278
* Use this method if your desired output type does not already have its own method

types/trigger.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from './storage';
2323
import { TimerTrigger, TimerTriggerOptions } from './timer';
2424
import { WarmupTrigger, WarmupTriggerOptions } from './warmup';
25-
25+
import { WebPubSubTrigger, WebPubSubTriggerOptions } from './webpubsub';
2626
/**
2727
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-http-webhook-trigger?&pivots=programming-language-javascript)
2828
*/
@@ -78,6 +78,11 @@ export function warmup(options: WarmupTriggerOptions): WarmupTrigger;
7878
*/
7979
export function sql(options: SqlTriggerOptions): SqlTrigger;
8080

81+
/**
82+
* [Link to docs and examples](https://docs.microsoft.com/azure/azure-functions/functions-bindings-web-pubsub-trigger?pivots=programming-language-javascript)
83+
*/
84+
export function webPubSub(options: WebPubSubTriggerOptions): WebPubSubTrigger;
85+
8186
/**
8287
* A generic option that can be used for any trigger type
8388
* Use this method if your desired trigger type does not already have its own method

types/webpubsub.d.ts

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import { FunctionInput, FunctionOptions, FunctionOutput, FunctionResult, FunctionTrigger } from './index';
5+
import { InvocationContext } from './InvocationContext';
6+
7+
export type WebPubSubHandler = (message: unknown, context: InvocationContext) => FunctionResult;
8+
9+
export interface WebPubSubFunctionOptions extends WebPubSubTriggerOptions, Partial<FunctionOptions> {
10+
handler: WebPubSubHandler;
11+
12+
trigger?: WebPubSubTrigger;
13+
}
14+
15+
export interface WebPubSubTriggerOptions {
16+
/**
17+
* Required - The variable name used in function code for the parameter that receives the event data
18+
*/
19+
name: string;
20+
21+
/**
22+
* Required - The name of the hub to which the function is bound
23+
*/
24+
hub: string;
25+
26+
/**
27+
* Required - The type of event to which the function should respond
28+
* Must be either 'user' or 'system'
29+
*/
30+
eventType: 'user' | 'system';
31+
32+
/**
33+
* Required - The name of the event to which the function should respond
34+
* For system event type: 'connect', 'connected', or 'disconnected'
35+
* For user-defined subprotocols: 'message'
36+
* For system supported subprotocol json.webpubsub.azure.v1: user-defined event name
37+
*/
38+
eventName: string;
39+
40+
/**
41+
* Optional - Specifies which client protocol can trigger the Web PubSub trigger functions
42+
* Default is 'all'
43+
*/
44+
clientProtocols?: 'all' | 'webPubSub' | 'mqtt';
45+
46+
/**
47+
* Optional - The name of an app setting or setting collection that specifies the upstream Azure Web PubSub service
48+
* Used for signature validation
49+
* Defaults to "WebPubSubConnectionString" if not specified
50+
* Set to null to disable validation
51+
*/
52+
connection?: string | null;
53+
}
54+
55+
export type WebPubSubTrigger = FunctionTrigger & WebPubSubTriggerOptions;
56+
57+
export interface WebPubSubConnectionInputOptions {
58+
/**
59+
* Required - Variable name used in function code for input connection binding object.
60+
*/
61+
name: string;
62+
63+
/**
64+
* Required - The name of the Web PubSub hub for the function to be triggered.
65+
* Can be set in the attribute (higher priority) or in app settings as a global value.
66+
*/
67+
hub: string;
68+
69+
/**
70+
* Optional - The value of the user identifier claim to be set in the access key token.
71+
*/
72+
userId?: string;
73+
74+
/**
75+
* Optional - The client protocol type.
76+
* Valid values are 'default' and 'mqtt'.
77+
* For MQTT clients, you must set it to 'mqtt'.
78+
* For other clients, you can omit the property or set it to 'default'.
79+
*/
80+
clientProtocol?: 'default' | 'mqtt';
81+
82+
/**
83+
* Optional - The name of the app setting that contains the Web PubSub Service connection string.
84+
* Defaults to "WebPubSubConnectionString".
85+
*/
86+
connection?: string;
87+
}
88+
export type WebPubSubConnectionInput = FunctionInput & WebPubSubConnectionInputOptions;
89+
90+
export interface WebPubSubContextInputOptions {
91+
/**
92+
* Required - Variable name used in function code for input Web PubSub request.
93+
*/
94+
name: string;
95+
96+
/**
97+
* Optional - The name of an app settings or setting collection that specifies the upstream Azure Web PubSub service.
98+
* The value is used for Abuse Protection and Signature validation.
99+
* The value is auto resolved with "WebPubSubConnectionString" by default.
100+
* Null means the validation isn't needed and always succeeds.
101+
*/
102+
connection?: string;
103+
}
104+
export type WebPubSubContextInput = FunctionInput & WebPubSubContextInputOptions;
105+
106+
export interface WebPubSubOutputOptions {
107+
/**
108+
* Required - Variable name used in function code for output binding object.
109+
*/
110+
name: string;
111+
112+
/**
113+
* Required - The name of the hub to which the function is bound.
114+
* Can be set in the attribute (higher priority) or in app settings as a global value.
115+
*/
116+
hub: string;
117+
118+
/**
119+
* Optional - The name of the app setting that contains the Web PubSub Service connection string.
120+
* Defaults to "WebPubSubConnectionString".
121+
*/
122+
connection?: string;
123+
}
124+
export type WebPubSubOutput = FunctionOutput & WebPubSubOutputOptions;

0 commit comments

Comments
 (0)