import {z} from 'zod' export type ProxyProfileWriteRequest = {email?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, middle_name?: string | undefined, phone?: string | undefined, type?: ('csbt_admin' | 'employee' | 'finance' | 'finance-gga' | 'manager' | 'master' | 'self_booker' | 'sub_agent_supervisor' | 'supervisor' | 'travel_manager') | undefined}; export type CreateApiApiB2bV3ProfilesEditArgs = {body: ProxyProfileWriteRequest}; export type CreateApiApiB2bV3ProfilesEditOptions = {baseUrl: string, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const proxyJSONBody = z.record(z.string(), z.unknown()); export const v3DebugEndpoint = z.object({endpoint: z.string().optional(), is_active: z.boolean().optional(), is_limited: z.boolean().optional(), remaining: z.number().int().optional(), requests_number: z.number().int().optional(), reset: z.string().optional(), seconds_number: z.number().int().optional()}); export const v3Debug = z.object({api_endpoint: v3DebugEndpoint.optional(), api_key_id: z.number().int().optional(), key_id: z.number().int().optional(), method: z.string().optional(), real_ip: z.string().optional(), request: z.array(z.number().int()).optional(), request_id: z.string().optional(), utcnow: z.string().optional(), validation_error: z.string().optional()}); export const createApiApiB2bV3ProfilesEditResponse = z.object({data: proxyJSONBody.optional(), debug: v3Debug.optional(), error: z.string().optional(), status: z.string().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 }; /** * Edit profile * * POST /api/b2b/v3/profiles/edit */ export const createApiApiB2bV3ProfilesEdit = async (args: CreateApiApiB2bV3ProfilesEditArgs, options: CreateApiApiB2bV3ProfilesEditOptions) => { const {baseUrl, 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(/\/+$/, '')}/api/b2b/v3/profiles/edit${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(createApiApiB2bV3ProfilesEditResponse, await res.json()) };