Skip to content

Commit c6b7922

Browse files
committed
Re-work examples
1 parent c66d4cc commit c6b7922

File tree

2 files changed

+98
-52
lines changed

2 files changed

+98
-52
lines changed

daprdocs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This page covers how the documentation is structured for the Dapr JavaScript SDK
44

55
## Dapr Docs
66

7-
All Dapr documentation is hosted at [docs.dapr.io](https://docs.dapr.io), including the docs for the [JavaScript SDK](https://docs.dapr.io/developing-applications/sdks/java/). Head over there if you want to read the docs.
7+
All Dapr documentation is hosted at [docs.dapr.io](https://docs.dapr.io), including the docs for the [JavaScript SDK](https://docs.dapr.io/developing-applications/sdks/javascript/). Head over there if you want to read the docs.
88

99
### JavaScript SDK docs source
1010

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

Lines changed: 97 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,33 @@ description: JavaScript SDK packages for developing Dapr applications
1010

1111
- [Dapr CLI]({{< ref install-dapr-cli.md >}}) installed
1212
- Initialized [Dapr environment]({{< ref install-dapr-selfhost.md >}})
13-
- [Node version 15 or greater](https://nodejs.org/en/)
13+
- [Latest LTS version of Node or greater](https://nodejs.org/en/)
1414

1515
## Installing and importing Dapr's JS SDK
1616

17-
1817
Install the SDK with npm:
1918
```
20-
npm i @roadwork/dapr-js-sdk
19+
npm i @dapr/js-sdk
2120
```
2221

23-
Import the libraries for the the given protocol you're using:
22+
Import the libraries for the the given protocol:
2423

2524
```javascript
26-
import { DaprClient, DaprServer } from "@roadwork/dapr-js-sdk/http";
27-
// OR (depending on the protocol)
28-
import { DaprClient, DaprServer } from "@roadwork/dapr-js-sdk/grpc";
25+
const daprHost = "127.0.0.1"; // Dapr Sidecar Host
26+
const daprPort = "50050"; // Dapr Sidecar Port of this Example Server
27+
const daprPortActor = "10002"; // Dapr Sidecar Port of the Actor Server
28+
const serverHost = "127.0.0.1"; // App Host of this Example Server
29+
const serverPort = "50051"; // App Port of this Example Server
30+
31+
import { DaprClient, DaprServer, CommunicationProtocolEnum } from "@dapr/js-sdk";
32+
33+
// HTTP
34+
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
35+
const client = new DaprClient(daprHost, daprPort);
36+
37+
// GRPC
38+
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);
39+
const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
2940
```
3041

3142
## Building blocks
@@ -35,60 +46,76 @@ The JavaScript SDK allows you to interface with all of the [Dapr building blocks
3546
### Invoke a service
3647

3748
```javascript
38-
import { DaprClient, HttpMethod } from "@roadwork/dapr-js-sdk/http";
49+
import { DaprClient, DaprServer } from "@dapr/js-sdk";
3950

40-
const daprHost = "127.0.0.1";
51+
const daprHost = "127.0.0.1";
4152
const daprPort = "50050";
53+
const serverHost = "127.0.0.1";
54+
const serverPort = "50051";
4255

4356
async function start() {
44-
const client = new DaprClient(daprHost, daprPort);
57+
const server = new DaprServer(serverHost, serverPort);
58+
59+
//POST listener
60+
await server.invoker.listen("hello-world", async (data: any) => {
61+
return { hello: "world received from POST" };
62+
}, { method: HttpMethod.POST });
4563

64+
//GET listener
65+
await server.invoker.listen("hello-world", async () => {
66+
return { hello: "world received from GET" };
67+
}, { method: HttpMethod.GET });
68+
69+
// Start server after instantiating listeners
70+
await server.startServer();
71+
72+
const client = new DaprClient(daprHost, daprPort);
73+
74+
const serviceAppId = "my-dapr-app-id";
75+
const serviceMethod = "say-hello";
76+
4677
//POST Request
47-
const response = await client.invoker.invoke(SERVICE_TO_INVOKE, METHOD_TO_INVOKE, HttpMethod.POST, {
48-
name: "World!"
49-
});
78+
const response = await client.invoker.invoke(serviceAppId , serviceMethod , HttpMethod.POST, { hello: "world" });
5079

5180
//GET Request
52-
const response = await client.invoker.invoke(SERVICE_TO_INVOKE, METHOD_TO_INVOKE, HttpMethod.GET);
81+
const response = await client.invoker.invoke(serviceAppId , serviceMethod , HttpMethod.GET);
5382
}
5483
```
5584
- For a full guide on service invocation visit [How-To: Invoke a service]({{< ref howto-invoke-discover-services.md >}}).
5685

5786
### Save & get application state
5887

5988
```javascript
60-
import { DaprClient } from "@roadwork/dapr-js-sdk/http";
89+
import { DaprClient } from "@dapr/js-sdk";
6190

62-
const daprHost = "127.0.0.1";
91+
const daprHost = "127.0.0.1";
6392
const daprPort = "50050";
6493

6594
async function start() {
66-
const client = new DaprClient(daprHost, daprPort);
95+
const client = new DaprClient(daprHost, daprPort);
96+
97+
const serviceStoreName = "my-dapr-state-store";
6798

6899
//Save state
69-
const response = await client.state.save(STATE_STORE_NAME, [
100+
const response = await client.state.save(serviceStoreName, [
70101
{
71-
key: FIRST_KEY_NAME
72-
value: FIRST_VALUE
102+
key: "first-key-name",
103+
value: "Hello"
73104
},
74105
{
75-
key: SECOND_KEY_NAME,
76-
value: SECOND_VALUE
77-
},
78-
{
79-
key: THIRD_KEY_NAME
80-
value: THIRD_VALUE
106+
key: "second-key-name",
107+
value: "World!"
81108
}
82109
]);
83110

84111
//Get State
85-
const response = await client.state.get(STATE_STORE_NAME, FIRST_KEY_NAME);
112+
const response = await client.state.get(serviceStoreName, "first-key-name");
86113

87114
//Get Bulk State
88-
const response = await client.state.getBulk(STATE_STORE_NAME, [ FIRST_KEY_NAME, SECOND_KEY_NAME ]);
115+
const response = await client.state.getBulk(serviceStoreName, ["first-key-name", "second-key-name"]);
89116

90117
//Delete State
91-
const response = await client.state.delete(STATE_STORE_NAME, FIRST_KEY_NAME);
118+
const response = await client.state.delete(serviceStoreName, "first-key-name");
92119
}
93120
```
94121
- For a full list of state operations visit [How-To: Get & save state]({{< ref howto-get-save-state.md >}}).
@@ -98,30 +125,39 @@ async function start() {
98125
##### Publish messages
99126

100127
```javascript
101-
import { DaprClient } from "@roadwork/dapr-js-sdk/http";
128+
import { DaprClient } from "@dapr/js-sdk";
102129

103-
const daprHost = "127.0.0.1";
130+
const daprHost = "127.0.0.1";
104131
const daprPort = "50050";
105132

106133
async function start() {
107134
const client = new DaprClient(daprHost, daprPort);
108135

109-
const response = await client.pubsub.publish(PUBSUB_NAME, TOPIC_NAME, { messsage });
136+
const pubSubName = "my-dapr-pubsub";
137+
const topic = "topic-a";
138+
const message = { hello: "world" }
139+
140+
const response = await client.pubsub.publish(pubSubName-redis, topic, message);
110141
}
111142
```
112143

113144
##### Subscribe to messages
114145

115146
```javascript
116-
import { DaprClient } from "@roadwork/dapr-js-sdk/http";
147+
import { DaprServer } from "@dapr/js-sdk";
117148

118-
const daprHost = "127.0.0.1";
119-
const daprPort = "50050";
149+
const serverHost = "127.0.0.1";
150+
const serverPort = "50051"
120151

121152
async function start() {
122-
const client = new DaprClient(daprHost, daprPort);
153+
const server = new DaprServer(serverHost, serverPort);
154+
155+
const pubSubName = "my-dapr-pubsub";
156+
const topic = "topic-a";
157+
158+
await server.pubsub.subscribe(pubSubName, topic, async (data: any) => console.log(`Got Data: ${JSON.stringify(data)}`));
123159

124-
const response = await server.pubsub.subscribe(PUBSUB_NAME, TOPIC_NAME, async (data) => console.log(`Got Data: ${JSON.stringify(data)}`));
160+
await server.startServer();
125161
}
126162
```
127163

@@ -131,30 +167,37 @@ async function start() {
131167

132168
**Output Bindings**
133169
```javascript
134-
import { DaprClient } from "@roadwork/dapr-js-sdk/http";
170+
import { DaprClient } from "@dapr/js-sdk";
135171

136-
const daprHost = "127.0.0.1";
172+
const daprHost = "127.0.0.1";
137173
const daprPort = "50050";
138174

139175
async function start() {
140176
const client = new DaprClient(daprHost, daprPort);
141177

142-
const response = await client.binding.send(BINDING_NAME, BINDING_OPERATION, { message });
178+
const bindingName = "my-binding-name";
179+
const bindingOperation = "create";
180+
const message = { hello: "world" };
181+
182+
const response = await client.binding.send(bindingName, bindingOperation, message);
143183
}
144184
```
145185

146186
**Input Bindings**
147187
```javascript
148-
import { DaprServer } from "@roadwork/dapr-js-sdk/http";
188+
import { DaprServer } from "@dapr/js-sdk";;
149189

150-
const daprHost = "127.0.0.1";
151-
const daprPort = "50050";
152-
const daprInternalServerPort = "50051"; // App Port of this Example Server
190+
const serverHost = "127.0.0.1";
191+
const serverPort = "50051";
153192

154193
async function start() {
155-
const server = new DaprServer(daprHost, daprPort, daprInternalServerPort);
194+
const server = new DaprServer(serverHost, serverPort);
195+
196+
const bindingName = "my-binding-name";
156197

157-
const response = await server.binding.receive(BINDING_NAME, async (data) => console.log(`Got Data: ${JSON.stringify(data)}`));
198+
const response = await server.binding.receive(bindingName, async (data: any) => console.log(`Got Data: ${JSON.stringify(data)}`));
199+
200+
await server.startServer();
158201
}
159202
```
160203

@@ -163,19 +206,22 @@ async function start() {
163206
### Retrieve secrets
164207

165208
```javascript
166-
import { DaprClient } from "@roadwork/dapr-js-sdk/http";
209+
import { DaprClient } from "@dapr/js-sdk";
167210

168-
const daprHost = "127.0.0.1";
211+
const daprHost = "127.0.0.1";
169212
const daprPort = "50050";
170213

171214
async function start() {
172215
const client = new DaprClient(daprHost, daprPort);
173216

174-
//Retrieve a single secret from secret store
175-
const response = await client.secret.get(SECRET_STORE_NAME, secretKey);
217+
const secretStoreName = "my-secret-store";
218+
const secretKey = "secret-key";
219+
220+
// Retrieve a single secret from secret store
221+
const response = await client.secret.get(secretStoreName, secretKey);
176222

177223
// Retrieve all secrets from secret store
178-
const response = await client.secret.getBulk(SECRET_STORE_NAME);
224+
const response = await client.secret.getBulk(secretStoreName);
179225
}
180226
```
181227

0 commit comments

Comments
 (0)