Skip to content

Commit 18a7281

Browse files
Merge pull request #174 from hhunter-ms/api-config
doc: Add api configuration example and update doc
2 parents 7886209 + d8af357 commit 18a7281

File tree

10 files changed

+775
-36
lines changed

10 files changed

+775
-36
lines changed

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

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ description: JavaScript Client SDK for developing Dapr applications
1515
## Installing and importing Dapr's JS SDK
1616

1717
Install the SDK with npm:
18-
```
18+
19+
```bash
1920
npm i dapr-client
2021
```
2122

2223
Import the libraries:
24+
2325
```javascript
2426
import { DaprClient, DaprServer, HttpMethod, CommunicationProtocolEnum } from "dapr-client";
2527

@@ -37,11 +39,49 @@ const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, Commun
3739
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
3840
```
3941

40-
##### DaprClient Library
42+
## Running
43+
44+
To run the examples, you can use two different protocols to interact with the Dapr sidecar: HTTP (default) or gRPC.
45+
46+
### Using HTTP (default)
47+
48+
```javascript
49+
import { DaprClient, DaprServer } from "dapr-client";
50+
const client = new DaprClient(daprHost, daprPort);
51+
const server= new DaprServer(appHost, appPort, daprHost, daprPort);
52+
```
53+
54+
```bash
55+
# Using dapr run
56+
dapr run --app-id <example-sdk> --app-port 50051 --app-protocol http npm run start
57+
58+
# or, using npm script
59+
npm run start:dapr-http
60+
```
61+
62+
### Using gRPC
63+
64+
Since HTTP is the default, you will have to adapt the communication protocol to use gRPC. You can do this by passing an extra argument to the client or server constructor.
65+
66+
```javascript
67+
import { DaprClient, DaprServer, CommunicationProtocol } from "dapr-client";
68+
const client = new DaprClient(daprHost, daprPort, CommunicationProtocol.GRPC);
69+
const server= new DaprServer(appHost, appPort, daprHost, daprPort, CommunicationProtocol.GRPC);
70+
```
71+
72+
```bash
73+
# Using dapr run
74+
dapr run --app-id <example-sdk> --app-port 50051 --app-protocol grpc npm run start
75+
76+
# or, using npm script
77+
npm run start:dapr-grpc
78+
```
79+
80+
### DaprClient Library
4181
A library that provides methods for how an application communicates with the Dapr sidecar.
4282

43-
##### DaprServer Library
44-
A library for how an application registers bindings / routes with Dapr. The `start()` method is used to start the server and bind the routes.
83+
### DaprServer Library
84+
A library for how an application registers bindings / routes with Dapr. The `start()` method is used to start the server and bind the routes.
4585

4686
## Building blocks
4787

@@ -50,13 +90,13 @@ The JavaScript SDK allows you to interface with all of the [Dapr building blocks
5090
### Invoke a service
5191

5292
```javascript
53-
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from "dapr-client";
93+
import { DaprClient, HttpMethod } from "dapr-client";
5494

5595
const daprHost = "127.0.0.1";
5696
const daprPort = "3500";
5797

5898
async function start() {
59-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
99+
const client = new DaprClient(daprHost, daprPort);
60100

61101
const serviceAppId = "my-app-id";
62102
const serviceMethod = "say-hello";
@@ -68,18 +108,19 @@ async function start() {
68108
const response = await client.invoker.invoke(serviceAppId , serviceMethod , HttpMethod.GET);
69109
}
70110
```
111+
71112
- For a full guide on service invocation visit [How-To: Invoke a service]({{< ref howto-invoke-discover-services.md >}}).
72113

73114
### Save, get and delete application state
74115

75116
```javascript
76-
import { DaprClient, CommunicationProtocolEnum } from "dapr-client";
117+
import { DaprClient } from "dapr-client";
77118

78119
const daprHost = "127.0.0.1";
79120
const daprPort = "3500";
80121

81122
async function start() {
82-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
123+
const client = new DaprClient(daprHost, daprPort);
83124

84125
const serviceStoreName = "my-state-store-name";
85126

@@ -122,20 +163,21 @@ async function start() {
122163
const response = await client.state.delete(serviceStoreName, "first-key-name");
123164
}
124165
```
166+
125167
- For a full list of state operations visit [How-To: Get & save state]({{< ref howto-get-save-state.md >}}).
126168

127169
### Publish & subscribe to messages
128170

129171
##### Publish messages
130172

131173
```javascript
132-
import { DaprClient, CommunicationProtocolEnum } from "dapr-client";
174+
import { DaprClient } from "dapr-client";
133175

134176
const daprHost = "127.0.0.1";
135177
const daprPort = "3500";
136178

137179
async function start() {
138-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
180+
const client = new DaprClient(daprHost, daprPort);
139181

140182
const pubSubName = "my-pubsub-name";
141183
const topic = "topic-a";
@@ -149,15 +191,15 @@ async function start() {
149191
##### Subscribe to messages
150192

151193
```javascript
152-
import { DaprServer, CommunicationProtocolEnum } from "dapr-client";
194+
import { DaprServer } from "dapr-client";
153195

154196
const daprHost = "127.0.0.1"; // Dapr Sidecar Host
155197
const daprPort = "3500"; // Dapr Sidecar Port of this Example Server
156198
const serverHost = "127.0.0.1"; // App Host of this Example Server
157199
const serverPort = "50051"; // App Port of this Example Server "
158200

159201
async function start() {
160-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);
202+
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
161203

162204
const pubSubName = "my-pubsub-name";
163205
const topic = "topic-a";
@@ -174,14 +216,15 @@ async function start() {
174216
### Interact with bindings
175217

176218
**Output Bindings**
219+
177220
```javascript
178-
import { DaprClient, CommunicationProtocolEnum } from "dapr-client";
221+
import { DaprClient } from "dapr-client";
179222

180223
const daprHost = "127.0.0.1";
181224
const daprPort = "3500";
182225

183226
async function start() {
184-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
227+
const client = new DaprClient(daprHost, daprPort);
185228

186229
const bindingName = "my-binding-name";
187230
const bindingOperation = "create";
@@ -192,16 +235,17 @@ async function start() {
192235
```
193236

194237
**Input Bindings**
238+
195239
```javascript
196-
import { DaprServer, CommunicationProtocolEnum } from "dapr-client";;
240+
import { DaprServer } from "dapr-client";;
197241

198242
const daprHost = "127.0.0.1";
199243
const daprPort = "3500";
200244
const serverHost = "127.0.0.1";
201245
const serverPort = "5051";
202246

203247
async function start() {
204-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);;
248+
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);;
205249

206250
const bindingName = "my-binding-name";
207251

@@ -216,13 +260,13 @@ async function start() {
216260
### Retrieve secrets
217261

218262
```javascript
219-
import { DaprClient, CommunicationProtocolEnum } from "dapr-client";
263+
import { DaprClient } from "dapr-client";
220264

221265
const daprHost = "127.0.0.1";
222266
const daprPort = "3500";
223267

224268
async function start() {
225-
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
269+
const client = new DaprClient(daprHost, daprPort);
226270

227271
const secretStoreName = "my-secret-store";
228272
const secretKey = "secret-key";
@@ -234,7 +278,36 @@ async function start() {
234278
const response = await client.secret.getBulk(secretStoreName);
235279
}
236280
```
281+
237282
- For a full guide on secrets visit [How-To: Retrieve secrets]({{< ref howto-secrets.md >}}).
238283

284+
### Get configuration keys
285+
286+
```javascript
287+
import { DaprClient } from "dapr-client";
288+
289+
const daprHost = "127.0.0.1";
290+
const daprAppId = "example-config";
291+
292+
async function start() {
293+
294+
const client = new DaprClient(
295+
daprHost,
296+
process.env.DAPR_HTTP_PORT
297+
);
298+
299+
const config = await client.configuration.get('config-store', ['key1', 'key2']);
300+
console.log(config);
301+
302+
console.log(JSON.stringify(config));
303+
}
304+
305+
start().catch((e) => {
306+
console.error(e);
307+
process.exit(1);
308+
});
309+
```
310+
239311
## Related links
312+
240313
- [JavaScript SDK examples](https://github.com/dapr/js-sdk/tree/master/examples)

examples/configuration/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Examples - Configuration
2+
3+
This example demonstrates how to use API Configuration.
4+
5+
## Run
6+
7+
```bash
8+
# If it is not already, initialize DAPR on the system
9+
dapr init
10+
11+
# Install dependencies
12+
npm install
13+
14+
# Run the example directly using dapr run
15+
dapr run --app-id example-config --app-port 50051 --app-protocol http npm run start
16+
17+
# or, using npm script
18+
npm run start:dapr-http
19+
```
20+
21+
## Switching from HTTP to gRPC
22+
23+
By default, the example will run using HTTP. To use gRPC instead:
24+
25+
- Add `CommunicationProtocolEnum.GRPC` to the DaprClient object creation:
26+
27+
```javascript
28+
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.GRPC);
29+
```
30+
31+
- To run:
32+
33+
```bash
34+
# Using dapr run
35+
dapr run --app-id example-config --app-port 50051 --app-protocol grpc npm run start
36+
37+
# or, using npm script
38+
npm run start:dapr-grpc
39+
```

0 commit comments

Comments
 (0)