@hszrdm/create-commerce 1.9.1 → 1.10.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 +53 -0
- package/package.json +1 -1
- package/src/cli.mjs +20 -53
- package/src/upgrade.mjs +345 -0
- package/templates/backend/.env.example +20 -10
- package/templates/backend/package.json +1 -1
- package/templates/backend/scripts/commerce-ops.mjs +36 -10
- package/templates/storefront/package.json +3 -1
- package/templates/storefront/src/app/error.tsx +16 -6
- package/templates/storefront/src/app/global-error.tsx +22 -0
- package/templates/storefront/src/app/robots.txt/route.ts +4 -0
- package/templates/storefront/src/app/sitemap.xml/route.ts +4 -0
- package/templates/storefront/src/components/checkout-flow.tsx +125 -47
- package/templates/storefront/src/components/stripe-payment.tsx +95 -16
- package/templates/storefront/src/lib/core/client.ts +15 -9
- package/templates/storefront/src/lib/core/public-error.ts +97 -0
- package/templates/storefront/src/lib/core/shipping-input-validation.ts +82 -0
- package/templates/storefront/tests/public-error.test.mjs +21 -0
- package/templates/storefront/tests/shipping-input-validation.test.mjs +61 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
export type UserLocale = "hu" | "en"
|
|
2
|
+
|
|
3
|
+
type ApiPayload = { message?: unknown; code?: unknown; type?: unknown }
|
|
4
|
+
|
|
5
|
+
const messages = {
|
|
6
|
+
hu: {
|
|
7
|
+
network: "Nem sikerült kapcsolódni a webshophoz. Ellenőrizd az internetkapcsolatot, és próbáld újra.",
|
|
8
|
+
temporary: "A webshop átmenetileg nem érhető el. Kérjük, próbáld újra néhány perc múlva.",
|
|
9
|
+
notFound: "A keresett adat már nem érhető el. Frissítsd az oldalt, és próbáld újra.",
|
|
10
|
+
unauthorized: "A folytatáshoz jelentkezz be újra.",
|
|
11
|
+
forbidden: "Ehhez a művelethez nincs jogosultságod.",
|
|
12
|
+
conflict: "Az adatok időközben megváltoztak. Frissítsd az oldalt, és próbáld újra.",
|
|
13
|
+
invalid: "Ellenőrizd a megadott adatokat, és javítsd a hibás mezőket.",
|
|
14
|
+
payment: "A fizetést nem sikerült feldolgozni. Ellenőrizd az adatokat, vagy válassz másik fizetési módot.",
|
|
15
|
+
shipping: "A szállítási adatok mentése nem sikerült. Ellenőrizd a címet és a kiválasztott csomagpontot.",
|
|
16
|
+
rateLimit: "Túl sok próbálkozás történt. Várj egy kicsit, majd próbáld újra.",
|
|
17
|
+
},
|
|
18
|
+
en: {
|
|
19
|
+
network: "Could not connect to the shop. Check your connection and try again.",
|
|
20
|
+
temporary: "The shop is temporarily unavailable. Please try again in a few minutes.",
|
|
21
|
+
notFound: "This information is no longer available. Refresh the page and try again.",
|
|
22
|
+
unauthorized: "Please sign in again to continue.",
|
|
23
|
+
forbidden: "You do not have permission to perform this action.",
|
|
24
|
+
conflict: "The information has changed. Refresh the page and try again.",
|
|
25
|
+
invalid: "Check the information you entered and correct any invalid fields.",
|
|
26
|
+
payment: "We couldn't process the payment. Check your details or try another payment method.",
|
|
27
|
+
shipping: "We couldn't save your shipping details. Check the address and pickup point.",
|
|
28
|
+
rateLimit: "Too many attempts. Please wait a moment and try again.",
|
|
29
|
+
},
|
|
30
|
+
} as const
|
|
31
|
+
|
|
32
|
+
// Only explicit Core business codes may supply their human-readable messages.
|
|
33
|
+
// Arbitrary server-provided strings, stack traces and HTML are never shown.
|
|
34
|
+
const publicCodes = new Set([
|
|
35
|
+
"LEGAL_CONSENT_REQUIRED",
|
|
36
|
+
"SHIPPING_SELECTION_INVALID",
|
|
37
|
+
"SHIPPING_INPUT_VALIDATION",
|
|
38
|
+
"FOXPOST_VALIDATION",
|
|
39
|
+
"GLS_VALIDATION",
|
|
40
|
+
"MPL_VALIDATION",
|
|
41
|
+
"PACKETA_VALIDATION",
|
|
42
|
+
"DPD_VALIDATION",
|
|
43
|
+
])
|
|
44
|
+
|
|
45
|
+
const unsafe = /(?:Error:|\b(?:TypeError|ReferenceError|SyntaxError|ECONN|ETIMEDOUT|SQL|SELECT|INSERT|UPDATE|DELETE FROM|Prisma|Postgres|database|password|secret|token|api[_ -]?key|node_modules|stack trace|PaymentIntent|Bearer|sk_live_|sk_test_)\b|\/var\/|\/home\/|\/private\/|https?:\/\/[^\s]*@)/i
|
|
46
|
+
|
|
47
|
+
function safeDetail(value: unknown): string | null {
|
|
48
|
+
if (typeof value !== "string") return null
|
|
49
|
+
const message = value.trim()
|
|
50
|
+
return message.length > 4 && message.length <= 240 && !unsafe.test(message) && !/[<>]/.test(message)
|
|
51
|
+
? message : null
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function publicApiError(
|
|
55
|
+
status: number,
|
|
56
|
+
payload: unknown,
|
|
57
|
+
locale: UserLocale = "hu",
|
|
58
|
+
path = "",
|
|
59
|
+
): string {
|
|
60
|
+
const m = messages[locale]
|
|
61
|
+
const data = payload && typeof payload === "object" ? payload as ApiPayload : {}
|
|
62
|
+
const code = typeof data.code === "string" ? data.code : ""
|
|
63
|
+
if (status < 500 && publicCodes.has(code)) {
|
|
64
|
+
const detail = safeDetail(data.message)
|
|
65
|
+
if (detail) return detail
|
|
66
|
+
}
|
|
67
|
+
if (status === 401) return m.unauthorized
|
|
68
|
+
if (status === 403) return m.forbidden
|
|
69
|
+
if (status === 404) return m.notFound
|
|
70
|
+
if (status === 409) return m.conflict
|
|
71
|
+
if (status === 429) return m.rateLimit
|
|
72
|
+
if (status >= 500 || status === 0) return m.temporary
|
|
73
|
+
if (/payment|stripe|simplepay/i.test(path)) return m.payment
|
|
74
|
+
if (/shipping|foxpost|gls|mpl|dpd|packeta/i.test(path)) return m.shipping
|
|
75
|
+
return m.invalid
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function networkError(locale: UserLocale = "hu") {
|
|
79
|
+
return messages[locale].network
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function stripePublicError(input: { type?: string; code?: string; decline_code?: string; message?: string } | null | undefined, locale: UserLocale = "hu") {
|
|
83
|
+
const m = messages[locale]
|
|
84
|
+
if (input?.code === "insufficient_funds") return locale === "hu"
|
|
85
|
+
? "Nincs elegendő fedezet a kártyán. Próbálj másik fizetési módot."
|
|
86
|
+
: "Your card has insufficient funds. Try another payment method."
|
|
87
|
+
if (input?.code === "expired_card") return locale === "hu"
|
|
88
|
+
? "A bankkártya lejárt. Adj meg másik kártyát."
|
|
89
|
+
: "Your card has expired. Use another card."
|
|
90
|
+
if (input?.code === "incorrect_cvc") return locale === "hu"
|
|
91
|
+
? "A biztonsági kód hibás. Ellenőrizd a CVC-kódot."
|
|
92
|
+
: "The security code is incorrect. Check your CVC."
|
|
93
|
+
if (input?.code === "card_declined" || input?.type === "card_error") return locale === "hu"
|
|
94
|
+
? "A bank elutasította a fizetést. Próbálj másik kártyát vagy fizetési módot."
|
|
95
|
+
: "Your bank declined the payment. Try another card or payment method."
|
|
96
|
+
return m.payment
|
|
97
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Isomorphic shipment contact validation shared between backend and storefront.
|
|
3
|
+
* The server always revalidates before sending requests to carriers.
|
|
4
|
+
* Phone values are returned in E.164 format; malformed values never reach carriers.
|
|
5
|
+
*/
|
|
6
|
+
export class ShippingInputValidationError extends Error {
|
|
7
|
+
readonly code = "SHIPPING_INPUT_VALIDATION"
|
|
8
|
+
readonly status = 400
|
|
9
|
+
readonly carrier: string
|
|
10
|
+
readonly field: string
|
|
11
|
+
readonly reason: string
|
|
12
|
+
constructor(carrier: string, field: string, reason: string) {
|
|
13
|
+
super(`${carrier}: ${field} – ${reason}`)
|
|
14
|
+
this.name = "ShippingInputValidationError"
|
|
15
|
+
this.carrier = carrier
|
|
16
|
+
this.field = field
|
|
17
|
+
this.reason = reason
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function normalizeShippingPhone(
|
|
22
|
+
input: unknown,
|
|
23
|
+
options: { carrier: string; field?: string; hungarianMobileOnly?: boolean },
|
|
24
|
+
): string {
|
|
25
|
+
const field = options.field ?? "Telefonszám"
|
|
26
|
+
if (typeof input !== "string" && typeof input !== "number") {
|
|
27
|
+
throw new ShippingInputValidationError(options.carrier, field, "telefonszám megadása kötelező.")
|
|
28
|
+
}
|
|
29
|
+
const raw = String(input).trim()
|
|
30
|
+
if (!raw) {
|
|
31
|
+
throw new ShippingInputValidationError(options.carrier, field, "telefonszám megadása kötelező.")
|
|
32
|
+
}
|
|
33
|
+
// Allow formatting separators, but never silently discard arbitrary characters.
|
|
34
|
+
if (!/^[+\d\s().-]+$/.test(raw) || (raw.match(/\+/g) ?? []).length > 1 ||
|
|
35
|
+
(raw.includes("+") && !raw.startsWith("+"))) {
|
|
36
|
+
throw new ShippingInputValidationError(options.carrier, field, "érvénytelen karaktereket tartalmaz.")
|
|
37
|
+
}
|
|
38
|
+
const compact = raw.replace(/[\s().-]/g, "")
|
|
39
|
+
let normalized: string
|
|
40
|
+
if (compact.startsWith("00")) normalized = `+${compact.slice(2)}`
|
|
41
|
+
else if (compact.startsWith("+")) normalized = compact
|
|
42
|
+
else if (compact.startsWith("06")) normalized = `+36${compact.slice(2)}`
|
|
43
|
+
else if (compact.startsWith("36") && compact.length === 11) normalized = `+${compact}`
|
|
44
|
+
else if (/^\d{8,9}$/.test(compact)) normalized = `+36${compact}`
|
|
45
|
+
else normalized = compact
|
|
46
|
+
|
|
47
|
+
if (!/^\+[1-9]\d{7,14}$/.test(normalized)) {
|
|
48
|
+
throw new ShippingInputValidationError(options.carrier, field,
|
|
49
|
+
"érvényes nemzetközi telefonszám szükséges (+országkód és telefonszám).")
|
|
50
|
+
}
|
|
51
|
+
// A valid E.164 total length alone is insufficient for a Hungarian mobile number.
|
|
52
|
+
if (/^\+36(?:20|30|31|50|51|70)/.test(normalized) && !/^\+36(?:20|30|31|50|51|70)\d{7}$/.test(normalized)) {
|
|
53
|
+
throw new ShippingInputValidationError(options.carrier, field,
|
|
54
|
+
"a magyar mobiltelefonszámnak pontosan 9 számjegyből kell állnia a +36 után.")
|
|
55
|
+
}
|
|
56
|
+
if (options.hungarianMobileOnly && !/^\+36(?:20|30|31|50|51|70)\d{7}$/.test(normalized)) {
|
|
57
|
+
throw new ShippingInputValidationError(options.carrier, field,
|
|
58
|
+
"érvényes magyar mobiltelefonszám szükséges +36 formátumban.")
|
|
59
|
+
}
|
|
60
|
+
return normalized
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function requireShippingText(
|
|
64
|
+
input: unknown,
|
|
65
|
+
options: { carrier: string; field: string; maxLength?: number },
|
|
66
|
+
): string {
|
|
67
|
+
const value = typeof input === "string" ? input.trim() : ""
|
|
68
|
+
if (!value) throw new ShippingInputValidationError(options.carrier, options.field, "kötelező mező.")
|
|
69
|
+
if (options.maxLength && value.length > options.maxLength) {
|
|
70
|
+
throw new ShippingInputValidationError(options.carrier, options.field,
|
|
71
|
+
`legfeljebb ${options.maxLength} karakter adható meg.`)
|
|
72
|
+
}
|
|
73
|
+
return value
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function validateShippingEmail(input: unknown, options: { carrier: string; field?: string }): string {
|
|
77
|
+
const value = requireShippingText(input, { carrier: options.carrier, field: options.field ?? "E-mail-cím" })
|
|
78
|
+
if (value.length > 254 || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
|
|
79
|
+
throw new ShippingInputValidationError(options.carrier, options.field ?? "E-mail-cím", "érvénytelen e-mail-cím.")
|
|
80
|
+
}
|
|
81
|
+
return value
|
|
82
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import test from "node:test"
|
|
2
|
+
import assert from "node:assert/strict"
|
|
3
|
+
import { publicApiError, networkError, stripePublicError } from "../src/lib/core/public-error.ts"
|
|
4
|
+
|
|
5
|
+
test("never shows internal API exception or stack data", () => {
|
|
6
|
+
assert.equal(publicApiError(500, { message: "Prisma Error: database password leaked", code: "DB_ERROR" }), "A webshop átmenetileg nem érhető el. Kérjük, próbáld újra néhány perc múlva.")
|
|
7
|
+
assert.equal(publicApiError(400, { message: "SQL SELECT * FROM users" }), "Ellenőrizd a megadott adatokat, és javítsd a hibás mezőket.")
|
|
8
|
+
assert.equal(publicApiError(400, { message: "<script>alert(1)</script>" }), "Ellenőrizd a megadott adatokat, és javítsd a hibás mezőket.")
|
|
9
|
+
})
|
|
10
|
+
test("returns controlled checkout validation and never arbitrary API text", () => {
|
|
11
|
+
assert.equal(publicApiError(400, { code: "FOXPOST_VALIDATION", message: "FOXPOST: Telefonszám – kötelező mező." }), "FOXPOST: Telefonszám – kötelező mező.")
|
|
12
|
+
assert.equal(publicApiError(400, { code: "FOXPOST_VALIDATION", message: "Prisma Error: SECRET=123" }), "Ellenőrizd a megadott adatokat, és javítsd a hibás mezőket.")
|
|
13
|
+
assert.equal(publicApiError(409, { code: "SOME_INTERNAL_CODE", message: "database unavailable" }), "Az adatok időközben megváltoztak. Frissítsd az oldalt, és próbáld újra.")
|
|
14
|
+
})
|
|
15
|
+
test("localized expected statuses and Stripe errors", () => {
|
|
16
|
+
assert.match(networkError("en"), /connect/)
|
|
17
|
+
assert.match(publicApiError(401, null, "hu"), /jelentkezz/)
|
|
18
|
+
assert.match(publicApiError(422, null, "hu", "/store/payment-sessions"), /fizetés/)
|
|
19
|
+
assert.match(stripePublicError({ code: "insufficient_funds" }), /fedezet/)
|
|
20
|
+
assert.doesNotMatch(stripePublicError({ message: "secret=abc" }), /secret/)
|
|
21
|
+
})
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import test from "node:test"
|
|
2
|
+
import assert from "node:assert/strict"
|
|
3
|
+
import {
|
|
4
|
+
normalizeShippingPhone,
|
|
5
|
+
validateShippingEmail,
|
|
6
|
+
requireShippingText,
|
|
7
|
+
ShippingInputValidationError,
|
|
8
|
+
} from "../src/lib/core/shipping-input-validation.ts"
|
|
9
|
+
|
|
10
|
+
test("checkout and backend share E.164 phone normalization", () => {
|
|
11
|
+
for (const [input, expected] of [
|
|
12
|
+
["06 30 123 4567", "+36301234567"],
|
|
13
|
+
["0036 30 123 4567", "+36301234567"],
|
|
14
|
+
["36 30 123 4567", "+36301234567"],
|
|
15
|
+
["30 123 4567", "+36301234567"],
|
|
16
|
+
["+421 900 123 456", "+421900123456"],
|
|
17
|
+
]) assert.equal(normalizeShippingPhone(input, { carrier: "GLS" }), expected)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test("FOXPOST enforces Hungarian mobile contact", () => {
|
|
21
|
+
assert.equal(normalizeShippingPhone("06 30 123 4567", {
|
|
22
|
+
carrier: "FOXPOST", hungarianMobileOnly: true,
|
|
23
|
+
}), "+36301234567")
|
|
24
|
+
assert.throws(() => normalizeShippingPhone("+421900123456", {
|
|
25
|
+
carrier: "FOXPOST", hungarianMobileOnly: true,
|
|
26
|
+
}), /magyar mobiltelefonszám/)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test("invalid phone fails with readable field-level error", () => {
|
|
30
|
+
assert.throws(() => normalizeShippingPhone("+36abc301234567", {
|
|
31
|
+
carrier: "MPL", field: "Telefonszám",
|
|
32
|
+
}), (error) => {
|
|
33
|
+
assert.ok(error instanceof ShippingInputValidationError)
|
|
34
|
+
assert.equal(error.status, 400)
|
|
35
|
+
assert.equal(error.field, "Telefonszám")
|
|
36
|
+
assert.match(error.message, /MPL.*Telefonszám/)
|
|
37
|
+
return true
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test("rejects short Hungarian mobile numbers in checkout just like backend", () => {
|
|
42
|
+
for (const value of ["06 30 123 45", "+363012345", "0036 70 123 45"]) {
|
|
43
|
+
assert.throws(
|
|
44
|
+
() => normalizeShippingPhone(value, { carrier: "GLS", field: "Címzett telefonszáma" }),
|
|
45
|
+
(error) => {
|
|
46
|
+
assert.ok(error instanceof ShippingInputValidationError)
|
|
47
|
+
assert.equal(error.status, 400)
|
|
48
|
+
assert.equal(error.field, "Címzett telefonszáma")
|
|
49
|
+
assert.match(error.message, /magyar mobiltelefonszám/)
|
|
50
|
+
return true
|
|
51
|
+
},
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test("contact validation trims email and rejects missing address fields", () => {
|
|
57
|
+
assert.equal(validateShippingEmail(" client@example.com ", { carrier: "DPD" }), "client@example.com")
|
|
58
|
+
assert.throws(() => validateShippingEmail("bad-email", { carrier: "DPD" }), /E-mail-cím/)
|
|
59
|
+
assert.equal(requireShippingText(" Budapest ", { carrier: "Packeta", field: "Város" }), "Budapest")
|
|
60
|
+
assert.throws(() => requireShippingText("", { carrier: "Packeta", field: "Város" }), /Város/)
|
|
61
|
+
})
|