My Moleculer-based microservices project
- node >= 18.17.x
npx degit jellydn/moleculer-typescript-template [PROJECT-NAME]- ⚡️ Progressive microservices framework for Node.js. Moleculer with Typescript template
- 📦 hygen - The scalable code generator that saves you time.
- 🦾 pino - super fast, all natural json logger
- 🔥 swagger-jsdoc - Generates swagger/openapi specification based on jsDoc comments and YAML files.
- ✨ moleculer-zod-validator - A validator for the Moleculer microservice framework to allow the use of Zod.
- 🔏 asteasolutions/zod-to-openapi - A library that generates OpenAPI (Swagger) docs from Zod schemas.
- 🪄 hey-api/openapi-ts - Turn your OpenAPI specification into a beautiful TypeScript client.
pnpm install# Copy env file
cp .env.example .env
pnpm devAfter starting, open the http://localhost:3000/ URL in your browser.
On the welcome page you can test the generated services via API Gateway and check the nodes & services.
pnpm cli --ns apiIn the terminal, try the following commands:
nodes- List all connected nodes.actions- List all registered service actions.call greeter.hello- Call thegreeter.helloaction.call greeter.welcome --username dunghd- Call thegreeter.welcomeaction with theusernameparameter.
This project uses hygen to generate code templates, saving you time and ensuring consistency across your codebase.
To add a new service to your project, use the following command:
pnpm generate:service [service-name]To add a new action to an existing service, use the following command:
pnpm generate:action [action-name] --service [service-name]To generate a service with Create, Read, Update, and Delete (CRUD) operations, use the following command:
pnpm generate:crud [service-name]This template also reads your JSDoc-annotated source code and generates an OpenAPI (Swagger) specification.
Run the following command to generate the Swagger documentation:
pnpm generate:swaggerOpen the http://localhost:3000/docs URL in your browser, you will see the Swagger UI as
This template automatically generates a type-safe TypeScript SDK from your OpenAPI specification using @hey-api/openapi-ts.
To generate the TypeScript SDK from your OpenAPI specification:
pnpm generate:sdkThis command:
- Reads the OpenAPI specification from
public/docs/open-api.json - Generates TypeScript client code in
generated/sdk/ - Creates type-safe functions for all your API endpoints
The generated SDK provides a type-safe client for your API:
import {
client,
getApiGreeterHello,
getApiGreeterWelcome,
postApiProductCart,
} from "./generated/sdk";
// Configure the client
client.setConfig({
baseUrl: "http://localhost:3000",
});
// Use type-safe API calls
const hello = await getApiGreeterHello();
const welcome = await getApiGreeterWelcome({
query: { username: "John" },
});
const cartItem = await postApiProductCart({
body: { name: "Product", qty: 1 },
});The SDK is automatically regenerated:
- During typecheck:
pnpm typecheckrunsgenerate:sdkfirst - In CI/CD: The deploy workflow ensures SDK is current before type checking
- After API changes: Regenerate manually when you modify services or DTOs
SDK generation is configured in openapi-ts.config.ts:
export default defineConfig({
input: "public/docs/open-api.json", // OpenAPI spec source
output: "generated/sdk", // Output directory
plugins: ["@hey-api/client-fetch"], // HTTP client plugin
exportCore: true, // Export client utilities
});Note: The generated/ directory is in .gitignore and should not be committed. The SDK is generated fresh in CI/CD and during development.
pnpm testThis template comes with two GitHub Actions that handle automatically deploying your app to production and staging environments.
Prior to your first deployment, you'll need to do a few things:
-
Sign up and log in to Fly
fly auth signup
-
Create two apps on Fly, one for staging and one for production:
fly create moleculer-typescript fly create moleculer-typescript-staging
-
Create a new GitHub Repository
-
Add a
FLY_API_TOKENto your GitHub repo. To do this, go to your user settings on Fly and create a new token, then add it to your repo secrets with the nameFLY_API_TOKEN.
Now that every is set up you can commit and push your changes to your repo. Every commit to your main branch will trigger a deployment to your production environment, and every commit to your dev branch will trigger a deployment to your staging environment.
We use GitHub Actions for continuous integration and deployment. Anything that gets into the main branch will be deployed to production after running tests/build/etc. Anything in the dev branch will be deployed to staging.
- Moleculer website: https://moleculer.services/
- Moleculer Documentation: https://moleculer.services/docs/0.14/
pnpm dev: Start development mode (load all services locally with hot-reload & watch)pnpm start: Start production mode (setSERVICESenv variable to load certain services)pnpm cli: Start a CLI and connect to production. Don't forget to set production namespace with--nsargument in scriptpnpm ci: Run continuous test mode with watchingpnpm test: Run tests & generate coverage reportpnpm dc:up: Start the stack with Docker Composepnpm dc:down: Stop the stack with Docker Compose
This template uses Pre-commit to run checks before you commit your code. This ensures that your code is formatted correctly and passes all tests before you push it to your repository.
pre-commit installTo run the checks manually, use the following command:
pre-commit run --all-files👤 Dung Huynh
- Website: https://productsway.com/
- Twitter: @jellydn
- Github: @jellydn
Give a ⭐️ if this project helped you!







