|
1
|
--- |
|
2
|
sidebar_label: "prices-in-major-units" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `prices-in-major-units - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule disallows price values that look like they're in the smallest currency unit, since Medusa stores prices in major units. |
|
12
|
|
|
13
|
## Severity |
|
14
|
|
|
15
|
`warn`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
16
|
|
|
17
|
## What it Targets |
|
18
|
|
|
19
|
This rule targets price-like properties, such as `amount`, `unit_price`, or `total`. It reports values that look like cents, such as a value multiplied by `100`, an identifier named with a `cents` suffix, or a large integer literal. |
|
20
|
|
|
21
|
The following code is reported by the rule: |
|
22
|
|
|
23
|
```ts title="src/api/store/custom/route.ts" |
|
24
|
const item = { amount: price * 100 } |
|
25
|
``` |
|
26
|
|
|
27
|
Instead, use the value in major units: |
|
28
|
|
|
29
|
```ts title="src/api/store/custom/route.ts" |
|
30
|
const item = { amount: price } |
|
31
|
``` |
|
32
|
|
|
33
|
## Why it's Important |
|
34
|
|
|
35
|
Medusa expects prices in major units, such as `19.99` rather than `1999`. Passing a value in the smallest currency unit results in prices that are inflated by a factor of the currency's minor units. |
|
36
|
|
|
37
|
## Fixable |
|
38
|
|
|
39
|
This rule isn't auto-fixable. |
|
40
|
|
|
41
|
## Turn it Off |
|
42
|
|
|
43
|
To turn off this rule, set it to `off` in your ESLint configuration: |
|
44
|
|
|
45
|
```ts title="eslint.config.ts" |
|
46
|
import { defineConfig } from "eslint/config" |
|
47
|
import medusa from "@medusajs/eslint-plugin" |
|
48
|
|
|
49
|
export default defineConfig([ |
|
50
|
...medusa.configs.recommended, |
|
51
|
{ |
|
52
|
rules: { |
|
53
|
"@medusajs/prices-in-major-units": "off", |
|
54
|
}, |
|
55
|
}, |
|
56
|
]) |
|
57
|
``` |
|
58
|
|
|
59
|
Or disable it for a single line using an inline comment: |
|
60
|
|
|
61
|
```ts |
|
62
|
// eslint-disable-next-line @medusajs/prices-in-major-units |
|
63
|
const item = { amount: price * 100 } |
|
64
|
``` |