This is an example to show how to share one kafka client with different backend services. Imagine you have a kafka cluster and two backend services(backend1, backend2) want to producer and consume the messages from the cluster. You dont want to repeat the same codes for kafka implementation in two backend 1 and 2 codebases. Then a kafka-client package comes in handy since you can import it and use it directly. One problem is how can we make it type safe and easy to use. I try to write the types that can autocomplete.
const kafkaClient = new KafkaClient("backend1", []); // you get the autocomplete for which services are "backend1" | "backend2"
kafkaClient.sendMessage("payment-request", {}); // auto suggest which topic to choose and get the correct message type to pass in
kafkaClient.startConsumer(["user-request"], (message, topic) => {}); // auto suggest what topics can be subscribed
How I make the sendMessage and startConsumer to get the autocomplete working. I have a predefined topics in the kafka-client
type TopicMessage = {
"user-request": UserRequestMessage;
"payment-request": PaymentRequestMessage;
};
type Topic = keyof TopicMessage;
// we can get the message type like this
// T is generic
T extends Topic
TopicMessage[T]
And I do some not elegant conditional check to make it work
type Message<T extends Topic> = T extends "user-request"
? UserRequestMessage
: T extends "payment-request"
? PaymentRequestMessage
: never;
One down side is if I want to add more topics, I need to do the check for each topic to get the correct message types
I am still learning typescript. I would like to get some help
if everyone happen to view this repo. Thanks in advance
Run the following command:
yarn
yarn dev
This Turborepo includes the following packages/apps:
backend1
: a nodejs appbackend2
: another nodejs appkafka-client
: a kakfka client package used by backend1 and backend2eslint-config-custom
:eslint
configurations (includeseslint-config-next
andeslint-config-prettier
)tsconfig
:tsconfig.json
s used throughout the monorepo
Each package/app is 100% TypeScript.
This Turborepo has some additional tools already setup for you:
- TypeScript for static type checking
- ESLint for code linting
- Prettier for code formatting
To build all apps and packages, run the following command:
yarn build
To develop all apps and packages, run the following command:
yarn dev
Turborepo can use a technique known as Remote Caching to share cache artifacts across machines, enabling you to share build caches with your team and CI/CD pipelines.
By default, Turborepo will cache locally. To enable Remote Caching you will need an account with Vercel. If you don't have an account you can create one, then enter the following commands:
cd my-turborepo
npx turbo login
This will authenticate the Turborepo CLI with your Vercel account.
Next, you can link your Turborepo to your Remote Cache by running the following command from the root of your Turborepo:
npx turbo link
Learn more about the power of Turborepo: