import {z} from 'zod' export type PropertiesIdentifier = string; export type DataPointPropertiesIdentifier = string; export type GetApiV3CollectionsIdentifierFieldsDatapointIdentifierArgs = {identifier: PropertiesIdentifier, datapoint_identifier: DataPointPropertiesIdentifier}; export type GetApiV3CollectionsIdentifierFieldsDatapointIdentifierOptions = {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 }; /** * Get data field for a collection assosciated with a data point. * * GET /v3/collections/{identifier}/fields/{datapoint_identifier} */ export const getApiV3CollectionsIdentifierFieldsDatapointIdentifier = async (args: GetApiV3CollectionsIdentifierFieldsDatapointIdentifierArgs, options?: GetApiV3CollectionsIdentifierFieldsDatapointIdentifierOptions) => { const {baseUrl = "https://api.affinda.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) 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()) };