import {z} from 'zod' export type IssuingCardId = string; export type DisplayName = string; export type IssuingAddress = {address_line1: string, address_line2?: string | undefined, city: string, state?: string | undefined, zip: string, country: string}; export type IssuingShippingInstruction = {shipping_address: IssuingAddress, shipping_recipient?: string | undefined, additional_comment?: string | undefined}; export type IssuingReference = string; export type IssuingCardMetadata = {udf1?: string | undefined, udf2?: string | undefined, udf3?: string | undefined, udf4?: string | undefined, udf5?: string | undefined}; export type PhysicalCard = {display_name?: DisplayName | undefined, shipping_instructions?: IssuingShippingInstruction | undefined, reference?: IssuingReference | undefined, metadata?: IssuingCardMetadata | undefined}; export type VirtualCard = {display_name?: DisplayName | undefined, reference?: IssuingReference | undefined, metadata?: IssuingCardMetadata | undefined}; export type CreateApiIssuingCardsCardIdRenewArgs = {cardId: IssuingCardId, body: PhysicalCard | VirtualCard}; export type CreateApiIssuingCardsCardIdRenewOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const issuingCardId = z.string().min(30).max(30); export const issuingCardholderId = z.string().min(30).max(30); export const issuingStatus = z.enum(["active", "inactive", "revoked", "suspended"]); export const issuingType = z.string(); export const issuingClientId = z.string().min(30).max(30); export const issuingEntityId = z.string().min(30).max(30); export const lastFour = z.string(); export const expiryYear = z.number().int(); export const expiryMonth = z.number().int().gte(1).lte(12); export const displayName = z.string().min(2).max(26); export const issuingReference = z.string().max(256); export const datetime = z.string(); export const issuingCurrency = z.string().min(3).max(3); export const issuingCountry = z.string().min(2).max(2); 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 renewedCardLinks = z.object({self: issuingLink, credentials: issuingLink, activate: issuingLink, controls: issuingLink}); export const renewCardResponse = z.object({parent_card_id: issuingCardId.optional(), cardholder_id: issuingCardholderId.optional(), status: issuingStatus.optional(), type: issuingType.optional(), id: issuingCardId.optional(), client_id: issuingClientId.optional(), entity_id: issuingEntityId.optional(), last_four: lastFour.optional(), expiry_year: expiryYear.optional(), expiry_month: expiryMonth.optional(), display_name: displayName.optional(), reference: issuingReference.optional(), created_date: datetime.optional(), billing_currency: issuingCurrency.optional(), issuing_country: issuingCountry.optional(), _links: renewedCardLinks.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 }; /** * Renew a card * * POST /issuing/cards/{cardId}/renew */ export const createApiIssuingCardsCardIdRenew = async (args: CreateApiIssuingCardsCardIdRenewArgs, options: CreateApiIssuingCardsCardIdRenewOptions) => { 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 (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/issuing/cards/${encodeURIComponent(String(args.cardId))}/renew${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(renewCardResponse, await res.json()) };