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

Typescript Support for Functions v4? #492

Open
MarekLani opened this issue Jun 20, 2024 · 1 comment
Open

Typescript Support for Functions v4? #492

MarekLani opened this issue Jun 20, 2024 · 1 comment

Comments

@MarekLani
Copy link

Please is there any plan to add support for Typescript/Node functions in v4 programming model?

@SchulteMarkus
Copy link

v4 can be used, but there should be updated documentation, and best case a trigger in https://github.com/Azure/azure-functions-nodejs-library/blob/v4.x/src/app.ts

I'm just dropping my "solution", which is inspired by https://github.com/Azure/azure-functions-kafka-extension/tree/dev/samples/typescript

import { app, FunctionTrigger, GenericFunctionOptions, InvocationContext } from '@azure/functions';

const kafkaFunctionTrigger: FunctionTrigger = {
  type: 'kafkaTrigger',
  name: 'TransferrerKafkaFunctionTrigger',
  direction: 'in',
  topic: '%EVENT_HUB_NAME%',
  brokerList: '%BrokerList%',
  username: '$ConnectionString',
  password: 'EVENT_HUB_CONNECTION_STRING',
  protocol: 'saslSsl',
  authenticationMode: 'plain',
  consumerGroup: 'a-consumer-group',
  dataType: 'string',
};
const transferrerKafkaFunctionOptions: GenericFunctionOptions = {
  handler: eventHubAsKafkaConsumer,
  trigger: kafkaFunctionTrigger,
};

app.generic('eventHubAsKafkaConsumer', transferrerKafkaFunctionOptions);

// This is to describe the metadata of a Kafka event
class KafkaEvent {
  Offset: number;
  Partition: number;
  Topic: string;
  Timestamp: string;
  Value: string;

  constructor(metadata: any) {
    this.Offset = metadata.Offset;
    this.Partition = metadata.Partition;
    this.Topic = metadata.Topic;
    this.Timestamp = metadata.Timestamp;
    this.Value = metadata.Value;
  }
}

export async function eventHubAsKafkaConsumer(event_str: string, context: InvocationContext): Promise<void> {
  let event_obj = new KafkaEvent(eval(event_str));

  context.log('Event Offset: ' + event_obj.Offset);
  context.log('Event Partition: ' + event_obj.Partition);
  context.log('Event Topic: ' + event_obj.Topic);
  context.log('Event Timestamp: ' + event_obj.Timestamp);
  context.log('>>> Event Value via KAFKA (as string): ' + event_obj.Value);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants