import {z} from 'zod' export type GetApiV1OrdersIdArgs = {id: number}; export type GetApiV1OrdersIdOptions = {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 revWebAPIModelsGetOrSearchOfRevAppModelsOrder = z.object({ok: z.boolean().optional(), 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 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 }; /** * View order * * GET /v1/Orders/{id} */ export const getApiV1OrdersId = async (args: GetApiV1OrdersIdArgs, options?: GetApiV1OrdersIdOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Orders/${encodeURIComponent(String(args.id))}${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(revWebAPIModelsGetOrSearchOfRevAppModelsOrder, await res.json()) };