import {z} from 'zod' export type UserInput = {agent_brand_ids?: Array | undefined, custom_role_id?: number | undefined, email: string, external_id?: string | undefined, identities?: Array<{type: string, value: string}> | undefined, name: string, organization?: {name: string} | undefined, organization_id?: number | undefined, role?: string | undefined} | {[key: string]: unknown} | {email?: string | undefined, name?: string | undefined, organization_id?: number | undefined, password?: string | undefined} | {[key: string]: unknown} | {id?: number | undefined} | {[key: string]: unknown}; export type UserRequest = {user: UserInput}; export type CreateApiApiV2UsersCreateOrUpdateArgs = {body: UserRequest}; export type CreateApiApiV2UsersCreateOrUpdateOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const userObject = z.union([z.object({active: z.boolean().optional(), alias: z.string().optional(), chat_only: z.boolean().optional(), created_at: z.string().optional(), custom_role_id: z.number().int().nullable().optional(), default_group_id: z.number().int().optional(), details: z.string().optional(), email: z.string().optional(), external_id: z.string().nullable().optional(), iana_time_zone: z.string().optional(), id: z.number().int().optional(), last_login_at: z.string().optional(), locale: z.string().optional(), locale_id: z.number().int().optional(), moderator: z.boolean().optional(), name: z.string(), notes: z.string().optional(), only_private_comments: z.boolean().optional(), organization_id: z.number().int().nullable().optional(), phone: z.string().nullable().optional(), photo: z.record(z.string(), z.unknown()).nullable().optional(), remote_photo_url: z.string().optional(), report_csv: z.boolean().optional(), restricted_agent: z.boolean().optional(), role: z.string().optional(), role_type: z.number().int().nullable().optional(), shared: z.boolean().optional(), shared_agent: z.boolean().optional(), shared_phone_number: z.boolean().nullable().optional(), signature: z.string().optional(), suspended: z.boolean().optional(), tags: z.array(z.string()).optional(), ticket_restriction: z.string().nullable().optional(), time_zone: z.string().optional(), two_factor_auth_enabled: z.boolean().nullable().optional(), updated_at: z.string().optional(), url: z.string().optional(), user_fields: z.record(z.string(), z.unknown()).optional(), verified: z.boolean().optional()}).and(z.record(z.string(), z.unknown())), z.object({created_at: z.string().optional(), email: z.string().optional(), iana_time_zone: z.string().optional(), id: z.number().int().optional(), locale: z.string().optional(), locale_id: z.number().int().optional(), name: z.string(), organization_id: z.number().int().optional(), phone: z.string().optional(), photo: z.record(z.string(), z.unknown()).optional(), role: z.string().optional(), shared_phone_number: z.boolean().optional(), time_zone: z.string().optional(), updated_at: z.string().optional(), url: z.string().optional(), verified: z.boolean().optional()}).and(z.record(z.string(), z.unknown()))]); export const userResponse = z.object({user: userObject.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 }; /** * Create Or Update User * * POST /api/v2/users/create_or_update */ export const createApiApiV2UsersCreateOrUpdate = async (args: CreateApiApiV2UsersCreateOrUpdateArgs, options?: CreateApiApiV2UsersCreateOrUpdateOptions) => { const {baseUrl = "https://example.zendesk.com", fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/users/create_or_update${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(userResponse, await res.json()) };