Skip to content
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

Add support for SDK types #203

Closed
wants to merge 13 commits into from
9 changes: 9 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ function convertToGenericOptions<T extends Omit<FunctionOptions, 'trigger'> & Pa
};
}

function addDeferredBindingsFlag(triggerType: string): { [key: string]: string } {
if (triggerType === 'blobTrigger') {
return { supportsDeferredBinding: 'true' };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should provide a convenient way for users to turn this off and on instead of hard-coding a value like this. For example in my http stream work, I added an app.setup method where they can enable it. Yours might look different though because I think sdk type bindings can be turned on/off for each individual function whereas for http streams I had to do all-or-nothing for the whole app

}

return { supportsDeferredBinding: 'false' };
}

export function get(name: string, optionsOrHandler: HttpMethodFunctionOptions | HttpHandler): void {
http(name, convertToHttpOptions(optionsOrHandler, 'GET'));
}
Expand Down Expand Up @@ -152,6 +160,7 @@ export function generic(name: string, options: GenericFunctionOptions): void {
...trigger,
direction: 'in',
type: isTrigger(trigger.type) ? trigger.type : trigger.type + 'Trigger',
properties: addDeferredBindingsFlag(options.trigger.type),
};

if (options.extraInputs) {
Expand Down
3 changes: 3 additions & 0 deletions src/converters/fromRpcTypedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { RpcTypedData } from '@azure/functions-core';
import { HttpRequest } from '../http/HttpRequest';
import { ConnectionInfo } from '../utils/ConnectionInfo';
import { isDefined } from '../utils/nonNull';

export function fromRpcTypedData(data: RpcTypedData | null | undefined): unknown {
Expand Down Expand Up @@ -30,6 +31,8 @@ export function fromRpcTypedData(data: RpcTypedData | null | undefined): unknown
return data.collectionDouble.double;
} else if (data.collectionSint64 && isDefined(data.collectionSint64.sint64)) {
return data.collectionSint64.sint64;
} else if (data.modelBindingData && isDefined(data.modelBindingData.content)) {
return new ConnectionInfo(data.modelBindingData);
} else {
return undefined;
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export * as input from './input';
export { InvocationContext } from './InvocationContext';
export * as output from './output';
export * as trigger from './trigger';
export { ConnectionInfo } from './utils/ConnectionInfo';
export { Disposable } from './utils/Disposable';
26 changes: 26 additions & 0 deletions src/utils/ConnectionInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.

import { ModelBindingData } from '@azure/functions-core';

export interface ConnectionInfoContent {
Connection: string;
ContainerName: string;
BlobName: string;
}

export class ConnectionInfo {
version: string;
source: string;
contentType: string;
content: ConnectionInfoContent | undefined;

constructor(modelBindingData: ModelBindingData) {
this.version = modelBindingData.version as string;
this.source = modelBindingData.source as string;
this.contentType = modelBindingData.contentType as string;
this.content = modelBindingData.content
? (JSON.parse(modelBindingData.content.toString()) as ConnectionInfoContent)
: undefined;
}
}
27 changes: 21 additions & 6 deletions types-core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,18 @@ declare module '@azure/functions-core' {
direction?: RpcBindingDirection | null;

dataType?: RpcBindingDataType | null;

properties?: RpcBindingProperties | null;
}

type RpcBindingDirection = 'in' | 'out' | 'inout';

type RpcBindingDataType = 'undefined' | 'string' | 'binary' | 'stream';

interface RpcBindingProperties {
supportsDeferredBinding?: 'true' | 'false' | null;
}

interface RpcRetryOptions {
maxRetryCount?: number | null;

Expand Down Expand Up @@ -395,24 +401,33 @@ declare module '@azure/functions-core' {
collectionDouble?: RpcCollectionDouble | null;

collectionSint64?: RpcCollectionSInt64 | null;

modelBindingData?: ModelBindingData | null;
}

interface RpcCollectionSInt64 {
sint64?: (number | Long)[] | null;
interface RpcCollectionBytes {
bytes?: Uint8Array[] | null;
}

interface RpcCollectionString {
string?: string[] | null;
}

interface RpcCollectionBytes {
bytes?: Uint8Array[] | null;
}

interface RpcCollectionDouble {
double?: number[] | null;
}

interface RpcCollectionSInt64 {
sint64?: (number | Long)[] | null;
}

interface ModelBindingData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure you sync these changes to the worker repo's core types, which is the source of truth: https://github.com/Azure/azure-functions-nodejs-worker/blob/v3.x/types-core/index.d.ts

Also, not sure if you were aware, but most of the "Rpc" types are auto-generated in the worker repo based on the protobuf api contract. When you run npm run build in the worker repo the rpc types are output to dist/azure-functions-language-worker-protobuf/src/rpc.d.ts. For the core api, we copy the minimal types we need and clean it up a tiny bit (remove unnecessary comments, usually rename it from IModelBindingData to RpcModelBindingData, etc.)

content?: Buffer | null;
contentType?: string | null;
source?: string | null;
version?: string | null;
}

interface RpcInvocationRequest {
invocationId?: string | null;

Expand Down
24 changes: 24 additions & 0 deletions types/connectionInfo.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.

export interface ModelBindingData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to expose ModelBindingData/ConnectionInfo outside this package necessarily. These seem more like internal properties that we should handle, not the user. Even in your example, the user's code is only using info.content instead of any properties directly on info.

For example, we might want to check info.version internally and throw an error if the major version doesn't match what we expect. I believe that version property is a schema version for the content property, so If the major version is different the ConnectionInfoContent type we're providing to the user probably won't be right and an error would be appropriate IMO

content?: Buffer | null;
contentType?: string | null;
source?: string | null;
version?: string | null;
}

export interface ConnectionInfoContent {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect this to be in types/storage.d.ts and have more storage/blob-specific naming

Connection: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically convert .NET casing to more standard Node.js camel casing (for example https://github.com/Azure/azure-functions-nodejs-library/blob/v4.x/src/converters/toCamelCase.ts)

ContainerName: string;
BlobName: string;
}

export declare class ConnectionInfo {
version: string;
source: string;
contentType: string;
content: ConnectionInfoContent | undefined;

constructor(modelBindingData: ModelBindingData);
}
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { InvocationContext } from './InvocationContext';

export * as app from './app';
export * from './connectionInfo';
export * from './cosmosDB';
export * from './cosmosDB.v3';
export * from './cosmosDB.v4';
Expand Down