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 RestoreMode = 'restore' | 'sync-struct-restore' | 'sync-data-restore'; export type DictionaryContent = {name: string, additional_info?: (string | null) | undefined, content: string}; export type RestoreRequest = {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: RestoreMode, input_path: string, validated_input_path?: (string | null) | undefined, 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, pg_restore_path?: (string | null) | undefined, pg_restore_options?: (string | null) | undefined, keep_fdw_user_mappings?: boolean | undefined, drop_custom_check_constr?: boolean | undefined, clean_db?: boolean | undefined, drop_db?: boolean | undefined, save_dicts?: boolean | undefined, ignore_privileges?: boolean | undefined}; export type CreateApiApiStatelessRestoreArgs = {body: RestoreRequest}; export type CreateApiApiStatelessRestoreOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiApiStatelessRestoreResponse = 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 }; /** * Run restore operation * * POST /api/stateless/restore */ export const createApiApiStatelessRestore = async (args: CreateApiApiStatelessRestoreArgs, options: CreateApiApiStatelessRestoreOptions) => { 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/restore${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(createApiApiStatelessRestoreResponse, await res.json()) };