chore: Improve typings around model methods (#6324)

This commit is contained in:
Tom Moor
2023-12-28 20:11:27 -04:00
committed by GitHub
parent ed1f345326
commit 55a55376c6
15 changed files with 89 additions and 65 deletions

View File

@@ -1,3 +1,4 @@
import invariant from "invariant";
import trim from "lodash/trim";
import { action, computed, observable, reaction, runInAction } from "mobx";
import {
@@ -148,12 +149,13 @@ export default class Collection extends ParanoidModel {
try {
this.isFetching = true;
const { data } = await client.post("/collections.documents", {
const res = await client.post("/collections.documents", {
id: this.id,
});
invariant(res?.data, "Data should be available");
runInAction("Collection#fetchDocuments", () => {
this.documents = data;
this.documents = res.data;
});
} finally {
this.isFetching = false;

View File

@@ -3,12 +3,13 @@ import i18n, { t } from "i18next";
import floor from "lodash/floor";
import { action, autorun, computed, observable, set } from "mobx";
import { ExportContentType } from "@shared/types";
import type { NavigationNode } from "@shared/types";
import type { JSONObject, NavigationNode } from "@shared/types";
import Storage from "@shared/utils/Storage";
import { isRTL } from "@shared/utils/rtl";
import slugify from "@shared/utils/slugify";
import DocumentsStore from "~/stores/DocumentsStore";
import User from "~/models/User";
import type { Properties } from "~/types";
import { client } from "~/utils/ApiClient";
import { settingsPath } from "~/utils/routeHelpers";
import Collection from "./Collection";
@@ -17,7 +18,7 @@ import ParanoidModel from "./base/ParanoidModel";
import Field from "./decorators/Field";
import Relation from "./decorators/Relation";
type SaveOptions = {
type SaveOptions = JSONObject & {
publish?: boolean;
done?: boolean;
autosave?: boolean;
@@ -388,9 +389,9 @@ export default class Document extends ParanoidModel {
@action
save = async (
fields?: Partial<Document> | undefined,
options?: SaveOptions | undefined
) => {
fields?: Properties<typeof this>,
options?: SaveOptions
): Promise<Document> => {
const params = fields ?? this.toAPI();
this.isSaving = true;

View File

@@ -1,5 +1,6 @@
import pick from "lodash/pick";
import { set, observable, action } from "mobx";
import { JSONObject } from "@shared/types";
import type Store from "~/stores/base/Store";
import Logger from "~/utils/Logger";
import { getFieldsForModel } from "../decorators/Field";
@@ -77,7 +78,7 @@ export default abstract class Model {
save = async (
params?: Record<string, any>,
options?: Record<string, string | boolean | number | undefined>
) => {
): Promise<Model> => {
this.isSaving = true;
try {
@@ -108,7 +109,7 @@ export default abstract class Model {
}
};
updateData = action((data: any) => {
updateData = action((data: Partial<Model>) => {
for (const key in data) {
this[key] = data[key];
}
@@ -117,7 +118,7 @@ export default abstract class Model {
this.persistedAttributes = this.toAPI();
});
fetch = (options?: any) => this.store.fetch(this.id, options);
fetch = (options?: JSONObject) => this.store.fetch(this.id, options);
refresh = () =>
this.fetch({

View File

@@ -1,8 +1,8 @@
import type Model from "../base/Model";
const fields = new Map<string, string[]>();
const fields = new Map<string, (string | number | symbol)[]>();
export const getFieldsForModel = (target: Model) =>
export const getFieldsForModel = <T extends Model>(target: T) =>
fields.get(target.constructor.name) ?? [];
/**
@@ -14,10 +14,7 @@ export const getFieldsForModel = (target: Model) =>
*/
const Field = <T>(target: any, propertyKey: keyof T) => {
const className = target.constructor.name;
fields.set(className, [
...(fields.get(className) || []),
propertyKey as string,
]);
fields.set(className, [...(fields.get(className) || []), propertyKey]);
};
export default Field;