Review changes, make Model.store non-nullable, include meta in APIPayload signatures

This commit is contained in:
Alexander Skvortsov 2021-11-25 15:26:56 -05:00
parent b85aa403cc
commit b0504597da
No known key found for this signature in database
GPG Key ID: C4E3BBF9C3412B4C
2 changed files with 14 additions and 16 deletions

View File

@ -2,16 +2,16 @@ import app from '../common/app';
import { FlarumRequestOptions } from './Application'; import { FlarumRequestOptions } from './Application';
import Store, { ApiPayloadSingle, ApiResponseSingle } from './Store'; import Store, { ApiPayloadSingle, ApiResponseSingle } from './Store';
interface ModelIdentifier { export interface ModelIdentifier {
type: string; type: string;
id: string; id: string;
} }
interface ModelAttributes { export interface ModelAttributes {
[key: string]: unknown; [key: string]: unknown;
} }
interface ModelRelationships { export interface ModelRelationships {
[relationship: string]: { [relationship: string]: {
data: ModelIdentifier | ModelIdentifier[]; data: ModelIdentifier | ModelIdentifier[];
}; };
@ -66,13 +66,13 @@ export default abstract class Model {
/** /**
* The data store that this resource should be persisted to. * The data store that this resource should be persisted to.
*/ */
protected store: Store | null; protected store: Store;
/** /**
* @param data A resource object from the API. * @param data A resource object from the API.
* @param store The data store that this model should be persisted to. * @param store The data store that this model should be persisted to.
*/ */
constructor(data: ModelData = {}, store = null) { constructor(data: ModelData = {}, store = app.store) {
this.data = data; this.data = data;
this.store = store; this.store = store;
} }
@ -218,10 +218,6 @@ export default abstract class Model {
// model exists now (if it didn't already), and we'll push the data that // model exists now (if it didn't already), and we'll push the data that
// the API returned into the store. // the API returned into the store.
(payload) => { (payload) => {
if (!this.store) {
throw new Error('Model has no store');
}
return this.store.pushPayload<this>(payload); return this.store.pushPayload<this>(payload);
}, },
@ -257,11 +253,7 @@ export default abstract class Model {
.then(() => { .then(() => {
this.exists = false; this.exists = false;
if (this.store) { this.store.remove(this);
this.store.remove(this);
} else {
throw new Error('Tried to delete a model without a store!');
}
}); });
} }
@ -319,7 +311,7 @@ export default abstract class Model {
} }
if (relationshipData) { if (relationshipData) {
return app.store.getById<M>(relationshipData.type, relationshipData.id) as M; return this.store.getById<M>(relationshipData.type, relationshipData.id) as M;
} }
} }
@ -345,7 +337,7 @@ export default abstract class Model {
} }
if (relationshipData) { if (relationshipData) {
return relationshipData.map((data) => app.store.getById<M>(data.type, data.id)); return relationshipData.map((data) => this.store.getById<M>(data.type, data.id));
} }
} }

View File

@ -6,6 +6,9 @@ export interface ApiQueryParamsSingle {
fields?: string[]; fields?: string[];
include?: string; include?: string;
bySlug?: boolean; bySlug?: boolean;
meta?: {
[key: string]: any;
};
} }
export interface ApiQueryParamsPlural { export interface ApiQueryParamsPlural {
@ -22,6 +25,9 @@ export interface ApiQueryParamsPlural {
size?: number; size?: number;
}; };
sort?: string; sort?: string;
meta?: {
[key: string]: any;
};
} }
export type ApiQueryParams = ApiQueryParamsPlural | ApiQueryParamsSingle; export type ApiQueryParams = ApiQueryParamsPlural | ApiQueryParamsSingle;