Skip to content

Commit 6dc0b8b

Browse files
adrien2pNicolasGorgaolivermrbl
authored
feat(): Introduce translation module and preliminary application of them (medusajs#14189)
* feat(): Translation first steps * feat(): locale middleware * feat(): readonly links * feat(): feature flag * feat(): modules sdk * feat(): translation module re export * start adding workflows * update typings * update typings * test(): Add integration tests * test(): centralize filters preparation * test(): centralize filters preparation * remove unnecessary importy * fix workflows * Define StoreLocale inside Store Module * Link definition to extend Store with supported_locales * store_locale migration * Add supported_locales handling in Store Module * Tests * Accept supported_locales in Store endpoints * Add locales to js-sdk * Include locale list and default locale in Store Detail section * Initialize local namespace in js-sdk * Add locales route * Make code primary key of locale table to facilitate upserts * Add locales routes * Show locale code as is * Add list translations api route * Batch endpoint * Types * New batchTranslationsWorkflow and various updates to existent ones * Edit default locale UI * WIP * Apply translation agnostically * middleware * Apply translation agnostically * fix Apply translation agnostically * apply translations to product list * Add feature flag * fetch translations by batches of 250 max * fix apply * improve and test util * apply to product list * dont manage translations if no locale * normalize locale * potential todo * Protect translations routes with feature flag * Extract normalize locale util to core/utils * Normalize locale on write * Normalize locale for read * Use feature flag to guard translations UI across the board * Avoid throwing incorrectly when locale_code not present in partial updates * move applyTranslations util * remove old tests * fix util tests * fix(): product end points * cleanup * update lock * remove unused var * cleanup * fix apply locale * missing new dep for test utils * Change entity_type, entity_id to reference, reference_id * Remove comment * Avoid registering translations route if ff not enabled * Prevent registering express handler for disabled route via defineFileConfig * Add tests * Add changeset * Update test * fix integration tests, module and internals * Add locale id plus fixed * Allow to pass array of reference_id * fix unit tests * fix link loading * fix store route * fix sales channel test * fix tests --------- Co-authored-by: Nicolas Gorga <[email protected]> Co-authored-by: Oli Juhl <[email protected]>
1 parent fea3d4e commit 6dc0b8b

File tree

130 files changed

+5643
-106
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+5643
-106
lines changed

.changeset/nine-paths-relax.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/framework": patch
3+
---
4+
5+
fix(framework): Prevent registering express handler for disabled routes

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ packages/*
2626
!packages/create-medusa-app
2727
!packages/modules/product
2828
!packages/modules/locking
29+
!packages/modules/translation
2930
!packages/core/orchestration
3031
!packages/core/workflows-sdk
3132
!packages/core/core-flows

.eslintrc.js

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -93,56 +93,57 @@ module.exports = {
9393
"./packages/design-system/toolbox/tsconfig.json",
9494

9595
"./packages/cli/create-medusa-app/tsconfig.json",
96-
"./packages/cli/medusa-cli/tsconfig.spec.json",
97-
"./packages/cli/oas/medusa-oas-cli/tsconfig.spec.json",
96+
"./packages/cli/medusa-cli/tsconfig.json",
97+
"./packages/cli/oas/medusa-oas-cli/tsconfig.json",
9898

9999
"./packages/core/orchestration/tsconfig.json",
100-
"./packages/core/workflows-sdk/tsconfig.spec.json",
100+
"./packages/core/workflows-sdk/tsconfig.json",
101101
"./packages/core/modules-sdk/tsconfig.json",
102102
"./packages/core/js-sdk/tsconfig.json",
103103
"./packages/core/types/tsconfig.json",
104-
"./packages/core/utils/tsconfig.spec.json",
104+
"./packages/core/utils/tsconfig.json",
105105
"./packages/core/medusa-test-utils/tsconfig.json",
106106

107107
"./packages/modules/product/tsconfig.json",
108-
"./packages/modules/event-bus-local/tsconfig.spec.json",
109-
"./packages/modules/event-bus-redis/tsconfig.spec.json",
110-
"./packages/modules/cache-redis/tsconfig.spec.json",
111-
"./packages/modules/cache-inmemory/tsconfig.spec.json",
112-
"./packages/modules/caching/tsconfig.spec.json",
113-
"./packages/modules/workflow-engine-redis/tsconfig.spec.json",
114-
"./packages/modules/workflow-engine-inmemory/tsconfig.spec.json",
115-
"./packages/modules/fulfillment/tsconfig.spec.json",
116-
"./packages/modules/api-key/tsconfig.spec.json",
117-
"./packages/modules/auth/tsconfig.spec.json",
118-
"./packages/modules/cart/tsconfig.spec.json",
119-
"./packages/modules/currency/tsconfig.spec.json",
120-
"./packages/modules/index/tsconfig.spec.json",
121-
"./packages/modules/customer/tsconfig.spec.json",
122-
"./packages/modules/file/tsconfig.spec.json",
123-
"./packages/modules/inventory-next/tsconfig.spec.json",
124-
"./packages/modules/stock-location-next/tsconfig.spec.json",
125-
"./packages/modules/order/tsconfig.spec.json",
126-
"./packages/modules/payment/tsconfig.spec.json",
127-
"./packages/modules/pricing/tsconfig.spec.json",
128-
"./packages/modules/promotion/tsconfig.spec.json",
129-
"./packages/modules/region/tsconfig.spec.json",
130-
"./packages/modules/sales-channel/tsconfig.spec.json",
131-
"./packages/modules/store/tsconfig.spec.json",
132-
"./packages/modules/tax/tsconfig.spec.json",
133-
"./packages/modules/workflow-engine-inmemory/tsconfig.spec.json",
134-
"./packages/modules/workflow-engine-redis/tsconfig.spec.json",
135-
"./packages/modules/link-modules/tsconfig.spec.json",
136-
"./packages/modules/user/tsconfig.spec.json",
137-
"./packages/modules/locking/tsconfig.spec.json",
108+
"./packages/modules/event-bus-local/tsconfig.json",
109+
"./packages/modules/event-bus-redis/tsconfig.json",
110+
"./packages/modules/cache-redis/tsconfig.json",
111+
"./packages/modules/cache-inmemory/tsconfig.json",
112+
"./packages/modules/caching/tsconfig.json",
113+
"./packages/modules/workflow-engine-redis/tsconfig.json",
114+
"./packages/modules/workflow-engine-inmemory/tsconfig.json",
115+
"./packages/modules/fulfillment/tsconfig.json",
116+
"./packages/modules/api-key/tsconfig.json",
117+
"./packages/modules/auth/tsconfig.json",
118+
"./packages/modules/cart/tsconfig.json",
119+
"./packages/modules/currency/tsconfig.json",
120+
"./packages/modules/index/tsconfig.json",
121+
"./packages/modules/customer/tsconfig.json",
122+
"./packages/modules/file/tsconfig.json",
123+
"./packages/modules/inventory-next/tsconfig.json",
124+
"./packages/modules/stock-location-next/tsconfig.json",
125+
"./packages/modules/order/tsconfig.json",
126+
"./packages/modules/payment/tsconfig.json",
127+
"./packages/modules/pricing/tsconfig.json",
128+
"./packages/modules/promotion/tsconfig.json",
129+
"./packages/modules/region/tsconfig.json",
130+
"./packages/modules/sales-channel/tsconfig.json",
131+
"./packages/modules/store/tsconfig.json",
132+
"./packages/modules/tax/tsconfig.json",
133+
"./packages/modules/workflow-engine-inmemory/tsconfig.json",
134+
"./packages/modules/workflow-engine-redis/tsconfig.json",
135+
"./packages/modules/link-modules/tsconfig.json",
136+
"./packages/modules/user/tsconfig.json",
137+
"./packages/modules/locking/tsconfig.json",
138+
"./packages/modules/translation/tsconfig.json",
138139

139-
"./packages/modules/providers/file-local/tsconfig.spec.json",
140-
"./packages/modules/providers/file-s3/tsconfig.spec.json",
141-
"./packages/modules/providers/fulfillment-manual/tsconfig.spec.json",
142-
"./packages/modules/providers/payment-stripe/tsconfig.spec.json",
143-
"./packages/modules/providers/locking-postgres/tsconfig.spec.json",
144-
"./packages/modules/providers/locking-redis/tsconfig.spec.json",
145-
"./packages/modules/providers/caching-redis/tsconfig.spec.json",
140+
"./packages/modules/providers/file-local/tsconfig.json",
141+
"./packages/modules/providers/file-s3/tsconfig.json",
142+
"./packages/modules/providers/fulfillment-manual/tsconfig.json",
143+
"./packages/modules/providers/payment-stripe/tsconfig.json",
144+
"./packages/modules/providers/locking-postgres/tsconfig.json",
145+
"./packages/modules/providers/locking-redis/tsconfig.json",
146+
"./packages/modules/providers/caching-redis/tsconfig.json",
146147

147148
"./packages/framework/tsconfig.json",
148149
],

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ The code snippets in this section assume that your forked Medusa project and the
117117
"@medusajs/draft-order": "file:../medusa/packages/plugins/draft-order",
118118
"@medusajs/deps": "file:../medusa/packages/deps",
119119
"@medusajs/caching-redis": "file:../medusa/packages/modules/providers/caching-redis",
120-
"@medusajs/caching": "file:../medusa/packages/modules/caching"
120+
"@medusajs/caching": "file:../medusa/packages/modules/caching",
121+
"@medusajs/translation": "file:../medusa/packages/modules/translation",
121122
}
122123
```
123124

integration-tests/http/__fixtures__/feature-flag/src/api/custom/route.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ defineFileConfig({
88
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
99
res.json({ message: "Custom GET" })
1010
}
11+
12+
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
13+
res.json({ message: "Custom POST", body: req.validatedBody })
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
defineMiddlewares,
3+
validateAndTransformBody,
4+
} from "@medusajs/framework/http"
5+
import { z } from "zod"
6+
7+
const CustomPostSchema = z.object({
8+
foo: z.string(),
9+
})
10+
11+
export default defineMiddlewares({
12+
routes: [
13+
{
14+
method: ["POST"],
15+
matcher: "/custom",
16+
middlewares: [validateAndTransformBody(CustomPostSchema)],
17+
},
18+
],
19+
})

integration-tests/http/__tests__/feature-flag/resources-with-flags.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ medusaIntegrationTestRunner({
4343

4444
it("should load endpoint when feature flag is enabled", async () => {
4545
expect((await api.get("/custom")).status).toBe(200)
46+
expect(
47+
(
48+
await api.post("/custom", {
49+
foo: "test",
50+
})
51+
).status
52+
).toBe(200)
53+
})
54+
55+
it("should return 400 for POST route with invalid body when feature flag is enabled", async () => {
56+
const response = await api
57+
.post("/custom", {
58+
invalid: 1,
59+
})
60+
.catch((e) => e)
61+
62+
expect(response.status).toBe(400)
4663
})
4764
})
4865
},

integration-tests/http/__tests__/feature-flag/resources-without-flags.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ medusaIntegrationTestRunner({
4141
it("should not load endpoint when feature flag is disabled", async () => {
4242
expect(api.get("/custom")).rejects.toThrow()
4343
})
44+
45+
it("should return 404 (not 400) for POST route with middleware when feature flag is disabled", async () => {
46+
const { response } = await api
47+
.post("/custom", {
48+
invalid: "test",
49+
})
50+
.catch((e) => e)
51+
52+
expect(response.status).toBe(404)
53+
})
4454
})
4555
},
4656
})

integration-tests/http/__tests__/price-preference/admin/price-preference.spec.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,20 @@ medusaIntegrationTestRunner({
207207
})
208208
)
209209

210-
expect(remainingPricePreferences).toEqual([
211-
expect.objectContaining({
212-
attribute: "currency_code",
213-
value: "EUR",
214-
is_tax_inclusive: true,
215-
}),
216-
])
210+
expect(remainingPricePreferences).toEqual(
211+
expect.arrayContaining([
212+
expect.objectContaining({
213+
attribute: "currency_code",
214+
value: "EUR",
215+
is_tax_inclusive: true,
216+
}),
217+
expect.objectContaining({
218+
attribute: "currency_code",
219+
value: "eur",
220+
is_tax_inclusive: false,
221+
}),
222+
])
223+
)
217224
})
218225
})
219226
})

integration-tests/http/__tests__/sales-channel/admin/sales-channel.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ medusaIntegrationTestRunner({
6767

6868
expect(response.status).toEqual(200)
6969
expect(response.data.sales_channels).toBeTruthy()
70-
expect(response.data.sales_channels.length).toBe(2)
70+
expect(response.data.sales_channels.length).toBe(3) // includes the default sales channel
7171
expect(response.data).toEqual(
7272
expect.objectContaining({
7373
sales_channels: expect.arrayContaining([

0 commit comments

Comments
 (0)