import {z} from 'zod' export type Notes = {[key: string]: string}; export type PatchApiV2AccountsAccountIdStakeholdersStakeholderIdArgs = {account_id: string, stakeholder_id: string, body: {name?: string | undefined, percentage_ownership?: number | undefined, relationship?: Record | undefined, phone?: Record | undefined, addresses?: Record | undefined, kyc?: Record | undefined, notes?: Notes | undefined}}; export type PatchApiV2AccountsAccountIdStakeholdersStakeholderIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const notes = z.record(z.string(), z.string()); export const stakeholder = z.object({id: z.string().optional(), entity: z.literal("stakeholder").optional(), name: z.string().optional(), email: z.string().optional(), percentage_ownership: z.number().optional(), relationship: z.object({director: z.boolean().optional(), executive: z.boolean().optional()}).optional(), phone: z.object({primary: z.object({number: z.string().optional(), country_code: z.string().optional()}).optional(), secondary: z.object({number: z.string().optional(), country_code: z.string().optional()}).optional()}).optional(), addresses: z.object({residential: z.object({street: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional()}).optional()}).optional(), kyc: z.object({pan: z.string().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 Stakeholder * * PATCH /v2/accounts/{account_id}/stakeholders/{stakeholder_id} * * Also accepts auth: oauth2 */ export const patchApiV2AccountsAccountIdStakeholdersStakeholderId = async (args: PatchApiV2AccountsAccountIdStakeholdersStakeholderIdArgs, options?: PatchApiV2AccountsAccountIdStakeholdersStakeholderIdOptions) => { 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))}/stakeholders/${encodeURIComponent(String(args.stakeholder_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(stakeholder, await res.json()) };