import {z} from 'zod' export type Table = 0 | 1 | 2 | 5 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 50 | 51 | 52 | 53 | 55 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 72; export type ActionEventType = 1 | 2 | 3 | 4 | 5 | 6 | 7; export type ViewType = 0 | 1; export type GetApiV1AutomationsArgs = {Id?: number | undefined, Name?: string | undefined, Description?: string | undefined, Status?: number | undefined, LinkedType?: Table | undefined, EventType?: ActionEventType | undefined, TimeBased?: boolean | undefined, Q?: string | undefined, Fields?: string | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ItemsPerPage?: number | undefined, ReturnTotalItems?: boolean | undefined, sortCol?: string | undefined, sortDir?: string | undefined}; export type GetApiV1AutomationsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const table = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(5), z.literal(6), z.literal(7), z.literal(10), z.literal(11), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(16), z.literal(19), z.literal(20), z.literal(21), z.literal(22), z.literal(23), z.literal(25), z.literal(26), z.literal(27), z.literal(31), z.literal(32), z.literal(34), z.literal(35), z.literal(36), z.literal(37), z.literal(38), z.literal(39), z.literal(40), z.literal(41), z.literal(42), z.literal(43), z.literal(44), z.literal(45), z.literal(46), z.literal(47), z.literal(48), z.literal(50), z.literal(51), z.literal(52), z.literal(53), z.literal(55), z.literal(60), z.literal(61), z.literal(62), z.literal(63), z.literal(64), z.literal(65), z.literal(66), z.literal(67), z.literal(68), z.literal(69), z.literal(72)]); export const actionEventType = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7)]); export const automationData = z.object({id: z.number().int().optional(), name: z.string().nullable().optional(), description: z.string().nullable().optional(), priority: z.number().int().optional(), departmentIds: z.string().nullable().optional(), linkedType: table.optional(), eventType: actionEventType, status: z.number().int().optional(), workingHourType: z.number().int().optional(), runCount: z.number().int().optional(), timeBased: z.boolean().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const automationDataQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(automationData).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 }; /** * List all existing automations * * GET /v1/automations * * Also accepts auth: API Key */ export const getApiV1Automations = async (args: GetApiV1AutomationsArgs, options: GetApiV1AutomationsOptions) => { 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.Name != null) query.append("Name", String(args.Name)) if (args.Description != null) query.append("Description", String(args.Description)) if (args.Status != null) query.append("Status", String(args.Status)) if (args.LinkedType != null) query.append("LinkedType", String(args.LinkedType)) if (args.EventType != null) query.append("EventType", String(args.EventType)) if (args.TimeBased != null) query.append("TimeBased", String(args.TimeBased)) if (args.Q != null) query.append("Q", String(args.Q)) if (args.Fields != null) query.append("Fields", String(args.Fields)) 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/automations${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(automationDataQueryResult, await res.json()) };