import {z} from 'zod' export type CreateApiMigrationsMigrationIdSourceConnectionArgs = {body: {source_key: string, profile_id?: (string | null) | undefined, config?: {[key: string]: unknown} | undefined, selected_streams?: (Array | null) | undefined, stream_state?: {[key: string]: unknown} | undefined, credentials?: {[key: string]: unknown} | undefined}}; export type CreateApiMigrationsMigrationIdSourceConnectionOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const sourceConnection = z.object({id: z.string().optional(), profile_id: z.string().nullable().optional(), source_key: z.string().optional(), name: z.string().optional(), category: z.string().nullable().optional(), config: z.record(z.string(), z.unknown()).optional(), selected_streams: z.array(z.string()).nullable().optional(), stream_state: z.record(z.string(), z.unknown()).optional(), creds_configured: z.boolean().optional(), bound_source_extractor_id: z.string().nullable().optional(), last_snapshot: z.object({}).nullable().optional(), last_sync_at: z.string().nullable().optional()}); export const createApiMigrationsMigrationIdSourceConnectionResponse = z.object({source_connection: sourceConnection.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 or update the source connection * * POST /migrations/{migration_id}/source-connection */ export const createApiMigrationsMigrationIdSourceConnection = async (args: CreateApiMigrationsMigrationIdSourceConnectionArgs, options?: CreateApiMigrationsMigrationIdSourceConnectionOptions) => { const {baseUrl = "https://app.vern.so/api/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/migrations/${encodeURIComponent(String(args.migration_id))}/source-connection${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(createApiMigrationsMigrationIdSourceConnectionResponse, await res.json()) };