import {z} from 'zod' export type GetApiApiEventsArgs = {created_datetime?: ({lt?: string | undefined, lte?: string | undefined, gt?: string | undefined, gte?: string | undefined}) | undefined, cursor?: string | undefined, limit?: number | undefined, object_id?: number | undefined, object_type?: ('Account' | 'AccountSetting' | 'Macro' | 'Tag' | 'Customer' | 'Team' | 'View' | 'Widget' | 'CustomField' | 'CustomFieldCondition' | 'User' | 'TicketMessage' | 'Ticket' | 'Rule' | 'Integration' | 'SatisfactionSurvey') | undefined, order_by?: ('created_datetime' | 'created_datetime:asc' | 'created_datetime:desc') | undefined, types?: (Array<'account-created' | 'account-deactivated' | 'account-setting-updated' | 'account-updated' | 'action-executed' | 'custom-field-condition-created' | 'custom-field-condition-deleted' | 'custom-field-condition-disabled' | 'custom-field-condition-enabled' | 'custom-field-condition-updated' | 'custom-field-created' | 'custom-field-updated' | 'customer-created' | 'customer-deleted' | 'customer-merged' | 'customer-updated' | 'integration-created' | 'integration-deactivated' | 'integration-deleted' | 'integration-reactivated' | 'integration-updated' | 'macro-created' | 'macro-deleted' | 'macro-updated' | 'rule-created' | 'rule-deleted' | 'rule-executed' | 'rule-priority-updated' | 'rule-updated' | 'satisfaction-survey-responded' | 'satisfaction-survey-sent' | 'tag-created' | 'tag-deleted' | 'tag-merged' | 'tag-renamed' | 'tag-updated' | 'team-created' | 'team-deleted' | 'team-updated' | 'ticket-assigned' | 'ticket-closed' | 'ticket-created' | 'ticket-customer-updated' | 'ticket-deleted' | 'ticket-excluded-from-auto-merge' | 'ticket-excluded-from-csat' | 'ticket-marked-spam' | 'ticket-merged' | 'ticket-message-created' | 'ticket-message-deleted' | 'ticket-message-failed' | 'ticket-message-summary-created' | 'ticket-message-updated' | 'ticket-reopened' | 'ticket-satisfaction-survey-skipped' | 'ticket-self-unsnoozed' | 'ticket-sla-policy-assigned' | 'ticket-snoozed' | 'ticket-split' | 'ticket-subject-updated' | 'ticket-tags-added' | 'ticket-tags-removed' | 'ticket-team-assigned' | 'ticket-team-unassigned' | 'ticket-trashed' | 'ticket-unassigned' | 'ticket-unmarked-spam' | 'ticket-untrashed' | 'ticket-updated' | 'user-2fa-changed' | 'user-availability-updated' | 'user-created' | 'user-deleted' | 'user-impersonated' | 'user-invited' | 'user-logged-in' | 'user-logged-out' | 'user-password-changed' | 'user-password-reset' | 'user-updated' | 'view-created' | 'view-deactivated' | 'view-deleted' | 'view-updated' | 'widget-created' | 'widget-deleted' | 'widget-updated'>) | undefined, user_ids?: Array | undefined}; export type GetApiApiEventsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const event = z.object({id: z.number().int().optional(), context: z.string().nullable().optional(), created_datetime: z.string().optional(), data: z.object({}).optional(), object_id: z.number().int().optional(), object_type: z.enum(["Account", "AccountSetting", "Customer", "CustomField", "CustomFieldCondition", "CustomUserAvailabilityStatus", "CustomerExternalData", "Integration", "Macro", "Order", "Role", "Rule", "SatisfactionSurvey", "Shopper", "ShopperAddress", "Subscription", "Tag", "Team", "TicketMessage", "Ticket", "User", "UserSetting", "View", "Widget"]).optional(), type: z.string().optional(), user_id: z.number().int().optional(), uri: z.string().optional()}); export const getApiApiEventsResponse = z.array(event); 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 events * * GET /api/events */ export const getApiApiEvents = async (args: GetApiApiEventsArgs, options?: GetApiApiEventsOptions) => { const {baseUrl = "https://your-domain.gorgias.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.created_datetime != null) query.append("created_datetime", JSON.stringify(args.created_datetime)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.object_id != null) query.append("object_id", String(args.object_id)) if (args.object_type != null) query.append("object_type", String(args.object_type)) if (args.order_by != null) query.append("order_by", String(args.order_by)) for (const item of args.types ?? []) query.append("types", String(item)) for (const item of args.user_ids ?? []) query.append("user_ids", String(item)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/events${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(getApiApiEventsResponse, await res.json()) };