@analogjs/router
Version:
Filesystem-based routing for Angular
266 lines (248 loc) • 9.64 kB
TypeScript
import * as _angular_core from '@angular/core';
import { Type, EnvironmentProviders, Injector } from '@angular/core';
import * as _angular_router from '@angular/router';
import { Route, ActivatedRoute, Router, CanActivateFn, DeprecatedGuard, CanActivateChildFn, CanDeactivateFn, CanMatchFn, ResolveFn, RouterFeatures, Routes, ActivatedRouteSnapshot } from '@angular/router';
import { H3EventContext, H3Event } from 'h3';
import { $Fetch } from 'nitropack';
import * as rxjs from 'rxjs';
import { Observable } from 'rxjs';
import * as _angular_common_module_d from '@angular/common/module.d';
import { HttpRequest, HttpHandlerFn } from '@angular/common/http';
import { SafeHtml } from '@angular/platform-browser';
type RouteOmitted = 'component' | 'loadComponent' | 'loadChildren' | 'path' | 'pathMatch';
type RestrictedRoute = Omit<Route, RouteOmitted>;
/**
* @deprecated Use `RouteMeta` type instead.
* For more info see: https://github.com/analogjs/analog/issues/223
*
* Defines additional route config metadata. This
* object is merged into the route config with
* the predefined file-based route.
*
* @usageNotes
*
* ```
* import { Component } from '@angular/core';
* import { defineRouteMeta } from '@analogjs/router';
*
* export const routeMeta = defineRouteMeta({
* title: 'Welcome'
* });
*
* @Component({
* template: `Home`,
* standalone: true,
* })
* export default class HomeComponent {}
* ```
*
* @param route
* @returns
*/
declare const defineRouteMeta: (route: RestrictedRoute) => RestrictedRoute;
/**
* Returns the instance of Angular Router
*
* @returns The router
*/
declare const injectRouter: () => Router;
/**
* Returns the instance of the Activate Route for the component
*
* @returns The activated route
*/
declare const injectActivatedRoute: () => ActivatedRoute;
declare const CHARSET_KEY = "charset";
declare const HTTP_EQUIV_KEY = "httpEquiv";
declare const NAME_KEY = "name";
declare const PROPERTY_KEY = "property";
declare const CONTENT_KEY = "content";
type MetaTag = (CharsetMetaTag & ExcludeRestMetaTagKeys<typeof CHARSET_KEY>) | (HttpEquivMetaTag & ExcludeRestMetaTagKeys<typeof HTTP_EQUIV_KEY>) | (NameMetaTag & ExcludeRestMetaTagKeys<typeof NAME_KEY>) | (PropertyMetaTag & ExcludeRestMetaTagKeys<typeof PROPERTY_KEY>);
type CharsetMetaTag = {
[CHARSET_KEY]: string;
};
type HttpEquivMetaTag = {
[HTTP_EQUIV_KEY]: string;
[CONTENT_KEY]: string;
};
type NameMetaTag = {
[NAME_KEY]: string;
[CONTENT_KEY]: string;
};
type PropertyMetaTag = {
[PROPERTY_KEY]: string;
[CONTENT_KEY]: string;
};
type MetaTagKey = typeof CHARSET_KEY | typeof HTTP_EQUIV_KEY | typeof NAME_KEY | typeof PROPERTY_KEY;
type ExcludeRestMetaTagKeys<Key extends MetaTagKey> = {
[K in Exclude<MetaTagKey, Key>]?: never;
};
type OmittedRouteProps = 'path' | 'matcher' | 'component' | 'loadComponent' | 'children' | 'loadChildren' | 'canLoad' | 'outlet';
interface DefaultRouteMeta extends Omit<Route, OmittedRouteProps | keyof RedirectRouteMeta> {
canActivate?: CanActivateFn[] | DeprecatedGuard[];
canActivateChild?: CanActivateChildFn[];
canDeactivate?: CanDeactivateFn<unknown>[];
canMatch?: CanMatchFn[];
resolve?: {
[key: string | symbol]: ResolveFn<unknown>;
};
title?: string | ResolveFn<string>;
meta?: MetaTag[] | ResolveFn<MetaTag[]>;
}
interface RedirectRouteMeta {
redirectTo: string;
pathMatch?: Route['pathMatch'];
}
type RouteMeta = (DefaultRouteMeta & {
redirectTo?: never;
}) | RedirectRouteMeta;
type RouteExport = {
default: Type<unknown>;
routeMeta?: RouteMeta | ReturnType<typeof defineRouteMeta>;
};
type Files = Record<string, () => Promise<RouteExport | string>>;
/**
* A function used to parse list of files and create configuration of routes.
*
* @param files
* @returns Array of routes
*/
declare function createRoutes(files: Files, debug?: boolean): Route[];
declare const routes: Route[];
/**
* Sets up providers for the Angular router, and registers
* file-based routes. Additional features can be provided
* to further configure the behavior of the router.
*
* @param features
* @returns Providers and features to configure the router with routes
*/
declare function provideFileRouter(...features: RouterFeatures[]): EnvironmentProviders;
/**
* Provides extra custom routes in addition to the routes
* discovered from the filesystem-based routing. These routes are
* inserted before the filesystem-based routes, and take priority in
* route matching.
*/
declare function withExtraRoutes(routes: Routes): RouterFeatures;
type PageServerLoad = {
params: H3EventContext['params'];
req: H3Event['node']['req'];
res: H3Event['node']['res'];
fetch: $Fetch;
event: H3Event;
};
type LoadResult<A extends (pageServerLoad: PageServerLoad) => Promise<any>> = Awaited<ReturnType<A>>;
declare function injectLoad<T extends (pageServerLoad: PageServerLoad) => Promise<any>>(options?: {
injector?: Injector;
}): Observable<Awaited<ReturnType<T>>>;
/**
* Get server load resolver data for the route
*
* @param route Provides the route to get server load resolver
* @returns Returns server load resolver data for the route
*/
declare function getLoadResolver<T>(route: ActivatedRouteSnapshot): Promise<T>;
/**
* Interceptor that is server-aware when making HttpClient requests.
* Server-side requests use the full URL
* Prerendering uses the internal Nitro $fetch function, along with state transfer
* Client-side requests use the window.location.origin
*
* @param req HttpRequest<unknown>
* @param next HttpHandlerFn
* @returns
*/
declare function requestContextInterceptor(req: HttpRequest<unknown>, next: HttpHandlerFn): rxjs.Observable<_angular_common_module_d.HttpEvent<unknown>>;
declare function injectRouteEndpointURL(route: ActivatedRouteSnapshot): URL;
declare class FormAction {
action: _angular_core.InputSignal<string>;
onSuccess: _angular_core.OutputEmitterRef<unknown>;
onError: _angular_core.OutputEmitterRef<unknown>;
state: _angular_core.OutputEmitterRef<"error" | "navigate" | "redirect" | "submitting" | "success">;
private router;
private route;
private path;
submitted($event: {
target: HTMLFormElement;
} & Event): void;
private _handleGet;
private _handlePost;
private _getPath;
private _isJSON;
static ɵfac: _angular_core.ɵɵFactoryDeclaration<FormAction, never>;
static ɵdir: _angular_core.ɵɵDirectiveDeclaration<FormAction, "form[action],form[method]", never, { "action": { "alias": "action"; "required": false; "isSignal": true; }; }, { "onSuccess": "onSuccess"; "onError": "onError"; "state": "state"; }, never, never, true, never>;
}
type DebugRoute = {
path: string;
filename: string;
isLayout: boolean;
children?: DebugRoute[];
};
declare function injectDebugRoutes(): (Route & DebugRoute)[];
type CollectedRoute = {
path: string;
filename: string;
file: string;
isLayout: boolean;
};
declare class DebugRoutesComponent {
collectedRoutes: CollectedRoute[];
debugRoutes: (_angular_router.Route & DebugRoute)[];
ngOnInit(): void;
traverseRoutes(routes: DebugRoute[], parent?: string): void;
static ɵfac: _angular_core.ɵɵFactoryDeclaration<DebugRoutesComponent, never>;
static ɵcmp: _angular_core.ɵɵComponentDeclaration<DebugRoutesComponent, "analogjs-debug-routes-page", never, {}, {}, never, never, true, never>;
}
declare namespace __debug_page {
export {
DebugRoutesComponent as default,
};
}
/**
* Provides routes that provide additional
* pages for displaying and debugging
* routes.
*/
declare function withDebugRoutes(): {
ɵkind: number;
ɵproviders: {
provide: _angular_core.InjectionToken<_angular_router.Route[][]>;
useValue: {
path: string;
loadComponent: () => Promise<typeof __debug_page>;
}[];
multi: boolean;
}[];
};
type ServerProps = Record<string, any>;
type ServerOutputs = Record<string, any>;
/**
* @description
* Component that defines the bridge between the client and server-only
* components. The component passes the component ID and props to the server
* and retrieves the rendered HTML and outputs from the server-only component.
*
* Status: experimental
*/
declare class ServerOnly {
component: _angular_core.InputSignal<string>;
props: _angular_core.InputSignal<ServerProps | undefined>;
outputs: _angular_core.OutputEmitterRef<ServerOutputs>;
private http;
private sanitizer;
protected content: _angular_core.WritableSignal<SafeHtml>;
private route;
private baseURL;
private transferState;
constructor();
updateContent(content: {
html: string;
outputs: ServerOutputs;
}): void;
getComponentUrl(componentId: string): string;
static ɵfac: _angular_core.ɵɵFactoryDeclaration<ServerOnly, never>;
static ɵcmp: _angular_core.ɵɵComponentDeclaration<ServerOnly, "server-only,ServerOnly,Server", never, { "component": { "alias": "component"; "required": true; "isSignal": true; }; "props": { "alias": "props"; "required": false; "isSignal": true; }; }, { "outputs": "outputs"; }, never, never, true, never>;
}
export { FormAction, ServerOnly, createRoutes, defineRouteMeta, getLoadResolver, injectActivatedRoute, injectDebugRoutes, injectLoad, injectRouteEndpointURL, injectRouter, provideFileRouter, requestContextInterceptor, routes, withDebugRoutes, withExtraRoutes };
export type { Files, LoadResult, MetaTag, PageServerLoad, RouteExport, RouteMeta };