import {z} from 'zod' export type AWSIAMRole = {aws_iam_role_arn: string}; export type S3DatalakeOptionsAWSIAMRole = {bucket_name: string, prefix: string, bucket_region: string, auth_method: 'aws_iam_role', aws_iam_role: AWSIAMRole}; export type AWSAccessKeys = {access_id: string, secret_key: string}; export type S3DatalakeOptionsAWSAccessKeys = {bucket_name: string, prefix: string, bucket_region: string, auth_method: 'aws_access_keys', aws_access_keys: AWSAccessKeys}; export type S3DatalakeOptions = S3DatalakeOptionsAWSIAMRole | S3DatalakeOptionsAWSAccessKeys; export type S3CompatibleDatalakeOptions = {prefix: string, bucket_name: string, bucket_region: string, bucket_host: string, bucket_port: number, aws_access_keys: AWSAccessKeys}; export type GCPDatalakeServiceAccountRole = {gcp_service_account_email: string}; export type GCSDatalakeOptionsGCPServiceAccountRole = {prefix: string, bucket_name: string, bucket_region: string, auth_method: 'gcp_service_account_role', gcp_service_account_role: GCPDatalakeServiceAccountRole}; export type GCPDatalakeServiceAccountKey = {service_account_email: string, service_account_key: unknown}; export type GCSDatalakeOptionsGCPServiceAccountKey = {prefix: string, bucket_name: string, bucket_region: string, auth_method: 'gcp_service_account_key', gcp_service_account_key: GCPDatalakeServiceAccountKey}; export type CredentialSource = {environment_id: string, region_url: string, url: string, regional_cred_verification_url: string}; export type GcpExternalAccountMetadata = {type: string, audience: string, subject_token_type: string, service_account_impersonation_url: string, token_url: string, credential_source: CredentialSource}; export type FederatedGCPDatalakeServiceAccountRole = {service_account_email: string, workload_identity_federation_metadata?: GcpExternalAccountMetadata | undefined}; export type GCSDatalakeOptionsFederatedGCPServiceAccountRole = {prefix: string, bucket_name: string, bucket_region: string, auth_method: 'federated_gcp_service_account_role', federated_gcp_service_account_role: FederatedGCPDatalakeServiceAccountRole}; export type GCSDatalakeOptions = GCSDatalakeOptionsGCPServiceAccountRole | GCSDatalakeOptionsGCPServiceAccountKey | GCSDatalakeOptionsFederatedGCPServiceAccountRole; export type AzureStorageAccountAccessKey = {storage_account_access_key: string}; export type ABSDatalakeOptions = {prefix: string, container_name: string, storage_account_name: string, auth_method: 'azure_storage_account_access_key', azure_storage_account_access_key?: AzureStorageAccountAccessKey | undefined}; export type DatalakeOptions = {storage: 's3', s3: S3DatalakeOptions, name: string, retention_window_days?: number | undefined} | {storage: 's3_compatible', s3_compatible: S3CompatibleDatalakeOptions, name: string, retention_window_days?: number | undefined} | {storage: 'gcs', gcs: GCSDatalakeOptions, name: string, retention_window_days?: number | undefined} | {storage: 'abs', abs: ABSDatalakeOptions, name: string, retention_window_days?: number | undefined}; export type CreateDatalakeRequest = {datalake: DatalakeOptions}; export type CreateApiImportDatalakesArgs = {body: CreateDatalakeRequest}; export type CreateApiImportDatalakesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const aWSIAMRoleResource = z.object({aws_iam_role: z.string()}); export const s3DatalakeResourceAWSIAMRole = z.object({prefix: z.string(), bucket_name: z.string(), bucket_region: z.string(), auth_method: z.literal("aws_iam_role"), aws_iam_role: aWSIAMRoleResource}); export const aWSAccessKeysResource = z.object({access_key_id: z.string()}); export const s3DatalakeResourceAWSAccessKeys = z.object({prefix: z.string(), bucket_name: z.string(), bucket_region: z.string(), auth_method: z.literal("aws_access_keys"), aws_access_keys: aWSAccessKeysResource}); export const s3DatalakeResource = z.discriminatedUnion("auth_method", [s3DatalakeResourceAWSIAMRole, s3DatalakeResourceAWSAccessKeys]); export const s3CompatibleDatalakeResource = z.object({prefix: z.string(), bucket_name: z.string(), bucket_region: z.string(), bucket_host: z.string(), bucket_port: z.number().int(), aws_access_keys: aWSAccessKeysResource}); export const gCPDatalakeServiceAccountRoleResource = z.object({gcp_service_account_email: z.string()}); export const gCSDatalakeResourceGCPServiceAccountRole = z.object({prefix: z.string(), bucket_name: z.string(), bucket_region: z.string(), auth_method: z.literal("gcp_service_account_role"), gcp_service_account_role: gCPDatalakeServiceAccountRoleResource}); export const gCPDatalakeServiceAccountKeyResource = z.object({service_account_email: z.string()}); export const gCSDatalakeResourceGCPServiceAccountKey = z.object({prefix: z.string(), bucket_name: z.string(), bucket_region: z.string(), auth_method: z.literal("gcp_service_account_key"), gcp_service_account_key: gCPDatalakeServiceAccountKeyResource}); export const federatedGCPDatalakeServiceAccountRoleResource = z.object({gcp_service_account_email: z.string()}); export const gCSDatalakeResourceFederatedGCPServiceAccountRole = z.object({prefix: z.string(), bucket_name: z.string(), bucket_region: z.string(), auth_method: z.literal("federated_gcp_service_account_role"), federated_gcp_service_account_role: federatedGCPDatalakeServiceAccountRoleResource}); export const gCSDatalakeResource = z.discriminatedUnion("auth_method", [gCSDatalakeResourceGCPServiceAccountRole, gCSDatalakeResourceGCPServiceAccountKey, gCSDatalakeResourceFederatedGCPServiceAccountRole]); export const azureStorageAccountAccessKeyResource = z.object({}); export const aBSDatalakeResource = z.object({prefix: z.string(), container_name: z.string(), storage_account_name: z.string(), auth_method: z.string(), azure_storage_account_access_key: azureStorageAccountAccessKeyResource.optional()}); export const datalakeResource = z.union([z.object({storage: z.literal("s3"), s3: s3DatalakeResource, id: z.string(), name: z.string(), retention_window_days: z.number().int().optional(), created_at: z.string(), updated_at: z.string()}), z.object({storage: z.literal("s3_compatible"), s3_compatible: s3CompatibleDatalakeResource, id: z.string(), name: z.string(), retention_window_days: z.number().int().optional(), created_at: z.string(), updated_at: z.string()}), z.object({storage: z.literal("gcs"), gcs: gCSDatalakeResource, id: z.string(), name: z.string(), retention_window_days: z.number().int().optional(), created_at: z.string(), updated_at: z.string()}), z.object({storage: z.literal("abs"), abs: aBSDatalakeResource, id: z.string(), name: z.string(), retention_window_days: z.number().int().optional(), created_at: z.string(), updated_at: z.string()})]); export const getDatalakeResponse = z.object({datalake: datalakeResource}); export const createApiImportDatalakesResponse = z.object({status: z.literal("success"), data: getDatalakeResponse, 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 datalake * * POST /import/datalakes */ export const createApiImportDatalakes = async (args: CreateApiImportDatalakesArgs, options?: CreateApiImportDatalakesOptions) => { 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(/\/+$/, '')}/import/datalakes${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(createApiImportDatalakesResponse, await res.json()) };