import {z} from 'zod' export type ABSMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'abs', bucket_name: string, bucket_access_id?: string | undefined}; export type AthenaMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'athena', bucket_name: string}; export type BigQueryMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'bigquery', host: string, bucket_name: string}; export type ClickhouseMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'clickhouse', host: string, bucket_vendor: 's3' | 'gcs', bucket_name: string}; export type DatabricksMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'databricks', host: string, metastore: 'unity_catalog' | 'hive', bucket_name?: string | undefined, bucket_vendor?: ('' | 's3') | undefined}; export type DeltaLakeMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'delta_lake', bucket_name: string, bucket_vendor: 's3' | 's3_compatible' | 'gcs' | 'abs', host?: string | undefined, bucket_access_id?: string | undefined}; export type GCSMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'gcs', bucket_name: string}; export type GoogleSheetsMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'google_sheets', host: string}; export type RedshiftMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'redshift' | 'redshift_serverless', host: string, bucket_name: string}; export type S3MagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 's3', bucket_name: string}; export type S3CompatibleMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 's3_compatible', host: string, bucket_name: string}; export type GenericMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'aurora_postgres' | 'aurora_mysql' | 'mongodb' | 'mysql' | 'oracle' | 'postgres' | 'sql_server' | 'singlestore' | 'sftp', host: string}; export type IcebergMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'iceberg', catalog: 'catalog_glue' | 'catalog_s3_tables' | 'catalog_iceberg_rest', bucket_name: string, host?: string | undefined, bucket_vendor?: ('s3' | 's3_compatible' | 'gcs' | 'abs') | undefined, bucket_host?: string | undefined, bucket_access_id?: string | undefined, table_bucket_arn?: string | undefined}; export type MotherDuckMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'motherduck'}; export type SnowflakeMagicLinkOptions = {name: string, set_destination_enabled?: boolean | undefined, redirect_url?: string | undefined, recipient_id?: string | undefined, vendor: 'snowflake', host: string}; export type MagicLinkOptions = ABSMagicLinkOptions | AthenaMagicLinkOptions | BigQueryMagicLinkOptions | ClickhouseMagicLinkOptions | DatabricksMagicLinkOptions | DeltaLakeMagicLinkOptions | GCSMagicLinkOptions | GoogleSheetsMagicLinkOptions | RedshiftMagicLinkOptions | RedshiftMagicLinkOptions | S3MagicLinkOptions | S3CompatibleMagicLinkOptions | GenericMagicLinkOptions | IcebergMagicLinkOptions | GenericMagicLinkOptions | MotherDuckMagicLinkOptions | GenericMagicLinkOptions | GenericMagicLinkOptions | GenericMagicLinkOptions | GenericMagicLinkOptions | GenericMagicLinkOptions | GenericMagicLinkOptions | GenericMagicLinkOptions | SnowflakeMagicLinkOptions; export type CreateMagicLinkRequest = {magic_link: MagicLinkOptions}; export type CreateApiMagicLinksArgs = {body: CreateMagicLinkRequest}; export type CreateApiMagicLinksOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const magicLinkResource = z.object({id: z.string(), name: z.string(), products: z.array(z.string()), id_in_provider_system: z.string(), recipient_id: z.string().optional(), host: z.string(), link: z.string(), has_been_used: z.boolean(), created_at: z.string(), bucket_name: z.string(), vendor: z.string(), available_models: z.array(z.string()), bucket_access_id: z.string().optional(), bucket_vendor: z.string().optional(), ssh_public_key: z.string().optional(), redirect_url: z.string().optional(), set_destination_enabled: z.boolean(), metastore: z.string().optional(), catalog: z.string().optional(), bucket_host: z.string().optional(), table_bucket_arn: z.string().optional()}); export const getMagicLinkResponse = z.object({magic_link: magicLinkResource}); export const createApiMagicLinksResponse = z.object({status: z.literal("success"), data: getMagicLinkResponse, 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 }; /** * Create magic link * * POST /magic-links */ export const createApiMagicLinks = async (args: CreateApiMagicLinksArgs, options?: CreateApiMagicLinksOptions) => { 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(/\/+$/, '')}/magic-links${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(createApiMagicLinksResponse, await res.json()) };