Azure / azure-functions-durable-js

JavaScript library for using the Durable Functions bindings
https://www.npmjs.com/package/durable-functions
MIT License
128 stars 46 forks source link

Add convenience methods to register durable client functions #536

Closed hossam-nasr closed 9 months ago

hossam-nasr commented 11 months ago

Partially resolves #414. This PR adds a namespace client, under df.app, which provides methods to quickly register durable client functions. These functions receive three arguments instead of the regular two: the trigger, the durable client, and the InvocationContext, in this order. This allows users to quickly and easily register durable client functions, without having to manually configure the durableClient input binding and calling the getClient method. This PR starts out with three methods: http to register HTTP-triggered durable client functions, timer, and generic. These behave exactly the same as the equivalent from @azure/functions, except accepting the additional DurableClient argument.

Example usage:

HTTP-triggered client function

const httpStart: HttpDurableClientHandler = async (
    request: HttpRequest,
    client: DurableClient,
    context: InvocationContext
): Promise<HttpResponse> => {
    const body: unknown = await request.json();
    const instanceId: string = await client.startNew(request.params.orchestratorName, {
        input: body,
    });

    context.log(`Started orchestration with ID = '${instanceId}'.`);

    return client.createCheckStatusResponse(request, instanceId);
};

df.app.client.http("httpStart", {
    route: "orchestrators/{orchestratorName}",
    handler: httpStart,
});

Timer-triggered client function

const timerStart: TimerDurableClientHandler = async (
    _timer: Timer,
    client: DurableClient,
    context: InvocationContext
): Promise<void> => {
    const instanceId: string = await client.startNew("helloSequence");
    context.log(`Started orchestration with ID = '${instanceId}'.`);
};

df.app.client.timer("timerStart", {
    schedule: "0 */1 * * * *",
    handler: timerStart,
});

Storage Queue-triggered client function (using generic)

const storageQueueStart: DurableClientHandler = async (
    message: string,
    client: DurableClient,
    context: InvocationContext
): Promise<void> => {
    const instanceId: string = await client.startNew(message);
    context.log(`Started orchestration with ID = '${instanceId}'.`);
};

df.app.client.generic("storageQueueStart", {
    trigger: trigger.storageQueue({ queueName: "orchestrations", connection: "StorageConnString" }),
    handler: storageQueueStart,  
})