Niedzielski has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/381917 )
Change subject: Demo: use types instead of data for RouteConfig
......................................................................
Demo: use types instead of data for RouteConfig
Change-Id: I65812adb3b0d41be74300d869d544a818182bafa
---
M src/common/routers/api.ts
M src/common/routers/route.ts
2 files changed, 83 insertions(+), 42 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/marvin refs/changes/17/381917/1
diff --git a/src/common/routers/api.ts b/src/common/routers/api.ts
index 0507147..a8a4471 100644
--- a/src/common/routers/api.ts
+++ b/src/common/routers/api.ts
@@ -5,42 +5,89 @@
} from "../pages/summary";
import { AnyRoute, NoPropsRoute, Route, newRoute } from "./route";
-export const home: NoPropsRoute = newRoute({
- path: "/",
- importModule: () =>
- import(/* webpackChunkName: "pages/home" */ "../pages/home"),
- chunkName: "pages/home"
-});
+class HomeRouteConfig {
+ get path() {
+ return "/";
+ }
+ get chunkName() {
+ return "pages/home";
+ }
+ importModule() {
+ return import(/* webpackChunkName: "pages/home" */ "../pages/home");
+ }
+}
+export const home: NoPropsRoute = newRoute(new HomeRouteConfig());
-export const about: NoPropsRoute = newRoute({
- path: "/about",
- importModule: () =>
- import(/* webpackChunkName: "pages/about" */ "../pages/about"),
- chunkName: "pages/about"
-});
+class AboutRouteConfig {
+ get path() {
+ return "/about";
+ }
+ get chunkName() {
+ return "pages/about";
+ }
+ get importModule() {
+ return import(/* webpackChunkName: "pages/about" */ "../pages/about");
+ }
+ get stattttttus() {
+ return 404; // fail
+ }
+}
+export const about: NoPropsRoute = newRoute(new AboutRouteConfig());
-export const summary: Route<SummaryParams, SummaryProps> = newRoute({
- path: "/page/summary/:title",
- importModule: () =>
- import(/* webpackChunkName: "pages/summary" */ "../pages/summary"),
- chunkName: "pages/summary"
-});
+class SummaryRouteConfig {
+ get path() {
+ return "/page/summary/:title";
+ }
+ get chunkName() {
+ return "pages/summary";
+ }
+ importModule() {
+ return import(/* webpackChunkName: "pages/summary" */ "../pages/summary");
+ }
+}
+export const summary: Route<SummaryParams, SummaryProps> = newRoute(
+ new SummaryRouteConfig()
+);
-export const styleGuide: NoPropsRoute = newRoute({
- path: "/dev/style-guide",
- importModule: () =>
- import(/* webpackChunkName: "pages/style-guide" */ "../pages/style-guide"),
- chunkName: "pages/style-guide"
-});
+class StyleGuideRouteConfig {
+ get path() {
+ return "/dev/style-guide";
+ }
+ get chunkName() {
+ return "pages/style-guide";
+ }
+ importModule() {
+ // eslint-disable-next-line max-len
+ return import(/* webpackChunkName: "pages/style-guide" */
"../pages/style-guide");
+ }
+}
+export const styleGuide: NoPropsRoute = newRoute(new StyleGuideRouteConfig());
-export const notFound: Route<NotFoundParams> = newRoute({
- // `(.*)` is the new `*`. See
- // https://github.com/pillarjs/path-to-regexp/issues/37.
- path: "(.*)",
- importModule: () =>
- import(/* webpackChunkName: "pages/not-found" */ "../pages/not-found"),
- chunkName: "pages/not-found",
- status: 404
-});
+class NotFoundRouteConfig {
+ // `(.*)` is the new `*`. See
https://github.com/pillarjs/path-to-regexp/issues/37.
+ get path() {
+ return "(.*)";
+ }
+ get chunkName() {
+ return "pages/not-found";
+ }
+ get status() {
+ return 404;
+ }
+ importModule() {
+ // eslint-disable-next-line max-len
+ return import(/* webpackChunkName: "pages/not-found" */
"../pages/not-found");
+ }
+}
+export const notFound: Route<NotFoundParams> = newRoute(
+ new NotFoundRouteConfig()
+);
+
+export type AnyRouteConfig =
+ | HomeRouteConfig
+ | AboutRouteConfig
+ | SummaryRouteConfig
+ | StyleGuideRouteConfig
+ | NotFoundRouteConfig;
export const routes: AnyRoute[] = [home, about, summary, styleGuide, notFound];
diff --git a/src/common/routers/route.ts b/src/common/routers/route.ts
index 443cf75..c672000 100644
--- a/src/common/routers/route.ts
+++ b/src/common/routers/route.ts
@@ -1,5 +1,6 @@
import * as pathToRegExp from "path-to-regexp";
import { AnyComponent } from "../components/preact-utils";
+import { AnyRouteConfig } from "./api";
/**
* A map of path-to-regexp router path names to matches. The keys must match
@@ -34,7 +35,7 @@
| { getInitialProps?: undefined; Component: AnyComponent<undefined, any> };
/** A plain configuration used to generate a Route. */
-export interface RouteConfig<
+export interface Route<
Params extends RouteParams | undefined = undefined,
Props = undefined
> {
@@ -51,13 +52,6 @@
chunkName: string;
/** The request status if import and request for properties succeed. */
- status?: number;
-}
-
-export interface Route<
- Params extends RouteParams | undefined = undefined,
- Props = undefined
-> extends RouteConfig<Params, Props> {
status: number;
/**
@@ -110,7 +104,7 @@
importModule,
chunkName,
status = 200
-}: RouteConfig<Params, Props>): Route<Params, Props> {
+}: AnyRouteConfig & { status?: number }): Route<Params, Props> {
const paramNames: pathToRegExp.Key[] = [];
const pathRegExp = pathToRegExp(path, paramNames);
return {
--
To view, visit https://gerrit.wikimedia.org/r/381917
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I65812adb3b0d41be74300d869d544a818182bafa
Gerrit-PatchSet: 1
Gerrit-Project: marvin
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <[email protected]>
Gerrit-Reviewer: Sniedzielski <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits