1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 07:27:39 +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

29
js/dist/forum.js vendored
View File

@@ -15763,7 +15763,7 @@ var Model = /*#__PURE__*/function () {
; ;
_proto.attribute = function attribute(_attribute) { _proto.attribute = function attribute(_attribute) {
return this.data.attributes[_attribute]; return this.data.attributes && this.data.attributes[_attribute];
} }
/** /**
* Merge new data into this model locally. * Merge new data into this model locally.
@@ -15777,23 +15777,23 @@ var Model = /*#__PURE__*/function () {
// Since most of the top-level items in a resource object are objects // Since most of the top-level items in a resource object are objects
// (e.g. relationships, attributes), we'll need to check and perform the // (e.g. relationships, attributes), we'll need to check and perform the
// merge at the second level if that's the case. // merge at the second level if that's the case.
for (var key in data) { for (var _key in data) {
if (typeof data[key] === 'object') { if (typeof data[_key] === 'object') {
this.data[key] = this.data[key] || {}; // For every item in a second-level object, we want to check if we've this.data[_key] = this.data[_key] || {}; // For every item in a second-level object, we want to check if we've
// been handed a Model instance. If so, we will convert it to a // been handed a Model instance. If so, we will convert it to a
// relationship data object. // relationship data object.
for (var innerKey in data[key]) { for (var innerKey in data[_key]) {
if (data[key][innerKey] instanceof Model) { if (data[_key][innerKey] instanceof Model) {
data[key][innerKey] = { data[_key][innerKey] = {
data: Model.getIdentifier(data[key][innerKey]) data: Model.getIdentifier(data[_key][innerKey])
}; };
} }
this.data[key][innerKey] = data[key][innerKey]; this.data[_key][innerKey] = data[_key][innerKey];
} }
} else { } else {
this.data[key] = data[key]; this.data[_key] = data[_key];
} }
} // Now that we've updated the data, we can say that the model is fresh. } // Now that we've updated the data, we can say that the model is fresh.
// This is an easy way to invalidate retained subtrees etc. // This is an easy way to invalidate retained subtrees etc.
@@ -15833,8 +15833,7 @@ var Model = /*#__PURE__*/function () {
var data = { var data = {
type: this.data.type, type: this.data.type,
id: this.data.id, id: this.data.id,
attributes: attributes, attributes: attributes
relationships: undefined
}; // If a 'relationships' key exists, extract it from the attributes hash and }; // If a 'relationships' key exists, extract it from the attributes hash and
// set it on the top-level data object instead. We will be sending this data // set it on the top-level data object instead. We will be sending this data
// object to the API for persistence. // object to the API for persistence.
@@ -15842,9 +15841,9 @@ var Model = /*#__PURE__*/function () {
if (attributes.relationships) { if (attributes.relationships) {
data.relationships = {}; data.relationships = {};
for (var key in attributes.relationships) { for (var _key2 in attributes.relationships) {
var model = attributes.relationships[key]; var model = attributes.relationships[_key2];
data.relationships[key] = { data.relationships[_key2] = {
data: model instanceof Array ? model.map(Model.getIdentifier) : Model.getIdentifier(model) data: model instanceof Array ? model.map(Model.getIdentifier) : Model.getIdentifier(model)
}; };
} }

File diff suppressed because one or more lines are too long

View File

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

View File

@@ -24,7 +24,7 @@ export default class Session {
/** /**
* Attempt to log in a user. * 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( return app.request(
Object.assign( 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 * 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. * 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 * 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 * @return The model, or null if no model class has been
* registered for this resource type. * registered for this resource type.
*/ */
pushObject(data): Model { pushObject(data): Model | null {
if (!this.models[data.type]) return null; if (!this.models[data.type]) return null;
const type = (this.data[data.type] = this.data[data.type] || {}); const type = (this.data[data.type] = this.data[data.type] || {});
@@ -87,7 +87,7 @@ export default class Store {
url += `/${id}`; url += `/${id}`;
} }
return app return <Promise<T[]>>app
.request( .request(
Object.assign( Object.assign(
{ {