-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialization guide for Typescript (#448)
* Serialization guide for Typescript * Update all the other snippets to use the new api for send
- Loading branch information
1 parent
7c31a8a
commit 4fe82f7
Showing
11 changed files
with
959 additions
and
886 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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,46 @@ | ||
import * as restate from "@restatedev/restate-sdk"; | ||
import {Context, WorkflowContext} from "@restatedev/restate-sdk"; | ||
|
||
// <start_service_definition> | ||
const myService = restate.service({ | ||
name: "MyService", | ||
handlers: { | ||
myHandler: restate.handlers.handler({ | ||
// Set the input serde here | ||
input: restate.serde.binary, | ||
// Set the output serde here | ||
output: restate.serde.binary | ||
}, async (ctx: Context, data: Uint8Array): Promise<Uint8Array> => { | ||
// Process the request | ||
return data; | ||
}), | ||
}, | ||
}); | ||
// <end_service_definition> | ||
|
||
let ctx: WorkflowContext = undefined as unknown as WorkflowContext; | ||
let input = new Uint8Array(); | ||
|
||
// <start_client> | ||
ctx.serviceClient(myService) | ||
.myHandler( | ||
input, | ||
restate.rpc.opts({ | ||
input: restate.serde.binary, | ||
output: restate.serde.binary | ||
}) | ||
); | ||
// <end_client> | ||
|
||
// <start_state> | ||
ctx.get("my-binary-data", restate.serde.binary); | ||
ctx.set("my-binary-data", new Uint8Array(), restate.serde.binary); | ||
// <end_state> | ||
|
||
// <start_awakeable> | ||
ctx.awakeable(restate.serde.binary) | ||
// <end_awakeable> | ||
|
||
// <start_run> | ||
ctx.run("my-side-effect", () => new Uint8Array(), { serde: restate.serde.binary }); | ||
// <end_run> |
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
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
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
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,43 @@ | ||
--- | ||
sidebar_position: 9 | ||
description: "How to serialize and deserialize data with the Restate SDK." | ||
--- | ||
|
||
# Serialization | ||
|
||
Restate sends data over the network for storing state, journaling actions, awakeables, etc. | ||
|
||
By default, Typescript SDK uses the built-in [JSON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) | ||
support to perform (de)serialization, but it is possible to override this behavior using the `Serde` interface. | ||
|
||
For example, to customize the `Serde` to use for the handler input and output: | ||
|
||
```typescript | ||
CODE_LOAD::ts/src/develop/serialization.ts#service_definition | ||
``` | ||
|
||
To customize the serde to use on requests: | ||
|
||
```typescript | ||
CODE_LOAD::ts/src/develop/serialization.ts#client | ||
``` | ||
|
||
When sending a request to a handler configured with custom serde(s) you always need to manually specify them, because the client does not automatically infer what serde(s) should be used. | ||
|
||
To customize the serde to use for state: | ||
|
||
```typescript | ||
CODE_LOAD::ts/src/develop/serialization.ts#state | ||
``` | ||
|
||
For awakeables: | ||
|
||
```typescript | ||
CODE_LOAD::ts/src/develop/serialization.ts#awakeable | ||
``` | ||
|
||
For `run`: | ||
|
||
```typescript | ||
CODE_LOAD::ts/src/develop/serialization.ts#run | ||
``` |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"RESTATE_VERSION": "1.0", | ||
"TYPESCRIPT_SDK_VERSION": "1.1.2", | ||
"TYPESCRIPT_SDK_VERSION": "1.3.0", | ||
"JAVA_SDK_VERSION": "1.0.1" | ||
} |