Skip to content

Commit f29ca9d

Browse files
Introduce DefaultServiceOptions, that are applied to every service bind to the endpoint. (#572)
* Moved discovery and components to endpoint, these are relevant only there. * Modified `EndpointBuilder`, now it's really used as a builder and has a final step `build()`. `GenericHandler` now uses the built `Endpoint`, instead of dealing with `EndpointBuilder`. This simplified the implementation of these new global options, and allowed to remove a bit of code as well. * Unit tests and example of the new config options. * Pruned some old unused code
1 parent 2e5b1dc commit f29ca9d

File tree

15 files changed

+583
-400
lines changed

15 files changed

+583
-400
lines changed

DEVELOPMENT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ E2E tests run automatically with
4040
## Re-generating the discovery manifest
4141

4242
```shell
43-
npx --package=json-schema-to-typescript json2ts endpoint_manifest_schema.json packages/restate-sdk/src/types/discovery.ts
43+
npx --package=json-schema-to-typescript json2ts endpoint_manifest_schema.json packages/restate-sdk/src/endpoint/discovery.ts
4444
```
4545

4646
## Releasing the package

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ const greeter = service({
4949

5050
export type Greeter = typeof greeter;
5151

52-
endpoint().bind(greeter).listen();
52+
endpoint()
53+
.bind(greeter)
54+
.defaultServiceOptions({
55+
// You can configure default service options that will be applied to every service.
56+
journalRetention: { days: 10 },
57+
})
58+
.listen();

packages/restate-sdk-testcontainers/src/public_api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ export type {
2020
Serde,
2121
RestateEndpoint,
2222
RestateEndpointBase,
23+
DefaultServiceOptions,
2324
LoggerTransport,
2425
LogMetadata,
2526
LogSource,
2627
RestateLogLevel,
2728
LoggerContext,
2829
Request,
30+
ServiceOptions,
31+
ObjectOptions,
32+
WorkflowOptions,
33+
TerminalError,
34+
RestateError,
2935
} from "@restatedev/restate-sdk";

packages/restate-sdk/src/common_api.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ export type {
100100
WorkflowDefinition,
101101
} from "@restatedev/restate-sdk-core";
102102

103-
export type { RestateEndpoint, RestateEndpointBase } from "./endpoint.js";
103+
export type {
104+
RestateEndpoint,
105+
RestateEndpointBase,
106+
DefaultServiceOptions,
107+
} from "./endpoint.js";
108+
104109
export {
105110
/**
106111
* @deprecated YOU MUST NOT USE THIS TYPE

packages/restate-sdk/src/endpoint.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ import type {
1616
WorkflowDefinition,
1717
} from "@restatedev/restate-sdk-core";
1818
import type { LoggerTransport } from "./logging/logger_transport.js";
19+
import type {
20+
ObjectOptions,
21+
ServiceOptions,
22+
WorkflowOptions,
23+
} from "./types/rpc.js";
24+
25+
export type DefaultServiceOptions = ServiceOptions &
26+
ObjectOptions &
27+
WorkflowOptions;
1928

2029
export interface RestateEndpointBase<E> {
2130
/**
@@ -41,6 +50,15 @@ export interface RestateEndpointBase<E> {
4150
*/
4251
withIdentityV1(...keys: string[]): E;
4352

53+
/**
54+
* Set default service options that will be used by all services bind to this endpoint.
55+
*
56+
* Options can be overridden on each service/handler.
57+
*
58+
* @param options
59+
*/
60+
defaultServiceOptions(options: DefaultServiceOptions): E;
61+
4462
/**
4563
* Replace the default console-based {@link LoggerTransport}
4664
* @param logger
@@ -68,8 +86,9 @@ export interface RestateEndpointBase<E> {
6886
/**
6987
* RestateEndpoint encapsulates all the Restate services served by this endpoint.
7088
*
71-
* A RestateEndpoint can either be served either as HTTP2 server, using the methods {@link listen} or {@link http2Handler},
72-
* or as Lambda, using the method {@link lambdaHandler}.
89+
* A RestateEndpoint can either be served as HTTP2 server, using the methods {@link RestateEndpoint.listen} or {@link RestateEndpoint.http2Handler}.
90+
*
91+
* For Lambda, check {@link LambdaEndpoint}
7392
*
7493
* @example
7594
* A typical endpoint served as HTTP server would look like this:
@@ -81,16 +100,6 @@ export interface RestateEndpointBase<E> {
81100
* .bind(myService)
82101
* .listen(8000);
83102
* ```
84-
* @example
85-
* A typical endpoint served as AWS Lambda would look like this:
86-
* ```
87-
* import * as restate from "@restatedev/restate-sdk/lambda";
88-
*
89-
* export const handler = restate
90-
* .endpoint()
91-
* .bind(myService)
92-
* .handler();
93-
* ```
94103
*/
95104
export interface RestateEndpoint extends RestateEndpointBase<RestateEndpoint> {
96105
/**
@@ -110,15 +119,15 @@ export interface RestateEndpoint extends RestateEndpointBase<RestateEndpoint> {
110119
* httpServer.listen(port);
111120
* ```
112121
*
113-
* If you need to manually control the server lifecycle, we suggest to manually instantiate the http2 server and use {@link http2Handler}.
122+
* If you need to manually control the server lifecycle, we suggest to manually instantiate the http2 server and use {@link RestateEndpoint.http2Handler}.
114123
*
115124
* @param port The port to listen at. May be undefined (see above).
116125
* @returns a Promise that resolves with the bound port, or rejects with a failure otherwise.
117126
*/
118127
listen(port?: number): Promise<number>;
119128

120129
/**
121-
* Returns an http2 server handler. See {@link listen} for more details.
130+
* Returns an http2 server handler. See {@link RestateEndpoint.listen} for more details.
122131
*/
123132
http2Handler(): (
124133
request: Http2ServerRequest,

packages/restate-sdk/src/types/components.ts renamed to packages/restate-sdk/src/endpoint/components.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import type {
1919
ObjectOptions,
2020
ServiceOptions,
2121
WorkflowOptions,
22-
} from "./rpc.js";
23-
import { HandlerKind } from "./rpc.js";
22+
} from "../types/rpc.js";
23+
import { HandlerKind } from "../types/rpc.js";
2424
import { millisOrDurationToMillis } from "@restatedev/restate-sdk-core";
2525

2626
//

0 commit comments

Comments
 (0)