From 968e5ffc1c646eba5dcde794242feb33e7c097c6 Mon Sep 17 00:00:00 2001 From: Giselle van Dongen Date: Thu, 1 Feb 2024 10:57:01 +0100 Subject: [PATCH] Use more concise handler API (#291) --- docs/services/sdk/kafka.mdx | 27 ++++++++++++++------------- docs/services/sdk/overview.mdx | 33 ++++++++++++++++----------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/docs/services/sdk/kafka.mdx b/docs/services/sdk/kafka.mdx index df90f4b0..c41f0135 100644 --- a/docs/services/sdk/kafka.mdx +++ b/docs/services/sdk/kafka.mdx @@ -37,22 +37,23 @@ You can handle events within keyed routers, where the key of the Kafka record ma ```typescript import * as restate from "@restatedev/restate-sdk"; -const eventHandler = async (ctx: restate.RpcContext, event: restate.Event) => { - // Extract event payload as json - const { myData } = event.json<{ myData: string }>(); - // OR extract event payload as raw bytes and deserialize it - // using the format of your choice (e.g. Avro, Protobuf, ...) - const bodyBuffer = event.body(); - - // --- Business logic -}; - const router = restate.keyedRouter({ - eventHandler: restate.keyedEventHandler(eventHandler), - // Other rpc or event handlers + eventHandler: restate.keyedEventHandler(async (ctx: restate.RpcContext, event: restate.Event) => { + // Extract event payload as json + const { myData } = event.json<{ myData: string }>(); + // OR extract event payload as raw bytes and deserialize it + // using the format of your choice (e.g. Avro, Protobuf, ...) + const bodyBuffer = event.body(); + + // --- Business logic), + // Other rpc or event handlers + }; }); -restate.createServer().bindKeyedRouter("myRouter", router).listen(); +restate + .createServer() + .bindKeyedRouter("myRouter", router) + .listen(); ``` The Kafka record key must be a valid UTF-8 string, otherwise Restate won't be able to deliver the event to the service. diff --git a/docs/services/sdk/overview.mdx b/docs/services/sdk/overview.mdx index 580e516b..34bfa1e2 100644 --- a/docs/services/sdk/overview.mdx +++ b/docs/services/sdk/overview.mdx @@ -111,30 +111,29 @@ This example shows a greeter service comprising two methods: - `countGreetings`: maintains a record of the number of times it has received a request for a given name. ```typescript -const doGreet = async (ctx: restate.RpcContext, name: string) => { - return `Hello ${name}!`; -}; - -const doCountGreetings = async (ctx: restate.RpcContext, name: string) => { - // Retrieve state; number of times this name was seen - let seen = await ctx.get("seen") || 0; - seen += 1; - // Set the incremented counter as the new state - ctx.set("seen", seen); - - return `Hello ${name} for the ${seen}th time!` -} +const router = restate.keyedRouter({ + greet: async (ctx: restate.RpcContext, name: string) => { + return `Hello ${name}!`; + }, + countGreetings: async (ctx: restate.RpcContext, name: string) => { + // Retrieve state; number of times this name was seen + let seen = await ctx.get("seen") || 0; + seen += 1; + // Set the incremented counter as the new state + ctx.set("seen", seen); + + return `Hello ${name} for the ${seen}th time!` + } +}) // Create the Restate server to accept requests restate .createServer() - .bindKeyedRouter("greeter", restate.keyedRouter({ - greet: sayHello, countGreetings: doCountGreetings - })) + .bindKeyedRouter("greeter", router) .listen(); ``` -The app logic is implemented inside the `doGreet` and `doCountGreetings` functions. +The app logic is implemented inside the `greet` and `countGreetings` functions. The `restate.RpcContext` is supplied as the first argument. The [Restate context](/services/sdk/restate-context) enables interaction with Restate (call other methods, retrieve state, etc.).