analogjs / analog

The fullstack meta-framework for Angular. Powered by Vite and Nitro
https://analogjs.org
MIT License
2.5k stars 240 forks source link

RFC: deprecate defineRouteMeta function in favor of RouteMeta type/interface #223

Closed brandonroberts closed 1 year ago

brandonroberts commented 1 year ago

Which scope/s are relevant/related to the feature request?

router

Information

The defineRouteMeta function causes issues when eagerly loading files using glob imports with Vite. The function is only used to enforce the type for restricting what can be provided as route metadata.

Proposed

Before:

// src/app/routes/index.ts
import { defineRouteMeta } from '@analogjs/router';

export const routeMeta = defineRouteMeta({
  title: 'Home',
});

After:

// src/app/routes/index.ts
import { RouteMeta } from '@analogjs/router';

export const routeMeta: RouteMeta = {
  title: 'Home',
};

Describe any alternatives/workarounds you're currently using

No response

I would be willing to submit a PR to fix this issue

markostanimirovic commented 1 year ago

Thoughts on defining RouteMeta type like this?

Differences compared to the original Angular Route interface:

brandonroberts commented 1 year ago

Looks reasonable to be. Should we not extend/override the Angular Route instead of recreating the route type for DefaultRouteMeta?

markostanimirovic commented 1 year ago

Looks reasonable to be. Should we not extend/override the Angular Route instead of recreating the route type for DefaultRouteMeta?

@brandonroberts We can define DefaultRouteMeta like this and override only weakly typed route props:

type OmittedRouteProps =
  | 'path'
  | 'pathMatch'
  | 'matcher'
  | 'redirectTo'
  | 'component'
  | 'loadComponent'
  | 'children'
  | 'loadChildren'
  | 'canLoad'
  | 'outlet';

interface DefaultRouteMeta extends Omit<Route, OmittedRouteProps> {
  canActivate?: CanActivateFn[];
  canActivateChild?: CanActivateChildFn[];
  canDeactivate?: CanDeactivateFn<unknown>[];
  canMatch?: CanMatchFn[];
  resolve?: { [key: string | symbol]: ResolveFn<unknown> };
  title?: string | ResolveFn<string>;
}

interface RedirectRouteMeta {
  redirectTo: string;
  pathMatch?: Route['pathMatch'];
}

export type RouteMeta =
  // enforce exclusive union
  | (DefaultRouteMeta & { redirectTo?: never })
  | RedirectRouteMeta;
brandonroberts commented 1 year ago

👍