import {z} from 'zod' export type DbConnectionParams = {host: string, port: number, db_name: string, user_login: string, user_password: string}; export type VerboseOptions = 'info' | 'debug' | 'error'; export type DumpMode = 'dump' | 'sync-struct-dump' | 'sync-data-dump'; export type DictionaryContent = {name: string, additional_info?: (string | null) | undefined, content: string}; export type DumpRequest = {operation_id: string, db_connection_params: DbConnectionParams, webhook_status_url: string, webhook_metadata: unknown, webhook_extra_headers?: ({[key: string]: string} | null) | undefined, webhook_verify_ssl?: boolean | undefined, web_debug?: boolean | undefined, log_level?: (VerboseOptions | null) | undefined, conn_count?: (number | null) | undefined, proc_conn_count?: (number | null) | undefined, type: DumpMode, sens_dict_contents: Array, partial_tables_dict_contents?: (Array | null) | undefined, partial_tables_exclude_dict_contents?: (Array | null) | undefined, schema_names?: (Array | null) | undefined, schema_masks?: (Array | null) | undefined, exclude_schema_names?: (Array | null) | undefined, exclude_schema_masks?: (Array | null) | undefined, output_path: string, validated_output_path?: (string | null) | undefined, pg_dump_path?: (string | null) | undefined, pg_dump_options?: (string | null) | undefined, allow_fdw_credentials?: boolean | undefined, proc_count?: (number | null) | undefined, save_dicts?: boolean | undefined, ignore_privileges?: boolean | undefined}; export type CreateApiApiStatelessDumpArgs = {body: DumpRequest}; export type CreateApiApiStatelessDumpOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiApiStatelessDumpResponse = z.unknown(); 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 new dump operation * * POST /api/stateless/dump */ export const createApiApiStatelessDump = async (args: CreateApiApiStatelessDumpArgs, options: CreateApiApiStatelessDumpOptions) => { 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/stateless/dump${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(createApiApiStatelessDumpResponse, await res.json()) };