Skip to content

Commit 1d35aed

Browse files
committed
feat(mdk-403): add CurrencySchema as single source of truth
Create shared CurrencySchema (z.enum(["USD", "SAT"])) in schemas/currency.ts and use it throughout the API contract for type safety and validation. - Add src/schemas/currency.ts with CurrencySchema and Currency type - Update checkout.ts, products.ts, invoice.ts to use CurrencySchema - Export CurrencySchema from index.ts
1 parent 362fd1c commit 1d35aed

7 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/contracts/checkout.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { oc } from "@orpc/contract";
22
import { z } from "zod";
33
import { CheckoutSchema } from "../schemas/checkout";
4+
import { CurrencySchema } from "../schemas/currency";
45

56
/**
67
* Helper to treat empty strings as undefined (not provided).
@@ -47,7 +48,7 @@ export type CustomerInput = z.infer<typeof CustomerInputSchema>;
4748
export const CreateCheckoutInputSchema = z.object({
4849
nodeId: z.string(),
4950
amount: z.number().optional(),
50-
currency: z.string().optional(),
51+
currency: CurrencySchema.optional(),
5152
products: z.array(z.string()).optional(),
5253
successUrl: z.string().optional(),
5354
allowDiscountCodes: z.boolean().optional(),

src/contracts/products.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { oc } from "@orpc/contract";
22
import { z } from "zod";
3+
import { CurrencySchema } from "../schemas/currency";
34

45
export const ProductPriceSchema = z.object({
56
id: z.string(),
67
amountType: z.enum(["FIXED", "CUSTOM", "FREE"]),
78
priceAmount: z.number().nullable(),
8-
currency: z.string(),
9+
currency: CurrencySchema,
910
});
1011

1112
export const ProductSchema = z.object({

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export type {
1919
} from "./contracts/onboarding";
2020
export type { Checkout } from "./schemas/checkout";
2121
export { CheckoutSchema } from "./schemas/checkout";
22+
export type { Currency } from "./schemas/currency";
23+
export { CurrencySchema } from "./schemas/currency";
2224
export type { Product, ProductPrice } from "./contracts/products";
2325
export {
2426
ProductSchema,

src/schemas/checkout.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
PaidInvoiceSchema,
77
} from "./invoice";
88
import { CheckoutProductSchema } from "./product";
9+
import { CurrencySchema } from "./currency";
910

1011
/**
1112
* Valid fields that can be required at checkout time.
@@ -42,7 +43,7 @@ const BaseCheckoutSchema = z.object({
4243
expiresAt: z.date(),
4344
userMetadata: z.record(z.any()).nullable(),
4445
customFieldData: z.record(z.any()).nullable(),
45-
currency: z.string(),
46+
currency: CurrencySchema,
4647
allowDiscountCodes: z.boolean(),
4748
/**
4849
* Array of customer fields required at checkout.

src/schemas/currency.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { z } from "zod";
2+
3+
/**
4+
* Supported currencies for pricing and payments.
5+
* - USD: US Dollars (amounts in cents)
6+
* - SAT: Satoshis (amounts in whole sats)
7+
*/
8+
export const CurrencySchema = z.enum(["USD", "SAT"]);
9+
export type Currency = z.infer<typeof CurrencySchema>;

src/schemas/invoice.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { z } from "zod";
2+
import { CurrencySchema } from "./currency";
23

34
export const BaseInvoiceSchema = z.object({
45
invoice: z.string(),
56
expiresAt: z.date(),
67
paymentHash: z.string(),
78
amountSats: z.number().nullable(),
89
amountSatsReceived: z.number().nullable(),
9-
currency: z.string(),
10+
currency: CurrencySchema,
1011
fiatAmount: z.number().nullable(),
1112
btcPrice: z.number().nullable(),
1213
});

src/schemas/product.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { z } from "zod";
2+
import { CurrencySchema } from "./currency";
23

34
export const CheckoutProductPriceSchema = z.object({
45
id: z.string(),
56
amountType: z.enum(["FIXED", "CUSTOM", "FREE"]),
67
priceAmount: z.number().nullable(),
7-
currency: z.string(),
8+
currency: CurrencySchema,
89
});
910

1011
export const CheckoutProductSchema = z.object({

0 commit comments

Comments
 (0)