import {z} from 'zod' export type ViewType = 0 | 1; export type GetApiV1TicketsTicketIdTimeentriesArgs = {Id?: number | undefined, Billable?: number | undefined, FromDate?: string | undefined, ToDate?: string | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ItemsPerPage?: number | undefined, ReturnTotalItems?: boolean | undefined, ticketId: number, sortCol?: string | undefined, sortDir?: string | undefined}; export type GetApiV1TicketsTicketIdTimeentriesOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const timeEntryResponseModel = z.object({id: z.number().int().optional(), note: z.string().nullable().optional(), agentId: z.number().int().optional(), billable: z.number().int().optional(), companyId: z.number().int().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), executedAt: z.string().nullable().optional(), startTime: z.string().optional(), timerRunning: z.number().int().optional(), timeSpent: z.number().int().optional(), ticketId: z.number().int().optional(), totalTimeSpent: z.number().int().optional()}); export const timeEntryResponseModelQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(timeEntryResponseModel).nullable().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 }; /** * Gets all time entries * * GET /v1/tickets/{ticketId}/timeentries * * Also accepts auth: API Key */ export const getApiV1TicketsTicketIdTimeentries = async (args: GetApiV1TicketsTicketIdTimeentriesArgs, options: GetApiV1TicketsTicketIdTimeentriesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Id != null) query.append("Id", String(args.Id)) if (args.Billable != null) query.append("Billable", String(args.Billable)) if (args.FromDate != null) query.append("FromDate", String(args.FromDate)) if (args.ToDate != null) query.append("ToDate", String(args.ToDate)) if (args.ViewType != null) query.append("ViewType", String(args.ViewType)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.ItemsPerPage != null) query.append("ItemsPerPage", String(args.ItemsPerPage)) if (args.ReturnTotalItems != null) query.append("ReturnTotalItems", String(args.ReturnTotalItems)) if (args.sortCol != null) query.append("sortCol", String(args.sortCol)) if (args.sortDir != null) query.append("sortDir", String(args.sortDir)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tickets/${encodeURIComponent(String(args.ticketId))}/timeentries${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(timeEntryResponseModelQueryResult, await res.json()) };