medusa-strapi-plugin 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.medusa/server/src/admin/index.js +78 -0
  2. package/.medusa/server/src/admin/index.mjs +77 -0
  3. package/.medusa/server/src/api/admin/strapi/sync/route.js +25 -0
  4. package/.medusa/server/src/links/strapi-categories.js +21 -0
  5. package/.medusa/server/src/links/strapi-collections.js +21 -0
  6. package/.medusa/server/src/links/strapi-product-variants.js +21 -0
  7. package/.medusa/server/src/links/strapi-products.js +21 -0
  8. package/.medusa/server/src/modules/strapi/index.js +15 -0
  9. package/.medusa/server/src/modules/strapi/loader/create-content-models.js +92 -0
  10. package/.medusa/server/src/modules/strapi/service.js +336 -0
  11. package/.medusa/server/src/subscribers/create-category.js +19 -0
  12. package/.medusa/server/src/subscribers/create-collection.js +19 -0
  13. package/.medusa/server/src/subscribers/create-product.js +19 -0
  14. package/.medusa/server/src/subscribers/create-variant.js +19 -0
  15. package/.medusa/server/src/subscribers/delete-category.js +19 -0
  16. package/.medusa/server/src/subscribers/delete-collection.js +19 -0
  17. package/.medusa/server/src/subscribers/delete-product.js +19 -0
  18. package/.medusa/server/src/subscribers/delete-variant.js +19 -0
  19. package/.medusa/server/src/subscribers/sync-categories.js +39 -0
  20. package/.medusa/server/src/subscribers/sync-collections.js +39 -0
  21. package/.medusa/server/src/subscribers/sync-products.js +39 -0
  22. package/.medusa/server/src/subscribers/update-category.js +19 -0
  23. package/.medusa/server/src/subscribers/update-collection.js +19 -0
  24. package/.medusa/server/src/subscribers/update-product.js +19 -0
  25. package/.medusa/server/src/subscribers/update-variant.js +19 -0
  26. package/.medusa/server/src/workflows/delete-categories-strapi.js +12 -0
  27. package/.medusa/server/src/workflows/delete-collections-strapi.js +12 -0
  28. package/.medusa/server/src/workflows/delete-product-variants-strapi.js +12 -0
  29. package/.medusa/server/src/workflows/delete-products-strapi.js +12 -0
  30. package/.medusa/server/src/workflows/steps/delete-categories-strapi.js +19 -0
  31. package/.medusa/server/src/workflows/steps/delete-collections-strapi.js +19 -0
  32. package/.medusa/server/src/workflows/steps/delete-product-variants-strapi.js +19 -0
  33. package/.medusa/server/src/workflows/steps/delete-products-strapi.js +19 -0
  34. package/.medusa/server/src/workflows/steps/upsert-categories-strapi.js +57 -0
  35. package/.medusa/server/src/workflows/steps/upsert-collections-strapi.js +57 -0
  36. package/.medusa/server/src/workflows/steps/upsert-product-variants-strapi.js +59 -0
  37. package/.medusa/server/src/workflows/steps/upsert-products-strapi.js +57 -0
  38. package/.medusa/server/src/workflows/upsert-categories-strapi.js +20 -0
  39. package/.medusa/server/src/workflows/upsert-collections-strapi.js +20 -0
  40. package/.medusa/server/src/workflows/upsert-product-variants-strapi.js +20 -0
  41. package/.medusa/server/src/workflows/upsert-products-strapi.js +26 -0
  42. package/README.md +335 -0
  43. package/package.json +90 -0
package/README.md ADDED
@@ -0,0 +1,335 @@
1
+ <p align="center">
2
+ <a href="https://www.medusajs.com">
3
+ <picture>
4
+ <source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/59018053/229103275-b5e482bb-4601-46e6-8142-244f531cebdb.svg">
5
+ <source media="(prefers-color-scheme: light)" srcset="https://user-images.githubusercontent.com/59018053/229103726-e5b529a3-9b3f-4970-8a1f-c6af37f087bf.svg">
6
+ <img alt="Medusa logo" src="https://user-images.githubusercontent.com/59018053/229103726-e5b529a3-9b3f-4970-8a1f-c6af37f087bf.svg">
7
+ </picture>
8
+ </a>
9
+ </p>
10
+ <h1 align="center">
11
+ Medusa Plugin - Strapi
12
+ </h1>
13
+
14
+ <h4 align="center">
15
+ <a href="https://docs.medusajs.com">Documentation</a> |
16
+ <a href="https://www.medusajs.com">Website</a>
17
+ </h4>
18
+
19
+ <p align="center">
20
+ A plugin for implementing Strapi as CMS for Medusa
21
+ </p>
22
+
23
+ ## What You'll Achieve
24
+
25
+ After completing this setup, you'll have:
26
+
27
+ - ✅ **Automatic sync** between Medusa and Strapi
28
+ - ✅ **Rich content management** for products in Strapi
29
+ - ✅ **Extended product data** with custom fields and media
30
+ - ✅ **Unified API** to query products with CMS content
31
+ - ✅ **Scalable architecture** for omnichannel commerce
32
+
33
+ ## Features
34
+
35
+ - 🔄 Seamless integration between Medusa and Strapi
36
+ - 📝 Flexible content management for your e-commerce store
37
+ - 🖼️ Rich media management for product images and assets
38
+ - 🚀 Extend product information with custom fields and content
39
+ - 📱 Headless architecture for omnichannel commerce
40
+ - 🔄 Automatic synchronization between Medusa and Strapi
41
+
42
+ ## Requirements
43
+
44
+ This plugin requires:
45
+
46
+ - [Medusa](https://docs.medusajs.com/) version >= 2.8.0
47
+ - [Strapi v5](https://docs.strapi.io/) (latest stable)
48
+ - Node.js >= 18.x
49
+ - A running Medusa application
50
+ - A running Strapi application
51
+
52
+ ## Prerequisites
53
+
54
+ Before starting, make sure you have:
55
+
56
+ 1. **A Medusa application** set up and running
57
+ - If you don't have one, follow the [Medusa quickstart guide](https://docs.medusajs.com/create-medusa-app)
58
+ 2. **A Strapi application** set up and running
59
+ - If you don't have one, create it with: `npx create-strapi-app@latest my-strapi-cms`
60
+ 3. **Basic knowledge** of:
61
+ - Medusa concepts (products, variants, collections, categories)
62
+ - Strapi content types and API tokens
63
+ - Environment variables
64
+
65
+ ## Quick Start Guide
66
+
67
+ > **⏱️ Estimated time:** 15-30 minutes
68
+ > **🎯 Difficulty:** Beginner to Intermediate
69
+ > **📋 You'll need:** Medusa app, Strapi app, and basic terminal knowledge
70
+
71
+ ### Step 1: Install the Plugin
72
+
73
+ ```bash
74
+ npm install @devx-commerce/strapi @strapi/client
75
+ # or
76
+ yarn add @devx-commerce/strapi @strapi/client
77
+ ```
78
+
79
+ ### Step 2: Configure Medusa
80
+
81
+ Add the plugin to your `medusa-config.js`:
82
+
83
+ ```js
84
+ const { defineConfig } = require("@medusajs/medusa"); // or "@medusajs/framework/utils"
85
+
86
+ module.exports = defineConfig({
87
+ // ... other config
88
+ plugins: [
89
+ // ... other plugins,
90
+ {
91
+ resolve: "@devx-commerce/strapi",
92
+ options: {
93
+ base_url: process.env.STRAPI_URL,
94
+ api_key: process.env.STRAPI_API_KEY,
95
+ },
96
+ },
97
+ ],
98
+ });
99
+ ```
100
+
101
+ ### Step 3: Set Up Strapi
102
+
103
+ #### 3.1 Create or Configure Your Strapi Project
104
+
105
+ 1. Install Strapi if you haven't already:
106
+
107
+ ```bash
108
+ npx create-strapi-app@latest my-strapi-cms
109
+ ```
110
+
111
+ 2. Start your Strapi server:
112
+
113
+ ```bash
114
+ cd my-strapi-cms
115
+ npm run develop
116
+ ```
117
+
118
+ #### 3.2 Create an API Token
119
+
120
+ - Go to **Settings > API Tokens**
121
+ - Create a new full access token
122
+ - Copy the token to use in your Medusa configuration
123
+
124
+ #### 3.3 Create Content Types
125
+
126
+ You have two options to create the content types:
127
+
128
+ ### Option A: Using Schema Files (Recommended)
129
+
130
+ This method is less error-prone and ensures exact field matching:
131
+
132
+ - Copy the schema files from the `strapi-schemas/` directory in this plugin to your Strapi project
133
+ - Place them in `src/api/[content-type-name]/content-types/[content-type-name]/schema.json`
134
+
135
+ **Required content types and their locations:**
136
+
137
+ - **Product**: `src/api/product/content-types/product/schema.json`
138
+ - **Product Variant**: `src/api/product-variant/content-types/product-variant/schema.json`
139
+ - **Category**: `src/api/category/content-types/category/schema.json`
140
+ - **Collection**: `src/api/collection/content-types/collection/schema.json`
141
+
142
+ After adding the schema files, restart your Strapi server to apply the changes.
143
+
144
+ ### Option B: Using Strapi Admin Panel (Manual)
145
+
146
+ If you prefer to create content types manually through the Strapi admin interface:
147
+
148
+ 1. **Start your Strapi server**: `npm run develop` or `yarn develop`
149
+ 2. **Access Strapi Admin**: Go to `http://localhost:1337/admin`
150
+ 3. **Navigate to Content-Type Builder** in the sidebar
151
+
152
+ #### Create Product Content Type:
153
+
154
+ 1. Click **"Create new collection type"**
155
+ 2. **Display name**: "Product"
156
+ 3. **API ID (singular)**: "product"
157
+ 4. **Add these fields**:
158
+ - `title` (Text, Required)
159
+ - `systemId` (Text, Required, Unique)
160
+ - `handle` (Text)
161
+ - `productType` (Text)
162
+ 5. **Save** the content type
163
+
164
+ #### Create Product Variant Content Type:
165
+
166
+ 1. Click **"Create new collection type"**
167
+ 2. **Display name**: "Product Variant"
168
+ 3. **API ID (singular)**: "product-variant" ⚠️ **Important: Use hyphen, not underscore**
169
+ 4. **Add these fields**:
170
+ - `title` (Text, Required)
171
+ - `systemId` (Text, Required, Unique)
172
+ - `sku` (Text)
173
+ 5. **Add relation field**:
174
+ - **Field name**: "product"
175
+ - **Relation type**: Many-to-One
176
+ - **Target**: Product
177
+ 6. **Save** the content type
178
+
179
+ #### Create Category Content Type:
180
+
181
+ 1. Click **"Create new collection type"**
182
+ 2. **Display name**: "Category"
183
+ 3. **API ID (singular)**: "category"
184
+ 4. **Add these fields**:
185
+ - `title` (Text, Required)
186
+ - `systemId` (Text, Required, Unique)
187
+ - `handle` (Text)
188
+ 5. **Save** the content type
189
+
190
+ #### Create Collection Content Type:
191
+
192
+ 1. Click **"Create new collection type"**
193
+ 2. **Display name**: "Collection"
194
+ 3. **API ID (singular)**: "collection"
195
+ 4. **Add these fields**:
196
+ - `title` (Text, Required)
197
+ - `systemId` (Text, Required, Unique)
198
+ - `handle` (Text)
199
+ 5. **Save** the content type
200
+
201
+ #### Complete Product Relations:
202
+
203
+ After creating Product Variant, go back to the **Product** content type and add:
204
+
205
+ - **Field name**: "variants"
206
+ - **Relation type**: One-to-Many
207
+ - **Target**: Product Variant
208
+
209
+ **⚠️ Critical Notes for Manual Creation:**
210
+
211
+ - Content type names must be exact: `product`, `product-variant`, `category`, `collection`
212
+ - Field names must match exactly: `title`, `systemId`, `handle`, `productType`, `sku`
213
+ - `systemId` field must be set as **Required** and **Unique**
214
+ - Relations must be properly configured between Product and Product Variant
215
+
216
+ **Required fields summary:**
217
+
218
+ - **Product**: `title`, `systemId` (unique), `handle`, `productType`, `variants` (relation)
219
+ - **Product Variant**: `title`, `systemId` (unique), `sku`, `product` (relation)
220
+ - **Category**: `title`, `systemId` (unique), `handle`
221
+ - **Collection**: `title`, `systemId` (unique), `handle`
222
+
223
+ ### Step 4: Configure Environment Variables
224
+
225
+ Add these variables to your Medusa `.env` file:
226
+
227
+ ```
228
+ STRAPI_URL=http://localhost:1337/api
229
+ STRAPI_API_KEY=your-api-token-here
230
+ ```
231
+
232
+ ### Step 5: Start Your Applications
233
+
234
+ 1. **Start Strapi**: `npm run develop` (in your Strapi directory)
235
+ 2. **Start Medusa**: `npm run dev` (in your Medusa directory)
236
+ 3. **Check the logs** for "Connected to Strapi" message
237
+
238
+ ### Step 6: Test the Integration
239
+
240
+ 1. **Create a test product** in Medusa Admin (`http://localhost:9000/app`)
241
+ 2. **Check Strapi Admin** (`http://localhost:1337/admin`) to see if the product appears
242
+ 3. **Add rich content** to the product in Strapi
243
+ 4. **Query the product** from your storefront to see the combined data
244
+
245
+ ## How It Works
246
+
247
+ After installation and setup, the plugin will automatically:
248
+
249
+ - Create and update products, collections & categories in Strapi when they are modified in Medusa
250
+ - Sync product, collection & category metadata between Medusa and Strapi
251
+ - Allow extending product data with Strapi's content types
252
+
253
+ ## Usage
254
+
255
+ Once the plugin is set up, you can use Strapi's admin panel to add rich content to your products and use the Strapi API to fetch this content for your storefront.
256
+
257
+ Example of fetching product content from Medusa (with Strapi fields):
258
+
259
+ ```js
260
+ // In your storefront using the Medusa JS SDK
261
+ import { sdk } from "@medusajs/js-sdk";
262
+
263
+ const medusa = sdk({
264
+ baseUrl: MEDUSA_BASE_URL,
265
+ publishableApiKey: STOREFRONT_PUBLISHABLE_API_KEY,
266
+ });
267
+
268
+ async function getProductContent(productId) {
269
+ const { product } = await medusa.store.product.retrieve(productId, {
270
+ fields: "cms_product.*",
271
+ });
272
+ return product;
273
+ }
274
+
275
+ // Alternative: Fetch product with Strapi content using query parameters
276
+ async function getProductWithStrapiContent(productId) {
277
+ const { product } = await medusa.store.product.retrieve(productId, {
278
+ fields: "+metadata.strapiId,+metadata.strapiSyncedAt",
279
+ });
280
+
281
+ // If you need to fetch additional Strapi content directly
282
+ if (product.metadata?.strapiId) {
283
+ // Use Strapi's API directly for rich content
284
+ const strapiResponse = await fetch(
285
+ `${STRAPI_BASE_URL}/products/${product.metadata.strapiId}`,
286
+ {
287
+ headers: {
288
+ Authorization: `Bearer ${STRAPI_API_KEY}`,
289
+ },
290
+ },
291
+ );
292
+ const strapiContent = await strapiResponse.json();
293
+ return { ...product, strapiContent: strapiContent.data };
294
+ }
295
+
296
+ return product;
297
+ }
298
+ ```
299
+
300
+ ## Troubleshooting
301
+
302
+ ### Common Issues
303
+
304
+ **1. Content Type Validation Errors**
305
+ If you see errors about missing content types or fields, ensure you've:
306
+
307
+ - Copied all schema files from `strapi-schemas/` to the correct locations in your Strapi project
308
+ - Restarted your Strapi server after adding the schema files
309
+ - Used the exact field names specified in the schemas
310
+
311
+ **2. HTTP 400/404 Errors**
312
+ The plugin now provides detailed error logging. Check your Medusa logs for:
313
+
314
+ - Full Strapi error responses
315
+ - Specific field validation errors
316
+ - Content type existence issues
317
+
318
+ **3. Product Variant Naming**
319
+ Ensure your Strapi content type is named `product-variant` (with hyphen), not just `Variant`.
320
+
321
+ **4. Missing Fields**
322
+ The plugin expects these exact field names:
323
+
324
+ - Products: `title`, `systemId`, `handle`, `productType`
325
+ - Product Variants: `title`, `systemId`, `sku`
326
+ - Categories: `title`, `systemId`, `handle`
327
+ - Collections: `title`, `systemId`, `handle`
328
+
329
+ ## Documentation
330
+
331
+ - [Medusa Documentation](https://docs.medusajs.com)
332
+ - [Strapi Documentation](https://docs.strapi.io)
333
+ - [Plugin Guide](./src/)
334
+
335
+ ---
package/package.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "name": "medusa-strapi-plugin",
3
+ "version": "0.0.12",
4
+ "description": "Medusa plugin for Strapi as CMS",
5
+ "author": "devx commerce (https://devxcommerce.com)",
6
+ "license": "MIT",
7
+ "files": [
8
+ ".medusa/server"
9
+ ],
10
+ "exports": {
11
+ "./package.json": "./package.json",
12
+ "./workflows": "./.medusa/server/src/workflows/index.js",
13
+ "./.medusa/server/src/modules/*": "./.medusa/server/src/modules/*/index.js",
14
+ "./modules/*": "./.medusa/server/src/modules/*/index.js",
15
+ "./providers/*": "./.medusa/server/src/providers/*/index.js",
16
+ "./*": "./.medusa/server/src/*.js",
17
+ "./admin": {
18
+ "import": "./.medusa/server/src/admin/index.mjs",
19
+ "require": "./.medusa/server/src/admin/index.js",
20
+ "default": "./.medusa/server/src/admin/index.js"
21
+ }
22
+ },
23
+ "keywords": [
24
+ "medusa-v2",
25
+ "medusa",
26
+ "plugin",
27
+ "medusa-plugin",
28
+ "medusa-plugin-cms",
29
+ "medusa-plugin-integration",
30
+ "medusa-plugin-strapi",
31
+ "strapi"
32
+ ],
33
+ "scripts": {
34
+ "build": "medusa plugin:build",
35
+ "dev": "medusa plugin:develop",
36
+ "prepublishOnly": "medusa plugin:build",
37
+ "release": "yarn standard-version"
38
+ },
39
+ "devDependencies": {
40
+ "@medusajs/admin-sdk": "^2.12.5",
41
+ "@medusajs/cli": "^2.12.5",
42
+ "@medusajs/core-flows": "^2.12.5",
43
+ "@medusajs/framework": "^2.12.5",
44
+ "@medusajs/icons": "^2.12.5",
45
+ "@medusajs/medusa": "^2.12.5",
46
+ "@medusajs/test-utils": "^2.12.5",
47
+ "@medusajs/ui": "^4.0.13",
48
+ "@mikro-orm/cli": "^6.4.16",
49
+ "@mikro-orm/core": "^6.4.16",
50
+ "@mikro-orm/knex": "^6.4.16",
51
+ "@mikro-orm/migrations": "^6.4.16",
52
+ "@mikro-orm/postgresql": "^6.4.16",
53
+ "@swc/core": "1.10.12",
54
+ "@types/node": "^22.13.4",
55
+ "@types/react": "^18.3.18",
56
+ "@types/react-dom": "^18.3.5",
57
+ "pg": "^8.13.0",
58
+ "prop-types": "^15.8.1",
59
+ "qs": "^6.14.0",
60
+ "react": "^18.3.1",
61
+ "react-dom": "^18.3.1",
62
+ "standard-version": "^9.5.0",
63
+ "ts-node": "^10.9.2",
64
+ "typescript": "^5.7.3",
65
+ "vite": "=5.2.11",
66
+ "yalc": "^1.0.0-pre.53"
67
+ },
68
+ "peerDependencies": {
69
+ "@medusajs/admin-sdk": "^2.12.5",
70
+ "@medusajs/cli": "^2.12.5",
71
+ "@medusajs/core-flows": "^2.12.5",
72
+ "@medusajs/framework": "^2.12.5",
73
+ "@medusajs/icons": "^2.12.5",
74
+ "@medusajs/medusa": "^2.12.5",
75
+ "@medusajs/test-utils": "^2.12.5",
76
+ "@medusajs/ui": "^4.0.13",
77
+ "@mikro-orm/cli": "^6.4.16",
78
+ "@mikro-orm/core": "^6.4.16",
79
+ "@mikro-orm/knex": "^6.4.16",
80
+ "@mikro-orm/migrations": "^6.4.16",
81
+ "@mikro-orm/postgresql": "^6.4.16",
82
+ "awilix": "^8.0.1",
83
+ "pg": "^8.13.0",
84
+ "qs": "^6.14.0"
85
+ },
86
+ "engines": {
87
+ "node": ">=20"
88
+ },
89
+ "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
90
+ }