import {z} from 'zod' export type UUID = string; export type DbtProjectTypeDBT = 'dbt'; export type DbtProjectEnvironmentVariable = {value: string, key: string}; export type DbtLocalProjectConfig = {type: DbtProjectTypeDBT, target?: string | undefined, environment?: Array | undefined, selector?: string | undefined, profiles_dir?: string | undefined, project_dir?: string | undefined} | {[key: string]: unknown}; export type DbtProjectTypeDBTCLOUDIDE = 'dbt_cloud_ide'; export type DbtCloudIDEProjectConfig = {type: DbtProjectTypeDBTCLOUDIDE, api_key: string, environment_id: string, discovery_api_endpoint?: string | undefined, tags?: Array | undefined, webhook_hmac_secret?: string | undefined} | {[key: string]: unknown}; export type DbtProjectTypeGITHUB = 'github'; export type DbtGithubProjectConfig = {type: DbtProjectTypeGITHUB, target?: string | undefined, environment?: Array | undefined, selector?: string | undefined, authorization_method: 'personal_access_token' | 'installation_id', personal_access_token?: string | undefined, installation_id?: string | undefined, repository: string, branch: string, project_sub_path: string, host_domain?: string | undefined} | {[key: string]: unknown}; export type DbtProjectTypeBITBUCKET = 'bitbucket'; export type DbtBitBucketProjectConfig = {type: DbtProjectTypeBITBUCKET, target?: string | undefined, environment?: Array | undefined, selector?: string | undefined, username: string, personal_access_token: string, repository: string, branch: string, project_sub_path: string, host_domain?: string | undefined} | {[key: string]: unknown}; export type DbtProjectTypeGITLAB = 'gitlab'; export type DbtGitlabProjectConfig = {type: DbtProjectTypeGITLAB, target?: string | undefined, environment?: Array | undefined, selector?: string | undefined, personal_access_token: string, repository: string, branch: string, project_sub_path: string, host_domain?: string | undefined} | {[key: string]: unknown}; export type DbtProjectTypeAZUREDEVOPS = 'azure_devops'; export type DbtAzureDevOpsProjectConfig = {type: DbtProjectTypeAZUREDEVOPS, target?: string | undefined, environment?: Array | undefined, selector?: string | undefined, personal_access_token: string, organization: string, project: string, repository: string, branch: string, project_sub_path: string} | {[key: string]: unknown}; export type DbtProjectTypeNONE = 'none'; export type DbtNoneProjectConfig = {type: DbtProjectTypeNONE, target?: string | undefined, environment?: Array | undefined, selector?: string | undefined, hideRefreshButton?: boolean | undefined} | {[key: string]: unknown}; export type DbtProjectTypeMANIFEST = 'manifest'; export type DbtManifestProjectConfig = {type: DbtProjectTypeMANIFEST, manifest: string, hideRefreshButton: boolean} | {[key: string]: unknown}; export type DbtProjectConfig = DbtLocalProjectConfig | DbtCloudIDEProjectConfig | DbtGithubProjectConfig | DbtBitBucketProjectConfig | DbtGitlabProjectConfig | DbtAzureDevOpsProjectConfig | DbtNoneProjectConfig | DbtManifestProjectConfig; export type ApiCreateProjectDbtSource = {dbtConnection: DbtProjectConfig, name: string}; export type CreateApiApiV1ProjectsProjectUuidDbtSourcesArgs = {projectUuid: UUID, body: ApiCreateProjectDbtSource}; export type CreateApiApiV1ProjectsProjectUuidDbtSourcesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const dbtProjectType = z.enum(["dbt", "dbt_cloud_ide", "github", "gitlab", "bitbucket", "azure_devops", "none", "manifest"]); export const projectDbtSourceSummary = z.object({hasCredentialError: z.boolean(), projectSubPath: z.string().nullable(), branch: z.string().nullable(), repository: z.string().nullable(), type: dbtProjectType.nullable(), precedence: z.number(), isPrimary: z.boolean(), name: z.string(), projectDbtSourceUuid: z.string()}); export const apiProjectDbtSourceResponse = z.object({results: projectDbtSourceSummary, 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 }; /** * Add dbt source * * POST /api/v1/projects/{projectUuid}/dbt-sources */ export const createApiApiV1ProjectsProjectUuidDbtSources = async (args: CreateApiApiV1ProjectsProjectUuidDbtSourcesArgs, options: CreateApiApiV1ProjectsProjectUuidDbtSourcesOptions) => { 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/projects/${encodeURIComponent(String(args.projectUuid))}/dbt-sources${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(apiProjectDbtSourceResponse, await res.json()) };