From 401ff35e94181fdea4c81b8397d580a3a54c4ef2 Mon Sep 17 00:00:00 2001 From: Wralith Date: Wed, 12 Apr 2023 04:38:06 +0300 Subject: [PATCH 1/3] Update javascript snippets Signed-off-by: Wralith --- .../bindings/howto-bindings.md | 6 ++++- .../bindings/howto-triggers.md | 10 ++++++- .../pubsub/howto-publish-subscribe.md | 22 +++++++++------ .../building-blocks/pubsub/pubsub-bulk.md | 17 ++++++++---- .../building-blocks/secrets/howto-secrets.md | 6 ++++- .../howto-invoke-discover-services.md | 7 ++++- .../state-management/howto-get-save-state.md | 27 ++++++++++++++++--- 7 files changed, 74 insertions(+), 21 deletions(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/bindings/howto-bindings.md b/daprdocs/content/en/developing-applications/building-blocks/bindings/howto-bindings.md index 7978fe198..a73fc8630 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/bindings/howto-bindings.md +++ b/daprdocs/content/en/developing-applications/building-blocks/bindings/howto-bindings.md @@ -270,7 +270,11 @@ const daprHost = "127.0.0.1"; async function sendOrder(orderId) { const BINDING_NAME = "checkout"; const BINDING_OPERATION = "create"; - const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); + const client = new DaprClient({ + daprHost, + daprPort: process.env.DAPR_HTTP_PORT, + communicationProtocol: CommunicationProtocolEnum.HTTP, + }); //Using Dapr SDK to invoke output binding const result = await client.binding.send(BINDING_NAME, BINDING_OPERATION, orderId); console.log("Sending message: " + orderId); diff --git a/daprdocs/content/en/developing-applications/building-blocks/bindings/howto-triggers.md b/daprdocs/content/en/developing-applications/building-blocks/bindings/howto-triggers.md index a98cc37ea..215ffd05d 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/bindings/howto-triggers.md +++ b/daprdocs/content/en/developing-applications/building-blocks/bindings/howto-triggers.md @@ -237,7 +237,15 @@ start().catch((e) => { }); async function start() { - const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.HTTP); + const server = new DaprServer({ + serverHost, + serverPort, + communicationProtocol: CommunicationProtocolEnum.HTTP, + clientOptions: { + daprHost, + daprPort, + } + }); await server.binding.receive('checkout', async (orderId) => console.log(`Received Message: ${JSON.stringify(orderId)}`)); await server.startServer(); } diff --git a/daprdocs/content/en/developing-applications/building-blocks/pubsub/howto-publish-subscribe.md b/daprdocs/content/en/developing-applications/building-blocks/pubsub/howto-publish-subscribe.md index 6589242ee..df28559d4 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/pubsub/howto-publish-subscribe.md +++ b/daprdocs/content/en/developing-applications/building-blocks/pubsub/howto-publish-subscribe.md @@ -355,13 +355,15 @@ start().catch((e) => { }); async function start(orderId) { - const server = new DaprServer( - serverHost, - serverPort, - daprHost, - process.env.DAPR_HTTP_PORT, - CommunicationProtocolEnum.HTTP - ); + const server = new DaprServer({ + serverHost, + serverPort, + communicationProtocol: CommunicationProtocolEnum.HTTP, + clientOptions: { + daprHost, + daprPort: process.env.DAPR_HTTP_PORT, + }, + }); //Subscribe to a topic await server.pubsub.subscribe("order-pub-sub", "orders", async (orderId) => { console.log(`Subscriber received: ${JSON.stringify(orderId)}`) @@ -625,7 +627,11 @@ var main = function() { async function start(orderId) { const PUBSUB_NAME = "order-pub-sub" const TOPIC_NAME = "orders" - const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); + const client = new DaprClient({ + daprHost, + daprPort: process.env.DAPR_HTTP_PORT, + communicationProtocol: CommunicationProtocolEnum.HTTP + }); console.log("Published data:" + orderId) //Using Dapr SDK to publish a topic await client.pubsub.publish(PUBSUB_NAME, TOPIC_NAME, orderId); diff --git a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-bulk.md b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-bulk.md index 106c9694e..c7e7bc20e 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-bulk.md +++ b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-bulk.md @@ -387,13 +387,20 @@ import { DaprServer } from "@dapr/dapr"; const pubSubName = "orderPubSub"; const topic = "topicbulk"; -const DAPR_HOST = process.env.DAPR_HOST || "127.0.0.1"; -const DAPR_HTTP_PORT = process.env.DAPR_HTTP_PORT || "3502"; -const SERVER_HOST = process.env.SERVER_HOST || "127.0.0.1"; -const SERVER_PORT = process.env.APP_PORT || 5001; +const daprHost = process.env.DAPR_HOST || "127.0.0.1"; +const daprPort = process.env.DAPR_HTTP_PORT || "3502"; +const serverHost = process.env.SERVER_HOST || "127.0.0.1"; +const serverPort = process.env.APP_PORT || 5001; async function start() { - const server = new DaprServer(SERVER_HOST, SERVER_PORT, DAPR_HOST, DAPR_HTTP_PORT); + const server = new DaprServer({ + serverHost, + serverPort, + clientOptions: { + daprHost, + daprPort, + }, + }); // Publish multiple messages to a topic with default config. await client.pubsub.bulkSubscribeWithDefaultConfig(pubSubName, topic, (data) => console.log("Subscriber received: " + JSON.stringify(data))); diff --git a/daprdocs/content/en/developing-applications/building-blocks/secrets/howto-secrets.md b/daprdocs/content/en/developing-applications/building-blocks/secrets/howto-secrets.md index 9c7477f9b..470693f5f 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/secrets/howto-secrets.md +++ b/daprdocs/content/en/developing-applications/building-blocks/secrets/howto-secrets.md @@ -218,7 +218,11 @@ import { DaprClient, HttpMethod, CommunicationProtocolEnum } from '@dapr/dapr'; const daprHost = "127.0.0.1"; async function main() { - const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); + const client = new DaprClient({ + daprHost, + daprPort: process.env.DAPR_HTTP_PORT, + communicationProtocol: CommunicationProtocolEnum.HTTP, + }); const SECRET_STORE_NAME = "localsecretstore"; //Using Dapr SDK to get a secret var secret = await client.secret.get(SECRET_STORE_NAME, "secret"); diff --git a/daprdocs/content/en/developing-applications/building-blocks/service-invocation/howto-invoke-discover-services.md b/daprdocs/content/en/developing-applications/building-blocks/service-invocation/howto-invoke-discover-services.md index 183b537f6..ba3740f0a 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/service-invocation/howto-invoke-discover-services.md +++ b/daprdocs/content/en/developing-applications/building-blocks/service-invocation/howto-invoke-discover-services.md @@ -347,7 +347,12 @@ var main = function() { } async function start(orderId) { - const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); + const client = new DaprClient({ + daprHost: daprHost, + daprPort: process.env.DAPR_HTTP_PORT, + communicationProtocol: CommunicationProtocolEnum.HTTP + }); + //Using Dapr SDK to invoke a method const result = await client.invoker.invoke('checkoutservice' , "checkout/" + orderId , HttpMethod.GET); console.log("Order requested: " + orderId); diff --git a/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md b/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md index 6ad92783e..9d8f08e93 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md +++ b/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-get-save-state.md @@ -266,7 +266,11 @@ var main = function() { } async function start(orderId) { - const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); + const client = new DaprClient({ + daprHost, + daprPort: process.env.DAPR_HTTP_PORT, + communicationProtocol: CommunicationProtocolEnum.HTTP, + }); const STATE_STORE_NAME = "statestore"; //Using Dapr SDK to save and get state await client.state.save(STATE_STORE_NAME, [ @@ -483,7 +487,12 @@ const daprHost = "127.0.0.1"; var main = function() { const STATE_STORE_NAME = "statestore"; //Using Dapr SDK to save and get state - const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); + const client = new DaprClient({ + daprHost, + daprPort: process.env.DAPR_HTTP_PORT, + communicationProtocol: CommunicationProtocolEnum.HTTP, + }); + await client.state.delete(STATE_STORE_NAME, "order_1"); } @@ -630,7 +639,12 @@ var main = function() { const STATE_STORE_NAME = "statestore"; var orderId = 100; //Using Dapr SDK to save and retrieve multiple states - const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); + const client = new DaprClient({ + daprHost, + daprPort: process.env.DAPR_HTTP_PORT, + communicationProtocol: CommunicationProtocolEnum.HTTP, + }); + await client.state.save(STATE_STORE_NAME, [ { key: "order_1", @@ -870,7 +884,12 @@ var main = function() { } async function start(orderId) { - const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); + const client = new DaprClient({ + daprHost, + daprPort: process.env.DAPR_HTTP_PORT, + communicationProtocol: CommunicationProtocolEnum.HTTP, + }); + const STATE_STORE_NAME = "statestore"; //Using Dapr SDK to save and retrieve multiple states await client.state.transaction(STATE_STORE_NAME, [ From 59b9621a499bad9ce3cc35a9405b874488ff3441 Mon Sep 17 00:00:00 2001 From: Wralith Date: Wed, 12 Apr 2023 18:53:01 +0300 Subject: [PATCH 2/3] Add missing snippets in quickstarts section Signed-off-by: Wralith --- .../quickstarts/pubsub-quickstart.md | 2 +- .../quickstarts/statemanagement-quickstart.md | 30 +++++++------------ 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/daprdocs/content/en/getting-started/quickstarts/pubsub-quickstart.md b/daprdocs/content/en/getting-started/quickstarts/pubsub-quickstart.md index 660282fe4..1bec12d37 100644 --- a/daprdocs/content/en/getting-started/quickstarts/pubsub-quickstart.md +++ b/daprdocs/content/en/getting-started/quickstarts/pubsub-quickstart.md @@ -273,7 +273,7 @@ dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 --resources In the `checkout` publisher service, we're publishing the orderId message to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. As soon as the service starts, it publishes in a loop: ```js -const client = new DaprClient(DAPR_HOST, DAPR_HTTP_PORT); +const client = new DaprClient({ daprHost, daprPort }); await client.pubsub.publish(PUBSUB_NAME, PUBSUB_TOPIC, order); console.log("Published data: " + JSON.stringify(order)); diff --git a/daprdocs/content/en/getting-started/quickstarts/statemanagement-quickstart.md b/daprdocs/content/en/getting-started/quickstarts/statemanagement-quickstart.md index 69a0d65f6..ac044f0b6 100644 --- a/daprdocs/content/en/getting-started/quickstarts/statemanagement-quickstart.md +++ b/daprdocs/content/en/getting-started/quickstarts/statemanagement-quickstart.md @@ -177,29 +177,19 @@ dapr run --app-id order-processor --resources-path ../../../resources/ -- npm ru The `order-processor` service writes, reads, and deletes an `orderId` key/value pair to the `statestore` instance [defined in the `statestore.yaml` component]({{< ref "#statestoreyaml-component-file" >}}). As soon as the service starts, it performs a loop. ```js - const client = new DaprClient(DAPR_HOST, DAPR_HTTP_PORT); +const client = new DaprClient({ daprHost, daprPort, communicationProtocol }) - // Save state into the state store - client.state.save(STATE_STORE_NAME, [ - { - key: orderId.toString(), - value: order - } - ]); - console.log("Saving Order: ", order); +// Save state into a state store +await client.state.save(DAPR_STATE_STORE_NAME, state) +console.log("Saving Order: ", order) - // Get state from the state store - var result = client.state.get(STATE_STORE_NAME, orderId.toString()); - result.then(function(val) { - console.log("Getting Order: ", val); - }); - - // Delete state from the state store - client.state.delete(STATE_STORE_NAME, orderId.toString()); - result.then(function(val) { - console.log("Deleting Order: ", val); - }); +// Get state from a state store +const savedOrder = await client.state.get(DAPR_STATE_STORE_NAME, order.orderId) +console.log("Getting Order: ", savedOrd +// Delete state from the state store +await client.state.delete(DAPR_STATE_STORE_NAME, order.orderId) +console.log("Deleting Order: ", order) ``` ### Step 3: View the order-processor outputs From 6aaed4095e882fe93c6cc14c11205228a9312f96 Mon Sep 17 00:00:00 2001 From: Wralith Date: Wed, 12 Apr 2023 19:24:22 +0300 Subject: [PATCH 3/3] Apply suggestions, omit options object to use defaults Co-authored-by: Shubham Sharma Signed-off-by: Wralith --- .../content/en/getting-started/quickstarts/pubsub-quickstart.md | 2 +- .../getting-started/quickstarts/statemanagement-quickstart.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/daprdocs/content/en/getting-started/quickstarts/pubsub-quickstart.md b/daprdocs/content/en/getting-started/quickstarts/pubsub-quickstart.md index 1bec12d37..cb6096c51 100644 --- a/daprdocs/content/en/getting-started/quickstarts/pubsub-quickstart.md +++ b/daprdocs/content/en/getting-started/quickstarts/pubsub-quickstart.md @@ -273,7 +273,7 @@ dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 --resources In the `checkout` publisher service, we're publishing the orderId message to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. As soon as the service starts, it publishes in a loop: ```js -const client = new DaprClient({ daprHost, daprPort }); +const client = new DaprClient(); await client.pubsub.publish(PUBSUB_NAME, PUBSUB_TOPIC, order); console.log("Published data: " + JSON.stringify(order)); diff --git a/daprdocs/content/en/getting-started/quickstarts/statemanagement-quickstart.md b/daprdocs/content/en/getting-started/quickstarts/statemanagement-quickstart.md index ac044f0b6..f59fed928 100644 --- a/daprdocs/content/en/getting-started/quickstarts/statemanagement-quickstart.md +++ b/daprdocs/content/en/getting-started/quickstarts/statemanagement-quickstart.md @@ -177,7 +177,7 @@ dapr run --app-id order-processor --resources-path ../../../resources/ -- npm ru The `order-processor` service writes, reads, and deletes an `orderId` key/value pair to the `statestore` instance [defined in the `statestore.yaml` component]({{< ref "#statestoreyaml-component-file" >}}). As soon as the service starts, it performs a loop. ```js -const client = new DaprClient({ daprHost, daprPort, communicationProtocol }) +const client = new DaprClient() // Save state into a state store await client.state.save(DAPR_STATE_STORE_NAME, state)