import {z} from 'zod' export type GetApiV1ServiceTypesArgs = {'search.service_type_id'?: Array | undefined, 'search.service_code'?: Array | undefined, 'search.description'?: Array | undefined, 'search.active'?: boolean | undefined, 'search.page_size'?: number | undefined, 'search.page'?: number | undefined, 'search.sort'?: string | undefined}; export type GetApiV1ServiceTypesOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revAppModelsServiceFieldBuiltinFieldModel = z.object({field: z.enum(["Account_Group_ID", "Activated_Date", "AddressA_ID", "AddressZ_ID", "AddressZWeight", "AltBtnId", "Bounty_Eligible_Flag", "CallPlanID", "Carrier_Account_ID", "Carrier_Account_Number", "Carrier_ID", "Channels", "Channels_Data", "Channels_Voice", "Circuit_Completion_Date", "Circuit_FOC_Date", "Circuit_Number", "Circuit_Number2", "Circuit_Order_Date", "Circular_Hunt", "CLLI_Code", "Close_Reason_ID", "Contract_End", "Contract_Start", "Customer_ID", "d_Channel", "Description", "Digits_Outpulsed", "Disconnect_Date", "DNIS", "Hunt_Group", "Hunt_Identifier", "Hunt_Sequence", "International_Access", "IPIC", "IsCircuit", "isDataOnly", "IsPorting", "Line_ID", "Line_Type_ID", "LineOCN", "LPIC", "MAC", "Notes", "Override_Class", "Override_Class_Flag", "Package_ID", "Pair", "PBX_Make", "PBX_Model", "PIC", "Port", "Program_ID", "Protocol", "Rate_Center", "RDT_Equipment", "Router_DHCP", "Router_IP", "Router_Password", "Router_Protocol", "Router_Subnet", "Shelf", "Slot", "Switch", "TN", "TN_800", "TN_LD_Access", "TN_Reservation", "TN_VoiceMail", "WanRouter_DHCP", "WanRouter_IP", "WanRouter_Password", "WanRouter_Protocol", "WanRouter_Subnet", "Wireless_Device_ID", "Wireless_Device_Type", "Exchange_Key", "UNEL_Cut_Date", "Circuit_Reference", "Relay_Rack", "Transferred_From_Line_ID"])}); export const revAppModelsServiceFieldCustomFieldModel = z.object({type: z.enum(["TEXTBOX", "TEXTAREA", "DROPDOWNLIST", "RADIOBUTTONLIST", "CHECKBOX", "DATE", "REGEX"]), required: z.boolean().optional(), options: z.array(z.string()).optional(), default_value: z.string().optional(), regex: z.string().optional(), regex_validation_message: z.string().optional()}); export const revAppComponentsLink = z.object({rel: z.string().optional(), href: z.string().optional()}); export const revAppModelsServiceFieldModel = z.object({display_name: z.string(), field_id: z.number().int().optional(), description: z.string().optional(), tab_name: z.string().optional(), builtin_field: revAppModelsServiceFieldBuiltinFieldModel.optional(), custom_field: revAppModelsServiceFieldCustomFieldModel.optional(), links: z.array(revAppComponentsLink).optional()}); export const revAppModelsServiceType = z.object({service_type_id: z.number().int().optional(), active: z.boolean().optional(), description: z.string().max(100), service_code: z.string().max(100), fields: z.array(revAppModelsServiceFieldModel).optional(), links: z.array(revAppComponentsLink).optional()}); export const revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsServiceType = z.object({ok: z.boolean().optional(), has_more: z.boolean().optional(), record_count: z.number().int().optional(), records: z.array(revAppModelsServiceType).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 }; /** * Search service types * * GET /v1/ServiceTypes */ export const getApiV1ServiceTypes = async (args: GetApiV1ServiceTypesArgs, options?: GetApiV1ServiceTypesOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['search.service_type_id'] ?? []) query.append("search.service_type_id", String(item)) for (const item of args['search.service_code'] ?? []) query.append("search.service_code", String(item)) for (const item of args['search.description'] ?? []) query.append("search.description", String(item)) if (args['search.active'] != null) query.append("search.active", String(args['search.active'])) if (args['search.page_size'] != null) query.append("search.page_size", String(args['search.page_size'])) if (args['search.page'] != null) query.append("search.page", String(args['search.page'])) if (args['search.sort'] != null) query.append("search.sort", String(args['search.sort'])) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ServiceTypes${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(revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsServiceType, await res.json()) };