import {z} from 'zod' export type CloudPlatformCredentialsConnectionProperties = {credentialSecretName?: string | undefined}; export type ComponentBasicConnectionProperties = {secretKey?: string | undefined, secretName: string}; export type AwsVaultLocation = {type?: ('AWS_SECRET_MANAGER' | 'AZURE_SECRET_MANAGER' | 'GCP_SECRET_MANAGER' | 'MATILLION_CUSTOMER_VAULT' | 'DEFAULT_SECRET_VAULT') | undefined, region?: string | undefined}; export type AzureVaultLocation = {type?: ('AWS_SECRET_MANAGER' | 'AZURE_SECRET_MANAGER' | 'GCP_SECRET_MANAGER' | 'MATILLION_CUSTOMER_VAULT' | 'DEFAULT_SECRET_VAULT') | undefined, vaultName?: string | undefined}; export type DefaultVaultLocation = {type?: ('AWS_SECRET_MANAGER' | 'AZURE_SECRET_MANAGER' | 'GCP_SECRET_MANAGER' | 'MATILLION_CUSTOMER_VAULT' | 'DEFAULT_SECRET_VAULT') | undefined}; export type GcpVaultLocation = {type?: ('AWS_SECRET_MANAGER' | 'AZURE_SECRET_MANAGER' | 'GCP_SECRET_MANAGER' | 'MATILLION_CUSTOMER_VAULT' | 'DEFAULT_SECRET_VAULT') | undefined, gcpProjectId?: string | undefined}; export type MatillionHostedVaultLocation = {type?: ('AWS_SECRET_MANAGER' | 'AZURE_SECRET_MANAGER' | 'GCP_SECRET_MANAGER' | 'MATILLION_CUSTOMER_VAULT' | 'DEFAULT_SECRET_VAULT') | undefined}; export type ConnectionDetails = {connectionProperties?: (CloudPlatformCredentialsConnectionProperties | ComponentBasicConnectionProperties) | undefined, description?: string | undefined, secretsLocation?: (AwsVaultLocation | AzureVaultLocation | DefaultVaultLocation | GcpVaultLocation | MatillionHostedVaultLocation) | undefined}; export type CreateConnectionRequest = {authProvider?: string | undefined, authType: 'GENERIC_SECRET' | 'CLOUD_PROVIDER_SECRET' | 'OAUTH_USER_TOKENS' | 'OAUTH_AUTHORIZATION_CODE' | 'OAUTH_CLIENT_CRED' | 'COMPOSITE_TOKEN' | 'OAUTH_1_0_X_ADS', connectionDetails?: ConnectionDetails | undefined, name: string, platformConnectionType: 'COMPONENT_BASIC' | 'CLOUD_PLATFORM_CREDENTIALS'}; export type CreateApiV1ProjectsProjectIdConnectionsArgs = {projectId: string, body: CreateConnectionRequest}; export type CreateApiV1ProjectsProjectIdConnectionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cloudPlatformCredentialsConnectionProperties = z.object({credentialSecretName: z.string().optional()}); export const componentBasicConnectionProperties = z.object({secretKey: z.string().optional(), secretName: z.string().min(1)}); export const awsVaultLocation = z.object({type: z.enum(["AWS_SECRET_MANAGER", "AZURE_SECRET_MANAGER", "GCP_SECRET_MANAGER", "MATILLION_CUSTOMER_VAULT", "DEFAULT_SECRET_VAULT"]).optional(), region: z.string().optional()}); export const azureVaultLocation = z.object({type: z.enum(["AWS_SECRET_MANAGER", "AZURE_SECRET_MANAGER", "GCP_SECRET_MANAGER", "MATILLION_CUSTOMER_VAULT", "DEFAULT_SECRET_VAULT"]).optional(), vaultName: z.string().optional()}); export const defaultVaultLocation = z.object({type: z.enum(["AWS_SECRET_MANAGER", "AZURE_SECRET_MANAGER", "GCP_SECRET_MANAGER", "MATILLION_CUSTOMER_VAULT", "DEFAULT_SECRET_VAULT"]).optional()}); export const gcpVaultLocation = z.object({type: z.enum(["AWS_SECRET_MANAGER", "AZURE_SECRET_MANAGER", "GCP_SECRET_MANAGER", "MATILLION_CUSTOMER_VAULT", "DEFAULT_SECRET_VAULT"]).optional(), gcpProjectId: z.string().optional()}); export const matillionHostedVaultLocation = z.object({type: z.enum(["AWS_SECRET_MANAGER", "AZURE_SECRET_MANAGER", "GCP_SECRET_MANAGER", "MATILLION_CUSTOMER_VAULT", "DEFAULT_SECRET_VAULT"]).optional()}); export const connectionDetailsResponse = z.object({connectionProperties: z.union([cloudPlatformCredentialsConnectionProperties, componentBasicConnectionProperties]).optional(), description: z.string().optional(), secretsLocation: z.union([awsVaultLocation, azureVaultLocation, defaultVaultLocation, gcpVaultLocation, matillionHostedVaultLocation]).optional()}); export const projectConnectionResponse = z.object({authProvider: z.string().optional(), authType: z.enum(["GENERIC_SECRET", "CLOUD_PROVIDER_SECRET", "OAUTH_USER_TOKENS", "OAUTH_AUTHORIZATION_CODE", "OAUTH_CLIENT_CRED", "COMPOSITE_TOKEN", "OAUTH_1_0_X_ADS"]).optional(), connectionDetails: connectionDetailsResponse.optional(), createdBy: z.string().optional(), createdDate: z.string().optional(), name: z.string().optional(), platformConnectionType: z.enum(["COMPONENT_BASIC", "CLOUD_PLATFORM_CREDENTIALS"]).optional(), projectId: z.string().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 a new project connection * * POST /v1/projects/{projectId}/connections */ export const createApiV1ProjectsProjectIdConnections = async (args: CreateApiV1ProjectsProjectIdConnectionsArgs, options?: CreateApiV1ProjectsProjectIdConnectionsOptions) => { const {baseUrl = "https://eu1.api.matillion.com/dpc", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/connections${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(projectConnectionResponse, await res.json()) };