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
No known key found for this signature in database
GPG Key ID: F764F1417E16B15F
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) {
return this.data.attributes[_attribute];
return this.data.attributes && this.data.attributes[_attribute];
}
/**
* 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
// (e.g. relationships, attributes), we'll need to check and perform the
// merge at the second level if that's the case.
for (var key in data) {
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
for (var _key in data) {
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
// been handed a Model instance. If so, we will convert it to a
// relationship data object.
for (var innerKey in data[key]) {
if (data[key][innerKey] instanceof Model) {
data[key][innerKey] = {
data: Model.getIdentifier(data[key][innerKey])
for (var innerKey in data[_key]) {
if (data[_key][innerKey] instanceof Model) {
data[_key][innerKey] = {
data: Model.getIdentifier(data[_key][innerKey])
};
}
this.data[key][innerKey] = data[key][innerKey];
this.data[_key][innerKey] = data[_key][innerKey];
}
} 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.
// This is an easy way to invalidate retained subtrees etc.
@ -15833,8 +15833,7 @@ var Model = /*#__PURE__*/function () {
var data = {
type: this.data.type,
id: this.data.id,
attributes: attributes,
relationships: undefined
attributes: attributes
}; // 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
// object to the API for persistence.
@ -15842,9 +15841,9 @@ var Model = /*#__PURE__*/function () {
if (attributes.relationships) {
data.relationships = {};
for (var key in attributes.relationships) {
var model = attributes.relationships[key];
data.relationships[key] = {
for (var _key2 in attributes.relationships) {
var model = attributes.relationships[_key2];
data.relationships[_key2] = {
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';
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(
{