Skip to content

Commit 22c6738

Browse files
authored
Create handler Alias (#577)
* Switch to 1.8.0 * Re-export common shortcuts for handlers * Add common shortcuts for handlers.*.handler * Apply lint rules
1 parent 221ad56 commit 22c6738

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

package-lock.json

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/restate-sdk-examples/src/object.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@
99
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
1010
*/
1111

12-
import * as restate from "@restatedev/restate-sdk";
12+
import {
13+
createObjectHandler,
14+
createObjectSharedHandler,
15+
object,
16+
type ObjectContext,
17+
type ObjectSharedContext,
18+
serde,
19+
serve,
20+
} from "@restatedev/restate-sdk";
1321

14-
export const counter = restate.object({
22+
export const counter = object({
1523
name: "counter",
1624
handlers: {
1725
/**
1826
* Add amount to the currently stored count
1927
*/
20-
add: async (ctx: restate.ObjectContext, amount: number) => {
28+
add: async (ctx: ObjectContext, amount: number) => {
2129
const current = await ctx.get<number>("count");
2230
const updated = (current ?? 0) + amount;
2331
ctx.set("count", updated);
@@ -31,8 +39,8 @@ export const counter = restate.object({
3139
* These handlers can be executed concurrently to the exclusive handlers (i.e. add)
3240
* But they can not modify the state (set is missing from the ctx).
3341
*/
34-
current: restate.handlers.object.shared(
35-
async (ctx: restate.ObjectSharedContext): Promise<number> => {
42+
current: createObjectSharedHandler(
43+
async (ctx: ObjectSharedContext): Promise<number> => {
3644
return (await ctx.get("count")) ?? 0;
3745
}
3846
),
@@ -44,12 +52,12 @@ export const counter = restate.object({
4452
* to call that handler with binary data, you can use the following curl command:
4553
* curl -X POST -H "Content-Type: application/octet-stream" --data-binary 'hello' ${RESTATE_INGRESS_URL}/counter/mykey/binary
4654
*/
47-
binary: restate.handlers.object.exclusive(
55+
binary: createObjectHandler(
4856
{
49-
input: restate.serde.binary,
50-
output: restate.serde.binary,
57+
input: serde.binary,
58+
output: serde.binary,
5159
},
52-
async (ctx: restate.ObjectContext, data: Uint8Array) => {
60+
async (ctx: ObjectContext, data: Uint8Array) => {
5361
// console.log("Received binary data", data);
5462
return data;
5563
}
@@ -59,4 +67,4 @@ export const counter = restate.object({
5967

6068
export type Counter = typeof counter;
6169

62-
restate.serve({ services: [counter] });
70+
serve({ services: [counter] });

packages/restate-sdk-examples/src/zod_greeter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const Greeting = z.object({
2020
const greeter = restate.service({
2121
name: "greeter",
2222
handlers: {
23-
greet: restate.handlers.handler(
23+
greet: restate.createServiceHandler(
2424
{
2525
input: serde.zod(Greeting),
2626
output: serde.zod(z.string()),

packages/restate-sdk/src/common_api.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,13 @@ export type {
124124
LogSource,
125125
} from "./logging/logger_transport.js";
126126
export type { EndpointOptions } from "./endpoint/types.js";
127+
128+
// re-exporting createHandler shortcuts
129+
130+
import { handlers } from "./types/rpc.js";
131+
132+
export const createServiceHandler = handlers.handler;
133+
export const createObjectHandler = handlers.object.exclusive;
134+
export const createObjectSharedHandler = handlers.object.shared;
135+
export const createWorkflowHandler = handlers.workflow.workflow;
136+
export const createWorkflowSharedHandler = handlers.workflow.shared;

0 commit comments

Comments
 (0)