diff --git a/js/src/common/Session.js b/js/src/common/Session.js deleted file mode 100644 index ca89f310a9..0000000000 --- a/js/src/common/Session.js +++ /dev/null @@ -1,56 +0,0 @@ -import app from '../common/app'; - -/** - * The `Session` class defines the current user session. It stores a reference - * to the current authenticated user, and provides methods to log in/out. - */ -export default class Session { - constructor(user, csrfToken) { - /** - * The current authenticated user. - * - * @type {User|null} - * @public - */ - this.user = user; - - /** - * The CSRF token. - * - * @type {String|null} - * @public - */ - this.csrfToken = csrfToken; - } - - /** - * Attempt to log in a user. - * - * @param {String} identification The username/email. - * @param {String} password - * @param {Object} [options] - * @return {Promise} - * @public - */ - login(body, options = {}) { - return app.request( - Object.assign( - { - method: 'POST', - url: `${app.forum.attribute('baseUrl')}/login`, - body, - }, - options - ) - ); - } - - /** - * Log the user out. - * - * @public - */ - logout() { - window.location = `${app.forum.attribute('baseUrl')}/logout?token=${this.csrfToken}`; - } -} diff --git a/js/src/common/Session.ts b/js/src/common/Session.ts new file mode 100644 index 0000000000..0997bf99fc --- /dev/null +++ b/js/src/common/Session.ts @@ -0,0 +1,55 @@ +import app from '../common/app'; +import User from './models/User'; +import { FlarumRequestOptions } from './Application'; + +export type LoginParams = { + /** + * The username/email + */ + identification: string; + + /** + * Password + */ + password: string; +}; + +/** + * The `Session` class defines the current user session. It stores a reference + * to the current authenticated user, and provides methods to log in/out. + */ +export default class Session { + /** + * The current authenticated user. + */ + user: User | null; + + /** + * The CSRF token. + */ + csrfToken: string; + + constructor(user: User | null, csrfToken: string) { + this.user = user; + this.csrfToken = csrfToken; + } + + /** + * Attempt to log in a user. + */ + login(body: LoginParams, options: Omit, 'url' | 'body' | 'method'> = {}) { + return app.request({ + method: 'POST', + url: `${app.forum.attribute('baseUrl')}/login`, + body, + ...options, + }); + } + + /** + * Log the user out. + */ + logout() { + window.location.href = `${app.forum.attribute('baseUrl')}/logout?token=${this.csrfToken}`; + } +} diff --git a/js/src/common/states/AlertManagerState.ts b/js/src/common/states/AlertManagerState.ts index 2ed21e5e08..076322ac16 100644 --- a/js/src/common/states/AlertManagerState.ts +++ b/js/src/common/states/AlertManagerState.ts @@ -55,7 +55,7 @@ export default class AlertManagerState { /** * Dismiss an alert. */ - dismiss(key: AlertIdentifier): void { + dismiss(key: AlertIdentifier | null): void { if (!key || !(key in this.activeAlerts)) return; delete this.activeAlerts[key]; diff --git a/js/src/common/utils/extractText.js b/js/src/common/utils/extractText.ts similarity index 52% rename from js/src/common/utils/extractText.js rename to js/src/common/utils/extractText.ts index 9604d6a16c..e405f3c7c4 100644 --- a/js/src/common/utils/extractText.js +++ b/js/src/common/utils/extractText.ts @@ -1,15 +1,14 @@ +import type Mithril from 'mithril'; + /** * Extract the text nodes from a virtual element. - * - * @param {VirtualElement} vdom - * @return {String} */ -export default function extractText(vdom) { +export default function extractText(vdom: Mithril.Children): string { if (vdom instanceof Array) { return vdom.map((element) => extractText(element)).join(''); } else if (typeof vdom === 'object' && vdom !== null) { - return vdom.children ? extractText(vdom.children) : vdom.text; + return vdom.children ? extractText(vdom.children) : String(vdom.text); } else { - return vdom; + return String(vdom); } }