@paykit-sdk/core
Version:
The Payment Toolkit for Typescript
93 lines (88 loc) • 2.81 kB
JavaScript
import { z } from 'zod';
// src/resources/customer.ts
var metadataSchema = z.record(z.string(), z.string());
// src/tools/utils.ts
var schema = () => {
return (schema2) => schema2;
};
var billingAddressSchema = schema()(
z.object({
name: z.string().min(1, "Recipient name is required"),
line1: z.string().min(1, "Address line 1 is required"),
line2: z.string().default("").optional(),
city: z.string().min(1, "City is required"),
state: z.string().optional(),
// Optional because not all countries use states
postal_code: z.string().min(1, "Postal code is required"),
country: z.string().length(2, "Country must be a 2-letter ISO code").toUpperCase(),
phone: z.string().optional()
})
);
var billingSchema = schema()(
z.object({
address: billingAddressSchema,
carrier: z.string().optional(),
currency: z.string().min(1, "Currency is required")
})
);
// src/resources/customer.ts
var customerSchema = schema()(
z.object({
id: z.string(),
email: z.string().email(),
name: z.string(),
phone: z.string().nullable(),
metadata: metadataSchema.optional(),
custom_fields: z.record(z.string(), z.unknown()).optional(),
created_at: z.date(),
updated_at: z.date().nullable()
})
);
var payeeSchema = schema()(
z.union([
z.object({ email: z.string().email() }),
z.object({ id: z.union([z.string(), z.number()]) })
])
);
var createCustomerSchema = schema()(
z.object({
email: z.string().email(),
name: z.string().optional(),
phone: z.string().nullable().optional(),
metadata: metadataSchema.optional(),
billing: billingSchema.nullable(),
provider_metadata: z.record(z.string(), z.unknown()).optional()
})
);
var updateCustomerSchema = schema()(
z.object({
email: z.string().email().optional(),
name: z.string().optional(),
metadata: metadataSchema.optional(),
provider_metadata: z.record(z.string(), z.unknown()).optional()
})
);
var retrieveCustomerSchema = schema()(
z.object({
id: z.string()
})
);
var isEmailCustomer = (customer) => {
return typeof customer === "object" && customer !== null && "email" in customer;
};
var isIdCustomer = (customer) => {
return typeof customer === "object" && customer !== null && "id" in customer;
};
var parseCustomerName = (params) => {
const full = params.name?.trim() || params.email.split("@")[0];
const parts = full.split(/\s+/);
const first = parts[0] || "";
const last = parts.slice(1).join(" ") || "";
return {
fullName: full,
firstName: first,
lastName: last,
initials: `${first[0] || ""}${last[0] || ""}`.toUpperCase()
};
};
export { createCustomerSchema, customerSchema, isEmailCustomer, isIdCustomer, parseCustomerName, payeeSchema, retrieveCustomerSchema, updateCustomerSchema };