import {z} from 'zod' export type PropertiesIdentifier = string; export type DataPointPropertiesIdentifier = 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 CollectionField = {label?: Label | undefined, 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?: Mapping | undefined}; export type PatchApiV3CollectionsIdentifierFieldsDatapointIdentifierArgs = {identifier: PropertiesIdentifier, datapoint_identifier: DataPointPropertiesIdentifier, body: CollectionField}; export type PatchApiV3CollectionsIdentifierFieldsDatapointIdentifierOptions = {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 dataSource = z.string().nullable(); export const mapping = z.string().nullable(); export const collectionField = z.object({label: label.optional(), fieldType: annotationContentType.optional(), mandatory: mandatory.optional(), showDropdown: showDropdown.optional(), displayEnumValue: displayEnumValue.optional(), autoValidationThreshold: propertiesAutoValidationThreshold.optional(), enableAutoValidationThreshold: propertiesEnableAutoValidationThreshold.optional(), dataSource: dataSource.optional(), mapping: mapping.optional(), displayRawText: mapping.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 }; /** * Update data field for a collection assosciated with a data point. * * PATCH /v3/collections/{identifier}/fields/{datapoint_identifier} */ export const patchApiV3CollectionsIdentifierFieldsDatapointIdentifier = async (args: PatchApiV3CollectionsIdentifierFieldsDatapointIdentifierArgs, options?: PatchApiV3CollectionsIdentifierFieldsDatapointIdentifierOptions) => { 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))}/fields/${encodeURIComponent(String(args.datapoint_identifier))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(collectionField, await res.json()) };