@stacksjs/stx
Version:
A performant UI Framework. Powered by Bun.
99 lines (98 loc) • 2.62 kB
TypeScript
import type { StxOptions } from './config-types';
/**
* Loop context object available within @foreach loops
* Provides iteration metadata for template logic
*/
export declare interface LoopContext {
index: number
iteration: number
first: boolean
last: boolean
count: number
}
/**
* Authentication context for @auth, @guest, @can directives
* Required shape for auth-related template directives
*
* @example
* ```typescript
* const authContext: AuthContext = {
* check: true,
* user: { id: 1, name: 'John', role: 'admin' }
* }
* ```
*/
export declare interface AuthContext {
check: boolean
user: Record<string, unknown> | null
}
/**
* Permissions context for @can, @cannot directives
*
* @example
* ```typescript
* const permissions: PermissionsContext = {
* check: (ability, type?, id?) => {
* // Check if user has the ability
* return user.abilities.includes(ability)
* }
* }
* ```
*/
export declare interface PermissionsContext {
check: (ability: string, type?: string, id?: unknown) => boolean
}
/**
* Translation context for @translate, @t directives
*/
export declare interface TranslationContext {
}
/**
* Base template context with common properties
* Extend this interface for specific template contexts
*/
export declare interface BaseTemplateContext {
auth?: AuthContext
permissions?: PermissionsContext
userCan?: Record<string, boolean>
__translations?: TranslationContext
__sections?: Record<string, string>
__stx_options?: StxOptions
loop?: LoopContext
$loop?: LoopContext
slot?: string
}
/**
* Template context type alias for backward compatibility
* Use BaseTemplateContext for new code
*/
export type TemplateContext = BaseTemplateContext
/**
* Generic template context that allows specifying custom typed properties
*
* @example
* ```typescript
* // Define your typed context
* interface MyContext extends TypedContext<{
* user: { name: string; email: string }
* items: string[]
* count: number
* }> {}
*
* // Use in template processing
* const ctx: MyContext = {
* user: { name: 'John', email: 'john@example.com' },
* items: ['a', 'b'],
* count: 42
* }
* ```
*/
export type TypedContext<T extends Record<string, unknown>> = BaseTemplateContext & T
/**
* Utility type to extract the value type from a context key
*/
export type ContextValue<C extends BaseTemplateContext, K extends keyof C> = C[K]
/**
* Utility type to make certain context properties required
*/
export type RequireContextKeys<C extends BaseTemplateContext, K extends keyof C> = Required<Pick<C, K>> & Omit<C, K>