import {z} from 'zod' export type GetApiV2EntityIngestionConfigsIngestionConfigIdExternalSchemaArgs = {ingestion_config_id: string}; export type GetApiV2EntityIngestionConfigsIngestionConfigIdExternalSchemaOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const googleProtobufValue = z.union([z.number(), z.string(), z.boolean(), z.array(z.unknown()), z.record(z.string(), z.unknown())]).nullable(); export const googleProtobufStruct = z.record(z.string(), googleProtobufValue); export const googleProtobufTimestamp = z.string(); export const svstoreInferredFieldMapping = z.object({externalFieldPath: z.string().optional(), internalFieldId: z.string().optional()}); export const svstoreEntityMappingInference = z.object({inferredEntityTypeId: z.string().nullable().optional(), fieldMappings: z.array(svstoreInferredFieldMapping).optional(), reasoning: z.string().optional()}); export const svstoreExternalEntitySchema = z.object({id: z.string().optional(), ingestionConfigId: z.string().optional(), jsonSchema: googleProtobufStruct.optional(), sampleSize: z.number().int().optional(), totalAssetsSampled: z.number().int().optional(), extractedAt: googleProtobufTimestamp.optional(), inference: svstoreEntityMappingInference.optional(), entityCount: z.number().int().optional(), sourceAppInstanceId: z.string().optional(), svflowUserIngestionConfigId: z.string().nullable().optional()}); export const svstorePublicapiGetExternalEntitySchemaResponse = z.object({data: svstoreExternalEntitySchema.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 External Entity Schema * * GET /v2/entity-ingestion-configs/{ingestion_config_id}/external-schema */ export const getApiV2EntityIngestionConfigsIngestionConfigIdExternalSchema = async (args: GetApiV2EntityIngestionConfigsIngestionConfigIdExternalSchemaArgs, options?: GetApiV2EntityIngestionConfigsIngestionConfigIdExternalSchemaOptions) => { const {baseUrl = "https://public.api.serval.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/entity-ingestion-configs/${encodeURIComponent(String(args.ingestion_config_id))}/external-schema${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(svstorePublicapiGetExternalEntitySchemaResponse, await res.json()) };