import {z} from 'zod' export type RevAppComponentsLink = {rel?: string | undefined, href?: string | undefined}; export type RevAppModelsFieldValue = {field_id?: number | undefined, label?: string | undefined, value?: string | undefined, links?: Array | undefined}; export type RevAppModelsTicket = {ticket_id?: number | undefined, description?: string | undefined, status?: ('OPEN' | 'INWORK' | 'CLOSED' | 'PENDING_CANCELLATION' | 'CANCELED' | 'PENDING_CLOSE' | 'PENDING') | undefined, resolution?: string | undefined, ticket_type_id?: number | undefined, ticket_step_id?: number | undefined, assigned_user_id?: number | undefined, assigned_group_id?: number | undefined, customer_id?: number | undefined, service_id?: number | undefined, priority_id?: number | undefined, contact_name?: string | undefined, contact_phone?: string | undefined, contact_email?: string | undefined, additional_contact_emails?: string | undefined, admin_ccs?: string | undefined, custom_status?: string | undefined, due_date?: string | undefined, created_date?: string | undefined, closed_date?: string | undefined, created_by?: number | undefined, fields?: Array | undefined, modified_date?: string | undefined, modified_by?: number | undefined, process_id?: number | undefined, contact_id?: number | undefined, provider_ticket_number?: string | undefined, provider_contact_ID?: number | undefined, process_phase_id?: number | undefined, creation_source?: string | undefined, links?: Array | undefined}; export type CreateApiV1TicketsArgs = {body: RevAppModelsTicket}; export type CreateApiV1TicketsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revWebAPIModelsCreate = z.object({ok: z.boolean().optional(), id: 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 }; /** * Create ticket * * POST /v1/Tickets */ export const createApiV1Tickets = async (args: CreateApiV1TicketsArgs, options?: CreateApiV1TicketsOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Tickets${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(revWebAPIModelsCreate, await res.json()) };