From b0504597da5da8a65ef57df95286f831824e0bc5 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Thu, 25 Nov 2021 15:26:56 -0500 Subject: [PATCH] Review changes, make Model.store non-nullable, include meta in APIPayload signatures --- js/src/common/Model.ts | 24 ++++++++---------------- js/src/common/Store.ts | 6 ++++++ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/js/src/common/Model.ts b/js/src/common/Model.ts index 06566b68b..160ec0eb0 100644 --- a/js/src/common/Model.ts +++ b/js/src/common/Model.ts @@ -2,16 +2,16 @@ import app from '../common/app'; import { FlarumRequestOptions } from './Application'; import Store, { ApiPayloadSingle, ApiResponseSingle } from './Store'; -interface ModelIdentifier { +export interface ModelIdentifier { type: string; id: string; } -interface ModelAttributes { +export interface ModelAttributes { [key: string]: unknown; } -interface ModelRelationships { +export interface ModelRelationships { [relationship: string]: { data: ModelIdentifier | ModelIdentifier[]; }; @@ -66,13 +66,13 @@ export default abstract class Model { /** * 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 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.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 // the API returned into the store. (payload) => { - if (!this.store) { - throw new Error('Model has no store'); - } - return this.store.pushPayload(payload); }, @@ -257,11 +253,7 @@ export default abstract class Model { .then(() => { this.exists = false; - if (this.store) { - this.store.remove(this); - } else { - throw new Error('Tried to delete a model without a store!'); - } + this.store.remove(this); }); } @@ -319,7 +311,7 @@ export default abstract class Model { } if (relationshipData) { - return app.store.getById(relationshipData.type, relationshipData.id) as M; + return this.store.getById(relationshipData.type, relationshipData.id) as M; } } @@ -345,7 +337,7 @@ export default abstract class Model { } if (relationshipData) { - return relationshipData.map((data) => app.store.getById(data.type, data.id)); + return relationshipData.map((data) => this.store.getById(data.type, data.id)); } } diff --git a/js/src/common/Store.ts b/js/src/common/Store.ts index a03405a95..6fb375101 100644 --- a/js/src/common/Store.ts +++ b/js/src/common/Store.ts @@ -6,6 +6,9 @@ export interface ApiQueryParamsSingle { fields?: string[]; include?: string; bySlug?: boolean; + meta?: { + [key: string]: any; + }; } export interface ApiQueryParamsPlural { @@ -22,6 +25,9 @@ export interface ApiQueryParamsPlural { size?: number; }; sort?: string; + meta?: { + [key: string]: any; + }; } export type ApiQueryParams = ApiQueryParamsPlural | ApiQueryParamsSingle;