import {z} from 'zod' export type Datasource = {id?: string | undefined, name?: string | undefined, description?: string | undefined, enabled?: boolean | undefined, updateTimeout?: number | undefined, runParameters?: ({onGeneration?: boolean | undefined, onNewNode?: boolean | undefined, schedule?: ({type?: ('scheduled' | 'notscheduled') | undefined}) | undefined}) | undefined, type?: ({name?: 'HTTP' | undefined, parameters?: ({url?: string | undefined, requestMethod?: ('GET' | 'POST') | undefined, headers?: (Array<{name?: string | undefined, value?: string | undefined}>) | undefined, path?: string | undefined, checkSsl?: boolean | undefined, requestTimeout?: number | undefined, requestMode?: ({name?: 'byNode' | undefined}) | undefined}) | undefined}) | undefined}; export type CreateApiDatasourcesDatasourceIdArgs = {datasourceId: string, body: Datasource}; export type CreateApiDatasourcesDatasourceIdOptions = {baseUrl?: string | undefined, APITokens?: string | undefined, fetch?: typeof fetch | undefined}; export const datasource = z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), enabled: z.boolean().optional(), updateTimeout: z.number().int().optional(), runParameters: z.object({onGeneration: z.boolean().optional(), onNewNode: z.boolean().optional(), schedule: z.object({type: z.enum(["scheduled", "notscheduled"]).optional()}).optional()}).optional(), type: z.object({name: z.literal("HTTP").optional(), parameters: z.object({url: z.string().optional(), requestMethod: z.enum(["GET", "POST"]).optional(), headers: z.array(z.object({name: z.string().optional(), value: z.string().optional()})).optional(), path: z.string().optional(), checkSsl: z.boolean().optional(), requestTimeout: z.number().int().optional(), requestMode: z.object({name: z.literal("byNode").optional()}).optional()}).optional()}).optional()}); export const createApiDatasourcesDatasourceIdResponse = z.object({result: z.enum(["success", "error"]), action: z.literal("updateDataSource"), data: z.object({datasources: z.array(datasource)})}); 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 }; /** * Update a data source configuration * * POST /datasources/{datasourceId} */ export const createApiDatasourcesDatasourceId = async (args: CreateApiDatasourcesDatasourceIdArgs, options?: CreateApiDatasourcesDatasourceIdOptions) => { const {baseUrl = "https://rudder.example.com/rudder/api/latest", fetch: fetchFn = fetch, APITokens} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APITokens !== undefined) headers.set("X-API-Token", APITokens) const url = `${baseUrl.replace(/\/+$/, '')}/datasources/${encodeURIComponent(String(args.datasourceId))}${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(createApiDatasourcesDatasourceIdResponse, await res.json()) };