import {z} from 'zod' export type GetApiV2RefPermissionsArgs = {usertype?: ('AccountUser' | 'EndUser' | 'ManagementUser' | 'AlianzaUser' | 'BusinessLines') | undefined}; export type GetApiV2RefPermissionsOptions = {baseUrl?: string | undefined, XAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const permissionTypeDefaultsX = z.object({type: z.enum(["NONE", "Account", "AccountHistory", "AccountCallHistory", "AccountBulkStatuses", "IpWhitelist", "PhoneNumber", "PhoneNumberServiceActivation", "PhoneNumberInventory", "Product", "ProductCallingPlan", "ProductGroups", "ProductUsers", "Reports", "RoutePlan", "SipCredentials", "EU_DeviceSwapCreate", "EU_Account", "EU_AccountMedia", "EU_CallHistory", "EU_Device", "EU_DeviceSwap", "EU_DeviceLine", "EU_IVR", "EU_LineAppearance", "EU_LineHardware", "EU_LineHuntGroup", "EU_Voicemail", "EU_VoicemailMessage", "EU_VoicemailUnlock", "EU_Telephone", "EU_SipTrunk", "EU_User", "EU_UserResetPassword", "EU_History", "EU_BUSINESS_LINE", "EU_BUSINESS_LINE_PROVISIONING", "EU_BUSINESS_LINE_VOICEMAIL", "EU_BUSINESS_LINE_HUNT_GROUP", "EU_911_ADDRESS", "EU_TELEPHONE_DESTINATION", "EU_ENDUSER_REPURPOSE", "ALIANZA_MasterRoutePlan", "ALIANZA_TogglzAdmin", "ANY_END_USER", "ANY_MANAGEMENT_USER", "ANY_ALIANZA_USER"]).optional(), maxAccessType: z.enum(["NONE", "READ_MYUSER", "READ", "EDIT_MYUSER", "EDIT", "CREATE_MYUSER", "CREATE", "DELETE_MYUSER", "DELETE", "DENIED"]).optional()}); export const getApiV2RefPermissionsResponse = z.array(permissionTypeDefaultsX); 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 }; /** * PermissionType and Access List * * GET /v2/ref/permissions */ export const getApiV2RefPermissions = async (args: GetApiV2RefPermissionsArgs, options?: GetApiV2RefPermissionsOptions) => { const {baseUrl = "https://api.alianza.com", fetch: fetchFn = fetch, XAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.usertype != null) query.append("usertype", String(args.usertype)) if (XAuthToken !== undefined) headers.set("X-AUTH-TOKEN", XAuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/v2/ref/permissions${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(getApiV2RefPermissionsResponse, await res.json()) };