-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5a2570
commit 43902f3
Showing
2 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2023 - Restate Software, Inc., Restate GmbH | ||
* | ||
* This file is part of the Restate SDK for Node.js/TypeScript, | ||
* which is released under the MIT license. | ||
* | ||
* You can find a copy of the license in file LICENSE in the root | ||
* directory of this repository or package, or at | ||
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE | ||
*/ | ||
|
||
/* eslint-disable no-console */ | ||
|
||
/* | ||
* A simple example program using the Restate's event handlers. | ||
*/ | ||
|
||
import * as restate from "../src/public_api"; | ||
|
||
const registration = async (ctx: restate.RpcContext, event: restate.Event) => { | ||
// store in state the user's information as coming from the registeration event | ||
const { name } = event.json<{ name: string }>(); | ||
ctx.set("name", name); | ||
}; | ||
|
||
const email = async (ctx: restate.RpcContext, event: restate.Event) => { | ||
// store in state the user's information as coming from the registeration event | ||
const { email } = event.json<{ email: string }>(); | ||
ctx.set("email", email); | ||
}; | ||
|
||
type UserProfile = { | ||
id: string; | ||
name: string; | ||
email: string; | ||
}; | ||
|
||
const get = async ( | ||
ctx: restate.RpcContext, | ||
id: string | ||
): Promise<UserProfile> => { | ||
return { | ||
id, | ||
name: (await ctx.get<string>("name")) ?? "", | ||
email: (await ctx.get<string>("email")) ?? "", | ||
}; | ||
}; | ||
|
||
const profile = restate.keyedRouter({ | ||
registration: restate.keyedEventHandler(registration), | ||
email: restate.keyedEventHandler(email), | ||
get, | ||
}); | ||
|
||
// restate server | ||
restate.createServer().bindKeyedRouter("profile", profile).listen(8080); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters