import {z} from 'zod' export type CustomersMfaSettings = {enabled: boolean, login: {challenge_type: 'oob', oob_channel: 'sms' | 'email'}, login_recovery: {challenge_type: 'oob', oob_channel: 'sms' | 'email'}}; export type AutoTokenType = {type: 'SHA256' | 'HMAC-SHA256'}; export type CustomersSettings = {users?: ({head?: {enabled: boolean} | undefined, phone_number_validation?: {allow_duplicates: boolean} | undefined, customer_id_format: 'uuid' | 'sequence', mfa?: CustomersMfaSettings | undefined, verifications?: ({phone_number?: 'sms' | undefined}) | undefined}) | undefined, auto_tokens?: ({phone_number?: Array | undefined, email?: Array | undefined}) | undefined, tokens?: ({event_expiry?: Array<{status: string, time_to_live: number}> | undefined}) | undefined, attributes?: (Array<{name: string, type: 'string' | 'integer' | 'real' | 'boolean' | 'phonenumber' | 'email' | 'date' | 'date-time', multiple_values?: boolean | undefined, private?: boolean | undefined}>) | undefined}; export type UpdateApiAccountsAidCustomersSettingsArgs = {aid: unknown, body: CustomersSettings}; export type UpdateApiAccountsAidCustomersSettingsOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const customersMfaSettings = z.object({enabled: z.boolean(), login: z.object({challenge_type: z.literal("oob"), oob_channel: z.enum(["sms", "email"])}), login_recovery: z.object({challenge_type: z.literal("oob"), oob_channel: z.enum(["sms", "email"])})}); export const autoTokenType = z.object({type: z.enum(["SHA256", "HMAC-SHA256"])}); export const customersSettings = z.object({users: z.object({head: z.object({enabled: z.boolean()}).optional(), phone_number_validation: z.object({allow_duplicates: z.boolean()}).optional(), customer_id_format: z.enum(["uuid", "sequence"]), mfa: customersMfaSettings.optional(), verifications: z.object({phone_number: z.literal("sms").optional()}).optional()}).optional(), auto_tokens: z.object({phone_number: z.array(autoTokenType).optional(), email: z.array(autoTokenType).optional()}).optional(), tokens: z.object({event_expiry: z.array(z.object({status: z.string(), time_to_live: z.number().int()})).optional()}).optional(), attributes: z.array(z.object({name: z.string(), type: z.enum(["string", "integer", "real", "boolean", "phonenumber", "email", "date", "date-time"]), multiple_values: z.boolean().optional(), private: z.boolean().optional()})).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 }; /** * aid_customers_atributes_put * * PUT /accounts/{aid}/customers/settings */ export const updateApiAccountsAidCustomersSettings = async (args: UpdateApiAccountsAidCustomersSettingsArgs, options?: UpdateApiAccountsAidCustomersSettingsOptions) => { 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))}/customers/settings${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(customersSettings, await res.json()) };