import {z} from 'zod' export type EqFilter = {eq: string}; export type SortAscDescDefaultDesc = 'asc' | 'desc'; export type SortAscDesc = 'asc' | 'desc'; export type GetApiCommentsArgs = {start_cursor?: string | undefined, per_page?: number | undefined, filter?: ({commentable_id?: EqFilter | undefined, commentable_type?: EqFilter | undefined, user_id?: EqFilter | undefined}) | undefined, sort?: ({id?: SortAscDescDefaultDesc | undefined, updated_at?: SortAscDesc | undefined}) | undefined}; export type GetApiCommentsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const eqFilter = z.object({eq: z.string()}); export const sortAscDesc = z.enum(["asc", "desc"]); export const commentableType = z.enum(["Contact", "FleetcorCard", "FuelEntry", "InventoryJournalEntry", "Issue", "Part", "PurchaseOrder", "ServiceEntry", "ServiceReminder", "SubmittedInspectionItem", "Vehicle", "VehicleAssignment", "Vendor", "WexCard", "WorkOrder"]); export const contactMini = z.object({id: z.number().int().optional(), email: z.string().nullable().optional(), name: z.string().nullable().optional(), group_id: z.number().int().nullable().optional(), employee: z.boolean().nullable().optional(), employee_number: z.string().nullable().optional(), default_image_url: z.string().nullable().optional()}).nullable(); export const commentSummary = z.object({id: z.number().int(), created_at: z.string().optional(), updated_at: z.string().optional(), commentable_type: commentableType, commentable_id: z.number().int(), user_id: z.number().int(), comment: z.string(), rich_content: z.object({}).nullable(), html_content: z.string().nullable(), with_mentions: z.boolean(), author: contactMini.optional()}); export const getApiCommentsResponse = z.object({start_cursor: z.string().nullable().optional(), next_cursor: z.string().nullable().optional(), per_page: z.number().int().optional(), estimated_remaining_count: z.number().int().optional(), filtered_by: z.array(z.union([z.object({commentable_id: eqFilter.optional()}), z.object({commentable_type: eqFilter.optional()}), z.object({user_id: eqFilter.optional()})])).optional(), sorted_by: z.array(z.union([z.object({updated_at: sortAscDesc.optional()}), z.object({id: sortAscDesc.optional()})])).optional(), records: z.array(commentSummary).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 }; /** * List Comments * * GET /comments */ export const getApiComments = async (args: GetApiCommentsArgs, options?: GetApiCommentsOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_cursor != null) query.append("start_cursor", String(args.start_cursor)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (args.sort != null) query.append("sort", JSON.stringify(args.sort)) if (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/comments${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(getApiCommentsResponse, await res.json()) };