import {z} from 'zod' export type IssuingCardholderId = string; export type CardLifetime = {unit?: ('Months' | 'Years') | undefined, value: number}; export type IssuingReference = string; export type IssuingCardMetadata = {udf1?: string | undefined, udf2?: string | undefined, udf3?: string | undefined, udf4?: string | undefined, udf5?: string | undefined}; export type IssuingScheduledRevocationDate = string; export type IssuingRevocationDate = string; export type IssuingScheduledActivationDate = string; export type IssuingCardProductId = string; export type AddCardRequest = {type: string, cardholder_id: IssuingCardholderId, lifetime?: CardLifetime | undefined, reference?: IssuingReference | undefined, metadata?: IssuingCardMetadata | undefined, scheduled_revocation_date?: IssuingScheduledRevocationDate | undefined, revocation_date?: IssuingRevocationDate | undefined, scheduled_activation_date?: IssuingScheduledActivationDate | undefined, card_product_id: IssuingCardProductId}; export type CreateApiIssuingCardsArgs = {'Cko-Idempotency-Key'?: string | undefined, body: AddCardRequest}; export type CreateApiIssuingCardsOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const issuingCardId = z.string().min(30).max(30); export const issuingClientId = z.string().min(30).max(30); export const issuingEntityId = z.string().min(30).max(30); export const displayName = z.string().min(2).max(26); export const lastFour = z.string(); export const expiryMonth = z.number().int().gte(1).lte(12); export const expiryYear = z.number().int(); export const issuingCurrency = z.string().min(3).max(3); export const issuingCountry = z.string().min(2).max(2); export const issuingStatus = z.enum(["active", "inactive", "revoked", "suspended"]); export const issuingType = z.string(); export const issuingReference = z.string().max(256); export const scheme = z.enum(["mastercard", "visa"]); export const lastActivatedOn = z.string().nullable(); export const issuingLink = z.object({href: z.string().max(100), actions: z.array(z.enum(["GET", "POST", "PUT", "DELETE", "PATCH"]).max(100)), types: z.array(z.string().max(100))}); export const cardLinks = z.object({self: issuingLink, credentials: issuingLink, revoke: issuingLink, controls: issuingLink}); export const addCardResponse = z.object({id: issuingCardId, client_id: issuingClientId, entity_id: issuingEntityId, display_name: displayName.optional(), last_four: lastFour, expiry_month: expiryMonth, expiry_year: expiryYear, billing_currency: issuingCurrency, issuing_country: issuingCountry, status: issuingStatus, type: issuingType, reference: issuingReference.optional(), scheme: scheme, created_date: z.string(), last_activated_on: lastActivatedOn.optional(), _links: cardLinks.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a card * * POST /issuing/cards */ export const createApiIssuingCards = async (args: CreateApiIssuingCardsArgs, options: CreateApiIssuingCardsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Cko-Idempotency-Key'] != null) headers.set("Cko-Idempotency-Key", String(args['Cko-Idempotency-Key'])) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/issuing/cards${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(addCardResponse, await res.json()) };