import {z} from 'zod' export type PinFormat = {length?: number | undefined, prefix?: string | undefined, symbols?: boolean | undefined, numbers?: boolean | undefined, characters?: boolean | undefined, barcode?: boolean | undefined}; export type PinGenerator = {format?: PinFormat | undefined, pin?: ({value?: string | undefined, barcode_128?: string | undefined}) | undefined}; export type CreateApiAccountsAidWalletsCardsCardIdRotatePinArgs = {aid: unknown, card_id: string, body: {pin: PinGenerator}}; export type CreateApiAccountsAidWalletsCardsCardIdRotatePinOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const pinFormat = z.object({length: z.number().int().gte(4).optional(), prefix: z.string().max(12).optional(), symbols: z.boolean().optional(), numbers: z.boolean().optional(), characters: z.boolean().optional(), barcode: z.boolean().optional()}); export const pinGenerator = z.object({format: pinFormat.optional(), pin: z.object({value: z.string().max(48).optional(), barcode_128: z.string().optional()}).optional()}); export const rotatePin = z.object({pin: pinGenerator}); 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 }; /** * aid_cards_cardid_rotate_pin_post * * POST /accounts/{aid}/wallets/cards/{card_id}/rotate-pin */ export const createApiAccountsAidWalletsCardsCardIdRotatePin = async (args: CreateApiAccountsAidWalletsCardsCardIdRotatePinArgs, options?: CreateApiAccountsAidWalletsCardsCardIdRotatePinOptions) => { const {baseUrl = "https://api.dintero.com/v1", fetch: fetchFn = fetch, JWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.aid))}/wallets/cards/${encodeURIComponent(String(args.card_id))}/rotate-pin${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(rotatePin, await res.json()) };