mirror of
https://github.com/flarum/framework.git
synced 2025-01-19 07:42:48 +08:00
Convert some common classes/utils to TS (#2929)
* Convert common/Session * Update common/states/AlertManagerState * Convert common/utils/extractText Co-authored-by: David Wheatley <hi@davwheat.dev>
This commit is contained in:
parent
458d8dea18
commit
5b14838770
|
@ -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}`;
|
||||
}
|
||||
}
|
55
framework/core/js/src/common/Session.ts
Normal file
55
framework/core/js/src/common/Session.ts
Normal file
|
@ -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<FlarumRequestOptions<any>, '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}`;
|
||||
}
|
||||
}
|
|
@ -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];
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user