@hszrdm/create-commerce 0.0.0-stage → 1.0.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.
- 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,107 @@
|
|
|
1
|
+
import type { CoreConfig } from "./types"
|
|
2
|
+
|
|
3
|
+
type CoreFetchInit = RequestInit & {
|
|
4
|
+
next?: { revalidate?: number; tags?: string[] }
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function getBackendUrl() {
|
|
8
|
+
const raw =
|
|
9
|
+
process.env.MEDUSA_BACKEND_URL ||
|
|
10
|
+
process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL ||
|
|
11
|
+
"http://localhost:9000"
|
|
12
|
+
return raw.replace(/\/+$/, "")
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function publishableKey() {
|
|
16
|
+
return process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY?.trim() || null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class CoreApiError extends Error {
|
|
20
|
+
constructor(
|
|
21
|
+
message: string,
|
|
22
|
+
readonly status: number,
|
|
23
|
+
readonly payload: unknown,
|
|
24
|
+
) {
|
|
25
|
+
super(message)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function medusaRawFetch(
|
|
30
|
+
path: string,
|
|
31
|
+
init: CoreFetchInit = {},
|
|
32
|
+
) {
|
|
33
|
+
const headers = new Headers(init.headers)
|
|
34
|
+
headers.set("accept", headers.get("accept") || "application/json")
|
|
35
|
+
if (init.body && !headers.has("content-type")) {
|
|
36
|
+
headers.set("content-type", "application/json")
|
|
37
|
+
}
|
|
38
|
+
const key = publishableKey()
|
|
39
|
+
if (key) headers.set("x-publishable-api-key", key)
|
|
40
|
+
|
|
41
|
+
return fetch(`${getBackendUrl()}${normalizePath(path)}`, {
|
|
42
|
+
...init,
|
|
43
|
+
headers,
|
|
44
|
+
credentials: "include",
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function medusaFetch<T = unknown>(
|
|
49
|
+
path: string,
|
|
50
|
+
init: CoreFetchInit = {},
|
|
51
|
+
): Promise<T> {
|
|
52
|
+
const response = await medusaRawFetch(path, init)
|
|
53
|
+
const contentType = response.headers.get("content-type") || ""
|
|
54
|
+
const payload = contentType.includes("application/json")
|
|
55
|
+
? await response.json().catch(() => null)
|
|
56
|
+
: await response.text().catch(() => "")
|
|
57
|
+
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
const message =
|
|
60
|
+
payload && typeof payload === "object" && "message" in payload
|
|
61
|
+
? String((payload as { message?: unknown }).message || "API hiba")
|
|
62
|
+
: `Medusa API hiba (${response.status})`
|
|
63
|
+
throw new CoreApiError(message, response.status, payload)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return payload as T
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function getCoreConfig(): Promise<CoreConfig> {
|
|
70
|
+
const config = await medusaFetch<CoreConfig>("/store/core-config", {
|
|
71
|
+
cache: "no-store",
|
|
72
|
+
})
|
|
73
|
+
if (config.schema_version !== 1) {
|
|
74
|
+
throw new Error(`Nem támogatott Core config schema: ${config.schema_version}`)
|
|
75
|
+
}
|
|
76
|
+
return config
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function endpoint(
|
|
80
|
+
template: string,
|
|
81
|
+
values: Record<string, string | number>,
|
|
82
|
+
) {
|
|
83
|
+
let result = template
|
|
84
|
+
for (const [key, value] of Object.entries(values)) {
|
|
85
|
+
result = result.replaceAll(`:${key}`, encodeURIComponent(String(value)))
|
|
86
|
+
}
|
|
87
|
+
return result
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function renderPathTemplate(
|
|
91
|
+
template: string,
|
|
92
|
+
values: Record<string, string>,
|
|
93
|
+
) {
|
|
94
|
+
let result = template
|
|
95
|
+
for (const [key, value] of Object.entries(values)) {
|
|
96
|
+
result = result.replaceAll(`{${key}}`, encodeURIComponent(value))
|
|
97
|
+
}
|
|
98
|
+
return result.startsWith("/") ? result : `/${result}`
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function absoluteBackendUrl(path: string) {
|
|
102
|
+
return `${getBackendUrl()}${normalizePath(path)}`
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function normalizePath(path: string) {
|
|
106
|
+
return path.startsWith("/") ? path : `/${path}`
|
|
107
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
export type JsonRecord = Record<string, unknown>
|
|
2
|
+
|
|
3
|
+
export function isRecord(value: unknown): value is JsonRecord {
|
|
4
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value))
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function asRecords(value: unknown): JsonRecord[] {
|
|
8
|
+
return Array.isArray(value) ? value.filter(isRecord) : []
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function stringValue(value: unknown) {
|
|
12
|
+
return typeof value === "string" ? value : value == null ? "" : String(value)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function numberValue(value: unknown, fallback = 0) {
|
|
16
|
+
const number = typeof value === "number" ? value : Number(value)
|
|
17
|
+
return Number.isFinite(number) ? number : fallback
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function cartItems(cart: JsonRecord | null | undefined) {
|
|
21
|
+
return asRecords(cart?.items)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function cartItemCount(cart: JsonRecord | null | undefined) {
|
|
25
|
+
return cartItems(cart).reduce((sum, item) => sum + numberValue(item.quantity), 0)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function cartCurrency(cart: JsonRecord | null | undefined, fallback = "HUF") {
|
|
29
|
+
return stringValue(cart?.currency_code || fallback).toUpperCase()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function cartTotal(cart: JsonRecord | null | undefined) {
|
|
33
|
+
return numberValue(cart?.total)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function cartSubtotal(cart: JsonRecord | null | undefined) {
|
|
37
|
+
return numberValue(cart?.subtotal)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function cartShippingTotal(cart: JsonRecord | null | undefined) {
|
|
41
|
+
return numberValue(cart?.shipping_total)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function cartTaxTotal(cart: JsonRecord | null | undefined) {
|
|
45
|
+
return numberValue(cart?.tax_total)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function cartDiscountTotal(cart: JsonRecord | null | undefined) {
|
|
49
|
+
return numberValue(cart?.discount_total)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function cartRegionId(cart: JsonRecord | null | undefined) {
|
|
53
|
+
return stringValue(cart?.region_id || (isRecord(cart?.region) ? cart.region.id : ""))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function cartPaymentCollection(cart: JsonRecord | null | undefined) {
|
|
57
|
+
return isRecord(cart?.payment_collection) ? cart.payment_collection : null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function paymentSessions(cart: JsonRecord | null | undefined) {
|
|
61
|
+
return asRecords(cartPaymentCollection(cart)?.payment_sessions)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function activePaymentSession(cart: JsonRecord | null | undefined) {
|
|
65
|
+
return paymentSessions(cart)[0] || null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function providerId(session: JsonRecord | null | undefined) {
|
|
69
|
+
return stringValue(session?.provider_id)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function paymentSessionData(session: JsonRecord | null | undefined) {
|
|
73
|
+
return isRecord(session?.data) ? session.data : {}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function paymentClientSecret(cart: JsonRecord | null | undefined) {
|
|
77
|
+
const data = paymentSessionData(activePaymentSession(cart))
|
|
78
|
+
return stringValue(data.client_secret || data.clientSecret) || null
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function simplePayUrl(cart: JsonRecord | null | undefined) {
|
|
82
|
+
const data = paymentSessionData(activePaymentSession(cart))
|
|
83
|
+
return stringValue(data.paymentUrl || data.payment_url) || null
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function selectedShippingMethod(cart: JsonRecord | null | undefined) {
|
|
87
|
+
return asRecords(cart?.shipping_methods)[0] || null
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function shippingMethodIsCod(cart: JsonRecord | null | undefined) {
|
|
91
|
+
const method = selectedShippingMethod(cart)
|
|
92
|
+
const data = isRecord(method?.data) ? method.data : {}
|
|
93
|
+
return data.cod === true || data.cod === "true"
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function lineUnitPrice(item: JsonRecord) {
|
|
97
|
+
const unit = numberValue(item.unit_price, Number.NaN)
|
|
98
|
+
if (Number.isFinite(unit)) return unit
|
|
99
|
+
const subtotal = numberValue(item.subtotal, Number.NaN)
|
|
100
|
+
const quantity = Math.max(1, numberValue(item.quantity, 1))
|
|
101
|
+
return Number.isFinite(subtotal) ? subtotal / quantity : 0
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function lineTotal(item: JsonRecord) {
|
|
105
|
+
const total = numberValue(item.total, Number.NaN)
|
|
106
|
+
if (Number.isFinite(total)) return total
|
|
107
|
+
return lineUnitPrice(item) * numberValue(item.quantity, 1)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function formatMoney(
|
|
111
|
+
amount: number,
|
|
112
|
+
currency: string,
|
|
113
|
+
locale = "hu-HU",
|
|
114
|
+
) {
|
|
115
|
+
try {
|
|
116
|
+
return new Intl.NumberFormat(locale, {
|
|
117
|
+
style: "currency",
|
|
118
|
+
currency: currency.toUpperCase(),
|
|
119
|
+
maximumFractionDigits: currency.toUpperCase() === "HUF" ? 0 : 2,
|
|
120
|
+
}).format(amount)
|
|
121
|
+
} catch {
|
|
122
|
+
return `${amount.toLocaleString(locale)} ${currency.toUpperCase()}`
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function productVariantPrice(variant: JsonRecord | null | undefined) {
|
|
127
|
+
const price = isRecord(variant?.calculated_price) ? variant.calculated_price : null
|
|
128
|
+
return price ? numberValue(price.calculated_amount, Number.NaN) : Number.NaN
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function productVariantCurrency(variant: JsonRecord | null | undefined, fallback: string) {
|
|
132
|
+
const price = isRecord(variant?.calculated_price) ? variant.calculated_price : null
|
|
133
|
+
return stringValue(price?.currency_code || fallback).toUpperCase()
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function orderDisplayId(order: JsonRecord) {
|
|
137
|
+
return stringValue(order.display_id || order.id)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function providerLabel(id: string, cod = false) {
|
|
141
|
+
const normalized = id.toLowerCase()
|
|
142
|
+
if (normalized.startsWith("pp_stripe_") || normalized.includes("stripe")) return "Bankkártya (Stripe)"
|
|
143
|
+
if (normalized.startsWith("pp_simplepay_") || normalized.includes("simplepay")) return "Bankkártya (OTP SimplePay)"
|
|
144
|
+
if (normalized.startsWith("pp_system_default") || normalized.includes("system")) {
|
|
145
|
+
return cod ? "Utánvét" : "Banki átutalás / manuális fizetés"
|
|
146
|
+
}
|
|
147
|
+
return id
|
|
148
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
export const COOKIE_CONSENT_KEY = "core:cookie-consent:v1"
|
|
2
|
+
export const COOKIE_CONSENT_EVENT = "core:cookie-consent-changed"
|
|
3
|
+
|
|
4
|
+
export type ConsentPreferences = {
|
|
5
|
+
version: 1
|
|
6
|
+
necessary: true
|
|
7
|
+
analytics: boolean
|
|
8
|
+
marketing: boolean
|
|
9
|
+
updated_at: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type GoogleConsentState = {
|
|
13
|
+
analytics_storage: "granted" | "denied"
|
|
14
|
+
ad_storage: "granted" | "denied"
|
|
15
|
+
ad_user_data: "granted" | "denied"
|
|
16
|
+
ad_personalization: "granted" | "denied"
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function defaultConsentPreferences(): ConsentPreferences {
|
|
20
|
+
return {
|
|
21
|
+
version: 1,
|
|
22
|
+
necessary: true,
|
|
23
|
+
analytics: false,
|
|
24
|
+
marketing: false,
|
|
25
|
+
updated_at: new Date().toISOString(),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function toGoogleConsentState(
|
|
30
|
+
preferences: Pick<ConsentPreferences, "analytics" | "marketing">,
|
|
31
|
+
): GoogleConsentState {
|
|
32
|
+
return {
|
|
33
|
+
analytics_storage: preferences.analytics ? "granted" : "denied",
|
|
34
|
+
ad_storage: preferences.marketing ? "granted" : "denied",
|
|
35
|
+
ad_user_data: preferences.marketing ? "granted" : "denied",
|
|
36
|
+
ad_personalization: preferences.marketing ? "granted" : "denied",
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function readConsentPreferences(): ConsentPreferences | null {
|
|
41
|
+
if (typeof window === "undefined") return null
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const raw = window.localStorage.getItem(COOKIE_CONSENT_KEY)
|
|
45
|
+
if (!raw) return null
|
|
46
|
+
const parsed = JSON.parse(raw) as Partial<ConsentPreferences>
|
|
47
|
+
if (parsed.version !== 1) return null
|
|
48
|
+
if (typeof parsed.analytics !== "boolean" || typeof parsed.marketing !== "boolean") {
|
|
49
|
+
return null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
version: 1,
|
|
54
|
+
necessary: true,
|
|
55
|
+
analytics: parsed.analytics,
|
|
56
|
+
marketing: parsed.marketing,
|
|
57
|
+
updated_at:
|
|
58
|
+
typeof parsed.updated_at === "string" && parsed.updated_at
|
|
59
|
+
? parsed.updated_at
|
|
60
|
+
: new Date().toISOString(),
|
|
61
|
+
}
|
|
62
|
+
} catch {
|
|
63
|
+
return null
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function applyGoogleConsent(preferences: ConsentPreferences) {
|
|
68
|
+
if (typeof window === "undefined") return
|
|
69
|
+
|
|
70
|
+
const target = window as typeof window & {
|
|
71
|
+
dataLayer?: unknown[]
|
|
72
|
+
gtag?: (...args: unknown[]) => void
|
|
73
|
+
}
|
|
74
|
+
target.dataLayer = target.dataLayer || []
|
|
75
|
+
target.gtag =
|
|
76
|
+
target.gtag ||
|
|
77
|
+
function gtag(...args: unknown[]) {
|
|
78
|
+
target.dataLayer!.push(args)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
target.gtag("consent", "update", toGoogleConsentState(preferences))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function saveConsentPreferences(input: {
|
|
85
|
+
analytics: boolean
|
|
86
|
+
marketing: boolean
|
|
87
|
+
}) {
|
|
88
|
+
if (typeof window === "undefined") return null
|
|
89
|
+
|
|
90
|
+
const preferences: ConsentPreferences = {
|
|
91
|
+
version: 1,
|
|
92
|
+
necessary: true,
|
|
93
|
+
analytics: input.analytics,
|
|
94
|
+
marketing: input.marketing,
|
|
95
|
+
updated_at: new Date().toISOString(),
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
window.localStorage.setItem(COOKIE_CONSENT_KEY, JSON.stringify(preferences))
|
|
99
|
+
applyGoogleConsent(preferences)
|
|
100
|
+
window.dispatchEvent(
|
|
101
|
+
new CustomEvent<ConsentPreferences>(COOKIE_CONSENT_EVENT, {
|
|
102
|
+
detail: preferences,
|
|
103
|
+
}),
|
|
104
|
+
)
|
|
105
|
+
return preferences
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function reopenCookieSettings() {
|
|
109
|
+
if (typeof window === "undefined") return
|
|
110
|
+
window.dispatchEvent(new CustomEvent("core:cookie-settings-open"))
|
|
111
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { absoluteBackendUrl, endpoint, medusaFetch } from "./client"
|
|
2
|
+
import type { CoreConfig, LegalDocument } from "./types"
|
|
3
|
+
|
|
4
|
+
export async function listLegalDocuments(config: CoreConfig) {
|
|
5
|
+
const result = await medusaFetch<{ documents: LegalDocument[] }>(
|
|
6
|
+
config.legal.documents_endpoint,
|
|
7
|
+
{ cache: "no-store" },
|
|
8
|
+
)
|
|
9
|
+
return {
|
|
10
|
+
...result,
|
|
11
|
+
documents: result.documents.map((document) => ({
|
|
12
|
+
...document,
|
|
13
|
+
pdfEndpoint: document.pdfEndpoint
|
|
14
|
+
? absoluteBackendUrl(document.pdfEndpoint)
|
|
15
|
+
: document.pdfEndpoint,
|
|
16
|
+
})),
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function acceptLegalDocument(
|
|
21
|
+
config: CoreConfig,
|
|
22
|
+
key: string,
|
|
23
|
+
body: Record<string, unknown>,
|
|
24
|
+
) {
|
|
25
|
+
return medusaFetch(
|
|
26
|
+
endpoint(config.legal.accept_endpoint, { key }),
|
|
27
|
+
{ method: "POST", body: JSON.stringify(body) },
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function legalPdfUrl(config: CoreConfig, key: string) {
|
|
32
|
+
return absoluteBackendUrl(endpoint(config.legal.pdf_endpoint, { key }))
|
|
33
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Metadata } from "next"
|
|
2
|
+
import { medusaFetch } from "./client"
|
|
3
|
+
import type { CoreConfig, SeoContractResponse } from "./types"
|
|
4
|
+
|
|
5
|
+
type SeoEntityQuery = {
|
|
6
|
+
type: "product" | "category"
|
|
7
|
+
handle: string
|
|
8
|
+
countryCode?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function getSeoContract(
|
|
12
|
+
config: CoreConfig,
|
|
13
|
+
query:
|
|
14
|
+
| { type: "product" | "category"; handle: string }
|
|
15
|
+
| { type: "page"; path: string },
|
|
16
|
+
) {
|
|
17
|
+
const params = new URLSearchParams({ type: query.type })
|
|
18
|
+
if ("handle" in query) params.set("handle", query.handle)
|
|
19
|
+
if ("path" in query) params.set("path", query.path)
|
|
20
|
+
return medusaFetch<SeoContractResponse>(
|
|
21
|
+
`${config.seo.metadata_endpoint}?${params.toString()}`,
|
|
22
|
+
{ next: { revalidate: 60 } },
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function getStructuredData(
|
|
27
|
+
config: CoreConfig,
|
|
28
|
+
query:
|
|
29
|
+
| { type: "site" }
|
|
30
|
+
| { type: "product" | "category"; handle: string }
|
|
31
|
+
| { type: "page"; path: string },
|
|
32
|
+
) {
|
|
33
|
+
const params = new URLSearchParams({ type: query.type })
|
|
34
|
+
if ("handle" in query) params.set("handle", query.handle)
|
|
35
|
+
if ("path" in query) params.set("path", query.path)
|
|
36
|
+
return medusaFetch<Record<string, unknown>>(
|
|
37
|
+
`${config.seo.structured_data_endpoint}?${params.toString()}`,
|
|
38
|
+
{ next: { revalidate: 60 } },
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function toNextMetadata(
|
|
43
|
+
response: SeoContractResponse,
|
|
44
|
+
config: CoreConfig,
|
|
45
|
+
): Metadata {
|
|
46
|
+
const contract = response.contract
|
|
47
|
+
return {
|
|
48
|
+
title: contract.title || undefined,
|
|
49
|
+
description: contract.description || undefined,
|
|
50
|
+
alternates: contract.canonical
|
|
51
|
+
? { canonical: contract.canonical }
|
|
52
|
+
: undefined,
|
|
53
|
+
robots: {
|
|
54
|
+
index: contract.robots.index,
|
|
55
|
+
follow: contract.robots.follow,
|
|
56
|
+
},
|
|
57
|
+
openGraph: {
|
|
58
|
+
title: contract.openGraph.title || undefined,
|
|
59
|
+
description: contract.openGraph.description || undefined,
|
|
60
|
+
url: contract.openGraph.url || undefined,
|
|
61
|
+
images: contract.openGraph.images,
|
|
62
|
+
siteName: config.seo.store_name,
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function metadataFromCore(
|
|
68
|
+
config: CoreConfig,
|
|
69
|
+
query: SeoEntityQuery,
|
|
70
|
+
): Promise<Metadata> {
|
|
71
|
+
if (!config.seo.enabled) return {}
|
|
72
|
+
const response = await getSeoContract(config, {
|
|
73
|
+
type: query.type,
|
|
74
|
+
handle: query.handle,
|
|
75
|
+
})
|
|
76
|
+
return toNextMetadata(response, config)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function structuredDataForEntity(
|
|
80
|
+
config: CoreConfig,
|
|
81
|
+
query: SeoEntityQuery,
|
|
82
|
+
) {
|
|
83
|
+
if (!config.seo.enabled) return null
|
|
84
|
+
return getStructuredData(config, {
|
|
85
|
+
type: query.type,
|
|
86
|
+
handle: query.handle,
|
|
87
|
+
})
|
|
88
|
+
}
|