import {z} from 'zod' export type VersionSelector = 'current' | 'draft' | 'working'; export type InNash = 'must' | 'aim_for' | 'report' | 'not_in_nash'; export type GetApiV1RuleBooksRuleBookIdRulesArgs = {rule_book_id: string, version?: VersionSelector | undefined, in_nash?: (InNash | null) | undefined, q?: (string | null) | undefined}; export type GetApiV1RuleBooksRuleBookIdRulesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const versionState = z.enum(["draft", "published", "superseded", "discarded"]); export const versionRef = z.object({id: z.string(), number: z.number().int(), state: versionState}); export const customerWords = z.object({text: z.string().min(1).max(2000), source_id: z.string().nullable().optional(), verbatim: z.boolean().optional()}); export const appliesTo = z.object({all_sites: z.boolean().optional(), site_ids: z.array(z.string().min(1).max(64)).optional()}); export const inNash = z.enum(["must", "aim_for", "report", "not_in_nash"]); export const valueScope = z.object({all_sites: z.boolean().optional(), site_id: z.string().min(1).max(64).nullable().optional()}); export const valueUnit = z.enum(["h", "min", "stops", "cube", "percent", "count", "trucks", "time_of_day", "window"]); export const nashValue = z.object({amount: z.union([z.number().int(), z.number()]).nullable().optional(), unit: valueUnit.nullable().optional(), text: z.string().min(1).max(200).nullable().optional()}); export const ruleStatus = z.enum(["confirmed", "assumed", "needs_answer"]); export const authoredValueStatus = z.enum(["assumed", "needs_answer"]); export const ruleValueRead = z.object({scope: valueScope, customer_value: z.string().min(1).max(2000).nullable(), nash_value: nashValue.nullable(), status: ruleStatus, authored_status: authoredValueStatus, status_reason: z.string().min(1).max(2000).nullable(), source_ids: z.array(z.string())}); export const sourceKind = z.enum(["sheet_cells", "file_columns", "document", "call", "written_answer", "nash_reading"]); export const cellExcerpt = z.object({ref: z.string(), value: z.string().min(1).max(2000)}); export const ruleSourceRead = z.object({id: z.string(), kind: sourceKind, label: z.string().min(1).max(200).nullable(), file_name: z.string().min(1).max(200).nullable(), sheet: z.string().min(1).max(200).nullable(), cells: z.array(z.string()), columns: z.array(z.string().min(1).max(200)), excerpt: z.array(cellExcerpt), locator: z.string().min(1).max(200).nullable(), url: z.string().max(2000).nullable(), title: z.string().min(1).max(200).nullable(), date: z.string().nullable(), author: z.string().min(1).max(200).nullable(), verbatim: z.boolean(), basis: z.string().min(1).max(2000).nullable(), verified: z.boolean()}); export const ruleStatusRead = z.object({state: ruleStatus, reason: z.string().nullable()}); export const confirmedBy = z.object({name: z.string().min(1).max(200), role: z.string().min(1).max(200).nullable().optional(), user_id: z.string().nullable().optional()}); export const confirmationChannel = z.enum(["call", "email", "written_answer", "meeting", "portal"]); export const actorType = z.enum(["person", "agent", "integration"]); export const actor = z.object({type: actorType, id: z.string(), display: z.string()}); export const actorAt = z.object({actor: actor, at: z.string()}); export const confirmationRead = z.object({id: z.string(), rule_id: z.string(), revision_id: z.string(), confirmed_by: confirmedBy, channel: confirmationChannel, confirmed_at: z.string(), source_url: z.string().nullable(), source_missing: z.boolean(), note: z.string().nullable(), recorded: actorAt}); export const ruleRead = z.object({id: z.string(), key: z.string(), aliases: z.array(z.string()), rule_book_id: z.string(), version: versionRef, revision_id: z.string(), revision_number: z.number().int(), added_in_version: z.number().int().nullable(), revised_in_version: z.number().int().nullable(), name: z.string().min(1).max(200), customer_words: customerWords.nullable(), applies_to: appliesTo, in_nash: inNash, how: z.string().min(1).max(2000).nullable(), values: z.array(ruleValueRead), sources: z.array(ruleSourceRead), status: ruleStatusRead, confirmations: z.array(confirmationRead), created: actorAt, updated: actorAt, etag: z.string()}); export const ruleList = z.object({version: versionRef.nullable(), rules: z.array(ruleRead)}); 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 }; /** * List Rules * * GET /v1/rule-books/{rule_book_id}/rules */ export const getApiV1RuleBooksRuleBookIdRules = async (args: GetApiV1RuleBooksRuleBookIdRulesArgs, options?: GetApiV1RuleBooksRuleBookIdRulesOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.version != null) query.append("version", String(args.version)) if (args.in_nash != null) query.append("in_nash", String(args.in_nash)) if (args.q != null) query.append("q", String(args.q)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/rule-books/${encodeURIComponent(String(args.rule_book_id))}/rules${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(ruleList, await res.json()) };