Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert routes to Typescript #3177

Merged
merged 3 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions js/src/admin/routes.js → js/src/admin/routes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AdminApplication from './AdminApplication';
import DashboardPage from './components/DashboardPage';
import BasicsPage from './components/BasicsPage';
import PermissionsPage from './components/PermissionsPage';
Expand All @@ -9,10 +10,8 @@ import ExtensionPageResolver from './resolvers/ExtensionPageResolver';

/**
* The `routes` initializer defines the forum app's routes.
*
* @param {App} app
*/
export default function (app) {
export default function (app: AdminApplication) {
app.routes = {
dashboard: { path: '/', component: DashboardPage },
basics: { path: '/basics', component: BasicsPage },
Expand Down
10 changes: 3 additions & 7 deletions js/src/common/Application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ import type { ComponentAttrs } from './Component';

export type FlarumScreens = 'phone' | 'tablet' | 'desktop' | 'desktop-hd';

export type FlarumGenericRoute = RouteItem<
Record<string, unknown>,
Component<{ routeName: string; [key: string]: unknown }>,
Record<string, unknown>
>;
export type FlarumGenericRoute = RouteItem<any, any, any>;

export interface FlarumRequestOptions<ResponseType> extends Omit<Mithril.RequestOptions<ResponseType>, 'extract'> {
errorHandler?: (error: RequestError) => void;
Expand Down Expand Up @@ -80,14 +76,14 @@ export type RouteItem<
/**
* The component to render when this route matches.
*/
component: { new (): Comp };
component: new () => Comp;
/**
* A custom resolver class.
*
* This should be the class itself, and **not** an instance of the
* class.
*/
resolverClass?: { new (): DefaultResolver<Attrs, Comp, RouteArgs> };
resolverClass?: new (component: new () => Comp, routeName: string) => DefaultResolver<Attrs, Comp, RouteArgs>;
}
| {
/**
Expand Down
4 changes: 2 additions & 2 deletions js/src/common/resolvers/DefaultResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export default class DefaultResolver<
RouteArgs extends Record<string, unknown> = {}
> implements RouteResolver<Attrs, Comp, RouteArgs>
{
component: { new (): Comp };
component: new () => Comp;
routeName: string;

constructor(component: { new (): Comp }, routeName: string) {
constructor(component: new () => Comp, routeName: string) {
this.component = component;
this.routeName = routeName;
}
Expand Down
6 changes: 5 additions & 1 deletion js/src/forum/ForumApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Composer from './components/Composer';
import DiscussionRenamedNotification from './components/DiscussionRenamedNotification';
import CommentPost from './components/CommentPost';
import DiscussionRenamedPost from './components/DiscussionRenamedPost';
import routes from './routes';
import routes, { makeRouteHelpers } from './routes';
import alertEmailConfirmation from './utils/alertEmailConfirmation';
import Application from '../common/Application';
import Navigation from '../common/components/Navigation';
Expand Down Expand Up @@ -73,10 +73,14 @@ export default class ForumApplication extends Application {
*/
discussions: DiscussionListState = new DiscussionListState({});

route: typeof Application.prototype.route & ReturnType<typeof makeRouteHelpers>;

constructor() {
super();

routes(this);

this.route = Object.assign({}, (Object.getPrototypeOf(Object.getPrototypeOf(this)) as Application).route.bind(this), makeRouteHelpers(this));
}

/**
Expand Down
66 changes: 31 additions & 35 deletions js/src/forum/routes.js → js/src/forum/routes.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import ForumApplication from './ForumApplication';
import IndexPage from './components/IndexPage';
import DiscussionPage from './components/DiscussionPage';
import PostsUserPage from './components/PostsUserPage';
import DiscussionsUserPage from './components/DiscussionsUserPage';
import SettingsPage from './components/SettingsPage';
import NotificationsPage from './components/NotificationsPage';
import DiscussionPageResolver from './resolvers/DiscussionPageResolver';
import Discussion from '../common/models/Discussion';
import Post from '../common/models/Post';
import User from '../common/models/User';

/**
* The `routes` initializer defines the forum app's routes.
*
* @param {App} app
*/
export default function (app) {
export default function (app: ForumApplication) {
app.routes = {
index: { path: '/all', component: IndexPage },

Expand All @@ -25,40 +27,34 @@ export default function (app) {
settings: { path: '/settings', component: SettingsPage },
notifications: { path: '/notifications', component: NotificationsPage },
};
}

/**
* Generate a URL to a discussion.
*
* @param {Discussion} discussion
* @param {Integer} [near]
* @return {String}
*/
app.route.discussion = (discussion, near) => {
return app.route(near && near !== 1 ? 'discussion.near' : 'discussion', {
id: discussion.slug(),
near: near && near !== 1 ? near : undefined,
});
};
export function makeRouteHelpers(app: ForumApplication) {
return {
/**
* Generate a URL to a discussion.
*/
discussion: (discussion: Discussion, near: number) => {
return app.route(near && near !== 1 ? 'discussion.near' : 'discussion', {
id: discussion.slug(),
near: near && near !== 1 ? near : undefined,
});
},

/**
* Generate a URL to a post.
*
* @param {Post} post
* @return {String}
*/
app.route.post = (post) => {
return app.route.discussion(post.discussion(), post.number());
};
/**
* Generate a URL to a post.
*/
post: (post: Post) => {
return app.route.discussion(post.discussion(), post.number());
},

/**
* Generate a URL to a user.
*
* @param {User} user
* @return {String}
*/
app.route.user = (user) => {
return app.route('user', {
username: user.slug(),
});
/**
* Generate a URL to a user.
*/
user: (user: User) => {
return app.route('user', {
username: user.slug(),
});
},
};
}