@minipim/sdk 0.3.1 → 0.4.1
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.
- package/README.md +28 -0
- package/dist/index.cjs +93 -4
- package/dist/index.d.cts +14 -6
- package/dist/index.d.ts +14 -6
- package/dist/index.js +85 -3
- package/dist/modifiers.cjs +120 -0
- package/dist/modifiers.d.cts +88 -0
- package/dist/modifiers.d.ts +88 -0
- package/dist/modifiers.js +87 -0
- package/dist/openapi.d.cts +1070 -125
- package/dist/openapi.d.ts +1070 -125
- package/package.json +14 -2
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// src/attributes.ts
|
|
2
|
+
function formatMoney(money, locale = "en-US") {
|
|
3
|
+
return new Intl.NumberFormat(locale, {
|
|
4
|
+
style: "currency",
|
|
5
|
+
currency: money.currency
|
|
6
|
+
}).format(money.amount_cents / 100);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// src/modifiers.ts
|
|
10
|
+
function asPriceAdjuster(value) {
|
|
11
|
+
if (!value || typeof value !== "object") return null;
|
|
12
|
+
const a = value;
|
|
13
|
+
if (a.adjuster === "fixed" && typeof a.amount_cents === "number" && typeof a.currency === "string") {
|
|
14
|
+
return { adjuster: "fixed", amount_cents: a.amount_cents, currency: a.currency };
|
|
15
|
+
}
|
|
16
|
+
if (a.adjuster === "percentage" && typeof a.basis_points === "number") {
|
|
17
|
+
return { adjuster: "percentage", basis_points: a.basis_points };
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
function asModifierConfig(config) {
|
|
22
|
+
if (!config || typeof config !== "object") return null;
|
|
23
|
+
const rawAdjusters = config.priceAdjusters;
|
|
24
|
+
if (!rawAdjusters || typeof rawAdjusters !== "object") return null;
|
|
25
|
+
const priceAdjusters = {};
|
|
26
|
+
for (const [key, raw] of Object.entries(rawAdjusters)) {
|
|
27
|
+
const pa = asPriceAdjuster(raw);
|
|
28
|
+
if (pa) priceAdjusters[key] = pa;
|
|
29
|
+
}
|
|
30
|
+
if (Object.keys(priceAdjusters).length === 0) return null;
|
|
31
|
+
const schemaVersion = config.schemaVersion;
|
|
32
|
+
return {
|
|
33
|
+
...typeof schemaVersion === "number" ? { schemaVersion } : {},
|
|
34
|
+
priceAdjusters
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function getModifierPriceAdjuster(modifier, choiceValue) {
|
|
38
|
+
return asModifierConfig(modifier.config)?.priceAdjusters[choiceValue] ?? null;
|
|
39
|
+
}
|
|
40
|
+
function getModifierChoices(modifier) {
|
|
41
|
+
const { validation } = modifier;
|
|
42
|
+
const options = validation && typeof validation === "object" ? validation.options : void 0;
|
|
43
|
+
if (!Array.isArray(options)) return [];
|
|
44
|
+
const adjusters = asModifierConfig(modifier.config)?.priceAdjusters ?? {};
|
|
45
|
+
const out = [];
|
|
46
|
+
for (const opt of options) {
|
|
47
|
+
if (!opt || typeof opt !== "object") continue;
|
|
48
|
+
const value = opt.value;
|
|
49
|
+
if (typeof value !== "string") continue;
|
|
50
|
+
const label = opt.label;
|
|
51
|
+
out.push({
|
|
52
|
+
value,
|
|
53
|
+
label: typeof label === "string" ? label : value,
|
|
54
|
+
priceAdjuster: adjusters[value] ?? null
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
function priceAdjusterCents(base, adjuster) {
|
|
60
|
+
return adjuster.adjuster === "fixed" ? adjuster.amount_cents : Math.round(base.amount_cents * adjuster.basis_points / 1e4);
|
|
61
|
+
}
|
|
62
|
+
function applyPriceAdjuster(base, adjuster) {
|
|
63
|
+
return {
|
|
64
|
+
amount_cents: base.amount_cents + priceAdjusterCents(base, adjuster),
|
|
65
|
+
currency: base.currency
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function formatPriceAdjuster(adjuster, locale = "en-US") {
|
|
69
|
+
if (adjuster.adjuster === "fixed") {
|
|
70
|
+
const money = formatMoney(
|
|
71
|
+
{ amount_cents: adjuster.amount_cents, currency: adjuster.currency },
|
|
72
|
+
locale
|
|
73
|
+
);
|
|
74
|
+
return adjuster.amount_cents > 0 ? `+${money}` : money;
|
|
75
|
+
}
|
|
76
|
+
const percent = adjuster.basis_points / 100;
|
|
77
|
+
return percent > 0 ? `+${percent}%` : `${percent}%`;
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
applyPriceAdjuster,
|
|
81
|
+
asModifierConfig,
|
|
82
|
+
asPriceAdjuster,
|
|
83
|
+
formatPriceAdjuster,
|
|
84
|
+
getModifierChoices,
|
|
85
|
+
getModifierPriceAdjuster,
|
|
86
|
+
priceAdjusterCents
|
|
87
|
+
};
|