mirror of
https://github.com/flarum/framework.git
synced 2025-02-22 00:31:11 +08:00
common: add more typings to Model, fix type issues with Session and Store
This commit is contained in:
parent
cc6619466e
commit
4484f3e35f
29
js/dist/forum.js
vendored
29
js/dist/forum.js
vendored
@ -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)
|
||||
};
|
||||
}
|
||||
|
2
js/dist/forum.js.map
vendored
2
js/dist/forum.js.map
vendored
File diff suppressed because one or more lines are too long
@ -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,
|
||||
|
@ -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(
|
||||
{
|
||||
|
@ -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(
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user