import {z} from 'zod' export type GetApiProductsHelpDeskOperatorsArgs = {team_id?: string | undefined, cursor?: string | undefined}; export type GetApiProductsHelpDeskOperatorsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const helpDeskOperatorListed = z.object({user_id: z.string(), name: z.string().nullable(), email: z.string(), team_id: z.string().nullable(), available: z.boolean(), display_first: z.string().nullable(), display_last_initial: z.string().nullable(), avatar_handle: z.string().nullable(), open_assigned_count: z.number().int(), in_training: z.boolean(), training_manager_id: z.string().nullable(), can_manage_training: z.boolean(), version: z.number().int().gte(0)}); export const helpDeskOperatorsPage = z.object({operators: z.array(helpDeskOperatorListed), count: z.number().int(), has_more: z.boolean(), next_cursor: z.string().nullable()}); 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 getApiProductsHelpDeskOperatorsResponse = z.object({data: helpDeskOperatorsPage, 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 }; /** * List the desk's operators * * GET /products/help-desk/operators */ export const getApiProductsHelpDeskOperators = async (args: GetApiProductsHelpDeskOperatorsArgs, options?: GetApiProductsHelpDeskOperatorsOptions) => { const {baseUrl = "https://api.sparkvault.com/v1", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.team_id != null) query.append("team_id", String(args.team_id)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/products/help-desk/operators${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(getApiProductsHelpDeskOperatorsResponse, await res.json()) };