import {z} from 'zod' export type GetApiApiOrgIdOncallTeamsTeamIdReachabilityArgs = {org_id: string, team_id: string}; export type GetApiApiOrgIdOncallTeamsTeamIdReachabilityOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const channel = z.enum(["email", "webhook", "sms", "voice", "slack"]); export const channelReadiness = z.object({blocked_because: z.string().nullable().optional(), channel: channel, configured_but_unverified: z.boolean(), deliverable: z.boolean()}); export const memberReachability = z.object({channels: z.array(channelReadiness), configured_but_unverified: z.array(z.string()), deliverable_channels: z.array(channel), is_org_user: z.boolean(), mailbox_shaped: z.boolean(), user_email: z.string(), why_not: z.string().nullable().optional(), would_a_page_land: z.boolean()}); export const teamReachability = z.object({members: z.array(memberReachability), reachable: z.number().int().gte(0), smtp_configured: z.boolean(), team_id: z.string(), team_name: z.string(), total: z.number().int().gte(0), unreachable_members: z.array(z.string())}); 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 }; /** * Whether a page would reach each member of a team * * GET /api/{org_id}/oncall/teams/{team_id}/reachability */ export const getApiApiOrgIdOncallTeamsTeamIdReachability = async (args: GetApiApiOrgIdOncallTeamsTeamIdReachabilityArgs, options: GetApiApiOrgIdOncallTeamsTeamIdReachabilityOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/oncall/teams/${encodeURIComponent(String(args.team_id))}/reachability${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(teamReachability, await res.json()) };