import {z} from 'zod' export type HelpDeskRoutingActions = {team_id?: string | undefined, add_tags?: Array | undefined, set_priority?: ('urgent' | 'high' | 'normal' | 'low') | undefined}; export type HelpDeskRoutingRuleInput = {rule_id?: (string | null) | undefined, name: string, enabled?: boolean | undefined, match: 'all' | 'any', domains?: Array | undefined, include_subdomains?: boolean | undefined, keywords?: Array | undefined, actions: HelpDeskRoutingActions}; export type UpdateApiProductsHelpDeskRoutingRulesArgs = {body: {rules: Array, version: number}}; export type UpdateApiProductsHelpDeskRoutingRulesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const helpDeskRoutingActions = z.object({team_id: z.string().optional(), add_tags: z.array(z.string().max(32)).optional(), set_priority: z.enum(["urgent", "high", "normal", "low"]).optional()}); export const helpDeskRoutingRule = z.object({rule_id: z.string(), name: z.string().max(80), enabled: z.boolean(), match: z.enum(["all", "any"]), domains: z.array(z.string()), include_subdomains: z.boolean(), keywords: z.array(z.string().max(64)), actions: helpDeskRoutingActions}); export const helpDeskRoutingRules = z.object({version: z.number().int(), rules: z.array(helpDeskRoutingRule), shadowed_rule_ids: z.array(z.string())}); export const responseMeta = z.object({api_version: z.string().optional(), request_id: z.string().optional(), response_ms: z.number().int().optional(), timestamp: z.number().int().optional(), pools: z.object({}).optional(), billing: z.object({past_due: z.boolean().optional()}).optional(), quota: z.object({limit: z.number().int().optional(), used: z.number().int().optional(), remaining: z.number().int().optional(), resets_at: z.number().int().optional()}).optional()}); export const updateApiProductsHelpDeskRoutingRulesResponse = z.object({data: helpDeskRoutingRules, meta: responseMeta.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 }; /** * Save the routing rules * * PUT /products/help-desk/routing-rules */ export const updateApiProductsHelpDeskRoutingRules = async (args: UpdateApiProductsHelpDeskRoutingRulesArgs, options?: UpdateApiProductsHelpDeskRoutingRulesOptions) => { const {baseUrl = "https://api.sparkvault.com/v1", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/products/help-desk/routing-rules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiProductsHelpDeskRoutingRulesResponse, await res.json()) };