import {z} from 'zod' export type GetApiV2DocumentTypeArgs = {page?: number | undefined, page_size?: number | undefined, authorization: string}; export type GetApiV2DocumentTypeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const documentType = z.enum(["License Agreement", "Beta Test Agreement", "Proof of Concept Agreement", "Statement of Work", "Service Level Agreement", "Master Service Agreement", "Non-Disclosure Agreement (NDA)", "Maintenance and Support Agreement (MSA)", "Software Development Agreement (SDA)", "End User License Agreement (EULA)", "Purchase Order", "Invoice", "Custom", "Custom Document", "Contract", "Service Organization Control 2 (SOC 2)"]); export const servicesAppcatalogTypesTableTableType = z.enum(["TABLE", "TASK"]); export const entityTypeResponse = z.object({slug: z.string(), name: z.string(), is_catalog: z.boolean(), is_hidden: z.boolean()}); export const tableResponse = z.object({id: z.string(), slug: z.string(), type: servicesAppcatalogTypesTableTableType, page_size: z.number().int().optional(), entity_type: entityTypeResponse}); export const servicesAppcatalogTypesItemItemType = z.enum(["OBJECTID", "TEXT", "RICHTEXT", "HIGHLIGHTEXT", "DATEDELTA", "DATE", "SINGLESELECT", "BINARY", "MULTISELECT", "PHONE", "EMAIL", "USER", "TEAM", "NUMBER", "FILE", "URL", "CURRENCY", "ACTION", "TABLE", "CATEGORIES", "ENTITYRELATION", "TAGS", "PICKLISTSELECT", "IDENTITY", "IDENTITYLIST", "CLAUSE", "RISKSEVERITY", "DASHBOARD", "TABLE_LOOKUP", "ENTITY_REFERENCE", "CHILD_ENTITY", "DOCUMENT_SUBTYPE"]); export const servicesAppcatalogModelComputationTypeComputationType = z.enum(["NONE", "FORMULA", "CONDITION", "TABLE_LOOKUP", "AI_PROMPT"]); export const computationArgsResponse = z.object({type: servicesAppcatalogModelComputationTypeComputationType, args: z.object({}).optional()}); export const itemResponse = z.object({id: z.string(), slug: z.string(), db_field: z.string(), name: z.string(), type: servicesAppcatalogTypesItemItemType, args: z.object({}).optional(), is_default: z.boolean(), is_required: z.boolean(), entity_type: entityTypeResponse.optional(), table: z.union([tableResponse, z.string()]).optional(), created_at: z.string(), updated_at: z.string(), deleted_at: z.string().optional(), computation: computationArgsResponse.optional()}); export const createPosition = z.object({placeholder_id: z.string().optional(), grid: z.object({}).optional(), sequence: z.number().int().optional(), size: z.number().int().optional()}); export const fieldResponse = z.object({id: z.string().optional(), name: z.string(), item_slug: z.string().optional(), is_table: z.boolean().optional(), item_type: servicesAppcatalogTypesItemItemType.optional(), item_args: z.object({}).optional(), description: z.string().optional(), generate_ai_summary: z.boolean().optional(), is_hidden: z.boolean().optional(), is_summary: z.boolean().optional(), item: itemResponse, position: createPosition}); export const sectionResponse = z.object({id: z.string(), name: z.string(), table: tableResponse.optional(), sequence: z.number().int(), is_hidden: z.boolean().optional(), created_at: z.string(), updated_at: z.string(), fields: z.array(fieldResponse).optional()}); export const pageResponse = z.object({id: z.string(), slug: z.string(), name: z.string(), sections: z.array(sectionResponse), sequence: z.number().int(), created_at: z.string(), updated_at: z.string(), is_hidden: z.boolean()}); export const documentSubTypeResponse = z.object({id: z.string(), name: z.string(), description: z.string().optional(), type: documentType, slug: z.string(), page: z.union([z.string(), pageResponse]), is_contract: z.boolean(), updated_at: z.string()}); export const documentSubTypeSearchResponse = z.object({data: z.array(documentSubTypeResponse), total: z.number().int(), page: z.number().int(), page_size: z.number().int()}); 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 }; /** * Fetch All Document Type * * GET /v2/document-type */ export const getApiV2DocumentType = async (args: GetApiV2DocumentTypeArgs, options: GetApiV2DocumentTypeOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/document-type${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(documentSubTypeSearchResponse, await res.json()) };