import {z} from 'zod' export type UpdateModelColumnDocumented = {name_in_source?: string | undefined, name_in_destination?: string | undefined, data_type?: string | undefined, is_primary_key?: boolean | undefined, is_last_modified?: boolean | undefined}; export type UpdateModelConfigDocumented = {description?: string | undefined, columns?: Array | undefined, source_id?: string | undefined, source_name?: string | undefined, source_table?: string | undefined, source_query?: string | undefined, organization_column?: string | undefined, is_append_only?: boolean | undefined, destination_table_name?: string | undefined, skip_transfer_source_validation?: boolean | undefined, model_validation_id_in_provider_system?: string | undefined, model_validation_start_time_epoch?: string | undefined, model_validation_end_time_epoch?: string | undefined, model_validation_tags?: {[key: string]: string} | undefined}; export type UpdateModelConfigRequestDocumented = {model?: UpdateModelConfigDocumented | undefined, skip_model_validation?: boolean | undefined}; export type PatchApiModelsModelIdArgs = {model_id: string, body: UpdateModelConfigRequestDocumented}; export type PatchApiModelsModelIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const modelColumnDocumented = z.object({name_in_source: z.string(), name_in_destination: z.string(), data_type: z.string(), is_primary_key: z.boolean(), is_last_modified: z.boolean()}); export const modelConfigDocumented = z.object({model_name: z.string(), description: z.string().optional(), columns: z.array(modelColumnDocumented), source_id: z.string(), source_table: z.string().optional(), source_query: z.string().optional(), destination_table_name: z.string().optional(), organization_column: z.string().optional(), skip_transfer_source_validation: z.boolean().optional(), is_append_only: z.boolean().optional(), model_validation_id_in_provider_system: z.string().optional(), model_validation_start_time_epoch: z.string().optional(), model_validation_end_time_epoch: z.string().optional(), model_validation_tags: z.record(z.string(), z.string()).optional()}); export const modelConfigResponseDocumented = z.object({model: modelConfigDocumented}); export const patchApiModelsModelIdResponse = z.object({status: z.literal("success"), data: modelConfigResponseDocumented, message: z.string()}); 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 model * * PATCH /models/{model_id} */ export const patchApiModelsModelId = async (args: PatchApiModelsModelIdArgs, options?: PatchApiModelsModelIdOptions) => { const {baseUrl = "https://api.prequel.co", 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("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/models/${encodeURIComponent(String(args.model_id))}${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(patchApiModelsModelIdResponse, await res.json()) };