medusa-blog-management
v0.0.9
Published
A starter for Medusa plugins.
Maintainers
Readme
Features
- Rich-Text Editor: Built-in Tiptap editor with support for bold, italic, headings, lists, images, and YouTube embeds.
- SEO Ready: Automatic handle (slug) management for clean, searchable URLs.
- Publication Workflow: Support for
draftandpublishedstatuses. - Read Metrics: Built-in
read_countandread_timecalculation/storage. - Categorization: Support for tags and short descriptions.
- Admin Dashboard: Full CRUD interface for managing posts within the Medusa Admin.
Compatibility
- Medusa v2 (>= 2.4.0)
- PostgreSQL
Installation
1. Install the Plugin
# Using npm
npm install medusa-blog-management
# Using yarn
yarn add medusa-blog-management2. Install Peer Dependencies (Required for Admin)
The Admin rich-text editor requires Tiptap dependencies to be installed in your main Medusa store:
npm install @tiptap/react @tiptap/starter-kit @tiptap/extension-image @tiptap/extension-youtube2. Configuration
Add the plugin to your medusa-config.ts. In Medusa v2, you need to register it in both modules (for database functionality) and plugins (for admin UI):
const { defineConfig } = require("@medusajs/framework/utils")
module.exports = defineConfig({
// ... rest of config
modules: {
blog: {
resolve: "medusa-blog-management",
},
},
plugins: [
{
resolve: "medusa-blog-management",
},
],
})[!IMPORTANT] Admin UI Not Showing? Make sure the plugin is registered in both
modulesandpluginsarrays. Themodulesentry enables database functionality, while thepluginsentry enables the admin UI routes.After adding the plugin:
- Rebuild the plugin:
cd medusa-blog-management && npm run build- Restart your Medusa server
- Hard refresh your browser (Ctrl+Shift+R or Cmd+Shift+R)
3. Database Migrations
Run the following command to create the blog_post table:
npx medusa db:migrateNote: Ensure your database environment variables (DB_URL or DB_HOST, DB_NAME, etc.) are set in your Medusa server's .env file.
Using Storefront Helpers
The plugin exports typed helpers for easy integration with your storefront.
import { getBlogList, getBlogPost } from "medusa-blog-management/helpers"
// Fetch list of posts
const { posts, count } = await getBlogList({
take: 10,
skip: 0
}, {
baseUrl: "https://api.yourstore.com",
publishableApiKey: "pk_..."
})
// Fetch single post
const { post } = await getBlogPost("post_handle_123", {
baseUrl: "https://api.yourstore.com",
publishableApiKey: "pk_..."
})Metadata Helper (Next.js Example)
import { getBlogMetadata } from "medusa-blog-management/helpers"
export async function generateMetadata({ params }) {
const { post } = await getBlogPost(params.id)
return getBlogMetadata(post)
}API Reference
Store API
List Posts
GET /store/blog-posts
- Supports pagination via
skipandtakequery parameters.
Get Post
GET /store/blog-posts/:id
- Retrieves a specific post and automatically increments its
read_count.
Admin API
Full CRUD Support
The plugin provides endpoints for creating, updating, and deleting posts under /admin/blog-posts.
Customization
The rich-text editor component is available for extension if you need to add custom Tiptap extensions.
Production Notes
Important Information
Type Safety: This plugin uses TypeScript strict mode and proper type definitions throughout. All API endpoints are fully typed with DTOs.
Input Validation: All POST/PUT endpoints use Zod schemas for validation. Invalid requests will return
400 Bad Requestwith detailed error messages.Error Handling: All API routes include comprehensive error handling with appropriate HTTP status codes:
400- Validation errors or bad requests404- Resource not found500- Internal server errors
Service Resolution: The service uses proper dependency injection patterns following Medusa v2 best practices. The
manageris correctly injected via the container.Database Transactions: The service properly handles database transactions through MedusaService's built-in transaction management.
API Endpoints
Admin API
Create Blog Post
POST /admin/blog-posts
Content-Type: application/json
{
"title": "My Blog Post",
"status": "draft", // optional, defaults to "draft"
"thumbnail": "https://example.com/image.jpg", // optional
"short_description": "Brief description", // optional
"read_time": 300, // optional, in seconds
"tags": ["tag1", "tag2"], // optional
"body": "<p>Content</p>", // optional
"created_by": "Author Name", // optional
"metadata": {} // optional
}Update Blog Post
POST /admin/blog-posts/:id
Content-Type: application/json
{
"title": "Updated Title", // all fields optional
"status": "published",
...
}Get Blog Post
GET /admin/blog-posts/:idList Blog Posts
GET /admin/blog-posts?skip=0&take=20Delete Blog Post
DELETE /admin/blog-posts/:idStore API
List Posts (Public)
GET /store/blog-posts?skip=0&take=10Get Post (Public, increments read_count)
GET /store/blog-posts/:idValidation Rules
title: Required, 1-500 charactershandle: Auto-generated from title if not provided, must be uniquestatus: Must be "draft" or "published"thumbnail: Must be a valid URL if providedshort_description: Max 1000 charactersread_time: Must be a non-negative integertags: Array of stringscreated_by: Max 255 characters
Troubleshooting
Error: "Cannot read properties of undefined (reading 'fork')"
Cause: This error occurs when the EntityManager is not available in the container. This typically happens when:
- The plugin isn't properly registered in
medusa-config.ts - The plugin wasn't rebuilt after code changes
- The Medusa server wasn't restarted
- The plugin isn't properly installed/linked in your main project
Solution: Follow these steps in order:
- Verify plugin registration in
medusa-config.ts:
const { defineConfig } = require("@medusajs/framework/utils")
module.exports = defineConfig({
// ... rest of config
modules: {
blog: {
resolve: "medusa-blog-management",
},
},
plugins: [
{
resolve: "medusa-blog-management",
},
],
})- Rebuild the plugin:
cd medusa-blog-management
npm run build- If using local development (yalc or file path), reinstall/link:
# If using yalc
cd medusa-blog-management
yalc push
# Then in your main project
yalc update medusa-blog-management
# Or if using npm/yarn link
cd medusa-blog-management
npm link
# Then in your main project
npm link medusa-blog-managementRestart your Medusa server completely (stop and start, don't just reload)
Verify database connection - Ensure your database environment variables are set:
# Check your .env file has:
DATABASE_URL=postgresql://...
# or
DB_HOST=...
DB_NAME=...
DB_USER=...
DB_PASSWORD=...- Check the build output - Ensure
.medusa/server/src/modules/blog/service.jsexists:
ls -la medusa-blog-management/.medusa/server/src/modules/blog/- Clear node_modules and reinstall (if still not working):
# In your main Medusa project
rm -rf node_modules package-lock.json
npm installIf the error persists, check the Medusa server logs for more details about module initialization.
Error: "Validation failed"
Cause: Request body doesn't match the expected schema.
Solution: Check the validation error response for specific field errors:
{
"message": "Validation failed",
"errors": [
{
"path": ["title"],
"message": "Title is required"
}
]
}Admin UI Not Loading / Blog Section Not Showing
Solution:
- Verify plugin registration: Ensure the plugin is registered in both
modulesandpluginsarrays inmedusa-config.ts:
modules: {
blog: {
resolve: "medusa-blog-management",
},
},
plugins: [
{
resolve: "medusa-blog-management",
},
],- Rebuild the plugin:
cd medusa-blog-management
npm run buildRestart your Medusa server completely
Ensure peer dependencies are installed in your main Medusa project:
npm install @tiptap/react @tiptap/starter-kit @tiptap/extension-image @tiptap/extension-youtubeClear browser cache and hard refresh (Ctrl+Shift+R or Cmd+Shift+R)
Check the browser console for any errors
Verify the admin export: The plugin should export admin routes. Check that
.medusa/server/src/admin/index.jsexists after building.
If the Blog section still doesn't appear, check:
- The plugin is installed:
npm list medusa-blog-management - The build output exists:
ls -la .medusa/server/src/admin/ - No TypeScript/build errors:
npm run buildcompletes successfully
Database Migration Issues
Solution:
- Ensure database connection is properly configured
- Run migrations:
npx medusa db:migrate- If migrations fail, check the migration file exists at:
src/modules/blog/migrations/Migration20260112153000.ts
TypeScript Errors
Solution: The plugin uses TypeScript strict mode. Ensure:
- Your project's TypeScript version is compatible (>= 5.6.2)
- All dependencies are properly installed
- Run
npm installto ensure all types are available
Development
Building the Plugin
npm run buildDevelopment Mode
npm run devThis will watch for changes and rebuild automatically.
Changelog
Version 0.0.7+
- Fixed critical service constructor bug causing manager undefined errors
- Added comprehensive input validation with Zod schemas
- Improved error handling with proper HTTP status codes
- Enhanced type safety throughout the codebase
- Removed all
anytypes and@ts-ignorecomments - Enabled TypeScript strict mode
- Added proper DTOs and type definitions
- Improved API route error handling
License
MIT
