import {z} from 'zod' export type QuestionsListQuestionsRequestCategoriesItem = 'state_tax_withholdings' | 'state_unemployment_insurance' | 'foreign_qualification' | 'annual_filing'; export type GetApiV1AgentQuestionsArgs = {state?: string | undefined, 'states[]'?: string | undefined, 'jurisdiction_slugs[]'?: string | undefined, 'categories[]'?: QuestionsListQuestionsRequestCategoriesItem | undefined}; export type GetApiV1AgentQuestionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const agentQuestionType = z.enum(["text", "number", "date", "date_part", "select", "multiselect", "boolean", "address", "document", "account", "textarea", "phone", "officers_table", "string"]); export const agentQuestionValidationDateItem = z.object({before: z.boolean(), date: z.string().nullable().optional(), key: z.string().optional(), message: z.string().optional()}); export const agentQuestionValidation = z.object({date: z.array(agentQuestionValidationDateItem).optional(), format: z.array(z.string()).optional(), min_value: z.number().int().optional()}); export const agentQuestionCompanyDependenciesItemItemValue = z.union([z.string(), z.array(z.string())]); export const agentQuestionCompanyDependenciesItemItemComparator = z.enum(["==", "!=", ">", "<", ">=", "<=", "starts_with", "len_gt", "missing_suffix", "has_special_chars"]); export const agentQuestionCompanyDependenciesItemItem = z.object({field: z.string(), value: agentQuestionCompanyDependenciesItemItemValue, comparator: agentQuestionCompanyDependenciesItemItemComparator.nullable().optional()}); export const agentQuestionQuestionDependencyValue = z.union([z.string(), z.array(z.string())]); export const agentQuestionQuestionDependencyComparator = z.enum(["==", "!=", ">", "<", ">=", "<=", "len_gt"]); export const agentQuestionQuestionDependency = z.object({key: z.string(), value: agentQuestionQuestionDependencyValue, comparator: agentQuestionQuestionDependencyComparator.nullable().optional()}); export const agentQuestionWarningDateItem = z.object({before: z.boolean(), date: z.string().nullable().optional(), key: z.string().optional(), offset_days: z.number().int().optional(), message: z.string()}); export const agentQuestionWarning = z.object({date: z.array(agentQuestionWarningDateItem).optional()}); export const agentQuestion = z.object({key: z.string(), label: z.string(), type: agentQuestionType, categories: z.array(z.string()).nullable().optional(), optional: z.boolean().nullable().optional(), validation: agentQuestionValidation.nullable().optional(), company_dependencies: z.array(z.array(agentQuestionCompanyDependenciesItemItem)).nullable().optional(), options: z.array(z.string()).nullable().optional(), sublabel: z.string().nullable().optional(), components: z.array(z.string()).nullable().optional(), question_dependency: agentQuestionQuestionDependency.nullable().optional(), warning: agentQuestionWarning.nullable().optional(), api_only: z.boolean().nullable().optional(), tooltip: z.string().nullable().optional(), jurisdiction_slugs: z.array(z.string()).nullable().optional(), updated_at: z.string().nullable().optional(), ai_validated: z.boolean().nullable().optional()}); export const questionsListQuestionsResponseDataOneItem = z.object({state: z.string().optional(), questions: z.array(agentQuestion).optional()}); export const questionsListQuestionsResponseData = z.union([z.array(agentQuestion), z.array(questionsListQuestionsResponseDataOneItem), z.record(z.string(), z.array(agentQuestion))]); export const questionsListQuestionsResponse = z.object({data: questionsListQuestionsResponseData.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 }; /** * List questions * * GET /v1/agent/questions */ export const getApiV1AgentQuestions = async (args: GetApiV1AgentQuestionsArgs, options?: GetApiV1AgentQuestionsOptions) => { const {baseUrl = "https://api.middesk.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.state != null) query.append("state", String(args.state)) if (args['states[]'] != null) query.append("states[]", String(args['states[]'])) if (args['jurisdiction_slugs[]'] != null) query.append("jurisdiction_slugs[]", String(args['jurisdiction_slugs[]'])) if (args['categories[]'] != null) query.append("categories[]", String(args['categories[]'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/agent/questions${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(questionsListQuestionsResponse, await res.json()) };