import {z} from 'zod' export type Notes = {[key: string]: string}; export type PatchApiV2AccountsAccountIdArgs = {account_id: string, body: {phone?: string | undefined, legal_business_name?: string | undefined, profile?: Record | undefined, legal_info?: Record | undefined, contact_name?: string | undefined, contact_info?: Record | undefined, apps?: Record | undefined, notes?: Notes | undefined}}; export type PatchApiV2AccountsAccountIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const notes = z.record(z.string(), z.string()); export const linkedAccount = z.object({id: z.string().optional(), type: z.literal("route").optional(), status: z.enum(["created", "suspended"]).optional(), email: z.string().optional(), phone: z.string().optional(), legal_business_name: z.string().optional(), business_type: z.literal("route").optional(), profile: z.object({category: z.string().optional(), subcategory: z.string().optional(), addresses: z.object({registered: z.object({street1: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional()}).optional(), operation: z.object({street1: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional()}).optional()}).optional()}).optional(), legal_info: z.object({pan: z.string().optional(), gst: z.string().optional()}).optional(), contact_name: z.string().optional(), contact_info: z.object({chargeback: z.object({email: z.string().optional(), phone: z.string().optional(), policy_url: z.string().optional()}).optional(), refund: z.object({email: z.string().optional(), phone: z.string().optional(), policy_url: z.string().optional()}).optional(), support: z.object({email: z.string().optional(), phone: z.string().optional(), policy_url: z.string().optional()}).optional()}).optional(), apps: z.object({websites: z.array(z.string()).optional(), android: z.array(z.object({url: z.string().optional(), name: z.string().optional()})).optional(), ios: z.array(z.object({url: z.string().optional(), name: z.string().optional()})).optional()}).optional(), notes: notes.optional(), created_at: z.number().int().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 }; /** * Update a Linked Account * * PATCH /v2/accounts/{account_id} * * Also accepts auth: oauth2 */ export const patchApiV2AccountsAccountId = async (args: PatchApiV2AccountsAccountIdArgs, options?: PatchApiV2AccountsAccountIdOptions) => { const {baseUrl = "https://api.razorpay.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/accounts/${encodeURIComponent(String(args.account_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(linkedAccount, await res.json()) };