import {z} from 'zod' export type BasicConfigs = {type: string, td_authentication_id: string, role?: string | undefined, warehouse: string, db: string, schema: string, incremental?: boolean | undefined} | {type: string, td_authentication_id: string, role?: string | undefined, warehouse: string, db: string, schema: string, incremental?: boolean | undefined}; export type AdvancedConfigs = Array<{target_table?: string | undefined, config?: ({type?: string | undefined, source_type?: ('table_view' | 'query') | undefined, query?: string | undefined, select?: string | undefined, table?: string | undefined, where?: string | undefined, order_by?: string | undefined, incremental?: boolean | undefined, incremental_columns?: Array | undefined, invalid_value_option?: ('fail_job' | 'insert_null' | 'ignore_row') | undefined} | {type?: string | undefined, source_type?: ('table_view' | 'query') | undefined, query?: string | undefined, select?: string | undefined, table?: string | undefined, where?: string | undefined, order_by?: string | undefined, incremental?: boolean | undefined, incremental_columns?: Array | undefined, invalid_value_option?: ('fail_job' | 'insert_null' | 'ignore_row') | undefined}) | undefined}>; export type OutConfigs = {database?: string | undefined, mode?: string | undefined, time_column?: string | undefined}; export type FilterConfigs = Array<{type?: string | undefined, from_column?: Record | undefined, to_column?: Record | undefined}>; export type CreateApiV1DwhArgs = {body: {schedule_type?: ('none' | 'daily' | 'hourly' | 'weekly' | 'monthly' | 'minutes_interval' | 'cron') | undefined, schedule_option?: string | undefined, timezone?: string | undefined, name: string, basic_configs: BasicConfigs, advanced_configs: AdvancedConfigs, output_config: OutConfigs, filter_configs?: FilterConfigs | undefined}}; export type CreateApiV1DwhOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dwhIntegration = z.object({schedule_type: z.enum(["none", "daily", "hourly", "weekly", "monthly", "minutes_interval", "cron"]).optional(), schedule_option: z.string().optional(), timezone: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), user_id: z.number().int().optional(), account_id: z.number().int().optional(), workflow_project_id: z.number().int().optional(), workflow_project_name: z.string().optional(), dwh_config_id: z.number().int().optional(), created_at: z.string().optional(), updated_at: 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 DWH Integration * * POST /v1/dwh/ */ export const createApiV1Dwh = async (args: CreateApiV1DwhArgs, options?: CreateApiV1DwhOptions) => { const {baseUrl = "https://dwh-integration-api.treasuredata.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/dwh/${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(dwhIntegration, await res.json()) };