import {z} from 'zod' export type Tags = {[key: string]: unknown} | null; export type UpdateUserTags = {enabled?: boolean | undefined, tags?: Tags | undefined}; export type UpdateUserDisable = {enabled?: boolean | undefined, tags?: Tags | undefined}; export type UpdateApiUsersUserIdArgs = {body: UpdateUserTags | UpdateUserDisable}; export type UpdateApiUsersUserIdOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string().min(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const tags = z.record(z.string(), z.unknown()).nullable(); export const user = z.object({id: id.optional(), created_at: createdAt.optional(), updated_at: updatedAt.optional(), created_by: z.string().optional(), email: z.string().min(1).nullable().optional(), enabled: z.boolean().optional(), first_name: z.string().max(50).nullable().optional(), identity: z.string().nullable().optional(), last_name: z.string().max(50).nullable().optional(), last_used_date: z.string().nullable().optional(), password: z.string().nullable().optional(), role: z.enum(["ROLE_PLATFORM", "ROLE_PARTNER", "ROLE_MERCHANT"]).optional(), tags: tags.optional(), _links: z.object({application: z.object({href: z.string().optional()}).optional(), applications: z.object({href: z.string().optional()}).optional(), self: z.object({href: z.string().optional()}).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 }; /** * Update a User * * PUT /users/{user_id} */ export const updateApiUsersUserId = async (args: UpdateApiUsersUserIdArgs, options?: UpdateApiUsersUserIdOptions) => { const {baseUrl = "https://finix.sandbox-payments-api.com", 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(/\/+$/, '')}/users/${encodeURIComponent(String(args.user_id))}${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(user, await res.json()) };