import {z} from 'zod' export type GetApiV2CatalogCatalogSlugArgs = {catalog_slug: string, authorization: string}; export type GetApiV2CatalogCatalogSlugOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; 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 exhausetiveEntityTypeExternalResponse = z.object({slug: z.string(), name: z.string(), is_hidden: z.boolean(), is_catalog: z.boolean(), pages: z.array(pageResponse)}); 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 Catalog Detail * * GET /v2/catalog/{catalog_slug} */ export const getApiV2CatalogCatalogSlug = async (args: GetApiV2CatalogCatalogSlugArgs, options: GetApiV2CatalogCatalogSlugOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/catalog/${encodeURIComponent(String(args.catalog_slug))}${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(exhausetiveEntityTypeExternalResponse, await res.json()) };