UNPKG

@vendure/core

Version:

A modern, headless ecommerce framework

149 lines (148 loc) 6.03 kB
import { OnModuleInit } from '@nestjs/common'; import { ModuleRef } from '@nestjs/core'; import { JsonCompatible } from '@vendure/common/lib/shared-types'; import { RequestContext } from '../../../api/common/request-context'; import { ConfigService } from '../../../config/config.service'; import { CleanupOrphanedSettingsStoreEntriesOptions, CleanupOrphanedSettingsStoreEntriesResult, OrphanedSettingsStoreEntry, SetSettingsStoreValueResult, SettingsStoreFieldConfig, SettingsStoreRegistration } from '../../../config/settings-store/settings-store-types'; import { TransactionalConnection } from '../../../connection/transactional-connection'; /** * @description * The SettingsStoreService provides a flexible settings storage system with support for * scoping, permissions, and validation. It allows plugins and the core system to * store and retrieve configuration data with fine-grained control over access and isolation. * * ## Usage * * Values are automatically scoped according to their field configuration: * * @example * ```ts * // In a service * const userTheme = await this.settingsStoreService.get('dashboard.theme', ctx); * await this.settingsStoreService.set('dashboard.theme', 'dark', ctx); * * // Get multiple values * const settings = await this.settingsStoreService.getMany([ * 'dashboard.theme', * 'dashboard.tableFilters' * ], ctx); * ``` * * @docsCategory services * @since 3.4.0 */ export declare class SettingsStoreService implements OnModuleInit { private readonly connection; private readonly moduleRef; private readonly configService; private readonly fieldRegistry; private readonly injector; constructor(connection: TransactionalConnection, moduleRef: ModuleRef, configService: ConfigService); onModuleInit(): void; /** * @description * Initialize field registrations from the Vendure configuration. * Called during module initialization. */ private initializeFieldRegistrations; /** * @description * Register settings store fields. This is typically called during application * bootstrap when processing the VendureConfig. */ register(registration: SettingsStoreRegistration): void; /** * @description * Get a value for the specified key. The value is automatically scoped * according to the field's scope configuration. * * @param key - The full key (namespace.field) * @param ctx - Request context for scoping and permissions * @returns The stored value or undefined if not found or access denied */ get<T = JsonCompatible<any>>(key: string, ctx: RequestContext): Promise<T | undefined>; /** * @description * Get multiple values efficiently. Each key is scoped according to * its individual field configuration. * * @param keys - Array of full keys to retrieve * @param ctx - Request context for scoping and permissions * @returns Object mapping keys to their values */ getMany(keys: string[], ctx: RequestContext): Promise<Record<string, JsonCompatible<any>>>; /** * @description * Set a value for the specified key with structured result feedback. * This version returns detailed information about the success or failure * of the operation instead of throwing errors. * * @param key - The full key (namespace.field) * @param value - The value to store (must be JSON serializable) * @param ctx - Request context for scoping and permissions * @returns SetSettingsStoreValueResult with operation status and error details */ set<T extends JsonCompatible<any> = JsonCompatible<any>>(key: string, value: T, ctx: RequestContext): Promise<SetSettingsStoreValueResult>; /** * @description * Set multiple values with structured result feedback for each operation. * This method will not throw errors but will return * detailed results for each key-value pair. * * @param values - Object mapping keys to their values * @param ctx - Request context for scoping and permissions * @returns Array of SetSettingsStoreValueResult with operation status for each key */ setMany(values: Record<string, JsonCompatible<any>>, ctx: RequestContext): Promise<SetSettingsStoreValueResult[]>; /** * @description * Get the field configuration for a key. */ getFieldDefinition(key: string): SettingsStoreFieldConfig | undefined; /** * @description * Validate a value against its field definition. */ validateValue(key: string, value: any, ctx: RequestContext): Promise<string | void>; /** * @description * Generate the scope key for a given field and context. */ private generateScope; /** * @description * Get field configuration, throwing if not found. */ private getFieldConfig; /** * @description * Find orphaned settings store entries that no longer have corresponding field definitions. * * @param options - Options for filtering orphaned entries * @returns Array of orphaned entries */ findOrphanedEntries(options?: CleanupOrphanedSettingsStoreEntriesOptions): Promise<OrphanedSettingsStoreEntry[]>; /** * @description * Clean up orphaned settings store entries from the database. * * @param options - Options for the cleanup operation * @returns Result of the cleanup operation */ cleanupOrphanedEntries(options?: CleanupOrphanedSettingsStoreEntriesOptions): Promise<CleanupOrphanedSettingsStoreEntriesResult>; /** * @description * Parse a duration string (e.g., '7d', '30m', '2h') into a Date object. */ private parseDuration; /** * @description * Get a preview of a value for logging purposes, truncating if too large. */ private getValuePreview; /** * @description * Check if the current user has permission to access a field. */ private hasPermission; }