import { IRoute } from '../interfaces'; import { ICancellablePromise, ICancellablePromiseNormalizedOptions, ICancellablePromiseOptions, NormalizeICancellablePromiseOptions, Reason, TAbortStrategy } from '@lifaon/observables'; import { ExecLastRouteOfRoutesPath } from './exec-last-route-of-routes-path'; import { GetRoutesPathParams } from './get-routes-path-params'; import { IRouteExecCallbackOptions } from '../types'; import { TPathMatcherParams } from '../../path-matcher/types'; import { IResolvableTree } from '../../resolvable-tree/interfaces'; export interface IResolveAndExecLastRouteOptions extends ICancellablePromiseOptions { path: string; } export interface IResolveAndExecLastRouteNormalizedOptions extends Pick, 'path'>, ICancellablePromiseNormalizedOptions { } export interface IResolveAndExecLastRouteCallbackOptions extends IRouteExecCallbackOptions { params: TPathMatcherParams; } export function ResolveAndExecLastRoute( route: IRoute, options: IResolveAndExecLastRouteOptions ): ICancellablePromise { const _options: IResolveAndExecLastRouteNormalizedOptions = NormalizeICancellablePromiseOptions(options) as IResolveAndExecLastRouteNormalizedOptions; return route.resolve(_options) .then((routesPath: IResolvableTree[]) => { if (routesPath.length === 0) { throw new Reason(`Cannot resolve the path '${ _options.path }'`, 'INVALID_PATH'); } else { return ExecLastRouteOfRoutesPath(routesPath as IRoute[], { ..._options, routesPath, params: GetRoutesPathParams(routesPath as IRoute[], _options.path), } as IResolveAndExecLastRouteCallbackOptions); } }); }