import {z} from 'zod' export type FsProviders = 0 | 1 | 2 | 3 | 4 | 5 | 6; export type OSFsConfig = {read_buffer_size?: number | undefined, write_buffer_size?: number | undefined}; export type Secret = {status?: ('Plain' | 'AES-256-GCM' | 'Secretbox' | 'GCP' | 'AWS' | 'VaultTransit' | 'AzureKeyVault' | 'Redacted') | undefined, payload?: string | undefined, key?: string | undefined, additional_data?: string | undefined, mode?: number | undefined}; export type S3Config = {bucket?: string | undefined, region?: string | undefined, access_key?: string | undefined, access_secret?: Secret | undefined, role_arn?: string | undefined, session_token?: string | undefined, endpoint?: string | undefined, storage_class?: string | undefined, acl?: string | undefined, upload_part_size?: number | undefined, upload_concurrency?: number | undefined, upload_part_max_time?: number | undefined, download_part_size?: number | undefined, download_concurrency?: number | undefined, download_part_max_time?: number | undefined, force_path_style?: boolean | undefined, key_prefix?: string | undefined}; export type GCSConfig = {bucket?: string | undefined, credentials?: Secret | undefined, automatic_credentials?: (0 | 1) | undefined, storage_class?: string | undefined, acl?: string | undefined, key_prefix?: string | undefined, upload_part_size?: number | undefined, upload_part_max_time?: number | undefined}; export type AzureBlobFsConfig = {container?: string | undefined, account_name?: string | undefined, account_key?: Secret | undefined, sas_url?: Secret | undefined, endpoint?: string | undefined, upload_part_size?: number | undefined, upload_concurrency?: number | undefined, download_part_size?: number | undefined, download_concurrency?: number | undefined, access_tier?: ('' | 'Archive' | 'Hot' | 'Cool') | undefined, key_prefix?: string | undefined, use_emulator?: boolean | undefined}; export type CryptFsConfig = {passphrase?: Secret | undefined, read_buffer_size?: number | undefined, write_buffer_size?: number | undefined}; export type SFTPFsConfig = {endpoint?: string | undefined, username?: string | undefined, password?: Secret | undefined, private_key?: Secret | undefined, key_passphrase?: Secret | undefined, fingerprints?: Array | undefined, prefix?: string | undefined, disable_concurrent_reads?: boolean | undefined, buffer_size?: number | undefined, equality_check_mode?: (0 | 1) | undefined}; export type HTTPFsConfig = {endpoint?: string | undefined, username?: string | undefined, password?: Secret | undefined, api_key?: Secret | undefined, skip_tls_verify?: boolean | undefined, equality_check_mode?: (0 | 1) | undefined}; export type FilesystemConfig = {provider?: FsProviders | undefined, osconfig?: OSFsConfig | undefined, s3config?: S3Config | undefined, gcsconfig?: GCSConfig | undefined, azblobconfig?: AzureBlobFsConfig | undefined, cryptconfig?: CryptFsConfig | undefined, sftpconfig?: SFTPFsConfig | undefined, httpconfig?: HTTPFsConfig | undefined}; export type VirtualFolder = {id?: number | undefined, name?: string | undefined, mapped_path?: string | undefined, description?: string | undefined, used_quota_size?: number | undefined, used_quota_files?: number | undefined, last_quota_update?: number | undefined, users?: Array | undefined, filesystem?: FilesystemConfig | undefined, virtual_path: string, quota_size?: number | undefined, quota_files?: number | undefined}; export type Permission = '*' | 'list' | 'download' | 'upload' | 'overwrite' | 'delete' | 'delete_files' | 'delete_dirs' | 'rename' | 'rename_files' | 'rename_dirs' | 'create_dirs' | 'create_symlinks' | 'chmod' | 'chown' | 'chtimes' | 'copy'; export type LoginMethods = 'publickey' | 'password' | 'password-over-SSH' | 'keyboard-interactive' | 'publickey+password' | 'publickey+keyboard-interactive' | 'TLSCertificate' | 'TLSCertificate+password'; export type SupportedProtocols = 'SSH' | 'FTP' | 'DAV' | 'HTTP'; export type PatternsFilter = {path?: string | undefined, allowed_patterns?: Array | undefined, denied_patterns?: Array | undefined, deny_policy?: (0 | 1) | undefined}; export type HooksFilter = {external_auth_disabled?: boolean | undefined, pre_login_disabled?: boolean | undefined, check_password_disabled?: boolean | undefined}; export type WebClientOptions = 'publickey-change-disabled' | 'tls-cert-change-disabled' | 'write-disabled' | 'mfa-disabled' | 'password-change-disabled' | 'api-key-auth-change-disabled' | 'info-change-disabled' | 'shares-disabled' | 'password-reset-disabled' | 'shares-without-password-disabled'; export type UserType = '' | 'LDAPUser' | 'OSUser'; export type BandwidthLimit = {sources?: Array | undefined, upload_bandwidth?: number | undefined, download_bandwidth?: number | undefined}; export type MFAProtocols = 'SSH' | 'FTP' | 'HTTP'; export type TimePeriod = {day_of_week?: (0 | 1 | 2 | 3 | 4 | 5 | 6) | undefined, from?: string | undefined, to?: string | undefined}; export type UserTOTPConfig = {enabled?: boolean | undefined, config_name?: string | undefined, secret?: Secret | undefined, protocols?: Array | undefined}; export type RecoveryCode = {secret?: Secret | undefined, used?: boolean | undefined}; export type UserFilters = {allowed_ip?: Array | undefined, denied_ip?: Array | undefined, denied_login_methods?: Array | undefined, denied_protocols?: Array | undefined, file_patterns?: Array | undefined, max_upload_file_size?: number | undefined, tls_username?: string | undefined, tls_certs?: Array | undefined, hooks?: HooksFilter | undefined, disable_fs_checks?: boolean | undefined, web_client?: Array | undefined, allow_api_key_auth?: boolean | undefined, user_type?: UserType | undefined, bandwidth_limits?: Array | undefined, external_auth_cache_time?: number | undefined, start_directory?: string | undefined, two_factor_protocols?: Array | undefined, ftp_security?: (0 | 1) | undefined, is_anonymous?: boolean | undefined, default_shares_expiration?: number | undefined, max_shares_expiration?: number | undefined, password_expiration?: number | undefined, password_strength?: number | undefined, access_time?: Array | undefined, require_password_change?: boolean | undefined, totp_config?: UserTOTPConfig | undefined, recovery_codes?: Array | undefined}; export type GroupMapping = {name?: string | undefined, type: unknown}; export type User = {id?: number | undefined, status?: (0 | 1) | undefined, username?: string | undefined, email?: string | undefined, description?: string | undefined, expiration_date?: number | undefined, password?: string | undefined, public_keys?: Array | undefined, has_password?: boolean | undefined, home_dir?: string | undefined, virtual_folders?: Array | undefined, uid?: number | undefined, gid?: number | undefined, max_sessions?: number | undefined, quota_size?: number | undefined, quota_files?: number | undefined, permissions?: {[key: string]: Array} | undefined, used_quota_size?: number | undefined, used_quota_files?: number | undefined, last_quota_update?: number | undefined, upload_bandwidth?: number | undefined, download_bandwidth?: number | undefined, upload_data_transfer?: number | undefined, download_data_transfer?: number | undefined, total_data_transfer?: number | undefined, used_upload_data_transfer?: number | undefined, used_download_data_transfer?: number | undefined, created_at?: number | undefined, updated_at?: number | undefined, last_login?: number | undefined, first_download?: number | undefined, first_upload?: number | undefined, last_password_change?: number | undefined, filters?: UserFilters | undefined, filesystem?: FilesystemConfig | undefined, additional_info?: string | undefined, groups?: Array | undefined, oidc_custom_fields?: {[key: string]: unknown} | undefined, role?: string | undefined}; export type CreateApiUsersArgs = {confidential_data?: number | undefined, body: User}; export type CreateApiUsersOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fsProviders = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6)]); export const oSFsConfig = z.object({read_buffer_size: z.number().int().gte(0).lte(10).optional(), write_buffer_size: z.number().int().gte(0).lte(10).optional()}); export const secret = z.object({status: z.enum(["Plain", "AES-256-GCM", "Secretbox", "GCP", "AWS", "VaultTransit", "AzureKeyVault", "Redacted"]).optional(), payload: z.string().optional(), key: z.string().optional(), additional_data: z.string().optional(), mode: z.number().int().optional()}); export const s3Config = z.object({bucket: z.string().min(1).optional(), region: z.string().min(1).optional(), access_key: z.string().optional(), access_secret: secret.optional(), role_arn: z.string().optional(), session_token: z.string().optional(), endpoint: z.string().optional(), storage_class: z.string().optional(), acl: z.string().optional(), upload_part_size: z.number().int().optional(), upload_concurrency: z.number().int().optional(), upload_part_max_time: z.number().int().optional(), download_part_size: z.number().int().optional(), download_concurrency: z.number().int().optional(), download_part_max_time: z.number().int().optional(), force_path_style: z.boolean().optional(), key_prefix: z.string().optional()}); export const gCSConfig = z.object({bucket: z.string().min(1).optional(), credentials: secret.optional(), automatic_credentials: z.union([z.literal(0), z.literal(1)]).optional(), storage_class: z.string().optional(), acl: z.string().optional(), key_prefix: z.string().optional(), upload_part_size: z.number().int().optional(), upload_part_max_time: z.number().int().optional()}); export const azureBlobFsConfig = z.object({container: z.string().optional(), account_name: z.string().optional(), account_key: secret.optional(), sas_url: secret.optional(), endpoint: z.string().optional(), upload_part_size: z.number().int().optional(), upload_concurrency: z.number().int().optional(), download_part_size: z.number().int().optional(), download_concurrency: z.number().int().optional(), access_tier: z.enum(["", "Archive", "Hot", "Cool"]).optional(), key_prefix: z.string().optional(), use_emulator: z.boolean().optional()}); export const cryptFsConfig = z.object({passphrase: secret.optional(), read_buffer_size: z.number().int().gte(0).lte(10).optional(), write_buffer_size: z.number().int().gte(0).lte(10).optional()}); export const sFTPFsConfig = z.object({endpoint: z.string().optional(), username: z.string().optional(), password: secret.optional(), private_key: secret.optional(), key_passphrase: secret.optional(), fingerprints: z.array(z.string()).optional(), prefix: z.string().optional(), disable_concurrent_reads: z.boolean().optional(), buffer_size: z.number().int().gte(0).lte(16).optional(), equality_check_mode: z.union([z.literal(0), z.literal(1)]).optional()}); export const hTTPFsConfig = z.object({endpoint: z.string().optional(), username: z.string().optional(), password: secret.optional(), api_key: secret.optional(), skip_tls_verify: z.boolean().optional(), equality_check_mode: z.union([z.literal(0), z.literal(1)]).optional()}); export const filesystemConfig = z.object({provider: fsProviders.optional(), osconfig: oSFsConfig.optional(), s3config: s3Config.optional(), gcsconfig: gCSConfig.optional(), azblobconfig: azureBlobFsConfig.optional(), cryptconfig: cryptFsConfig.optional(), sftpconfig: sFTPFsConfig.optional(), httpconfig: hTTPFsConfig.optional()}); export const virtualFolder = z.object({id: z.number().int().gte(1).optional(), name: z.string().optional(), mapped_path: z.string().optional(), description: z.string().optional(), used_quota_size: z.number().int().optional(), used_quota_files: z.number().int().optional(), last_quota_update: z.number().int().optional(), users: z.array(z.string()).optional(), filesystem: filesystemConfig.optional(), virtual_path: z.string(), quota_size: z.number().int().optional(), quota_files: z.number().int().optional()}); export const permission = z.enum(["*", "list", "download", "upload", "overwrite", "delete", "delete_files", "delete_dirs", "rename", "rename_files", "rename_dirs", "create_dirs", "create_symlinks", "chmod", "chown", "chtimes", "copy"]); export const loginMethods = z.enum(["publickey", "password", "password-over-SSH", "keyboard-interactive", "publickey+password", "publickey+keyboard-interactive", "TLSCertificate", "TLSCertificate+password"]); export const supportedProtocols = z.enum(["SSH", "FTP", "DAV", "HTTP"]); export const patternsFilter = z.object({path: z.string().optional(), allowed_patterns: z.array(z.string()).optional(), denied_patterns: z.array(z.string()).optional(), deny_policy: z.union([z.literal(0), z.literal(1)]).optional()}); export const hooksFilter = z.object({external_auth_disabled: z.boolean().optional(), pre_login_disabled: z.boolean().optional(), check_password_disabled: z.boolean().optional()}); export const webClientOptions = z.enum(["publickey-change-disabled", "tls-cert-change-disabled", "write-disabled", "mfa-disabled", "password-change-disabled", "api-key-auth-change-disabled", "info-change-disabled", "shares-disabled", "password-reset-disabled", "shares-without-password-disabled"]); export const userType = z.enum(["", "LDAPUser", "OSUser"]); export const bandwidthLimit = z.object({sources: z.array(z.string()).optional(), upload_bandwidth: z.number().int().optional(), download_bandwidth: z.number().int().optional()}); export const mFAProtocols = z.enum(["SSH", "FTP", "HTTP"]); export const timePeriod = z.object({day_of_week: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6)]).optional(), from: z.string().optional(), to: z.string().optional()}); export const userTOTPConfig = z.object({enabled: z.boolean().optional(), config_name: z.string().optional(), secret: secret.optional(), protocols: z.array(mFAProtocols).optional()}); export const recoveryCode = z.object({secret: secret.optional(), used: z.boolean().optional()}); export const userFilters = z.object({allowed_ip: z.array(z.string()).optional(), denied_ip: z.array(z.string()).optional(), denied_login_methods: z.array(loginMethods).optional(), denied_protocols: z.array(supportedProtocols).optional(), file_patterns: z.array(patternsFilter).optional(), max_upload_file_size: z.number().int().optional(), tls_username: z.string().optional(), tls_certs: z.array(z.string()).optional(), hooks: hooksFilter.optional(), disable_fs_checks: z.boolean().optional(), web_client: z.array(webClientOptions).optional(), allow_api_key_auth: z.boolean().optional(), user_type: userType.optional(), bandwidth_limits: z.array(bandwidthLimit).optional(), external_auth_cache_time: z.number().int().optional(), start_directory: z.string().optional(), two_factor_protocols: z.array(mFAProtocols).optional(), ftp_security: z.union([z.literal(0), z.literal(1)]).optional(), is_anonymous: z.boolean().optional(), default_shares_expiration: z.number().int().optional(), max_shares_expiration: z.number().int().optional(), password_expiration: z.number().int().optional(), password_strength: z.number().int().optional(), access_time: z.array(timePeriod).optional(), require_password_change: z.boolean().optional(), totp_config: userTOTPConfig.optional(), recovery_codes: z.array(recoveryCode).optional()}); export const groupMapping = z.object({name: z.string().optional(), type: z.unknown()}); export const user = z.object({id: z.number().int().gte(1).optional(), status: z.union([z.literal(0), z.literal(1)]).optional(), username: z.string().optional(), email: z.string().optional(), description: z.string().optional(), expiration_date: z.number().int().optional(), password: z.string().optional(), public_keys: z.array(z.string()).optional(), has_password: z.boolean().optional(), home_dir: z.string().optional(), virtual_folders: z.array(virtualFolder).optional(), uid: z.number().int().gte(0).lte(2147483647).optional(), gid: z.number().int().gte(0).lte(2147483647).optional(), max_sessions: z.number().int().optional(), quota_size: z.number().int().optional(), quota_files: z.number().int().optional(), permissions: z.record(z.string(), z.array(permission)).optional(), used_quota_size: z.number().int().optional(), used_quota_files: z.number().int().optional(), last_quota_update: z.number().int().optional(), upload_bandwidth: z.number().int().optional(), download_bandwidth: z.number().int().optional(), upload_data_transfer: z.number().int().optional(), download_data_transfer: z.number().int().optional(), total_data_transfer: z.number().int().optional(), used_upload_data_transfer: z.number().int().optional(), used_download_data_transfer: z.number().int().optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional(), last_login: z.number().int().optional(), first_download: z.number().int().optional(), first_upload: z.number().int().optional(), last_password_change: z.number().int().optional(), filters: userFilters.optional(), filesystem: filesystemConfig.optional(), additional_info: z.string().optional(), groups: z.array(groupMapping).optional(), oidc_custom_fields: z.record(z.string(), z.unknown()).optional(), role: 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 }; /** * Add user * * POST /users * * Also accepts auth: APIKeyAuth */ export const createApiUsers = async (args: CreateApiUsersArgs, options: CreateApiUsersOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.confidential_data != null) query.append("confidential_data", String(args.confidential_data)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/users${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(user, await res.json()) };