import {z} from 'zod' export type PropertiesIdentifier = string; export type CategoryLabel = string; export type Label = string; export type AnnotationContentType = 'text' | 'integer' | 'float' | 'decimal' | 'date' | 'datetime' | 'daterange' | 'boolean' | 'enum' | 'location' | 'phonenumber' | 'json' | 'table' | 'expectedremuneration' | 'jobtitle' | 'language' | 'skill' | 'yearsexperience' | 'group' | 'table_deprecated' | 'url' | 'image' | 'docclf'; export type Mandatory = boolean; export type ShowDropdown = boolean; export type DisplayEnumValue = boolean; export type PropertiesAutoValidationThreshold = number | null; export type PropertiesEnableAutoValidationThreshold = boolean | null; export type DataSource = string | null; export type Mapping = string | null; export type DisplayRawText = boolean; export type DataFieldCreateField = {label: Label, fieldType?: AnnotationContentType | undefined, mandatory?: Mandatory | undefined, showDropdown?: ShowDropdown | undefined, displayEnumValue?: DisplayEnumValue | undefined, autoValidationThreshold?: PropertiesAutoValidationThreshold | undefined, enableAutoValidationThreshold?: PropertiesEnableAutoValidationThreshold | undefined, dataSource?: DataSource | undefined, mapping?: Mapping | undefined, displayRawText?: DisplayRawText | undefined}; export type DataPointPropertiesName = string; export type Slug = string; export type Description = string | null; export type Multiple = boolean; export type NoRect = boolean; export type PropertiesParent = string | null; export type ManualEntry = boolean; export type MappingDataSource = string | null; export type DataFieldCreateDataPoint = {name: DataPointPropertiesName, slug: Slug, description?: Description | undefined, type: AnnotationContentType, multiple?: Multiple | undefined, noRect?: NoRect | undefined, parent?: PropertiesParent | undefined, manualEntry?: ManualEntry | undefined, mappingDataSource?: MappingDataSource | undefined}; export type DataFieldCreate = {categoryLabel?: CategoryLabel | undefined, field: DataFieldCreateField, dataPoint: DataFieldCreateDataPoint}; export type CreateApiV3CollectionsIdentifierCreateDataFieldArgs = {identifier: PropertiesIdentifier, body: DataFieldCreate}; export type CreateApiV3CollectionsIdentifierCreateDataFieldOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const label = z.string(); export const annotationContentType = z.enum(["text", "integer", "float", "decimal", "date", "datetime", "daterange", "boolean", "enum", "location", "phonenumber", "json", "table", "expectedremuneration", "jobtitle", "language", "skill", "yearsexperience", "group", "table_deprecated", "url", "image", "docclf"]); export const mandatory = z.boolean(); export const showDropdown = z.boolean(); export const displayEnumValue = z.boolean(); export const propertiesAutoValidationThreshold = z.number().gte(0).lte(1).nullable(); export const propertiesEnableAutoValidationThreshold = z.boolean().nullable(); export const field: z.ZodType = z.object({label: z.string(), dataPoint: z.string(), fieldType: annotationContentType, dataSource: z.string().nullable().optional(), mapping: z.string().nullable().optional(), mandatory: z.boolean().optional(), autoValidationThreshold: z.number().gte(0).lte(1).nullable().optional(), enableAutoValidationThreshold: z.boolean().nullable().optional(), showDropdown: z.boolean().optional(), displayEnumValue: z.boolean().optional(), hideEnumDetail: z.boolean().optional(), dropNull: z.boolean().optional(), enabledChildFields: z.array(z.lazy(() => field)).optional(), disabledChildFields: z.array(z.lazy(() => field)).optional(), slug: z.string().nullable().optional(), displayRawText: z.boolean().optional(), fields: z.array(z.object({})).nullable().optional()}).and(z.record(z.string(), z.unknown())); export const enabledChildFields = z.array(field); export const disabledChildFields = z.array(field); export const dataSource = z.string().nullable(); export const mapping = z.string().nullable(); export const displayRawText = z.boolean(); export const dataPointPropertiesIdentifier = z.string(); export const dataPointPropertiesName = z.string(); export const slug = z.string(); export const description = z.string().nullable(); export const multiple = z.boolean(); export const noRect = z.boolean(); export const propertiesParent = z.string().nullable(); export const organizationRole = z.enum(["admin", "member"]); export const paletteColorOptions = z.object({main: z.string(), light: z.string().optional(), dark: z.string().optional(), contrastText: z.string().optional()}); export const themeConfig = z.object({palette: z.object({mode: z.enum(["light", "dark"]).optional(), background: z.union([z.string(), z.object({default: z.string().optional(), paper: z.string().optional()})]).optional(), text: z.object({primary: z.string().optional(), secondary: z.string().optional(), disabled: z.string().optional()}).optional(), divider: z.string().optional(), primary: paletteColorOptions.optional(), secondary: paletteColorOptions.optional(), success: paletteColorOptions.optional(), annotation: paletteColorOptions.optional(), error: paletteColorOptions.optional(), info: paletteColorOptions.optional(), warning: paletteColorOptions.optional()}).optional(), typography: z.object({fontFamily: z.string().optional(), fontSize: z.union([z.string(), z.number()]).optional(), fontWeightRegular: z.string().optional(), fontWeightMedium: z.string().optional(), fontWeightBold: z.string().optional()}).optional(), borderRadius: z.number().optional(), fontUrl: z.string().optional()}); export const organization = z.object({identifier: z.string(), name: z.string(), userRole: organizationRole.nullable(), avatar: z.string().nullable(), resthookSignatureKey: z.string().nullable(), isTrial: z.boolean(), validationToolConfig: z.object({theme: themeConfig.optional(), hideActions: z.boolean().optional(), hideCollection: z.boolean().optional(), hideEditPages: z.boolean().optional(), hideExport: z.boolean().optional(), hideFilename: z.boolean().optional(), hideShowRawValues: z.boolean().optional(), hideReject: z.boolean().optional(), hideReparse: z.boolean().optional(), hideRunOcr: z.boolean().optional(), hideTags: z.boolean().optional(), hideWarnings: z.boolean().optional(), restrictDocumentSplitting: z.boolean().optional(), disableCurrencyFormatting: z.boolean().optional(), disableEditDocumentMetadata: z.boolean().optional(), disableManualAnnotationEditing: z.boolean().optional(), hideDocumentStatus: z.boolean().optional()}).nullable(), showCustomFieldCreation: z.boolean(), allowDataSourceRecordCreation: z.boolean()}); export const extractorPropertiesIdentifier = z.string(); export const mappingDataSource = z.object({identifier: z.string(), name: z.string().nullable().optional(), keyProperty: z.string(), displayProperty: z.string(), organization: z.string().nullable(), workspace: z.string().nullable(), schema: z.object({}).optional()}); export const dataPoint: z.ZodType = z.object({identifier: z.string(), name: z.string(), slug: z.string(), description: z.string().nullable().optional(), annotationContentType: annotationContentType, organization: organization, extractor: extractorPropertiesIdentifier, multiple: z.boolean().optional(), noRect: z.boolean().optional(), parent: z.string().nullable().optional(), children: z.array(z.lazy(() => dataPoint)).optional(), availableDataSources: z.array(mappingDataSource).optional(), mappingDataSource: z.string().nullable().optional(), manualEntry: z.boolean().optional()}); export const children = z.array(dataPoint); export const manualEntry = z.boolean(); export const availableDataSources = z.array(mappingDataSource); export const dataField = z.object({categoryLabel: z.string().optional(), field: z.object({label: label, fieldType: annotationContentType.optional(), mandatory: mandatory, showDropdown: showDropdown.optional(), displayEnumValue: displayEnumValue, autoValidationThreshold: propertiesAutoValidationThreshold, enableAutoValidationThreshold: propertiesEnableAutoValidationThreshold.optional(), enabledChildFields: enabledChildFields, disabledChildFields: disabledChildFields, dataSource: dataSource.optional(), mapping: mapping.optional(), displayRawText: displayRawText.optional()}), dataPoint: z.object({identifier: dataPointPropertiesIdentifier, name: dataPointPropertiesName, slug: slug, description: description, type: annotationContentType, multiple: multiple, noRect: noRect, parent: propertiesParent, children: children, manualEntry: manualEntry.optional(), availableDataSources: availableDataSources.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 }; /** * Create data field for a collection along with a new data point. * * POST /v3/collections/{identifier}/create_data_field */ export const createApiV3CollectionsIdentifierCreateDataField = async (args: CreateApiV3CollectionsIdentifierCreateDataFieldArgs, options?: CreateApiV3CollectionsIdentifierCreateDataFieldOptions) => { const {baseUrl = "https://api.affinda.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/collections/${encodeURIComponent(String(args.identifier))}/create_data_field${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dataField, await res.json()) };