import {z} from 'zod' export type GetApiV1OrdersArgs = {'search.order_id'?: Array | undefined, 'search.customer_id'?: Array | undefined, 'search.status'?: (Array<'NOT_SPECIFIED' | 'NEW' | 'SENT' | 'ERROR' | 'CONFIRMED' | 'COMPLETE' | 'CANCELED' | 'HOLD' | 'FLOW_ERROR' | 'PAYMENT_HOLD' | 'LIFELINE_HOLD' | 'JEOPARDY' | 'PROVISIONED'>) | undefined, 'search.order_type'?: (Array<'NOT_SPECIFIED' | 'ADD_LINE' | 'CHANGE_FEATURE' | 'CHANGE_LISTING' | 'CHANGE_TN' | 'CONVERSION' | 'DENY' | 'DISCONNECT' | 'LD_BLOCK' | 'LD_RESTORE' | 'MIGRATION' | 'MIGRATION_UNEL' | 'NEW' | 'PORT' | 'REFRESH' | 'RESTORE' | 'SUSPEND' | 'TRANSFER' | 'CNAM' | 'E911' | 'FIBER_CONVERSION'>) | undefined, 'search.pon'?: Array | undefined, 'search.assigned_to'?: Array | undefined, 'search.request_id'?: Array | undefined, 'search.created_by'?: Array | undefined, 'search.created_date_start'?: string | undefined, 'search.created_date_end'?: string | undefined, 'search.status_date_start'?: string | undefined, 'search.status_date_end'?: string | undefined, 'search.page_size'?: number | undefined, 'search.page'?: number | undefined, 'search.sort'?: string | undefined}; export type GetApiV1OrdersOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revAppComponentsLink = z.object({rel: z.string().optional(), href: z.string().optional()}); export const revAppModelsFieldValue = z.object({field_id: z.number().int().optional(), label: z.string().optional(), value: z.string().optional(), links: z.array(revAppComponentsLink).optional()}); export const revAppModelsOrder = z.object({order_id: z.number().int().optional(), pon: z.string().max(50).optional(), customer_id: z.number().int().optional(), order_type: z.enum(["NOT_SPECIFIED", "ADD_LINE", "CHANGE_FEATURE", "CHANGE_LISTING", "CHANGE_TN", "CONVERSION", "DENY", "DISCONNECT", "LD_BLOCK", "LD_RESTORE", "MIGRATION", "MIGRATION_UNEL", "NEW", "PORT", "REFRESH", "RESTORE", "SUSPEND", "TRANSFER", "CNAM", "E911", "FIBER_CONVERSION"]).optional(), status: z.enum(["NOT_SPECIFIED", "NEW", "SENT", "ERROR", "CONFIRMED", "COMPLETE", "CANCELED", "HOLD", "FLOW_ERROR", "PAYMENT_HOLD", "LIFELINE_HOLD", "JEOPARDY", "PROVISIONED"]).optional(), provider_id: z.number().int().optional(), assigned_to: z.number().int().optional(), request_id: z.number().int().optional(), process_id: z.number().int().optional(), created_date: z.string().optional(), created_by: z.number().int().optional(), completed_date: z.string().optional(), due_date: z.string().optional(), status_date: z.string().optional(), order_group_id: z.number().int().optional(), hold_reason_id: z.number().int().optional(), retain_listing: z.boolean().optional(), process_phase_id: z.number().int().optional(), flow_through: z.boolean().optional(), latest_send_response: z.string().optional(), fields: z.array(revAppModelsFieldValue).optional(), links: z.array(revAppComponentsLink).optional()}); export const revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsOrder = z.object({ok: z.boolean().optional(), has_more: z.boolean().optional(), record_count: z.number().int().optional(), records: z.array(revAppModelsOrder).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 orders * * GET /v1/Orders */ export const getApiV1Orders = async (args: GetApiV1OrdersArgs, options?: GetApiV1OrdersOptions) => { 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.order_id'] ?? []) query.append("search.order_id", String(item)) for (const item of args['search.customer_id'] ?? []) query.append("search.customer_id", String(item)) for (const item of args['search.status'] ?? []) query.append("search.status", String(item)) for (const item of args['search.order_type'] ?? []) query.append("search.order_type", String(item)) for (const item of args['search.pon'] ?? []) query.append("search.pon", String(item)) for (const item of args['search.assigned_to'] ?? []) query.append("search.assigned_to", String(item)) for (const item of args['search.request_id'] ?? []) query.append("search.request_id", String(item)) for (const item of args['search.created_by'] ?? []) query.append("search.created_by", String(item)) if (args['search.created_date_start'] != null) query.append("search.created_date_start", String(args['search.created_date_start'])) if (args['search.created_date_end'] != null) query.append("search.created_date_end", String(args['search.created_date_end'])) if (args['search.status_date_start'] != null) query.append("search.status_date_start", String(args['search.status_date_start'])) if (args['search.status_date_end'] != null) query.append("search.status_date_end", String(args['search.status_date_end'])) 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/Orders${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(revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsOrder, await res.json()) };