import {z} from 'zod' export type WarehouseTypesREDSHIFT = 'redshift'; export type PickCreateRedshiftCredentialsTypeOrUserOrPassword = {type: WarehouseTypesREDSHIFT, user: string, password?: string | undefined}; export type RedshiftAuthenticationType = 'password' | 'iam' | 'iam_browser'; export type PickCreateRedshiftCredentialsTypeOrUserOrAuthenticationTypeOrAccessKeyIdOrSecretAccessKeyOrSessionTokenOrAssumeRoleArnOrAssumeRoleExternalId = {type: WarehouseTypesREDSHIFT, user: string, accessKeyId?: string | undefined, secretAccessKey?: string | undefined, sessionToken?: string | undefined, authenticationType?: RedshiftAuthenticationType | undefined, assumeRoleArn?: string | undefined, assumeRoleExternalId?: string | undefined}; export type WarehouseTypesPOSTGRES = 'postgres'; export type PickCreatePostgresCredentialsTypeOrUserOrPassword = {type: WarehouseTypesPOSTGRES, user: string, password: string}; export type WarehouseTypesSNOWFLAKE = 'snowflake'; export type SnowflakeAuthenticationType = 'password' | 'private_key' | 'sso' | 'external_browser' | 'oauth_authorization_code' | 'none'; export type PickCreateSnowflakeCredentialsTypeOrUserOrPasswordOrAuthenticationTypeOrRefreshToken = {type: WarehouseTypesSNOWFLAKE, user: string, password?: string | undefined, refreshToken?: string | undefined, authenticationType?: SnowflakeAuthenticationType | undefined}; export type WarehouseTypesTRINO = 'trino'; export type PickCreateTrinoCredentialsTypeOrUserOrPassword = {type: WarehouseTypesTRINO, user: string, password: string}; export type WarehouseTypesCLICKHOUSE = 'clickhouse'; export type PickCreateClickhouseCredentialsTypeOrUserOrPassword = {type: WarehouseTypesCLICKHOUSE, user: string, password: string}; export type WarehouseTypesBIGQUERY = 'bigquery'; export type BigqueryAuthenticationType = 'sso' | 'private_key' | 'adc'; export type PickCreateBigqueryCredentialsTypeOrKeyfileContentsOrAuthenticationType = {type: WarehouseTypesBIGQUERY, keyfileContents: {[key: string]: string}, authenticationType?: BigqueryAuthenticationType | undefined}; export type WarehouseTypesDATABRICKS = 'databricks'; export type DatabricksAuthenticationType = 'personal_access_token' | 'oauth_m2m' | 'oauth_u2m'; export type WarehouseTypesATHENA = 'athena'; export type PickCreateAthenaCredentialsTypeOrAccessKeyIdOrSecretAccessKey = {type: WarehouseTypesATHENA, accessKeyId?: string | undefined, secretAccessKey?: string | undefined}; export type WarehouseTypesDUCKDB = 'duckdb'; export type PickCreateDuckdbMotherduckCredentialsTypeOrToken = {type: WarehouseTypesDUCKDB, token: string}; export type UpsertUserWarehouseCredentials = {credentials: PickCreateRedshiftCredentialsTypeOrUserOrPassword | PickCreateRedshiftCredentialsTypeOrUserOrAuthenticationTypeOrAccessKeyIdOrSecretAccessKeyOrSessionTokenOrAssumeRoleArnOrAssumeRoleExternalId | PickCreatePostgresCredentialsTypeOrUserOrPassword | PickCreateSnowflakeCredentialsTypeOrUserOrPasswordOrAuthenticationTypeOrRefreshToken | PickCreateTrinoCredentialsTypeOrUserOrPassword | PickCreateClickhouseCredentialsTypeOrUserOrPassword | PickCreateBigqueryCredentialsTypeOrKeyfileContentsOrAuthenticationType | {type: WarehouseTypesDATABRICKS, personalAccessToken?: string | undefined, refreshToken?: string | undefined, authenticationType?: DatabricksAuthenticationType | undefined, oauthClientId?: string | undefined, database?: string | undefined, serverHostName?: string | undefined, httpPath?: string | undefined} | PickCreateAthenaCredentialsTypeOrAccessKeyIdOrSecretAccessKey | PickCreateDuckdbMotherduckCredentialsTypeOrToken, name: string}; export type CreateApiApiV1UserWarehouseCredentialsArgs = {body: UpsertUserWarehouseCredentials}; export type CreateApiApiV1UserWarehouseCredentialsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const projectType = z.enum(["DEFAULT", "PREVIEW"]); export const userWarehouseCredentialsProject = z.object({type: projectType, name: z.string(), projectUuid: z.string()}); export const warehouseTypesREDSHIFT = z.literal("redshift"); export const redshiftAuthenticationType = z.enum(["password", "iam", "iam_browser"]); export const pickCreateRedshiftCredentialsTypeOrUserOrAuthenticationTypeOrAssumeRoleArn = z.object({type: warehouseTypesREDSHIFT, user: z.string(), authenticationType: redshiftAuthenticationType.optional(), assumeRoleArn: z.string().optional()}); export const warehouseTypesPOSTGRES = z.literal("postgres"); export const warehouseTypesSNOWFLAKE = z.literal("snowflake"); export const warehouseTypesTRINO = z.literal("trino"); export const warehouseTypesCLICKHOUSE = z.literal("clickhouse"); export const pickCreatePostgresCredentialsOrCreateSnowflakeCredentialsOrCreateTrinoCredentialsOrCreateClickhouseCredentialsTypeOrUser = z.object({type: z.union([warehouseTypesPOSTGRES, warehouseTypesSNOWFLAKE, warehouseTypesTRINO, warehouseTypesCLICKHOUSE]), user: z.string()}); export const warehouseTypesBIGQUERY = z.literal("bigquery"); export const pickCreateBigqueryCredentialsType = z.object({type: warehouseTypesBIGQUERY}); export const warehouseTypesDATABRICKS = z.literal("databricks"); export const pickCreateDatabricksCredentialsType = z.object({type: warehouseTypesDATABRICKS}); export const warehouseTypesATHENA = z.literal("athena"); export const pickCreateAthenaCredentialsType = z.object({type: warehouseTypesATHENA}); export const warehouseTypesDUCKDB = z.literal("duckdb"); export const pickCreateDuckdbCredentialsType = z.object({type: warehouseTypesDUCKDB}); export const userWarehouseCredentials = z.object({project: userWarehouseCredentialsProject.nullable(), credentials: z.union([pickCreateRedshiftCredentialsTypeOrUserOrAuthenticationTypeOrAssumeRoleArn, pickCreatePostgresCredentialsOrCreateSnowflakeCredentialsOrCreateTrinoCredentialsOrCreateClickhouseCredentialsTypeOrUser, pickCreateBigqueryCredentialsType, pickCreateDatabricksCredentialsType, pickCreateAthenaCredentialsType, pickCreateDuckdbCredentialsType]), updatedAt: z.string(), createdAt: z.string(), name: z.string(), userUuid: z.string(), uuid: z.string()}); export const createApiApiV1UserWarehouseCredentialsResponse = z.object({results: userWarehouseCredentials, status: z.literal("ok")}); 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 warehouse credentials * * POST /api/v1/user/warehouseCredentials */ export const createApiApiV1UserWarehouseCredentials = async (args: CreateApiApiV1UserWarehouseCredentialsArgs, options: CreateApiApiV1UserWarehouseCredentialsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/user/warehouseCredentials${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(createApiApiV1UserWarehouseCredentialsResponse, await res.json()) };