import {z} from 'zod' export type BankAccountDetails = {name?: string | undefined, type?: ('checking' | 'savings') | undefined, routing_number?: string | undefined, account_number?: string | undefined}; export type Allocation = {type?: 'amount' | undefined, value?: (number | string) | undefined, min_amount?: (number | string) | undefined, targets?: Array | undefined}; export type LinkUserAuthenticationDataObjCreate = {social_security_number?: string | undefined, date_of_birth?: string | undefined, last_name?: string | undefined, first_name?: string | undefined, mobile_phone_number?: string | undefined, home_address_zip_code?: string | undefined, email?: string | undefined}; export type EndUser = {platform_matching?: LinkUserAuthenticationDataObjCreate | undefined}; export type CardDetails = {card_name: string, card_number: string, cvc?: string | undefined, expiration_date: string, name_on_card: string, card_zip_code: string, billing_address?: string | undefined, billing_address_two?: string | undefined, city?: string | undefined, state?: ('AL' | 'AK' | 'AZ' | 'AR' | 'CA' | 'CO' | 'CT' | 'DE' | 'DC' | 'FL' | 'GA' | 'HI' | 'ID' | 'IL' | 'IN' | 'IA' | 'KS' | 'KY' | 'LA' | 'ME' | 'MD' | 'MA' | 'MI' | 'MN' | 'MS' | 'MO' | 'MT' | 'NE' | 'NV' | 'NH' | 'NJ' | 'NM' | 'NY' | 'NC' | 'ND' | 'OH' | 'OK' | 'OR' | 'PA' | 'RI' | 'SC' | 'SD' | 'TN' | 'TX' | 'UT' | 'VT' | 'VA' | 'WA' | 'WV' | 'WI' | 'WY') | undefined}; export type LinkTokenCreateV20250708 = {org_name: string, allocation?: Allocation | undefined, skip_intro_screen?: boolean | undefined, employer_id?: string | undefined, disable_direct_deposit_splitting?: boolean | undefined, platform_id?: string | undefined, platform_type?: ('payroll' | 'time_and_attendance' | 'tax' | 'merchant') | undefined, language?: ('en' | 'es' | 'es-419' | 'pt-br') | undefined, end_user_id?: string | undefined, account_id?: string | undefined, document_uploads?: ('direct' | 'fallback' | 'disabled') | undefined, tags?: {[key: string]: string} | undefined, deposit_forms?: ('direct' | 'fallback' | 'disabled') | undefined, use_case?: ('account_onboarding' | 'account_servicing' | 'underwriting') | undefined, end_user?: EndUser | undefined, cards?: Array | undefined, partner_customer_id?: string | undefined, reseller_customer_id?: string | undefined, solution: 'Bill Manager' | 'Bill Switch' | 'Switch Kit' | 'Deposit Switch' | 'Verify' | 'Paycheck Viewer', features?: (Array<'income' | 'identity' | 'bill_cancellation' | 'bill_switch' | 'direct_deposit_switch' | 'tax_forms' | 'paystubs' | 'direct_deposit_payment' | 'direct_deposit_allocations' | 'shifts' | 'employment'>) | undefined}; export type CreateApiLinkTokensArgs = {'Pinwheel-Version': '2025-07-08' | '2023-11-22' | '2023-07-18' | '2023-04-18' | '2022-09-09' | '2022-06-22' | '2022-03-02', body: LinkTokenCreateV20250708}; export type CreateApiLinkTokensOptions = {baseUrl?: string | undefined, apiSecret?: string | undefined, fetch?: typeof fetch | undefined}; export const linkTokenObjResponseV20210728 = z.object({mode: z.enum(["sandbox", "development", "production"]), id: z.string(), token: z.string(), smart_branch_url: z.string().optional(), expires: z.string()}); export const createApiLinkTokensResponse = z.object({data: linkTokenObjResponseV20210728}); 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 Link Token * * POST /link_tokens */ export const createApiLinkTokens = async (args: CreateApiLinkTokensArgs, options?: CreateApiLinkTokensOptions) => { const {baseUrl = "https://api.getpinwheel.com/v1", fetch: fetchFn = fetch, apiSecret} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Pinwheel-Version'] != null) headers.set("Pinwheel-Version", String(args['Pinwheel-Version'])) if (apiSecret !== undefined) headers.set("X-API-SECRET", apiSecret) const url = `${baseUrl.replace(/\/+$/, '')}/link_tokens${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(createApiLinkTokensResponse, await res.json()) };