@hartl-services/medusa-storefront 0.0.0-stage → 0.2.0

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 (194) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +103 -2
  3. package/dist/browser/account-verification-channel.d.ts +11 -0
  4. package/dist/browser/account-verification-channel.js +32 -0
  5. package/dist/browser/actions.d.ts +53 -0
  6. package/dist/browser/actions.js +1 -0
  7. package/dist/browser/affiliate-referral.d.ts +152 -0
  8. package/dist/browser/affiliate-referral.js +85 -0
  9. package/dist/browser/browser-session.d.ts +3 -0
  10. package/dist/browser/browser-session.js +27 -0
  11. package/dist/browser/cart-mutations.d.ts +36 -0
  12. package/dist/browser/cart-mutations.js +137 -0
  13. package/dist/browser/cart-refresh.d.ts +11 -0
  14. package/dist/browser/cart-refresh.js +33 -0
  15. package/dist/browser/client.d.ts +15 -0
  16. package/dist/browser/client.js +20 -0
  17. package/dist/browser/fresh-order-marker.d.ts +11 -0
  18. package/dist/browser/fresh-order-marker.js +30 -0
  19. package/dist/browser/index.d.ts +946 -0
  20. package/dist/browser/index.js +70 -0
  21. package/dist/browser/query-invalidation.d.ts +4 -0
  22. package/dist/browser/query-invalidation.js +39 -0
  23. package/dist/browser/query-keys.d.ts +43 -0
  24. package/dist/browser/query-keys.js +28 -0
  25. package/dist/browser/query-options.d.ts +781 -0
  26. package/dist/browser/query-options.js +165 -0
  27. package/dist/browser/region.d.ts +5 -0
  28. package/dist/browser/region.js +15 -0
  29. package/dist/browser/unwrap-medusa-action.d.ts +7 -0
  30. package/dist/browser/unwrap-medusa-action.js +25 -0
  31. package/dist/core/action-result.d.ts +17 -0
  32. package/dist/core/action-result.js +12 -0
  33. package/dist/core/addresses.d.ts +46 -0
  34. package/dist/core/addresses.js +137 -0
  35. package/dist/core/affiliate/affiliate-application-errors.d.ts +2 -0
  36. package/dist/core/affiliate/affiliate-application-errors.js +14 -0
  37. package/dist/core/affiliate/affiliate-commission-presentation.d.ts +15 -0
  38. package/dist/core/affiliate/affiliate-commission-presentation.js +28 -0
  39. package/dist/core/affiliate/affiliate-coupon-code-presentation.d.ts +10 -0
  40. package/dist/core/affiliate/affiliate-coupon-code-presentation.js +22 -0
  41. package/dist/core/affiliate/affiliate-coupon-label.d.ts +3 -0
  42. package/dist/core/affiliate/affiliate-coupon-label.js +4 -0
  43. package/dist/core/affiliate/affiliate-date-range.d.ts +6 -0
  44. package/dist/core/affiliate/affiliate-date-range.js +16 -0
  45. package/dist/core/affiliate/affiliate-rate-presentation.d.ts +1 -0
  46. package/dist/core/affiliate/affiliate-rate-presentation.js +8 -0
  47. package/dist/core/affiliate/partner-company-address.d.ts +24 -0
  48. package/dist/core/affiliate/partner-company-address.js +31 -0
  49. package/dist/core/auth/customer-sign-in.d.ts +36 -0
  50. package/dist/core/auth/customer-sign-in.js +55 -0
  51. package/dist/core/auth/link-token.d.ts +8 -0
  52. package/dist/core/auth/link-token.js +11 -0
  53. package/dist/core/auth/login-code-auto-send.d.ts +7 -0
  54. package/dist/core/auth/login-code-auto-send.js +11 -0
  55. package/dist/core/auth/password-rule-label.d.ts +4 -0
  56. package/dist/core/auth/password-rule-label.js +13 -0
  57. package/dist/core/cart/cart-line-item-image-url.d.ts +9 -0
  58. package/dist/core/cart/cart-line-item-image-url.js +14 -0
  59. package/dist/core/cart/cart-normalization.d.ts +2 -0
  60. package/dist/core/cart/cart-normalization.js +11 -0
  61. package/dist/core/cart/cart-price-breakdown.d.ts +30 -0
  62. package/dist/core/cart/cart-price-breakdown.js +45 -0
  63. package/dist/core/cart/cart-tax-breakdown.d.ts +17 -0
  64. package/dist/core/cart/cart-tax-breakdown.js +18 -0
  65. package/dist/core/cart/coupon-promotions.d.ts +8 -0
  66. package/dist/core/cart/coupon-promotions.js +9 -0
  67. package/dist/core/cart/payment-session.d.ts +3 -0
  68. package/dist/core/cart/payment-session.js +12 -0
  69. package/dist/core/cart/refunded-cart.d.ts +27 -0
  70. package/dist/core/cart/refunded-cart.js +46 -0
  71. package/dist/core/catalog/category-filter.d.ts +16 -0
  72. package/dist/core/catalog/category-filter.js +33 -0
  73. package/dist/core/catalog/inventory.d.ts +10 -0
  74. package/dist/core/catalog/inventory.js +9 -0
  75. package/dist/core/catalog/medusa-product-image.d.ts +8 -0
  76. package/dist/core/catalog/medusa-product-image.js +13 -0
  77. package/dist/core/catalog/product-price.d.ts +8 -0
  78. package/dist/core/catalog/product-price.js +21 -0
  79. package/dist/core/catalog/selected-variant.d.ts +7 -0
  80. package/dist/core/catalog/selected-variant.js +9 -0
  81. package/dist/core/checkout/checkout-account-offer-state.d.ts +41 -0
  82. package/dist/core/checkout/checkout-account-offer-state.js +32 -0
  83. package/dist/core/checkout/checkout-address-draft.d.ts +39 -0
  84. package/dist/core/checkout/checkout-address-draft.js +78 -0
  85. package/dist/core/checkout/checkout-address-form.d.ts +24 -0
  86. package/dist/core/checkout/checkout-address-form.js +99 -0
  87. package/dist/core/checkout/checkout-coupon.d.ts +9 -0
  88. package/dist/core/checkout/checkout-coupon.js +13 -0
  89. package/dist/core/checkout/checkout-steps.d.ts +20 -0
  90. package/dist/core/checkout/checkout-steps.js +44 -0
  91. package/dist/core/checkout/customer-login-required.d.ts +12 -0
  92. package/dist/core/checkout/customer-login-required.js +14 -0
  93. package/dist/core/checkout/payment-authorization.d.ts +8 -0
  94. package/dist/core/checkout/payment-authorization.js +9 -0
  95. package/dist/core/checkout/stripe-redirect-return.d.ts +13 -0
  96. package/dist/core/checkout/stripe-redirect-return.js +29 -0
  97. package/dist/core/errors.d.ts +12 -0
  98. package/dist/core/errors.js +18 -0
  99. package/dist/core/fields.d.ts +18 -0
  100. package/dist/core/fields.js +20 -0
  101. package/dist/core/form-values.d.ts +162 -0
  102. package/dist/core/form-values.js +234 -0
  103. package/dist/core/format/money.d.ts +7 -0
  104. package/dist/core/format/money.js +6 -0
  105. package/dist/core/i18n.d.ts +24 -0
  106. package/dist/core/i18n.js +15 -0
  107. package/dist/core/image-url.d.ts +18 -0
  108. package/dist/core/image-url.js +34 -0
  109. package/dist/core/index.d.ts +48 -0
  110. package/dist/core/index.js +49 -0
  111. package/dist/core/order/order-payment-summary.d.ts +15 -0
  112. package/dist/core/order/order-payment-summary.js +48 -0
  113. package/dist/core/plugin-features.d.ts +21 -0
  114. package/dist/core/plugin-features.js +30 -0
  115. package/dist/core/query-string.d.ts +6 -0
  116. package/dist/core/query-string.js +16 -0
  117. package/dist/core/region.d.ts +11 -0
  118. package/dist/core/region.js +13 -0
  119. package/dist/core/report-error.d.ts +12 -0
  120. package/dist/core/report-error.js +1 -0
  121. package/dist/core/routing-locale.d.ts +1 -0
  122. package/dist/core/routing-locale.js +25 -0
  123. package/dist/core/settle-within.d.ts +8 -0
  124. package/dist/core/settle-within.js +14 -0
  125. package/dist/core/tax-compliance/address-tax-identification.d.ts +37 -0
  126. package/dist/core/tax-compliance/address-tax-identification.js +51 -0
  127. package/dist/core/tax-compliance/tax-identification.d.ts +18 -0
  128. package/dist/core/tax-compliance/tax-identification.js +58 -0
  129. package/dist/core/texts-de.d.ts +136 -0
  130. package/dist/core/texts-de.js +152 -0
  131. package/dist/react/cart-error-boundary.d.ts +24 -0
  132. package/dist/react/cart-error-boundary.js +57 -0
  133. package/dist/react/context.d.ts +17 -0
  134. package/dist/react/context.js +46 -0
  135. package/dist/react/index.d.ts +12 -0
  136. package/dist/react/index.js +14 -0
  137. package/dist/react/use-account.d.ts +214 -0
  138. package/dist/react/use-account.js +294 -0
  139. package/dist/react/use-affiliate.d.ts +566 -0
  140. package/dist/react/use-affiliate.js +573 -0
  141. package/dist/react/use-auth.d.ts +136 -0
  142. package/dist/react/use-auth.js +402 -0
  143. package/dist/react/use-cart.d.ts +57 -0
  144. package/dist/react/use-cart.js +137 -0
  145. package/dist/react/use-checkout-address-form.d.ts +156 -0
  146. package/dist/react/use-checkout-address-form.js +95 -0
  147. package/dist/react/use-checkout.d.ts +268 -0
  148. package/dist/react/use-checkout.js +873 -0
  149. package/dist/react/use-countdown.d.ts +5 -0
  150. package/dist/react/use-countdown.js +23 -0
  151. package/dist/react/use-link-token.d.ts +12 -0
  152. package/dist/react/use-link-token.js +17 -0
  153. package/dist/react/use-plugin-feature.d.ts +16 -0
  154. package/dist/react/use-plugin-feature.js +31 -0
  155. package/dist/react/use-verification-watch.d.ts +7 -0
  156. package/dist/react/use-verification-watch.js +49 -0
  157. package/dist/registry/account.json +248 -0
  158. package/dist/registry/affiliate.json +302 -0
  159. package/dist/registry/cart.json +127 -0
  160. package/dist/registry/checkout.json +169 -0
  161. package/dist/registry/form.json +145 -0
  162. package/dist/registry/registry.json +917 -0
  163. package/dist/registry/setup.json +76 -0
  164. package/dist/server/affiliate.d.ts +124 -0
  165. package/dist/server/affiliate.js +145 -0
  166. package/dist/server/cart.d.ts +10 -0
  167. package/dist/server/cart.js +113 -0
  168. package/dist/server/catalog.d.ts +28 -0
  169. package/dist/server/catalog.js +82 -0
  170. package/dist/server/checkout.d.ts +6 -0
  171. package/dist/server/checkout.js +12 -0
  172. package/dist/server/context.d.ts +38 -0
  173. package/dist/server/context.js +34 -0
  174. package/dist/server/customer.d.ts +84 -0
  175. package/dist/server/customer.js +362 -0
  176. package/dist/server/documents.d.ts +29 -0
  177. package/dist/server/documents.js +55 -0
  178. package/dist/server/index.d.ts +258 -0
  179. package/dist/server/index.js +45 -0
  180. package/dist/server/orders.d.ts +9 -0
  181. package/dist/server/orders.js +31 -0
  182. package/dist/server/plugin-features.d.ts +13 -0
  183. package/dist/server/plugin-features.js +52 -0
  184. package/dist/server/region.d.ts +6 -0
  185. package/dist/server/region.js +17 -0
  186. package/dist/server/route-response.d.ts +2 -0
  187. package/dist/server/route-response.js +17 -0
  188. package/dist/server/run-medusa-action.d.ts +7 -0
  189. package/dist/server/run-medusa-action.js +40 -0
  190. package/dist/server/session.d.ts +29 -0
  191. package/dist/server/session.js +106 -0
  192. package/dist/server/tax-compliance.d.ts +26 -0
  193. package/dist/server/tax-compliance.js +85 -0
  194. package/package.json +86 -3
@@ -0,0 +1,162 @@
1
+ import type { CustomerPasswordPolicy } from "@hartl-services/medusa-contracts/schemas/password-policy";
2
+ import { z } from "zod";
3
+ import type { Translate } from "./i18n.js";
4
+ export declare const customerAuthSchema: z.ZodObject<{
5
+ email: z.ZodString;
6
+ password: z.ZodString;
7
+ }, z.core.$strip>;
8
+ export declare function createNewPasswordSchema(t: Translate, policy: CustomerPasswordPolicy | null): z.ZodString;
9
+ export declare function createCustomerRegistrationSchema(t: Translate, policy: CustomerPasswordPolicy | null): z.ZodObject<{
10
+ email: z.ZodString;
11
+ password: z.ZodString;
12
+ }, z.core.$strip>;
13
+ /** The address a registration may carry (stored as the customer's default address). */
14
+ export declare const registrationAddressSchema: z.ZodObject<{
15
+ address_1: z.ZodString;
16
+ city: z.ZodString;
17
+ country_code: z.ZodString;
18
+ postal_code: z.ZodString;
19
+ }, z.core.$strip>;
20
+ /** What the server takes for a registration with profile (and optional address). */
21
+ export declare const customerRegistrationProfileSchema: z.ZodObject<{
22
+ email: z.ZodString;
23
+ password: z.ZodString;
24
+ address: z.ZodOptional<z.ZodObject<{
25
+ address_1: z.ZodString;
26
+ city: z.ZodString;
27
+ country_code: z.ZodString;
28
+ postal_code: z.ZodString;
29
+ }, z.core.$strip>>;
30
+ first_name: z.ZodString;
31
+ last_name: z.ZodString;
32
+ phone: z.ZodString;
33
+ }, z.core.$strip>;
34
+ /**
35
+ * The registration form with profile, address, a repeated password and the
36
+ * accepted terms. Address fields are all-or-nothing: none or a complete
37
+ * address.
38
+ */
39
+ export declare function createCustomerRegistrationFormSchema(t: Translate, policy: CustomerPasswordPolicy | null): z.ZodObject<{
40
+ accept_terms: z.ZodBoolean;
41
+ address_1: z.ZodString;
42
+ city: z.ZodString;
43
+ confirmPassword: z.ZodString;
44
+ country_code: z.ZodString;
45
+ email: z.ZodString;
46
+ first_name: z.ZodString;
47
+ last_name: z.ZodString;
48
+ password: z.ZodString;
49
+ phone: z.ZodString;
50
+ postal_code: z.ZodString;
51
+ }, z.core.$strip>;
52
+ /** The registration form's values as the server takes them. */
53
+ export declare function toRegistrationProfile(value: CustomerRegistrationFormValues): CustomerRegistrationProfileValues;
54
+ export declare function createPasswordResetSchema(t: Translate, policy: CustomerPasswordPolicy | null): z.ZodObject<{
55
+ confirmPassword: z.ZodString;
56
+ password: z.ZodString;
57
+ }, z.core.$strip>;
58
+ export declare function createVerificationResendSchema(t: Translate): z.ZodObject<{
59
+ email: z.ZodString;
60
+ }, z.core.$strip>;
61
+ export declare const createLoginCodeRequestSchema: typeof createVerificationResendSchema;
62
+ export declare function createLoginCodeSchema(t: Translate): z.ZodObject<{
63
+ code: z.ZodString;
64
+ }, z.core.$strip>;
65
+ export declare const customerAddressSchema: z.ZodObject<{
66
+ address_1: z.ZodString;
67
+ address_2: z.ZodString;
68
+ address_name: z.ZodString;
69
+ city: z.ZodString;
70
+ company: z.ZodString;
71
+ country_code: z.ZodString;
72
+ first_name: z.ZodString;
73
+ last_name: z.ZodString;
74
+ phone: z.ZodString;
75
+ postal_code: z.ZodString;
76
+ province: z.ZodString;
77
+ vat_number: z.ZodString;
78
+ }, z.core.$strip>;
79
+ /**
80
+ * Stricter address for a company: company and everything the UID check
81
+ * compares are required; the UID itself is optional (not every company has
82
+ * one), but checked for length when given.
83
+ */
84
+ export declare function createCompanyAddressSchema(t: Translate): z.ZodObject<{
85
+ address_2: z.ZodString;
86
+ address_name: z.ZodString;
87
+ country_code: z.ZodString;
88
+ first_name: z.ZodString;
89
+ last_name: z.ZodString;
90
+ phone: z.ZodString;
91
+ province: z.ZodString;
92
+ address_1: z.ZodString;
93
+ city: z.ZodString;
94
+ company: z.ZodString;
95
+ postal_code: z.ZodString;
96
+ vat_number: z.ZodString;
97
+ }, z.core.$strip>;
98
+ export declare const customerProfileSchema: z.ZodObject<{
99
+ first_name: z.ZodString;
100
+ last_name: z.ZodString;
101
+ phone: z.ZodString;
102
+ }, z.core.$strip>;
103
+ type CheckoutBillingRoleValues = {
104
+ billing_address: {
105
+ country_code: string;
106
+ };
107
+ has_different_billing_address: boolean;
108
+ shipping_address: {
109
+ country_code: string;
110
+ };
111
+ };
112
+ /**
113
+ * The UID belongs to the billing address (the shipping address while billing
114
+ * is the same). A company in the merchant's country is never checked, so the
115
+ * UID is asked only outside it; without a known merchant country everywhere.
116
+ */
117
+ export declare function asksForVatNumber(values: CheckoutBillingRoleValues, merchantCountryCode: string | null): boolean;
118
+ /** True for an address outside the merchant country, and for every address while that country is unknown. */
119
+ export declare function isOutsideMerchantCountry(countryCode: string, merchantCountryCode: string | null): boolean;
120
+ /**
121
+ * Checkout address rules; a UID that is not asked for (see asksForVatNumber)
122
+ * is ignored, and so is every UID while B2B sales are off (`hasB2b`).
123
+ */
124
+ export declare const createCheckoutAddressSchema: (t: Translate, merchantCountryCode: string | null, hasB2b?: boolean) => z.ZodObject<{
125
+ email: z.ZodString;
126
+ has_different_billing_address: z.ZodBoolean;
127
+ shipping_address: z.ZodObject<{
128
+ address_1: z.ZodString;
129
+ city: z.ZodString;
130
+ country_code: z.ZodString;
131
+ postal_code: z.ZodString;
132
+ first_name: z.ZodString;
133
+ last_name: z.ZodString;
134
+ phone: z.ZodString;
135
+ address_2: z.ZodString;
136
+ company: z.ZodString;
137
+ province: z.ZodString;
138
+ }, z.core.$strip>;
139
+ billing_address: z.ZodObject<{
140
+ address_1: z.ZodString;
141
+ city: z.ZodString;
142
+ country_code: z.ZodString;
143
+ postal_code: z.ZodString;
144
+ first_name: z.ZodString;
145
+ last_name: z.ZodString;
146
+ phone: z.ZodString;
147
+ address_2: z.ZodString;
148
+ company: z.ZodString;
149
+ province: z.ZodString;
150
+ }, z.core.$strip>;
151
+ vat_number: z.ZodString;
152
+ }, z.core.$strip>;
153
+ export type CustomerAuthValues = z.infer<typeof customerAuthSchema>;
154
+ export type CustomerRegistrationProfileValues = z.infer<typeof customerRegistrationProfileSchema>;
155
+ export type CustomerRegistrationFormValues = z.infer<ReturnType<typeof createCustomerRegistrationFormSchema>>;
156
+ export type LoginCodeRequestValues = z.infer<ReturnType<typeof createLoginCodeRequestSchema>>;
157
+ export type PasswordResetValues = z.infer<ReturnType<typeof createPasswordResetSchema>>;
158
+ export type VerificationResendValues = z.infer<ReturnType<typeof createVerificationResendSchema>>;
159
+ export type CustomerAddressValues = z.infer<typeof customerAddressSchema>;
160
+ export type CustomerProfileValues = z.infer<typeof customerProfileSchema>;
161
+ export type CheckoutAddressValues = z.infer<ReturnType<typeof createCheckoutAddressSchema>>;
162
+ export {};
@@ -0,0 +1,234 @@
1
+ import { unmetPasswordRulesMessage } from "@hartl-services/medusa-contracts/schemas/password-policy";
2
+ import { LOGIN_CODE_LENGTH } from "@hartl-services/medusa-contracts/schemas/customer-auth";
3
+ import { z } from "zod";
4
+ // Login checks no length, so older, shorter passwords keep working.
5
+ export const customerAuthSchema = z.object({
6
+ email: z.string().trim().min(1),
7
+ password: z.string().min(1),
8
+ });
9
+ // The backend enforces the shop's password rules on registration and reset;
10
+ // the same rules here only give the earlier hint. While the policy is unknown
11
+ // (null: not loaded or failed to load) only a non-empty password is required,
12
+ // and the backend's 400 message tells the rest. The unmet-rules message comes
13
+ // from the contracts package, like the backend's own.
14
+ export function createNewPasswordSchema(t, policy) {
15
+ if (!policy) {
16
+ return z.string().min(1, t("form.password_required"));
17
+ }
18
+ return z.string().superRefine((password, context) => {
19
+ const message = unmetPasswordRulesMessage(policy, password);
20
+ if (message) {
21
+ context.addIssue({ code: "custom", message });
22
+ }
23
+ });
24
+ }
25
+ export function createCustomerRegistrationSchema(t, policy) {
26
+ return customerAuthSchema.extend({
27
+ password: createNewPasswordSchema(t, policy),
28
+ });
29
+ }
30
+ /** The address a registration may carry (stored as the customer's default address). */
31
+ export const registrationAddressSchema = z.object({
32
+ address_1: z.string().trim().min(1),
33
+ city: z.string().trim().min(1),
34
+ country_code: z.string().trim().min(1),
35
+ postal_code: z.string().trim().min(1),
36
+ });
37
+ /** What the server takes for a registration with profile (and optional address). */
38
+ export const customerRegistrationProfileSchema = customerAuthSchema.extend({
39
+ address: registrationAddressSchema.optional(),
40
+ first_name: z.string().trim().min(1),
41
+ last_name: z.string().trim().min(1),
42
+ phone: z.string().trim(),
43
+ });
44
+ /**
45
+ * The registration form with profile, address, a repeated password and the
46
+ * accepted terms. Address fields are all-or-nothing: none or a complete
47
+ * address.
48
+ */
49
+ export function createCustomerRegistrationFormSchema(t, policy) {
50
+ return z
51
+ .object({
52
+ accept_terms: z.boolean().refine((accepted) => accepted, t("registration.terms_required")),
53
+ address_1: z.string().trim(),
54
+ city: z.string().trim(),
55
+ confirmPassword: z.string(),
56
+ country_code: z.string().trim(),
57
+ email: z.string().trim().min(1, t("form.email_required")).email(t("form.email_invalid")),
58
+ first_name: z.string().trim().min(1, t("address.first_name_required")),
59
+ last_name: z.string().trim().min(1, t("address.last_name_required")),
60
+ password: createNewPasswordSchema(t, policy),
61
+ phone: z.string().trim(),
62
+ postal_code: z.string().trim(),
63
+ })
64
+ .superRefine((value, context) => {
65
+ if (value.password !== value.confirmPassword) {
66
+ context.addIssue({ code: "custom", message: t("form.passwords_mismatch"), path: ["confirmPassword"] });
67
+ }
68
+ const addressFields = [
69
+ ["address_1", "address.address_1_required"],
70
+ ["postal_code", "address.postal_code_required"],
71
+ ["city", "address.city_required"],
72
+ ["country_code", "address.country_required"],
73
+ ];
74
+ if (addressFields.some(([field]) => value[field])) {
75
+ for (const [field, code] of addressFields) {
76
+ if (!value[field]) {
77
+ context.addIssue({ code: "custom", message: t(code), path: [field] });
78
+ }
79
+ }
80
+ }
81
+ });
82
+ }
83
+ /** The registration form's values as the server takes them. */
84
+ export function toRegistrationProfile(value) {
85
+ const hasAddress = Boolean(value.address_1 && value.postal_code && value.city && value.country_code);
86
+ return {
87
+ email: value.email.trim(),
88
+ first_name: value.first_name.trim(),
89
+ last_name: value.last_name.trim(),
90
+ password: value.password,
91
+ phone: value.phone.trim(),
92
+ ...(hasAddress ? { address: { address_1: value.address_1.trim(), city: value.city.trim(), country_code: value.country_code.trim().toLowerCase(), postal_code: value.postal_code.trim() } } : {}),
93
+ };
94
+ }
95
+ export function createPasswordResetSchema(t, policy) {
96
+ return z
97
+ .object({
98
+ confirmPassword: z.string(),
99
+ password: createNewPasswordSchema(t, policy),
100
+ })
101
+ .refine((value) => value.password === value.confirmPassword, {
102
+ message: t("form.passwords_mismatch"),
103
+ path: ["confirmPassword"],
104
+ });
105
+ }
106
+ export function createVerificationResendSchema(t) {
107
+ return z.object({
108
+ email: z.string().trim().min(1, t("form.email_required")).email(t("form.email_invalid")),
109
+ });
110
+ }
111
+ export const createLoginCodeRequestSchema = createVerificationResendSchema;
112
+ export function createLoginCodeSchema(t) {
113
+ return z.object({
114
+ code: z.string().regex(new RegExp(`^\\d{${LOGIN_CODE_LENGTH}}$`), t("form.login_code_invalid", { length: LOGIN_CODE_LENGTH })),
115
+ });
116
+ }
117
+ export const customerAddressSchema = z.object({
118
+ address_1: z.string(),
119
+ address_2: z.string(),
120
+ address_name: z.string(),
121
+ city: z.string(),
122
+ company: z.string(),
123
+ country_code: z.string().trim().min(1),
124
+ first_name: z.string(),
125
+ last_name: z.string(),
126
+ phone: z.string(),
127
+ postal_code: z.string(),
128
+ province: z.string(),
129
+ vat_number: z.string(),
130
+ });
131
+ /**
132
+ * Stricter address for a company: company and everything the UID check
133
+ * compares are required; the UID itself is optional (not every company has
134
+ * one), but checked for length when given.
135
+ */
136
+ export function createCompanyAddressSchema(t) {
137
+ return customerAddressSchema.extend({
138
+ address_1: z.string().trim().min(1, t("company_address.address_required")),
139
+ city: z.string().trim().min(1, t("company_address.city_required")),
140
+ company: z.string().trim().min(1, t("company_address.company_required")),
141
+ postal_code: z.string().trim().min(1, t("company_address.postal_code_required")),
142
+ vat_number: z.string().refine((value) => {
143
+ const vatNumber = value.replace(/\s+/g, "");
144
+ return !vatNumber || (vatNumber.length >= 4 && vatNumber.length <= 32);
145
+ }, t("company_address.vat_number_invalid")),
146
+ });
147
+ }
148
+ export const customerProfileSchema = z.object({
149
+ first_name: z.string(),
150
+ last_name: z.string(),
151
+ phone: z.string(),
152
+ });
153
+ const checkoutAddressFields = customerAddressSchema.pick({
154
+ address_1: true,
155
+ address_2: true,
156
+ city: true,
157
+ company: true,
158
+ country_code: true,
159
+ first_name: true,
160
+ last_name: true,
161
+ phone: true,
162
+ postal_code: true,
163
+ province: true,
164
+ });
165
+ const REQUIRED_ADDRESS_FIELDS = [
166
+ ["first_name", "address.first_name_required"],
167
+ ["last_name", "address.last_name_required"],
168
+ ["address_1", "address.address_1_required"],
169
+ ["postal_code", "address.postal_code_required"],
170
+ ["city", "address.city_required"],
171
+ ];
172
+ function checkRequiredAddressFields(t, address, path, context) {
173
+ for (const [field, code] of REQUIRED_ADDRESS_FIELDS) {
174
+ if (address[field].trim().length === 0) {
175
+ context.addIssue({
176
+ code: z.ZodIssueCode.custom,
177
+ message: t(code),
178
+ path: [path, field],
179
+ });
180
+ }
181
+ }
182
+ }
183
+ /**
184
+ * The UID belongs to the billing address (the shipping address while billing
185
+ * is the same). A company in the merchant's country is never checked, so the
186
+ * UID is asked only outside it; without a known merchant country everywhere.
187
+ */
188
+ export function asksForVatNumber(values, merchantCountryCode) {
189
+ return isOutsideMerchantCountry((values.has_different_billing_address ? values.billing_address : values.shipping_address).country_code, merchantCountryCode);
190
+ }
191
+ /** True for an address outside the merchant country, and for every address while that country is unknown. */
192
+ export function isOutsideMerchantCountry(countryCode, merchantCountryCode) {
193
+ return !merchantCountryCode || countryCode.trim().toLowerCase() !== merchantCountryCode.trim().toLowerCase();
194
+ }
195
+ /**
196
+ * Checkout address rules; a UID that is not asked for (see asksForVatNumber)
197
+ * is ignored, and so is every UID while B2B sales are off (`hasB2b`).
198
+ */
199
+ export const createCheckoutAddressSchema = (t, merchantCountryCode, hasB2b = true) => z
200
+ .object({
201
+ email: z.string().trim().min(1),
202
+ has_different_billing_address: z.boolean(),
203
+ shipping_address: checkoutAddressFields,
204
+ billing_address: checkoutAddressFields,
205
+ /** UID of the billing address (the shipping address while billing is the same); stored in its metadata. */
206
+ vat_number: z.string(),
207
+ })
208
+ .superRefine((value, context) => {
209
+ checkRequiredAddressFields(t, value.shipping_address, "shipping_address", context);
210
+ const vatNumber = hasB2b && asksForVatNumber(value, merchantCountryCode) ? value.vat_number.replace(/\s+/g, "") : "";
211
+ if (vatNumber) {
212
+ const billingRole = value.has_different_billing_address ? "billing_address" : "shipping_address";
213
+ if (vatNumber.length < 4 || vatNumber.length > 32) {
214
+ context.addIssue({ code: z.ZodIssueCode.custom, message: t("address.vat_number_invalid"), path: ["vat_number"] });
215
+ }
216
+ if (!value[billingRole].company.trim()) {
217
+ context.addIssue({
218
+ code: z.ZodIssueCode.custom,
219
+ message: t("address.company_required_for_vat_number"),
220
+ path: [billingRole, "company"],
221
+ });
222
+ }
223
+ }
224
+ if (value.has_different_billing_address) {
225
+ checkRequiredAddressFields(t, value.billing_address, "billing_address", context);
226
+ if (value.billing_address.country_code.trim().length === 0) {
227
+ context.addIssue({
228
+ code: z.ZodIssueCode.custom,
229
+ message: t("address.country_required"),
230
+ path: ["billing_address", "country_code"],
231
+ });
232
+ }
233
+ }
234
+ });
@@ -0,0 +1,7 @@
1
+ interface FormatMoneyInput {
2
+ amount: number;
3
+ currencyCode: string;
4
+ locale: string;
5
+ }
6
+ export declare function formatMoney({ amount, currencyCode, locale }: FormatMoneyInput): string;
7
+ export {};
@@ -0,0 +1,6 @@
1
+ export function formatMoney({ amount, currencyCode, locale }) {
2
+ return new Intl.NumberFormat(locale, {
3
+ style: "currency",
4
+ currency: currencyCode.toUpperCase(),
5
+ }).format(amount);
6
+ }
@@ -0,0 +1,24 @@
1
+ import type { TextCode } from "./texts-de.js";
2
+ export type { TextCode };
3
+ export type TextParams = Readonly<Record<string, string | number>>;
4
+ /** Every storefront text by code; a shop passes its own set or overrides single codes. */
5
+ export type StorefrontTexts = {
6
+ readonly [Code in TextCode]: string;
7
+ };
8
+ /**
9
+ * Turns a text code into the shop's wording. `locale` is the BCP 47 tag the
10
+ * texts are written in; it drives the Intl formatting that belongs to them
11
+ * (country names, sorting, percentages).
12
+ */
13
+ export interface Translate {
14
+ (code: TextCode, params?: TextParams): string;
15
+ readonly locale: string;
16
+ /**
17
+ * For a message that may be a code or already a text (an error message from
18
+ * the backend, which the storefront shows as it is): the text of a known
19
+ * code, anything else unchanged.
20
+ */
21
+ readonly message: (value: string, params?: TextParams) => string;
22
+ }
23
+ export declare function createTranslator(texts: StorefrontTexts, locale: string, overrides?: Partial<StorefrontTexts>): Translate;
24
+ export declare function isTextCode(texts: StorefrontTexts, value: string): value is TextCode;
@@ -0,0 +1,15 @@
1
+ export function createTranslator(texts, locale, overrides = {}) {
2
+ const merged = { ...texts, ...overrides };
3
+ const translate = (code, params) => formatText(merged[code], params);
4
+ const message = (value, params) => (isTextCode(merged, value) ? translate(value, params) : value);
5
+ return Object.assign(translate, { locale, message });
6
+ }
7
+ function formatText(template, params) {
8
+ if (!params) {
9
+ return template;
10
+ }
11
+ return template.replace(/\{(\w+)\}/g, (placeholder, name) => (Object.hasOwn(params, name) ? String(params[name]) : placeholder));
12
+ }
13
+ export function isTextCode(texts, value) {
14
+ return Object.hasOwn(texts, value);
15
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Image URL helpers bound to the shop's public Medusa URL (never the
3
+ * server-only one), since the result is rendered into <img src> in the
4
+ * browser, even when this runs server-side.
5
+ */
6
+ export declare function createMedusaImageUrls(publicBackendUrl: string): {
7
+ /**
8
+ * Resolves a potentially relative Medusa image URL to an absolute URL.
9
+ * Medusa stores some media paths as relative URLs.
10
+ */
11
+ resolveMedusaImageUrl(url: string | null | undefined): string;
12
+ /**
13
+ * Whether next/image may optimize this URL: the shop's next.config.ts
14
+ * allows exactly the public Medusa backend as a remote image host, so any
15
+ * other host must render unoptimized instead of failing in the optimizer.
16
+ */
17
+ isMedusaHostedImageUrl(url: string): boolean;
18
+ };
@@ -0,0 +1,34 @@
1
+ function trimTrailingSlash(value) {
2
+ return value.replace(/\/+$/, "");
3
+ }
4
+ /**
5
+ * Image URL helpers bound to the shop's public Medusa URL (never the
6
+ * server-only one), since the result is rendered into <img src> in the
7
+ * browser, even when this runs server-side.
8
+ */
9
+ export function createMedusaImageUrls(publicBackendUrl) {
10
+ const backendUrl = trimTrailingSlash(publicBackendUrl);
11
+ return {
12
+ /**
13
+ * Resolves a potentially relative Medusa image URL to an absolute URL.
14
+ * Medusa stores some media paths as relative URLs.
15
+ */
16
+ resolveMedusaImageUrl(url) {
17
+ if (!url) {
18
+ return "";
19
+ }
20
+ if (url.startsWith("http://") || url.startsWith("https://")) {
21
+ return url;
22
+ }
23
+ return `${backendUrl}${url}`;
24
+ },
25
+ /**
26
+ * Whether next/image may optimize this URL: the shop's next.config.ts
27
+ * allows exactly the public Medusa backend as a remote image host, so any
28
+ * other host must render unoptimized instead of failing in the optimizer.
29
+ */
30
+ isMedusaHostedImageUrl(url) {
31
+ return url.startsWith(`${backendUrl}/`);
32
+ },
33
+ };
34
+ }
@@ -0,0 +1,48 @@
1
+ export * from "./addresses.js";
2
+ export * from "./affiliate/affiliate-application-errors.js";
3
+ export * from "./affiliate/affiliate-commission-presentation.js";
4
+ export * from "./affiliate/affiliate-coupon-code-presentation.js";
5
+ export * from "./affiliate/affiliate-coupon-label.js";
6
+ export * from "./affiliate/affiliate-date-range.js";
7
+ export * from "./affiliate/affiliate-rate-presentation.js";
8
+ export * from "./affiliate/partner-company-address.js";
9
+ export * from "./auth/customer-sign-in.js";
10
+ export * from "./auth/link-token.js";
11
+ export * from "./auth/login-code-auto-send.js";
12
+ export * from "./auth/password-rule-label.js";
13
+ export * from "./cart/cart-line-item-image-url.js";
14
+ export * from "./cart/cart-normalization.js";
15
+ export * from "./cart/cart-price-breakdown.js";
16
+ export * from "./cart/cart-tax-breakdown.js";
17
+ export * from "./cart/coupon-promotions.js";
18
+ export * from "./cart/payment-session.js";
19
+ export * from "./cart/refunded-cart.js";
20
+ export * from "./catalog/category-filter.js";
21
+ export * from "./catalog/inventory.js";
22
+ export * from "./catalog/medusa-product-image.js";
23
+ export * from "./catalog/product-price.js";
24
+ export * from "./catalog/selected-variant.js";
25
+ export * from "./checkout/checkout-account-offer-state.js";
26
+ export * from "./checkout/checkout-address-form.js";
27
+ export * from "./checkout/checkout-address-draft.js";
28
+ export * from "./checkout/checkout-coupon.js";
29
+ export * from "./checkout/checkout-steps.js";
30
+ export * from "./checkout/customer-login-required.js";
31
+ export * from "./checkout/payment-authorization.js";
32
+ export * from "./checkout/stripe-redirect-return.js";
33
+ export * from "./errors.js";
34
+ export * from "./action-result.js";
35
+ export * from "./report-error.js";
36
+ export * from "./fields.js";
37
+ export * from "./form-values.js";
38
+ export * from "./format/money.js";
39
+ export * from "./i18n.js";
40
+ export * from "./image-url.js";
41
+ export * from "./order/order-payment-summary.js";
42
+ export * from "./plugin-features.js";
43
+ export * from "./region.js";
44
+ export * from "./query-string.js";
45
+ export * from "./routing-locale.js";
46
+ export * from "./settle-within.js";
47
+ export * from "./tax-compliance/address-tax-identification.js";
48
+ export * from "./tax-compliance/tax-identification.js";
@@ -0,0 +1,49 @@
1
+ // Framework-free storefront logic: runs on the server, in the browser and in tests.
2
+ export * from "./addresses.js";
3
+ export * from "./affiliate/affiliate-application-errors.js";
4
+ export * from "./affiliate/affiliate-commission-presentation.js";
5
+ export * from "./affiliate/affiliate-coupon-code-presentation.js";
6
+ export * from "./affiliate/affiliate-coupon-label.js";
7
+ export * from "./affiliate/affiliate-date-range.js";
8
+ export * from "./affiliate/affiliate-rate-presentation.js";
9
+ export * from "./affiliate/partner-company-address.js";
10
+ export * from "./auth/customer-sign-in.js";
11
+ export * from "./auth/link-token.js";
12
+ export * from "./auth/login-code-auto-send.js";
13
+ export * from "./auth/password-rule-label.js";
14
+ export * from "./cart/cart-line-item-image-url.js";
15
+ export * from "./cart/cart-normalization.js";
16
+ export * from "./cart/cart-price-breakdown.js";
17
+ export * from "./cart/cart-tax-breakdown.js";
18
+ export * from "./cart/coupon-promotions.js";
19
+ export * from "./cart/payment-session.js";
20
+ export * from "./cart/refunded-cart.js";
21
+ export * from "./catalog/category-filter.js";
22
+ export * from "./catalog/inventory.js";
23
+ export * from "./catalog/medusa-product-image.js";
24
+ export * from "./catalog/product-price.js";
25
+ export * from "./catalog/selected-variant.js";
26
+ export * from "./checkout/checkout-account-offer-state.js";
27
+ export * from "./checkout/checkout-address-form.js";
28
+ export * from "./checkout/checkout-address-draft.js";
29
+ export * from "./checkout/checkout-coupon.js";
30
+ export * from "./checkout/checkout-steps.js";
31
+ export * from "./checkout/customer-login-required.js";
32
+ export * from "./checkout/payment-authorization.js";
33
+ export * from "./checkout/stripe-redirect-return.js";
34
+ export * from "./errors.js";
35
+ export * from "./action-result.js";
36
+ export * from "./report-error.js";
37
+ export * from "./fields.js";
38
+ export * from "./form-values.js";
39
+ export * from "./format/money.js";
40
+ export * from "./i18n.js";
41
+ export * from "./image-url.js";
42
+ export * from "./order/order-payment-summary.js";
43
+ export * from "./plugin-features.js";
44
+ export * from "./region.js";
45
+ export * from "./query-string.js";
46
+ export * from "./routing-locale.js";
47
+ export * from "./settle-within.js";
48
+ export * from "./tax-compliance/address-tax-identification.js";
49
+ export * from "./tax-compliance/tax-identification.js";
@@ -0,0 +1,15 @@
1
+ import type { HttpTypes } from "@medusajs/types";
2
+ type OrderPayment = Pick<NonNullable<HttpTypes.StorePaymentCollection["payments"]>[number], "amount" | "authorized_amount" | "canceled_at" | "captured_amount" | "id" | "provider_id">;
3
+ type PaymentAmountKind = "authorized" | "captured";
4
+ type PaymentCollectionSource = Pick<HttpTypes.StorePaymentCollection, "authorized_amount" | "captured_amount" | "status"> & {
5
+ payments?: OrderPayment[];
6
+ };
7
+ type PaymentSummaryPayment = OrderPayment & {
8
+ actualAmount?: number;
9
+ };
10
+ export declare function getOrderPaymentSummary(paymentCollections: PaymentCollectionSource[] | undefined, paymentStatus: string): {
11
+ amountKind: PaymentAmountKind | null;
12
+ payments: PaymentSummaryPayment[];
13
+ total: number | null;
14
+ };
15
+ export {};
@@ -0,0 +1,48 @@
1
+ function getPaymentAmountKind(paymentStatus) {
2
+ if (["captured", "partially_captured", "partially_refunded", "refunded"].includes(paymentStatus)) {
3
+ return "captured";
4
+ }
5
+ if (["authorized", "partially_authorized"].includes(paymentStatus)) {
6
+ return "authorized";
7
+ }
8
+ return null;
9
+ }
10
+ function isCanceledCollection(collection) {
11
+ return collection.status === "canceled" || collection.status === "failed";
12
+ }
13
+ function getActualPaymentAmount(payment, amountKind) {
14
+ if (payment.canceled_at) {
15
+ return undefined;
16
+ }
17
+ return amountKind === "captured" ? payment.captured_amount : payment.authorized_amount;
18
+ }
19
+ function getCollectionActualTotal(collection, amountKind) {
20
+ const payments = collection.payments ?? [];
21
+ const activePayments = payments.filter((payment) => !payment.canceled_at);
22
+ const hasCanceledPayment = activePayments.length !== payments.length;
23
+ const paymentAmounts = activePayments.map((payment) => getActualPaymentAmount(payment, amountKind));
24
+ if (activePayments.length > 0 && paymentAmounts.every((amount) => typeof amount === "number")) {
25
+ return paymentAmounts.reduce((total, amount) => total + amount, 0);
26
+ }
27
+ if (hasCanceledPayment) {
28
+ return undefined;
29
+ }
30
+ return amountKind === "captured" ? collection.captured_amount : collection.authorized_amount;
31
+ }
32
+ export function getOrderPaymentSummary(paymentCollections, paymentStatus) {
33
+ const amountKind = getPaymentAmountKind(paymentStatus);
34
+ const activeCollections = paymentCollections?.filter((collection) => !isCanceledCollection(collection)) ?? [];
35
+ const payments = activeCollections.flatMap((collection) => (collection.payments ?? [])
36
+ .filter((payment) => !payment.canceled_at)
37
+ .map((payment) => ({
38
+ ...payment,
39
+ actualAmount: amountKind ? getActualPaymentAmount(payment, amountKind) : undefined,
40
+ })));
41
+ const collectionTotals = amountKind ? activeCollections.map((collection) => getCollectionActualTotal(collection, amountKind)) : [];
42
+ const total = amountKind && collectionTotals.length > 0 && collectionTotals.every((amount) => typeof amount === "number") ? collectionTotals.reduce((sum, amount) => sum + amount, 0) : null;
43
+ return {
44
+ amountKind,
45
+ payments,
46
+ total,
47
+ };
48
+ }