import {z} from 'zod' export type CreateApiRolesArgs = {body: {name: string, permissions: {general?: ({voicemail_view_only?: boolean | undefined, voicemail_full_access?: boolean | undefined, meeting?: boolean | undefined}) | undefined, system?: ({information?: boolean | undefined, dealer?: boolean | undefined, tenant?: boolean | undefined}) | undefined, trunk?: ({view_only?: boolean | undefined, full_access?: boolean | undefined}) | undefined, company?: ({view_only?: boolean | undefined, full_access?: boolean | undefined}) | undefined, company_call_log?: ({call_log_view_only?: boolean | undefined, call_log_full_access?: boolean | undefined, call_recording_view_only?: boolean | undefined, call_recording_full_access?: boolean | undefined}) | undefined, phone_system?: ({view_only?: boolean | undefined, full_access?: boolean | undefined}) | undefined, user_management?: ({roles?: boolean | undefined, users?: boolean | undefined}) | undefined, user_call_log?: ({call_log_view_only?: boolean | undefined, call_log_full_access?: boolean | undefined, call_recording_view_only?: boolean | undefined, call_recording_full_access?: boolean | undefined}) | undefined, policies?: ({domestic_calls?: boolean | undefined, internal_calls?: boolean | undefined, international_calls?: boolean | undefined}) | undefined, analytics?: ({call_report_view_only?: boolean | undefined, call_report_full_access?: boolean | undefined}) | undefined, billing?: ({view_only?: boolean | undefined, full_access?: boolean | undefined}) | undefined}, description?: string | undefined}}; export type CreateApiRolesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiRolesResponse = z.object({name: z.string().min(1).max(64).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 }; /** * Create a role * * POST /roles */ export const createApiRoles = async (args: CreateApiRolesArgs, options?: CreateApiRolesOptions) => { const {baseUrl = "https://HOSTNAME:8887/api", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/roles${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiRolesResponse, await res.json()) };