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

@@ -10,6 +10,7 @@ import {
NavigationNode,
} from "@shared/types";
import Collection from "~/models/Collection";
import { Properties } from "~/types";
import { client } from "~/utils/ApiClient";
import RootStore from "./RootStore";
import Store from "./base/Store";
@@ -165,14 +166,14 @@ export default class CollectionsStore extends Store<Collection> {
}
};
async update(params: Record<string, any>): Promise<Collection> {
async update(params: Properties<Collection>): Promise<Collection> {
const result = await super.update(params);
// If we're changing sharing permissions on the collection then we need to
// remove all locally cached policies for documents in the collection as they
// are now invalid
if (params.sharing !== undefined) {
this.rootStore.documents.inCollection(params.id).forEach((document) => {
this.rootStore.documents.inCollection(result.id).forEach((document) => {
this.rootStore.policies.remove(document.id);
});
}

View File

@@ -87,7 +87,7 @@ export default class CommentsStore extends Store<Comment> {
documentId,
...options,
});
invariant(res && res.data, "Comment list not available");
invariant(res?.data, "Comment list not available");
runInAction("CommentsStore#fetchDocumentComments", () => {
res.data.forEach(this.add);

View File

@@ -5,7 +5,12 @@ import find from "lodash/find";
import omitBy from "lodash/omitBy";
import orderBy from "lodash/orderBy";
import { observable, action, computed, runInAction } from "mobx";
import { DateFilter, NavigationNode, PublicTeam } from "@shared/types";
import type {
DateFilter,
JSONObject,
NavigationNode,
PublicTeam,
} from "@shared/types";
import { subtractDate } from "@shared/utils/date";
import { bytesToHumanReadable } from "@shared/utils/files";
import naturalSort from "@shared/utils/naturalSort";
@@ -13,7 +18,12 @@ import RootStore from "~/stores/RootStore";
import Store from "~/stores/base/Store";
import Document from "~/models/Document";
import env from "~/env";
import { FetchOptions, PaginationParams, SearchResult } from "~/types";
import type {
FetchOptions,
PaginationParams,
Properties,
SearchResult,
} from "~/types";
import { client } from "~/utils/ApiClient";
import { extname } from "~/utils/files";
@@ -704,8 +714,8 @@ export default class DocumentsStore extends Store<Document> {
@action
async update(
params: Partial<Document>,
options?: Record<string, string | boolean | number | undefined>
params: Properties<Document>,
options?: JSONObject
): Promise<Document> {
this.isSaving = true;

View File

@@ -43,7 +43,7 @@ export default class NotificationsStore extends Store<Notification> {
@action
markAllAsRead = async () => {
await client.post("/notifications.update_all", {
viewedAt: new Date(),
viewedAt: new Date().toISOString(),
});
runInAction("NotificationsStore#markAllAsRead", () => {

View File

@@ -4,7 +4,10 @@ import find from "lodash/find";
import isUndefined from "lodash/isUndefined";
import sortBy from "lodash/sortBy";
import { action, computed } from "mobx";
import type { Required } from "utility-types";
import type { JSONObject } from "@shared/types";
import Share from "~/models/Share";
import type { Properties } from "~/types";
import { client } from "~/utils/ApiClient";
import RootStore from "./RootStore";
import Store, { RPCAction } from "./base/Store";
@@ -40,7 +43,7 @@ export default class SharesStore extends Store<Share> {
};
@action
async create(params: Record<string, any>) {
async create(params: Required<Properties<Share>, "documentId">) {
const item = this.getByDocumentId(params.documentId);
if (item) {
return item;
@@ -49,10 +52,7 @@ export default class SharesStore extends Store<Share> {
}
@action
async fetch(
documentId: string,
options: Record<string, any> = {}
): Promise<any> {
async fetch(documentId: string, options: JSONObject = {}): Promise<any> {
const item = this.getByDocumentId(documentId);
if (item && !options.force) {
return item;

View File

@@ -2,7 +2,7 @@ import invariant from "invariant";
import filter from "lodash/filter";
import orderBy from "lodash/orderBy";
import { observable, computed, action, runInAction } from "mobx";
import { UserRole } from "@shared/types";
import { type JSONObject, UserRole } from "@shared/types";
import User from "~/models/User";
import { client } from "~/utils/ApiClient";
import RootStore from "./RootStore";
@@ -179,7 +179,7 @@ export default class UsersStore extends Store<User> {
};
@action
async delete(user: User, options: Record<string, any> = {}) {
async delete(user: User, options: JSONObject = {}) {
await super.delete(user, options);
if (!user.isSuspended && user.lastActiveAt) {

View File

@@ -5,11 +5,12 @@ import orderBy from "lodash/orderBy";
import { observable, action, computed, runInAction } from "mobx";
import pluralize from "pluralize";
import { Pagination } from "@shared/constants";
import { type JSONObject } from "@shared/types";
import RootStore from "~/stores/RootStore";
import Policy from "~/models/Policy";
import Model from "~/models/base/Model";
import { getInverseRelationsForModelClass } from "~/models/decorators/Relation";
import { PaginationParams, PartialWithId } from "~/types";
import type { PaginationParams, PartialWithId, Properties } from "~/types";
import { client } from "~/utils/ApiClient";
import { AuthorizationError, NotFoundError } from "~/utils/errors";
@@ -125,12 +126,9 @@ export default abstract class Store<T extends Model> {
this.data.delete(id);
}
save(
params: Partial<T>,
options: Record<string, string | boolean | number | undefined> = {}
): Promise<T> {
save(params: Properties<T>, options: JSONObject = {}): Promise<T> {
const { isNew, ...rest } = options;
if (isNew || !params.id) {
if (isNew || !("id" in params)) {
return this.create(params, rest);
}
return this.update(params, rest);
@@ -141,10 +139,7 @@ export default abstract class Store<T extends Model> {
}
@action
async create(
params: Partial<T>,
options?: Record<string, string | boolean | number | undefined>
): Promise<T> {
async create(params: Properties<T>, options?: JSONObject): Promise<T> {
if (!this.actions.includes(RPCAction.Create)) {
throw new Error(`Cannot create ${this.modelName}`);
}
@@ -168,10 +163,7 @@ export default abstract class Store<T extends Model> {
}
@action
async update(
params: Partial<T>,
options?: Record<string, string | boolean | number | undefined>
): Promise<T> {
async update(params: Properties<T>, options?: JSONObject): Promise<T> {
if (!this.actions.includes(RPCAction.Update)) {
throw new Error(`Cannot update ${this.modelName}`);
}
@@ -195,7 +187,7 @@ export default abstract class Store<T extends Model> {
}
@action
async delete(item: T, options: Record<string, any> = {}) {
async delete(item: T, options: JSONObject = {}) {
if (!this.actions.includes(RPCAction.Delete)) {
throw new Error(`Cannot delete ${this.modelName}`);
}
@@ -218,7 +210,7 @@ export default abstract class Store<T extends Model> {
}
@action
async fetch(id: string, options: Record<string, any> = {}): Promise<T> {
async fetch(id: string, options: JSONObject = {}): Promise<T> {
if (!this.actions.includes(RPCAction.Info)) {
throw new Error(`Cannot fetch ${this.modelName}`);
}