Dapr provides several building blocks that can be used in a TypeScript application:
TypeScript provides type definitions for Dapr SDKs, which helps in catching type - related errors at compile - time. This makes the code more robust and easier to understand and maintain. For example, when making a service invocation or interacting with the state store, the TypeScript types ensure that the input and output data are in the correct format.
npm init -y
to initialize a new Node.js project.npm install @dapr/dapr
to install the Dapr TypeScript SDK.npm install typescript @types/node --save - dev
. Create a tsconfig.json
file with the following basic configuration:{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
The following is an example of making a service invocation from one TypeScript service to another:
Sender Service (src/sender.ts
)
import { DaprClient, HttpMethod } from '@dapr/dapr';
const daprHost = '127.0.0.1';
const daprPort = '3500';
async function main() {
const client = new DaprClient(daprHost, daprPort);
const appId = 'receiver-service';
const method = 'hello';
const data = { message: 'Hello from sender' };
const result = await client.invoker.invoke(appId, method, HttpMethod.POST, data);
console.log('Response from receiver:', result);
}
main().catch((error) => {
console.error('Error:', error);
process.exit(1);
});
Receiver Service (src/receiver.ts
)
import { DaprServer, HttpMethod } from '@dapr/dapr';
const daprHost = '127.0.0.1';
const daprPort = '3500';
const serverPort = '3000';
async function main() {
const server = new DaprServer(daprHost, daprPort, serverPort);
await server.binding.receive('hello', HttpMethod.POST, async (data) => {
console.log('Received data:', data);
return { message: 'Hello back from receiver' };
});
await server.start();
}
main().catch((error) => {
console.error('Error:', error);
process.exit(1);
});
The following is an example of storing and retrieving state using Dapr in a TypeScript application:
import { DaprClient } from '@dapr/dapr';
const daprHost = '127.0.0.1';
const daprPort = '3500';
async function main() {
const client = new DaprClient(daprHost, daprPort);
const stateStoreName = 'statestore';
const key = 'myKey';
const value = { data: 'Hello, Dapr State!' };
// Save state
await client.state.save(stateStoreName, [
{
key: key,
value: value
}
]);
// Retrieve state
const retrievedValue = await client.state.get(stateStoreName, key);
console.log('Retrieved state:', retrievedValue);
}
main().catch((error) => {
console.error('Error:', error);
process.exit(1);
});
The following is an example of publishing and subscribing to a topic using Dapr in a TypeScript application:
Publisher (src/publisher.ts
)
import { DaprClient } from '@dapr/dapr';
const daprHost = '127.0.0.1';
const daprPort = '3500';
async function main() {
const client = new DaprClient(daprHost, daprPort);
const pubSubName = 'pubsub';
const topic = 'myTopic';
const data = { message: 'Hello from publisher' };
await client.pubsub.publish(pubSubName, topic, data);
console.log('Message published');
}
main().catch((error) => {
console.error('Error:', error);
process.exit(1);
});
Subscriber (src/subscriber.ts
)
import { DaprServer } from '@dapr/dapr';
const daprHost = '127.0.0.1';
const daprPort = '3500';
const serverPort = '3001';
async function main() {
const server = new DaprServer(daprHost, daprPort, serverPort);
await server.pubsub.subscribe('pubsub', 'myTopic', async (data) => {
console.log('Received message:', data);
});
await server.start();
}
main().catch((error) => {
console.error('Error:', error);
process.exit(1);
});
try - catch
blocks to handle potential errors.Dapr TypeScript is a powerful combination for building distributed applications. By understanding the fundamental concepts, usage methods, common practices, and best practices, developers can build more reliable, scalable, and maintainable applications. The Dapr TypeScript SDK provides a convenient way to interact with Dapr building blocks, and TypeScript’s type system enhances the code quality. Whether you are building a small - scale microservices application or a large - scale distributed system, Dapr TypeScript can be a great choice.