All files / coherent.js/packages/koa/types index.d.ts

0% Statements 0/1
100% Branches 1/1
100% Functions 1/1
0% Lines 0/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156                                                                                                                                                                                                                                                                                                                       
/**
 * Coherent.js Koa Integration Types
 * TypeScript definitions for Koa.js framework integration
 * 
 * @version 1.1.1
 */
 
import Koa, { Context, Middleware, Next } from 'koa';
import { CoherentNode } from '@coherent/core';
 
// ============================================================================
// Koa Integration Types
// ============================================================================
 
/** Coherent Koa options */
export interface CoherentKoaOptions {
  viewEngine?: string;
  viewsDirectory?: string;
  cache?: boolean;
  development?: boolean;
  renderOptions?: {
    pretty?: boolean;
    doctype?: string;
  };
  errorHandler?: (error: Error, ctx: Context, next: Next) => Promise<void>;
}
 
/** Enhanced Koa context with Coherent.js utilities */
export interface CoherentContext extends Context {
  renderComponent<P = any>(component: (props: P) => CoherentNode, props?: P): string;
  renderCoherent(component: CoherentNode, options?: any): void;
  sendComponent<P = any>(component: (props: P) => CoherentNode, props?: P): void;
  getComponent<P = any>(name: string, props?: P): CoherentNode | undefined;
  
  // State management
  coherentState: any;
  setCoherentState(state: any): void;
  getCoherentState(): any;
}
 
/** Koa application with Coherent.js support */
export interface CoherentKoaApplication extends Koa {
  context: CoherentContext;
  renderCoherent(component: CoherentNode, options?: any): void;
}
 
// ============================================================================
// Middleware Types
// ============================================================================
 
/** Component rendering middleware options */
export interface KoaComponentMiddlewareOptions {
  componentDirectory?: string;
  autoRegister?: boolean;
  cache?: boolean;
  development?: boolean;
}
 
/** Route configuration for component-based routing */
export interface KoaComponentRoute {
  path: string | RegExp;
  method?: string | string[];
  component: (props: any) => CoherentNode;
  middleware?: Middleware[];
  props?: (ctx: Context) => any | Promise<any>;
  layout?: (props: any) => CoherentNode;
  meta?: {
    title?: string;
    description?: string;
    keywords?: string[];
  };
}
 
/** SSR configuration for Koa */
export interface KoaSSRConfig {
  enabled?: boolean;
  cache?: boolean;
  cacheMaxAge?: number;
  bundlePath?: string;
  templatePath?: string;
  clientManifest?: any;
  serverBundle?: any;
  renderToStream?: boolean;
}
 
/** SSR context for Koa */
export interface KoaSSRContext {
  ctx: Context;
  url: string;
  state: any;
  meta: {
    title?: string;
    description?: string;
    keywords?: string[];
    og?: Record<string, string>;
    twitter?: Record<string, string>;
  };
  assets: {
    css: string[];
    js: string[];
  };
}
 
// ============================================================================
// Main Functions
// ============================================================================
 
/** Setup Coherent.js with Koa application */
export function setupCoherent(
  app: Koa,
  options?: CoherentKoaOptions
): CoherentKoaApplication;
 
/** Create component-based route middleware */
export function createComponentRoute(config: KoaComponentRoute): Middleware;
 
/** Middleware for component rendering */
export function componentMiddleware(options?: KoaComponentMiddlewareOptions): Middleware;
 
/** SSR middleware for Koa */
export function ssrMiddleware(config?: KoaSSRConfig): Middleware;
 
/** Error handling middleware for Coherent.js */
export function errorMiddleware(options?: {
  showStack?: boolean;
  logErrors?: boolean;
}): Middleware;
 
// ============================================================================
// Utility Functions
// ============================================================================
 
/** Create enhanced Koa app with Coherent.js */
export function createCoherentApp(options?: CoherentKoaOptions): CoherentKoaApplication;
 
/** Register component routes */
export function registerRoutes(
  app: Koa,
  routes: KoaComponentRoute[]
): void;
 
// ============================================================================
// Default Export
// ============================================================================
 
declare const coherentKoa: {
  setupCoherent: typeof setupCoherent;
  createComponentRoute: typeof createComponentRoute;
  componentMiddleware: typeof componentMiddleware;
  ssrMiddleware: typeof ssrMiddleware;
  errorMiddleware: typeof errorMiddleware;
  createCoherentApp: typeof createCoherentApp;
  registerRoutes: typeof registerRoutes;
};
 
export default coherentKoa;