Skip to content

Commit f9b99b6

Browse files
authored
Update DaprClient API (#458)
* Initial pass - DaprClient Signed-off-by: Shubham Sharma <[email protected]> * Actors pass Signed-off-by: Shubham Sharma <[email protected]> * Complete code refactor Signed-off-by: Shubham Sharma <[email protected]> * Fix unit tests Signed-off-by: Shubham Sharma <[email protected]> * Prettify Signed-off-by: Shubham Sharma <[email protected]> * Update e2e tests Signed-off-by: Shubham Sharma <[email protected]> * Prettify Signed-off-by: Shubham Sharma <[email protected]> * Refactor examples Signed-off-by: Shubham Sharma <[email protected]> * Refactor documentation Signed-off-by: Shubham Sharma <[email protected]> * Address comments Signed-off-by: Shubham Sharma <[email protected]> --------- Signed-off-by: Shubham Sharma <[email protected]>
1 parent c965ad2 commit f9b99b6

File tree

50 files changed

+482
-459
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+482
-459
lines changed

daprdocs/content/en/js-sdk-docs/js-actors/_index.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ import { CommunicationProtocolEnum, DaprClient, DaprServer } from "@dapr/dapr";
7979

8080
// Configure the actor runtime with the DaprClientOptions.
8181
const clientOptions = {
82+
daprHost: daprHost,
83+
daprPort: daprPort,
84+
communicationProtocol: CommunicationProtocolEnum.HTTP,
8285
actor: {
8386
actorIdleTimeout: "1h",
8487
actorScanInterval: "30s",
@@ -97,7 +100,7 @@ const clientOptions = {
97100
// Note, DaprServer creates a DaprClient internally, which needs to be configured with clientOptions.
98101
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, clientOptions);
99102

100-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.HTTP, clientOptions);
103+
const client = new DaprClient(clientOptions);
101104
```
102105

103106
## Registering Actors
@@ -136,7 +139,7 @@ import ParkingSensorInterface from "./ParkingSensorInterface";
136139
const daprHost = "127.0.0.1";
137140
const daprPort = "50000";
138141

139-
const client = new DaprClient(daprHost, daprPort);
142+
const client = new DaprClient({ daprHost, daprPort });
140143

141144
// Create a new actor builder. It can be used to create multiple actors of a type.
142145
const builder = new ActorProxyBuilder<ParkingSensorInterface>(ParkingSensorImpl, client);

daprdocs/content/en/js-sdk-docs/js-client/_index.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ const serverHost = "127.0.0.1"; // App Host of this Example Server
3535
const serverPort = "50051"; // App Port of this Example Server
3636

3737
// HTTP Example
38-
const client = new DaprClient(daprHost, daprPort);
38+
const client = new DaprClient({ daprHost, daprPort });
3939

4040
// GRPC Example
41-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
41+
const client = new DaprClient({ daprHost, daprPort, communicationProtocol: CommunicationProtocolEnum.GRPC });
4242
```
4343

4444
## Running
@@ -49,7 +49,7 @@ To run the examples, you can use two different protocols to interact with the Da
4949

5050
```typescript
5151
import { DaprClient } from "@dapr/dapr";
52-
const client = new DaprClient(daprHost, daprPort);
52+
const client = new DaprClient({ daprHost, daprPort });
5353
```
5454

5555
```bash
@@ -66,7 +66,7 @@ Since HTTP is the default, you will have to adapt the communication protocol to
6666

6767
```typescript
6868
import { DaprClient, CommunicationProtocol } from "@dapr/dapr";
69-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocol.GRPC);
69+
const client = new DaprClient({ daprHost, daprPort, communicationProtocol: CommunicationProtocol.GRPC });
7070
```
7171

7272
```bash
@@ -88,7 +88,12 @@ import { DaprClient, CommunicationProtocol } from "@dapr/dapr";
8888

8989
// Allow a body size of 10Mb to be used
9090
// The default is 4Mb
91-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocol.HTTP, { maxBodySizeMb: 10 });
91+
const client = new DaprClient({
92+
daprHost,
93+
daprPort,
94+
communicationProtocol: CommunicationProtocol.HTTP,
95+
maxBodySizeMb: 10,
96+
});
9297
```
9398

9499
### Proxying Requests
@@ -102,7 +107,7 @@ To perform gRPC proxying, simply create a proxy by calling the `client.proxy.cre
102107
```typescript
103108
// As always, create a client to our dapr sidecar
104109
// this client takes care of making sure the sidecar is started, that we can communicate, ...
105-
const clientSidecar = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
110+
const clientSidecar = new DaprClient({ daprHost, daprPort, communicationProtocol: CommunicationProtocol.GRPC });
106111

107112
// Create a Proxy that allows us to use our gRPC code
108113
const clientProxy = await clientSidecar.proxy.create<GreeterClient>(GreeterClient);
@@ -134,7 +139,7 @@ const daprHost = "127.0.0.1";
134139
const daprPort = "3500";
135140

136141
async function start() {
137-
const client = new DaprClient(daprHost, daprPort);
142+
const client = new DaprClient({ daprHost, daprPort });
138143

139144
const serviceAppId = "my-app-id";
140145
const serviceMethod = "say-hello";
@@ -174,7 +179,7 @@ const daprHost = "127.0.0.1";
174179
const daprPort = "3500";
175180

176181
async function start() {
177-
const client = new DaprClient(daprHost, daprPort);
182+
const client = new DaprClient({ daprHost, daprPort });
178183

179184
const serviceStoreName = "my-state-store-name";
180185

@@ -231,7 +236,7 @@ start().catch((e) => {
231236
import { DaprClient } from "@dapr/dapr";
232237

233238
async function start() {
234-
const client = new DaprClient(daprHost, daprPort);
239+
const client = new DaprClient({ daprHost, daprPort });
235240

236241
const res = await client.state.query("state-mongodb", {
237242
filter: {
@@ -282,7 +287,7 @@ const daprHost = "127.0.0.1";
282287
const daprPort = "3500";
283288

284289
async function start() {
285-
const client = new DaprClient(daprHost, daprPort);
290+
const client = new DaprClient({ daprHost, daprPort });
286291

287292
const pubSubName = "my-pubsub-name";
288293
const topic = "topic-a";
@@ -355,7 +360,7 @@ const daprHost = "127.0.0.1";
355360
const daprPort = "3500";
356361

357362
async function start() {
358-
const client = new DaprClient(daprHost, daprPort);
363+
const client = new DaprClient({ daprHost, daprPort });
359364

360365
const bindingName = "my-binding-name";
361366
const bindingOperation = "create";
@@ -383,7 +388,7 @@ const daprHost = "127.0.0.1";
383388
const daprPort = "3500";
384389

385390
async function start() {
386-
const client = new DaprClient(daprHost, daprPort);
391+
const client = new DaprClient({ daprHost, daprPort });
387392

388393
const secretStoreName = "my-secret-store";
389394
const secretKey = "secret-key";
@@ -414,7 +419,7 @@ const daprHost = "127.0.0.1";
414419
const daprAppId = "example-config";
415420

416421
async function start() {
417-
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT);
422+
const client = new DaprClient({ daprHost, daprPort: process.env.DAPR_HTTP_PORT });
418423

419424
const config = await client.configuration.get("config-store", ["key1", "key2"]);
420425
console.log(config);
@@ -438,7 +443,7 @@ const daprHost = "127.0.0.1";
438443
const daprPortDefault = "3500";
439444

440445
async function start() {
441-
const client = new DaprClient(daprHost, daprPort);
446+
const client = new DaprClient({ daprHost, daprPort });
442447

443448
const storeName = "redislock";
444449
const resourceId = "resourceId";

daprdocs/content/en/js-sdk-docs/js-logger/_index.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ There are five levels of logging in **descending order of importance** - `error`
2020
import { CommunicationProtocolEnum, DaprClient, LogLevel } from "@dapr/dapr";
2121

2222
// create a client instance with log level set to verbose.
23-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.HTTP, {
23+
const client = new DaprClient({
24+
daprHost,
25+
daprPort,
26+
communicationProtocol: CommunicationProtocolEnum.HTTP,
2427
logger: { level: LogLevel.Verbose },
2528
});
2629
```
@@ -88,7 +91,10 @@ import { WinstonLoggerService } from "./WinstonLoggerService";
8891
const winstonLoggerService = new WinstonLoggerService();
8992

9093
// create a client instance with log level set to verbose and logger service as winston.
91-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.HTTP, {
94+
const client = new DaprClient({
95+
daprHost,
96+
daprPort,
97+
communicationProtocol: CommunicationProtocolEnum.HTTP,
9298
logger: { level: LogLevel.Verbose, service: winstonLoggerService },
9399
});
94100
```

daprdocs/content/en/js-sdk-docs/js-server/_index.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,11 @@ const serverHost = "127.0.0.1";
454454
const serverPort = "5051";
455455

456456
async function start() {
457-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
457+
const client = new DaprClient({
458+
daprHost,
459+
daprPort,
460+
communicationProtocol: CommunicationProtocolEnum.GRPC,
461+
});
458462
const config = await client.configuration.get("config-redis", ["myconfigkey1", "myconfigkey2"]);
459463
}
460464

@@ -475,7 +479,11 @@ const serverHost = "127.0.0.1";
475479
const serverPort = "5051";
476480

477481
async function start() {
478-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
482+
const client = new DaprClient({
483+
daprHost,
484+
daprPort,
485+
communicationProtocol: CommunicationProtocolEnum.GRPC,
486+
});
479487
const stream = await client.configuration.subscribeWithKeys("config-redis", ["myconfigkey1", "myconfigkey2"], () => {
480488
// Received a key update
481489
});

examples/configuration/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const daprHost = "127.0.0.1";
1717
const daprPortDefault = "3500";
1818

1919
async function start() {
20-
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT ?? daprPortDefault);
20+
const client = new DaprClient({ daprHost, daprPort: process.env.DAPR_HTTP_PORT ?? daprPortDefault });
2121

2222
const config = await client.configuration.get("config-store", ["key1", "key2"]);
2323
console.log(config);

examples/distributedLock/TryLockApplication/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const daprHost = "127.0.0.1";
1818
const daprPortDefault = "3500";
1919

2020
async function start() {
21-
const client = new DaprClient(
21+
const client = new DaprClient({
2222
daprHost,
23-
process.env.DAPR_GRPC_PORT ?? daprPortDefault,
24-
CommunicationProtocolEnum.GRPC,
25-
);
23+
daprPort: process.env.DAPR_GRPC_PORT ?? daprPortDefault,
24+
communicationProtocol: CommunicationProtocolEnum.GRPC,
25+
});
2626

2727
const storeName = "redislock";
2828
const resourceId = "resourceId";

examples/distributedLock/UnLockApplication/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const daprHost = "127.0.0.1";
1818
const daprPortDefault = "3500";
1919

2020
async function start() {
21-
const client = new DaprClient(
21+
const client = new DaprClient({
2222
daprHost,
23-
process.env.DAPR_GRPC_PORT ?? daprPortDefault,
24-
CommunicationProtocolEnum.GRPC,
25-
);
23+
daprPort: process.env.DAPR_GRPC_PORT ?? daprPortDefault,
24+
communicationProtocol: CommunicationProtocolEnum.GRPC,
25+
});
2626

2727
const storeName = "redislock";
2828
const resourceId = "resourceId";

0 commit comments

Comments
 (0)