@@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
11
11
limitations under the License.
12
12
*/
13
13
14
- import { DaprServer , DaprClient , HttpMethod } from "@dapr/dapr" ;
14
+ import { DaprServer , DaprClient , HttpMethod , DaprInvokerCallbackContent } from "@dapr/dapr" ;
15
15
16
16
// Common settings
17
17
const daprAppId = "example-invocation" ;
@@ -20,20 +20,19 @@ const daprHost = "127.0.0.1";
20
20
const serverPort = "50051" ; // App Port of this Example Server
21
21
22
22
async function start ( ) {
23
- // Note that the DAPR_HTTP_PORT and DAPR_GRPC_PORT environment variables are set by DAPR itself. https://docs.dapr.io/reference/environment/
24
- const server = new DaprServer ( serverHost , serverPort , daprHost , process . env . DAPR_HTTP_PORT ) ;
25
-
26
- const client = new DaprClient ( daprHost , process . env . DAPR_HTTP_PORT ) ;
23
+ const server = new DaprServer ( serverHost , serverPort , daprHost ) ;
24
+ const client = new DaprClient ( daprHost ) ;
27
25
28
26
// Note that invoker listeners can be set up after start() has been called
29
27
await server . start ( ) ;
30
28
31
29
console . log ( "Setting up invocation endpoints" ) ;
32
30
await server . invoker . listen (
33
31
"hello-world" ,
34
- async ( data : Record < string , any > ) => {
32
+ async ( data : DaprInvokerCallbackContent ) => {
35
33
// Data is automatically parsed when received
36
- console . log ( `Received: ${ JSON . stringify ( data . body ) } on POST hello-world` ) ;
34
+ console . log ( `Received: ${ JSON . stringify ( data . body ) } as body on POST hello-world` ) ;
35
+ console . log ( `Received: ${ JSON . stringify ( data . headers ) } as headers on POST hello-world` ) ;
37
36
return { hello : "world received from POST" } ;
38
37
} ,
39
38
{ method : HttpMethod . POST } ,
@@ -48,10 +47,17 @@ async function start() {
48
47
{ method : HttpMethod . GET } ,
49
48
) ;
50
49
50
+ // Wait for 500ms to allow the server to start
51
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
52
+
51
53
console . log ( "Invoking endpoints" ) ;
52
- const r = await client . invoker . invoke ( daprAppId , "hello-world" , HttpMethod . POST , {
53
- hello : "world" ,
54
- } ) ;
54
+ const r = await client . invoker . invoke (
55
+ daprAppId ,
56
+ "hello-world" ,
57
+ HttpMethod . POST ,
58
+ { hello : "world" } ,
59
+ { headers : { "X-Application-Type" : "examples/invocation" } } ,
60
+ ) ;
55
61
console . log ( `Response to POST request: ${ JSON . stringify ( r ) } ` ) ;
56
62
const r2 = await client . invoker . invoke ( daprAppId , "hello-world" , HttpMethod . GET ) ;
57
63
console . log ( `Response to GET request: ${ JSON . stringify ( r2 ) } ` ) ;
0 commit comments