All files / router/src constant.ts

100% Statements 35/35
100% Branches 8/8
100% Functions 3/3
100% Lines 35/35

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 1021x 1x                             1x 1x   1x 1x   1x 1x 1x 1x 1x 1x                                           1x 26x 26x   1x 1721x 1721x 1721x 1721x 3269473x   3269473x 4668818x   4668808x 1632476x 1632476x 4668818x   3269473x 3269473x 1721x 1721x 1721x                 1x                 1x                   1x  
import { isObject } from '@anchorlib/core';
import { PRELOAD_MODE, RENDER_MODE, RETRY_MODE, ROUTE_TYPE } from './enum.js';
import type { RouteOptions, RouterOptions } from './types.js';
 
/**
 * Default configuration options for the router.
 *
 * These values are used as fallbacks when specific options are not provided.
 * Can be modified using the {@link configure} function.
 *
 * @example
 * ```ts
 * // Modify default configuration
 * configure({ baseUrl: 'https://example.com', maxAge: 60000 });
 * ```
 */
export const DEFAULT_CONFIG: RouterOptions = {
  baseUrl: 'http://localhost',
 
  maxAge: 0,
  keepAlive: false,
 
  retryMode: RETRY_MODE.LINEAR,
  retryDelay: 0,
  maxRetries: 0,
  renderMode: RENDER_MODE.IMMEDIATE,
  preloadMode: PRELOAD_MODE.MANUAL,
};
 
/**
 * Configures the default router options.
 *
 * Merges the provided configuration with the existing DEFAULT_CONFIG.
 * This affects all routers created after this call.
 *
 * @param config - Partial configuration options to merge with defaults
 *
 * @example
 * ```ts
 * configure({
 *   baseUrl: 'https://api.example.com',
 *   maxAge: 300000, // 5 minutes
 *   keepAlive: true,
 *   retryMode: 'exponential',
 *   retryDelay: 1000,
 *   maxRetries: 3
 * });
 * ```
 */
export function configure(config: Partial<RouterOptions>) {
  Object.assign(DEFAULT_CONFIG, config);
}
 
export function inheritConfig(...overrides: Array<RouteOptions | undefined>) {
  return new Proxy(
    {},
    {
      get(_target, key) {
        let value = DEFAULT_CONFIG[key as keyof RouterOptions];
 
        for (const override of overrides) {
          if (!isObject(override)) continue;
 
          if (typeof override[key as keyof RouterOptions] !== 'undefined') {
            value = override[key as keyof RouterOptions] as RouterOptions[keyof RouterOptions];
          }
        }
 
        return value;
      },
    }
  ) as RouteOptions;
}
 
/**
 * Symbol used as a key for dynamic routes in the route registry.
 *
 * Dynamic routes are routes with parameters like `:id` that match any value.
 *
 * @internal
 */
export const DYNAMIC_ROUTE_KEY = Symbol(ROUTE_TYPE.DYNAMIC);
 
/**
 * Symbol used as a key for wildcard routes in the route registry.
 *
 * Wildcard routes match any remaining path segments using `*`.
 *
 * @internal
 */
export const WILDCARD_ROUTE_KEY = Symbol(ROUTE_TYPE.WILDCARD);
 
/**
 * WeakMap linking routes to their registries.
 *
 * Maintains a bidirectional relationship between Route instances
 * and their RouteRegistry instances without preventing garbage collection.
 *
 * @internal
 */
export const ROUTE_MAP_LINK = new WeakMap();