1
0
mirror of https://github.com/flarum/core.git synced 2025-08-25 09:22:52 +02:00

common: add more typings to Model, fix type issues with Session and Store

This commit is contained in:
David Sevilla Martin
2020-03-14 10:30:47 -04:00
parent cc6619466e
commit 4484f3e35f
5 changed files with 38 additions and 29 deletions

View File

@@ -6,11 +6,21 @@
*/
import Store from './Store';
export interface Identifier {
type: string;
id: string;
}
export interface Data extends Identifier {
attributes?: { [key: string]: any };
relationships?: { [key: string]: { data: Identifier | Identifier[] } };
}
export default class Model {
/**
* The resource object from the API.
*/
data: any;
data: Data;
payload: any;
@@ -56,7 +66,7 @@ export default class Model {
* @final
*/
attribute(attribute: string): any {
return this.data.attributes[attribute];
return this.data.attributes && this.data.attributes[attribute];
}
/**
@@ -110,7 +120,7 @@ export default class Model {
* @return {Promise}
*/
save(attributes: any, options: any = {}): Promise<Model | Model[]> {
const data = {
const data: Data = {
type: this.data.type,
id: this.data.id,
attributes,
@@ -223,7 +233,7 @@ export default class Model {
* @param [transform] A function to transform the attribute value
*/
static attribute(name: string, transform?: Function): () => any {
return function() {
return function(this: Model) {
const value = this.data.attributes && this.data.attributes[name];
return transform ? transform(value) : value;
@@ -239,7 +249,7 @@ export default class Model {
* has not been loaded; or the model if it has been loaded.
*/
static hasOne(name: string): () => Model | boolean {
return function() {
return function(this: Model) {
if (this.data.relationships) {
const relationship = this.data.relationships[name];
@@ -260,8 +270,8 @@ export default class Model {
* exists; an array if it does, containing models if they have been
* loaded, and undefined for those that have not.
*/
static hasMany(name: string): () => [] | boolean {
return function() {
static hasMany(name: string): () => any[] | false {
return function(this: Model) {
if (this.data.relationships) {
const relationship = this.data.relationships[name];
@@ -277,14 +287,14 @@ export default class Model {
/**
* Transform the given value into a Date object.
*/
static transformDate(value: string): Date {
static transformDate(value: string): Date | null {
return value ? new Date(value) : null;
}
/**
* Get a resource identifier object for the given model.
*/
protected static getIdentifier(model: Model): { type: string; id: string } {
protected static getIdentifier(model: Model): Identifier {
return {
type: model.data.type,
id: model.data.id,

View File

@@ -24,7 +24,7 @@ export default class Session {
/**
* Attempt to log in a user.
*/
login(body: { identification: string; password: string; remember?: string }, options = {}) {
login(body: { identification: string; password: string; remember?: boolean }, options = {}) {
return app.request(
Object.assign(
{

View File

@@ -9,7 +9,7 @@ export default class Store {
* The local data store. A tree of resource types to IDs, such that
* accessing data[type][id] will return the model for that type/ID.
*/
data: { [key: string]: { [key: number]: Model } } = {};
data: { [key: string]: Model[] } = {};
/**
* The model registry. A map of resource types to the model class that
@@ -49,7 +49,7 @@ export default class Store {
* @return The model, or null if no model class has been
* registered for this resource type.
*/
pushObject(data): Model {
pushObject(data): Model | null {
if (!this.models[data.type]) return null;
const type = (this.data[data.type] = this.data[data.type] || {});
@@ -87,7 +87,7 @@ export default class Store {
url += `/${id}`;
}
return app
return <Promise<T[]>>app
.request(
Object.assign(
{