@hszrdm/create-commerce 0.0.0-stage → 1.0.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 +50 -2
- package/bin/create-commerce.mjs +51 -0
- package/package.json +23 -4
- package/src/cli.mjs +585 -0
- package/templates/backend/.env.example +113 -0
- package/templates/backend/README.md +7 -0
- package/templates/backend/instrumentation.ts +24 -0
- package/templates/backend/medusa-config.ts +11 -0
- package/templates/backend/package.json +42 -0
- package/templates/backend/scripts/prepare-project.mjs +49 -0
- package/templates/backend/src/project/README.md +13 -0
- package/templates/backend/src/project/api/.gitkeep +0 -0
- package/templates/backend/src/project/jobs/.gitkeep +0 -0
- package/templates/backend/src/project/modules/.gitkeep +0 -0
- package/templates/backend/src/project/subscribers/.gitkeep +0 -0
- package/templates/backend/src/project/workflows/.gitkeep +0 -0
- package/templates/backend/src/scripts/core-init.ts +21 -0
- package/templates/backend/src/scripts/create-storefront-key.ts +12 -0
- package/templates/backend/src/scripts/verify-bootstrap.ts +1 -0
- package/templates/backend/tsconfig.json +33 -0
- package/templates/storefront/.env.example +5 -0
- package/templates/storefront/README.md +108 -0
- package/templates/storefront/next-env.d.ts +4 -0
- package/templates/storefront/next.config.ts +8 -0
- package/templates/storefront/package.json +24 -0
- package/templates/storefront/src/app/[countryCode]/categories/[handle]/page.tsx +38 -0
- package/templates/storefront/src/app/[countryCode]/products/[handle]/page.tsx +51 -0
- package/templates/storefront/src/app/[countryCode]/products/page.tsx +47 -0
- package/templates/storefront/src/app/account/orders/[id]/page.tsx +8 -0
- package/templates/storefront/src/app/account/page.tsx +11 -0
- package/templates/storefront/src/app/cart/page.tsx +12 -0
- package/templates/storefront/src/app/checkout/page.tsx +15 -0
- package/templates/storefront/src/app/checkout/success/page.tsx +72 -0
- package/templates/storefront/src/app/error.tsx +14 -0
- package/templates/storefront/src/app/globals.css +250 -0
- package/templates/storefront/src/app/layout.tsx +59 -0
- package/templates/storefront/src/app/legal/page.tsx +26 -0
- package/templates/storefront/src/app/loading.tsx +3 -0
- package/templates/storefront/src/app/not-found.tsx +14 -0
- package/templates/storefront/src/app/page.tsx +47 -0
- package/templates/storefront/src/app/reset-password/page.tsx +14 -0
- package/templates/storefront/src/app/robots.txt/route.ts +13 -0
- package/templates/storefront/src/app/sitemap.xml/route.ts +13 -0
- package/templates/storefront/src/components/account-dashboard.tsx +325 -0
- package/templates/storefront/src/components/add-to-cart.tsx +71 -0
- package/templates/storefront/src/components/analytics.tsx +90 -0
- package/templates/storefront/src/components/cart-provider.tsx +236 -0
- package/templates/storefront/src/components/cart-view.tsx +135 -0
- package/templates/storefront/src/components/checkout-flow.tsx +534 -0
- package/templates/storefront/src/components/consent-banner.tsx +131 -0
- package/templates/storefront/src/components/order-detail.tsx +273 -0
- package/templates/storefront/src/components/product-card.tsx +38 -0
- package/templates/storefront/src/components/product-view-tracker.tsx +37 -0
- package/templates/storefront/src/components/reset-password-form.tsx +65 -0
- package/templates/storefront/src/components/storefront-header.tsx +42 -0
- package/templates/storefront/src/components/stripe-payment.tsx +90 -0
- package/templates/storefront/src/lib/core/account.ts +170 -0
- package/templates/storefront/src/lib/core/after-sales.ts +50 -0
- package/templates/storefront/src/lib/core/catalog.ts +56 -0
- package/templates/storefront/src/lib/core/checkout.ts +330 -0
- package/templates/storefront/src/lib/core/client.ts +107 -0
- package/templates/storefront/src/lib/core/commerce.ts +148 -0
- package/templates/storefront/src/lib/core/consent.ts +111 -0
- package/templates/storefront/src/lib/core/legal.ts +33 -0
- package/templates/storefront/src/lib/core/seo.ts +88 -0
- package/templates/storefront/src/lib/core/tracking.ts +584 -0
- package/templates/storefront/src/lib/core/types.ts +123 -0
- package/templates/storefront/tsconfig.json +23 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { endpoint, medusaFetch } from "./client"
|
|
2
|
+
import type { CoreConfig } from "./types"
|
|
3
|
+
|
|
4
|
+
export function accountApi(config: CoreConfig) {
|
|
5
|
+
const paths = config.account.endpoints
|
|
6
|
+
|
|
7
|
+
const profile = () =>
|
|
8
|
+
medusaFetch<{ customer: Record<string, unknown> }>(paths.profile, {
|
|
9
|
+
cache: "no-store",
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
registrationToken: (email: string, password: string) =>
|
|
14
|
+
medusaFetch<{ token: string }>(paths.registration_token, {
|
|
15
|
+
method: "POST",
|
|
16
|
+
body: JSON.stringify({ email, password }),
|
|
17
|
+
}),
|
|
18
|
+
|
|
19
|
+
registerCustomer: (token: string, body: Record<string, unknown>) =>
|
|
20
|
+
medusaFetch<{ customer?: Record<string, unknown> }>(paths.register_customer, {
|
|
21
|
+
method: "POST",
|
|
22
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
23
|
+
body: JSON.stringify(body),
|
|
24
|
+
}),
|
|
25
|
+
|
|
26
|
+
register: async (input: {
|
|
27
|
+
email: string
|
|
28
|
+
password: string
|
|
29
|
+
first_name?: string
|
|
30
|
+
last_name?: string
|
|
31
|
+
phone?: string
|
|
32
|
+
}) => {
|
|
33
|
+
const token = await medusaFetch<{ token: string }>(paths.registration_token, {
|
|
34
|
+
method: "POST",
|
|
35
|
+
body: JSON.stringify({ email: input.email, password: input.password }),
|
|
36
|
+
})
|
|
37
|
+
await medusaFetch(paths.register_customer, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
headers: { Authorization: `Bearer ${token.token}` },
|
|
40
|
+
body: JSON.stringify({
|
|
41
|
+
email: input.email,
|
|
42
|
+
first_name: input.first_name || undefined,
|
|
43
|
+
last_name: input.last_name || undefined,
|
|
44
|
+
phone: input.phone || undefined,
|
|
45
|
+
}),
|
|
46
|
+
})
|
|
47
|
+
const login = await medusaFetch<{ token: string }>(paths.login, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
body: JSON.stringify({ email: input.email, password: input.password }),
|
|
50
|
+
})
|
|
51
|
+
await medusaFetch(paths.session, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
headers: { Authorization: `Bearer ${login.token}` },
|
|
54
|
+
})
|
|
55
|
+
return profile()
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
login: async (email: string, password: string) => {
|
|
59
|
+
const result = await medusaFetch<{ token: string }>(paths.login, {
|
|
60
|
+
method: "POST",
|
|
61
|
+
body: JSON.stringify({ email, password }),
|
|
62
|
+
})
|
|
63
|
+
await medusaFetch(paths.session, {
|
|
64
|
+
method: "POST",
|
|
65
|
+
headers: { Authorization: `Bearer ${result.token}` },
|
|
66
|
+
})
|
|
67
|
+
return result
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
logout: () => medusaFetch(paths.logout, { method: "DELETE" }),
|
|
71
|
+
|
|
72
|
+
profile,
|
|
73
|
+
|
|
74
|
+
updateProfile: (body: Record<string, unknown>) =>
|
|
75
|
+
medusaFetch<{ customer: Record<string, unknown> }>(paths.profile, {
|
|
76
|
+
method: "POST",
|
|
77
|
+
body: JSON.stringify(body),
|
|
78
|
+
}),
|
|
79
|
+
|
|
80
|
+
orders: () =>
|
|
81
|
+
medusaFetch<{ orders: Array<Record<string, unknown>> }>(
|
|
82
|
+
`${paths.orders}?limit=100&order=-created_at`,
|
|
83
|
+
{ cache: "no-store" },
|
|
84
|
+
),
|
|
85
|
+
|
|
86
|
+
order: (orderId: string) =>
|
|
87
|
+
medusaFetch<{ order: Record<string, unknown> }>(
|
|
88
|
+
`${endpoint(paths.order, { id: orderId })}?fields=+items,+shipping_methods,+fulfillments,+fulfillments.labels`,
|
|
89
|
+
{ cache: "no-store" },
|
|
90
|
+
),
|
|
91
|
+
|
|
92
|
+
addresses: () =>
|
|
93
|
+
medusaFetch<{
|
|
94
|
+
addresses: Array<Record<string, unknown>>
|
|
95
|
+
count?: number
|
|
96
|
+
}>(`${paths.addresses}?limit=100`, { cache: "no-store" }),
|
|
97
|
+
|
|
98
|
+
createAddress: (body: Record<string, unknown>) =>
|
|
99
|
+
medusaFetch(paths.addresses, {
|
|
100
|
+
method: "POST",
|
|
101
|
+
body: JSON.stringify(body),
|
|
102
|
+
}),
|
|
103
|
+
|
|
104
|
+
updateAddress: (addressId: string, body: Record<string, unknown>) =>
|
|
105
|
+
medusaFetch(endpoint(paths.address, { address_id: addressId }), {
|
|
106
|
+
method: "POST",
|
|
107
|
+
body: JSON.stringify(body),
|
|
108
|
+
}),
|
|
109
|
+
|
|
110
|
+
deleteAddress: (addressId: string) =>
|
|
111
|
+
medusaFetch(endpoint(paths.address, { address_id: addressId }), {
|
|
112
|
+
method: "DELETE",
|
|
113
|
+
}),
|
|
114
|
+
|
|
115
|
+
changePassword: (currentPassword: string, newPassword: string) =>
|
|
116
|
+
medusaFetch(paths.password_change, {
|
|
117
|
+
method: "POST",
|
|
118
|
+
body: JSON.stringify({
|
|
119
|
+
current_password: currentPassword,
|
|
120
|
+
new_password: newPassword,
|
|
121
|
+
}),
|
|
122
|
+
}),
|
|
123
|
+
|
|
124
|
+
requestPasswordReset: (email: string) =>
|
|
125
|
+
medusaFetch(paths.password_reset_request, {
|
|
126
|
+
method: "POST",
|
|
127
|
+
body: JSON.stringify({ identifier: email }),
|
|
128
|
+
}),
|
|
129
|
+
|
|
130
|
+
completePasswordReset: (token: string, email: string, password: string) =>
|
|
131
|
+
medusaFetch(paths.password_reset_complete, {
|
|
132
|
+
method: "POST",
|
|
133
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
134
|
+
body: JSON.stringify({ email, password }),
|
|
135
|
+
}),
|
|
136
|
+
|
|
137
|
+
requestEmailVerification: () =>
|
|
138
|
+
medusaFetch(paths.email_verification, {
|
|
139
|
+
method: "PUT",
|
|
140
|
+
body: JSON.stringify({}),
|
|
141
|
+
}),
|
|
142
|
+
|
|
143
|
+
confirmEmailVerification: (code: string) =>
|
|
144
|
+
medusaFetch(paths.email_verification, {
|
|
145
|
+
method: "POST",
|
|
146
|
+
body: JSON.stringify({ code }),
|
|
147
|
+
}),
|
|
148
|
+
|
|
149
|
+
requestEmailChange: (currentPassword: string, newEmail: string) =>
|
|
150
|
+
medusaFetch(paths.email_change, {
|
|
151
|
+
method: "POST",
|
|
152
|
+
body: JSON.stringify({
|
|
153
|
+
current_password: currentPassword,
|
|
154
|
+
new_email: newEmail,
|
|
155
|
+
}),
|
|
156
|
+
}),
|
|
157
|
+
|
|
158
|
+
confirmEmailChange: (code: string) =>
|
|
159
|
+
medusaFetch(paths.email_change, {
|
|
160
|
+
method: "PUT",
|
|
161
|
+
body: JSON.stringify({ code }),
|
|
162
|
+
}),
|
|
163
|
+
|
|
164
|
+
deleteAccount: (currentPassword: string) =>
|
|
165
|
+
medusaFetch(paths.delete_account, {
|
|
166
|
+
method: "POST",
|
|
167
|
+
body: JSON.stringify({ current_password: currentPassword }),
|
|
168
|
+
}),
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { endpoint, medusaFetch } from "./client"
|
|
2
|
+
import type { CoreConfig } from "./types"
|
|
3
|
+
|
|
4
|
+
export type AfterSalesCaseType = "return" | "exchange" | "claim"
|
|
5
|
+
|
|
6
|
+
export function afterSalesApi(config: CoreConfig) {
|
|
7
|
+
const paths = config.after_sales.endpoints
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
status: (orderId: string) =>
|
|
11
|
+
medusaFetch<Record<string, unknown>>(
|
|
12
|
+
endpoint(paths.order_status, { id: orderId }),
|
|
13
|
+
{ cache: "no-store" },
|
|
14
|
+
),
|
|
15
|
+
|
|
16
|
+
returnReasons: () =>
|
|
17
|
+
medusaFetch<{ return_reasons: Array<Record<string, unknown>> }>(
|
|
18
|
+
paths.return_reasons,
|
|
19
|
+
{ cache: "no-store" },
|
|
20
|
+
),
|
|
21
|
+
|
|
22
|
+
createReturn: (orderId: string, body: Record<string, unknown>) =>
|
|
23
|
+
medusaFetch(endpoint(paths.returns, { id: orderId }), {
|
|
24
|
+
method: "POST",
|
|
25
|
+
body: JSON.stringify(body),
|
|
26
|
+
}),
|
|
27
|
+
|
|
28
|
+
createExchange: (orderId: string, body: Record<string, unknown>) =>
|
|
29
|
+
medusaFetch(endpoint(paths.exchanges, { id: orderId }), {
|
|
30
|
+
method: "POST",
|
|
31
|
+
body: JSON.stringify(body),
|
|
32
|
+
}),
|
|
33
|
+
|
|
34
|
+
createClaim: (orderId: string, body: Record<string, unknown>) =>
|
|
35
|
+
medusaFetch(endpoint(paths.claims, { id: orderId }), {
|
|
36
|
+
method: "POST",
|
|
37
|
+
body: JSON.stringify(body),
|
|
38
|
+
}),
|
|
39
|
+
|
|
40
|
+
cancel: (orderId: string, caseType: AfterSalesCaseType, caseId: string) =>
|
|
41
|
+
medusaFetch(
|
|
42
|
+
endpoint(paths.cancel, {
|
|
43
|
+
id: orderId,
|
|
44
|
+
case_type: caseType,
|
|
45
|
+
case_id: caseId,
|
|
46
|
+
}),
|
|
47
|
+
{ method: "POST", body: JSON.stringify({}) },
|
|
48
|
+
),
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { medusaFetch } from "./client"
|
|
2
|
+
|
|
3
|
+
export type ProductListQuery = {
|
|
4
|
+
q?: string
|
|
5
|
+
categoryId?: string
|
|
6
|
+
order?: string
|
|
7
|
+
regionId?: string
|
|
8
|
+
limit?: number
|
|
9
|
+
offset?: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function listProducts(query: ProductListQuery = {}) {
|
|
13
|
+
const params = new URLSearchParams()
|
|
14
|
+
params.set("limit", String(query.limit ?? 24))
|
|
15
|
+
params.set("offset", String(query.offset ?? 0))
|
|
16
|
+
params.set("fields", "+variants.calculated_price,+categories,+images")
|
|
17
|
+
if (query.q?.trim()) params.set("q", query.q.trim())
|
|
18
|
+
if (query.categoryId?.trim()) params.set("category_id", query.categoryId.trim())
|
|
19
|
+
if (query.order?.trim()) params.set("order", query.order.trim())
|
|
20
|
+
if (query.regionId?.trim()) params.set("region_id", query.regionId.trim())
|
|
21
|
+
return medusaFetch<{
|
|
22
|
+
products: Array<Record<string, unknown>>
|
|
23
|
+
count?: number
|
|
24
|
+
offset?: number
|
|
25
|
+
limit?: number
|
|
26
|
+
}>(`/store/products?${params.toString()}`, { cache: "no-store" })
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function listCategories() {
|
|
30
|
+
return medusaFetch<{
|
|
31
|
+
product_categories: Array<Record<string, unknown>>
|
|
32
|
+
count?: number
|
|
33
|
+
}>("/store/product-categories?limit=100&order=rank", {
|
|
34
|
+
next: { revalidate: 60 },
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function getCategoryByHandle(handle: string) {
|
|
39
|
+
return medusaFetch<{ product_categories: Array<Record<string, unknown>> }>(
|
|
40
|
+
`/store/product-categories?handle=${encodeURIComponent(handle)}&limit=1`,
|
|
41
|
+
{ next: { revalidate: 60 } },
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function getProductByHandle(handle: string, regionId?: string) {
|
|
46
|
+
const params = new URLSearchParams({
|
|
47
|
+
handle,
|
|
48
|
+
limit: "1",
|
|
49
|
+
fields: "+variants.calculated_price,+categories,+images",
|
|
50
|
+
})
|
|
51
|
+
if (regionId) params.set("region_id", regionId)
|
|
52
|
+
return medusaFetch<{ products: Array<Record<string, unknown>> }>(
|
|
53
|
+
`/store/products?${params.toString()}`,
|
|
54
|
+
{ next: { revalidate: 60 } },
|
|
55
|
+
)
|
|
56
|
+
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { endpoint, medusaFetch } from "./client"
|
|
2
|
+
import {
|
|
3
|
+
trackCartEvent,
|
|
4
|
+
trackCartLineChange,
|
|
5
|
+
trackPaymentInfo,
|
|
6
|
+
trackPurchase,
|
|
7
|
+
trackShippingInfo,
|
|
8
|
+
} from "./tracking"
|
|
9
|
+
import {
|
|
10
|
+
asRecords,
|
|
11
|
+
isRecord,
|
|
12
|
+
shippingMethodIsCod,
|
|
13
|
+
stringValue,
|
|
14
|
+
} from "./commerce"
|
|
15
|
+
import type { CoreConfig } from "./types"
|
|
16
|
+
|
|
17
|
+
export function checkoutApi(config: CoreConfig) {
|
|
18
|
+
const paths = config.checkout.endpoints
|
|
19
|
+
|
|
20
|
+
const getCart = (cartId: string) =>
|
|
21
|
+
medusaFetch<{ cart: Record<string, unknown> }>(endpoint(paths.cart, { id: cartId }), {
|
|
22
|
+
cache: "no-store",
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
const regions = () =>
|
|
26
|
+
medusaFetch<{ regions: Array<Record<string, unknown>> }>(paths.regions, {
|
|
27
|
+
cache: "no-store",
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const resolveDefaultRegion = async () => {
|
|
31
|
+
const result = await regions()
|
|
32
|
+
const wanted = config.store.default_country.toLowerCase()
|
|
33
|
+
const matching = result.regions.find((region) =>
|
|
34
|
+
asRecords(region.countries).some(
|
|
35
|
+
(country) => stringValue(country.iso_2).toLowerCase() === wanted,
|
|
36
|
+
),
|
|
37
|
+
)
|
|
38
|
+
const region = matching || result.regions[0]
|
|
39
|
+
if (!region?.id) {
|
|
40
|
+
throw new Error("Nincs storefront számára elérhető Medusa region.")
|
|
41
|
+
}
|
|
42
|
+
return region
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const updateCart = (cartId: string, body: Record<string, unknown>) =>
|
|
46
|
+
medusaFetch<{ cart: Record<string, unknown> }>(endpoint(paths.cart, { id: cartId }), {
|
|
47
|
+
method: "POST",
|
|
48
|
+
body: JSON.stringify(body),
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const bankTransferDetails = () =>
|
|
52
|
+
medusaFetch<Record<string, unknown>>(paths.bank_transfer_details, {
|
|
53
|
+
cache: "no-store",
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const initializePaymentSession = (
|
|
57
|
+
paymentCollectionId: string,
|
|
58
|
+
providerId: string,
|
|
59
|
+
) =>
|
|
60
|
+
medusaFetch<Record<string, unknown>>(
|
|
61
|
+
endpoint(paths.payment_sessions, {
|
|
62
|
+
payment_collection_id: paymentCollectionId,
|
|
63
|
+
}),
|
|
64
|
+
{ method: "POST", body: JSON.stringify({ provider_id: providerId }) },
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const markPaymentMode = async (cartId: string, selectedProviderId: string) => {
|
|
68
|
+
const current = (await getCart(cartId)).cart
|
|
69
|
+
const metadata = isRecord(current.metadata) ? current.metadata : {}
|
|
70
|
+
const systemProvider = selectedProviderId.toLowerCase().includes("system")
|
|
71
|
+
let marker = "online"
|
|
72
|
+
|
|
73
|
+
if (systemProvider && shippingMethodIsCod(current)) {
|
|
74
|
+
marker = "cod"
|
|
75
|
+
} else if (systemProvider && config.payments.bank_transfer.enabled) {
|
|
76
|
+
const details = await bankTransferDetails()
|
|
77
|
+
const configured = isRecord(details.cart_metadata) ? details.cart_metadata : {}
|
|
78
|
+
await updateCart(cartId, { metadata: { ...metadata, ...configured } })
|
|
79
|
+
return
|
|
80
|
+
} else if (systemProvider) {
|
|
81
|
+
marker = "manual"
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
await updateCart(cartId, {
|
|
85
|
+
metadata: { ...metadata, core_payment_method: marker },
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
regions,
|
|
91
|
+
resolveDefaultRegion,
|
|
92
|
+
|
|
93
|
+
createCart: async (body: Record<string, unknown> = {}) => {
|
|
94
|
+
let regionId = typeof body.region_id === "string" ? body.region_id : ""
|
|
95
|
+
if (!regionId) {
|
|
96
|
+
const region = await resolveDefaultRegion()
|
|
97
|
+
regionId = String(region.id)
|
|
98
|
+
}
|
|
99
|
+
return medusaFetch<{ cart: Record<string, unknown> }>(paths.create_cart, {
|
|
100
|
+
method: "POST",
|
|
101
|
+
body: JSON.stringify({ region_id: regionId, locale: config.store.locale, ...body }),
|
|
102
|
+
})
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
getCart,
|
|
106
|
+
|
|
107
|
+
transferCart: (cartId: string) =>
|
|
108
|
+
medusaFetch<{ cart: Record<string, unknown> }>(
|
|
109
|
+
endpoint(paths.transfer_cart, { id: cartId }),
|
|
110
|
+
{ method: "POST", body: JSON.stringify({}) },
|
|
111
|
+
),
|
|
112
|
+
|
|
113
|
+
viewCart: async (cartId: string) => {
|
|
114
|
+
const result = await getCart(cartId)
|
|
115
|
+
trackCartEvent(config.analytics, "view_cart", result.cart)
|
|
116
|
+
return result
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
beginCheckout: async (cartId: string) => {
|
|
120
|
+
const result = await getCart(cartId)
|
|
121
|
+
trackCartEvent(config.analytics, "begin_checkout", result.cart)
|
|
122
|
+
return result
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
updateCart,
|
|
126
|
+
|
|
127
|
+
addLineItem: async (cartId: string, variantId: string, quantity = 1) => {
|
|
128
|
+
const result = await medusaFetch<{ cart: Record<string, unknown> }>(
|
|
129
|
+
endpoint(paths.add_line_item, { id: cartId }),
|
|
130
|
+
{
|
|
131
|
+
method: "POST",
|
|
132
|
+
body: JSON.stringify({ variant_id: variantId, quantity }),
|
|
133
|
+
},
|
|
134
|
+
)
|
|
135
|
+
const line = findLine(result.cart, (item) => item.variant_id === variantId)
|
|
136
|
+
if (line) {
|
|
137
|
+
trackCartLineChange(config.analytics, "add_to_cart", result.cart, line, quantity)
|
|
138
|
+
}
|
|
139
|
+
return result
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
updateLineItem: async (cartId: string, lineId: string, quantity: number) => {
|
|
143
|
+
const before = await getCart(cartId)
|
|
144
|
+
const previousLine = findLine(before.cart, (item) => item.id === lineId)
|
|
145
|
+
const previousQuantity = numeric(previousLine?.quantity) || 0
|
|
146
|
+
|
|
147
|
+
const result = await medusaFetch<{ cart: Record<string, unknown> }>(
|
|
148
|
+
endpoint(paths.line_item, { id: cartId, line_id: lineId }),
|
|
149
|
+
{
|
|
150
|
+
method: "POST",
|
|
151
|
+
body: JSON.stringify({ quantity }),
|
|
152
|
+
},
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
const delta = quantity - previousQuantity
|
|
156
|
+
if (delta > 0) {
|
|
157
|
+
const line = findLine(result.cart, (item) => item.id === lineId)
|
|
158
|
+
if (line) trackCartLineChange(config.analytics, "add_to_cart", result.cart, line, delta)
|
|
159
|
+
} else if (delta < 0 && previousLine) {
|
|
160
|
+
trackCartLineChange(
|
|
161
|
+
config.analytics,
|
|
162
|
+
"remove_from_cart",
|
|
163
|
+
before.cart,
|
|
164
|
+
previousLine,
|
|
165
|
+
Math.abs(delta),
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
return result
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
removeLineItem: async (cartId: string, lineId: string) => {
|
|
172
|
+
const before = await getCart(cartId)
|
|
173
|
+
const line = findLine(before.cart, (item) => item.id === lineId)
|
|
174
|
+
const result = await medusaFetch(endpoint(paths.line_item, { id: cartId, line_id: lineId }), {
|
|
175
|
+
method: "DELETE",
|
|
176
|
+
})
|
|
177
|
+
if (line) {
|
|
178
|
+
trackCartLineChange(
|
|
179
|
+
config.analytics,
|
|
180
|
+
"remove_from_cart",
|
|
181
|
+
before.cart,
|
|
182
|
+
line,
|
|
183
|
+
numeric(line.quantity) || 1,
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
return result
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
addPromotion: (cartId: string, code: string) =>
|
|
190
|
+
medusaFetch<{ cart: Record<string, unknown> }>(endpoint(paths.add_promotion, { id: cartId }), {
|
|
191
|
+
method: "POST",
|
|
192
|
+
body: JSON.stringify({ promo_codes: [code] }),
|
|
193
|
+
}),
|
|
194
|
+
|
|
195
|
+
removePromotion: (cartId: string, code: string) =>
|
|
196
|
+
medusaFetch<{ cart: Record<string, unknown> }>(endpoint(paths.remove_promotion, { id: cartId }), {
|
|
197
|
+
method: "DELETE",
|
|
198
|
+
body: JSON.stringify({ promo_codes: [code] }),
|
|
199
|
+
}),
|
|
200
|
+
|
|
201
|
+
shippingOptions: (cartId: string) =>
|
|
202
|
+
medusaFetch<{ shipping_options: Array<Record<string, unknown>> }>(
|
|
203
|
+
endpoint(paths.shipping_options, { id: cartId }),
|
|
204
|
+
{ cache: "no-store" },
|
|
205
|
+
),
|
|
206
|
+
|
|
207
|
+
calculateShippingOption: (
|
|
208
|
+
optionId: string,
|
|
209
|
+
body: Record<string, unknown> = {},
|
|
210
|
+
) =>
|
|
211
|
+
medusaFetch(endpoint(paths.calculate_shipping_option, { option_id: optionId }), {
|
|
212
|
+
method: "POST",
|
|
213
|
+
body: JSON.stringify(body),
|
|
214
|
+
}),
|
|
215
|
+
|
|
216
|
+
addShippingMethod: async (
|
|
217
|
+
cartId: string,
|
|
218
|
+
optionId: string,
|
|
219
|
+
data?: Record<string, unknown>,
|
|
220
|
+
) => {
|
|
221
|
+
const result = await medusaFetch<{ cart: Record<string, unknown> }>(
|
|
222
|
+
endpoint(paths.add_shipping_method, { id: cartId }),
|
|
223
|
+
{
|
|
224
|
+
method: "POST",
|
|
225
|
+
body: JSON.stringify({ option_id: optionId, ...(data ? { data } : {}) }),
|
|
226
|
+
},
|
|
227
|
+
)
|
|
228
|
+
trackShippingInfo(config.analytics, result.cart, shippingTier(result.cart, optionId))
|
|
229
|
+
return result
|
|
230
|
+
},
|
|
231
|
+
|
|
232
|
+
foxpostLockers: () =>
|
|
233
|
+
medusaFetch<{ lockers: Array<Record<string, unknown>> }>(paths.foxpost_lockers, {
|
|
234
|
+
cache: "no-store",
|
|
235
|
+
}),
|
|
236
|
+
|
|
237
|
+
glsDeliveryPoints: () =>
|
|
238
|
+
medusaFetch<{ deliveryPoints: Array<Record<string, unknown>> }>(paths.gls_delivery_points, {
|
|
239
|
+
cache: "no-store",
|
|
240
|
+
}),
|
|
241
|
+
|
|
242
|
+
dpdPickupPoints: (query: Record<string, string> = {}) => {
|
|
243
|
+
const params = new URLSearchParams(query)
|
|
244
|
+
const suffix = params.size ? `?${params.toString()}` : ""
|
|
245
|
+
return medusaFetch<{ pickupPoints: Array<Record<string, unknown>> }>(
|
|
246
|
+
`${paths.dpd_pickup_points}${suffix}`,
|
|
247
|
+
{ cache: "no-store" },
|
|
248
|
+
)
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
mplDeliveryPlaces: (query: Record<string, string> = {}) => {
|
|
252
|
+
const params = new URLSearchParams(query)
|
|
253
|
+
const suffix = params.size ? `?${params.toString()}` : ""
|
|
254
|
+
return medusaFetch<{ deliveryPlaces: Array<Record<string, unknown>> }>(
|
|
255
|
+
`${paths.mpl_delivery_places}${suffix}`,
|
|
256
|
+
{ cache: "no-store" },
|
|
257
|
+
)
|
|
258
|
+
},
|
|
259
|
+
|
|
260
|
+
paymentProviders: (regionId: string) =>
|
|
261
|
+
medusaFetch<{ payment_providers: Array<Record<string, unknown>> }>(
|
|
262
|
+
endpoint(paths.payment_providers, { region_id: regionId }),
|
|
263
|
+
{ cache: "no-store" },
|
|
264
|
+
),
|
|
265
|
+
|
|
266
|
+
createPaymentCollection: (cartId: string) =>
|
|
267
|
+
medusaFetch<{ payment_collection: Record<string, unknown> }>(
|
|
268
|
+
paths.create_payment_collection,
|
|
269
|
+
{ method: "POST", body: JSON.stringify({ cart_id: cartId }) },
|
|
270
|
+
),
|
|
271
|
+
|
|
272
|
+
initializePaymentSession,
|
|
273
|
+
|
|
274
|
+
selectPayment: async (
|
|
275
|
+
cartId: string,
|
|
276
|
+
paymentCollectionId: string,
|
|
277
|
+
selectedProviderId: string,
|
|
278
|
+
) => {
|
|
279
|
+
await markPaymentMode(cartId, selectedProviderId)
|
|
280
|
+
const result = await initializePaymentSession(paymentCollectionId, selectedProviderId)
|
|
281
|
+
const { cart } = await getCart(cartId)
|
|
282
|
+
trackPaymentInfo(config.analytics, cart, selectedProviderId)
|
|
283
|
+
return { result, cart }
|
|
284
|
+
},
|
|
285
|
+
|
|
286
|
+
bankTransferDetails,
|
|
287
|
+
|
|
288
|
+
complete: async (cartId: string) => {
|
|
289
|
+
const result = await medusaFetch<Record<string, unknown>>(
|
|
290
|
+
endpoint(paths.complete, { id: cartId }),
|
|
291
|
+
{
|
|
292
|
+
method: "POST",
|
|
293
|
+
body: JSON.stringify({}),
|
|
294
|
+
},
|
|
295
|
+
)
|
|
296
|
+
if (result.type === "order" && isRecord(result.order)) {
|
|
297
|
+
trackPurchase(config.analytics, result.order)
|
|
298
|
+
}
|
|
299
|
+
return result
|
|
300
|
+
},
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function findLine(
|
|
305
|
+
cart: Record<string, unknown>,
|
|
306
|
+
predicate: (item: Record<string, unknown>) => boolean,
|
|
307
|
+
) {
|
|
308
|
+
if (!Array.isArray(cart.items)) return null
|
|
309
|
+
return cart.items.filter(isRecord).find(predicate) || null
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function shippingTier(cart: Record<string, unknown>, optionId: string) {
|
|
313
|
+
if (!Array.isArray(cart.shipping_methods)) return optionId
|
|
314
|
+
const selected = cart.shipping_methods
|
|
315
|
+
.filter(isRecord)
|
|
316
|
+
.find((method) => method.shipping_option_id === optionId)
|
|
317
|
+
const shippingOption = selected && isRecord(selected.shipping_option)
|
|
318
|
+
? selected.shipping_option
|
|
319
|
+
: null
|
|
320
|
+
return String(selected?.name || shippingOption?.name || optionId)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function numeric(value: unknown) {
|
|
324
|
+
if (typeof value === "number" && Number.isFinite(value)) return value
|
|
325
|
+
if (typeof value === "string" && value.trim()) {
|
|
326
|
+
const parsed = Number(value)
|
|
327
|
+
if (Number.isFinite(parsed)) return parsed
|
|
328
|
+
}
|
|
329
|
+
return undefined
|
|
330
|
+
}
|