JSON to client (#5553)
This commit is contained in:
@@ -22,6 +22,7 @@ import env from "~/env";
|
||||
import type {
|
||||
FetchOptions,
|
||||
PaginationParams,
|
||||
PartialWithId,
|
||||
Properties,
|
||||
SearchResult,
|
||||
} from "~/types";
|
||||
@@ -473,6 +474,14 @@ export default class DocumentsStore extends Store<Document> {
|
||||
return this.data.get(res.data.id);
|
||||
};
|
||||
|
||||
override fetch = (id: string, options: FetchOptions = {}) =>
|
||||
super.fetch(
|
||||
id,
|
||||
options,
|
||||
(res: { data: { document: PartialWithId<Document> } }) =>
|
||||
res.data.document
|
||||
);
|
||||
|
||||
@action
|
||||
fetchWithSharedTree = async (
|
||||
id: string,
|
||||
@@ -507,7 +516,6 @@ export default class DocumentsStore extends Store<Document> {
|
||||
const res = await client.post("/documents.info", {
|
||||
id,
|
||||
shareId: options.shareId,
|
||||
apiVersion: 2,
|
||||
});
|
||||
|
||||
invariant(res?.data, "Document not available");
|
||||
|
||||
@@ -35,7 +35,6 @@ export default class RevisionsStore extends Store<Revision> {
|
||||
id: "latest",
|
||||
documentId: document.id,
|
||||
title: document.title,
|
||||
text: document.text,
|
||||
createdAt: document.updatedAt,
|
||||
createdBy: document.createdBy,
|
||||
},
|
||||
|
||||
@@ -229,7 +229,11 @@ export default abstract class Store<T extends Model> {
|
||||
}
|
||||
|
||||
@action
|
||||
async fetch(id: string, options: JSONObject = {}): Promise<T> {
|
||||
async fetch(
|
||||
id: string,
|
||||
options: JSONObject = {},
|
||||
accessor = (res: unknown) => (res as { data: PartialWithId<T> }).data
|
||||
): Promise<T> {
|
||||
if (!this.actions.includes(RPCAction.Info)) {
|
||||
throw new Error(`Cannot fetch ${this.modelName}`);
|
||||
}
|
||||
@@ -248,7 +252,7 @@ export default abstract class Store<T extends Model> {
|
||||
return runInAction(`info#${this.modelName}`, () => {
|
||||
invariant(res?.data, "Data should be available");
|
||||
this.addPolicies(res.policies);
|
||||
return this.add(res.data);
|
||||
return this.add(accessor(res));
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof AuthorizationError || err instanceof NotFoundError) {
|
||||
|
||||
Reference in New Issue
Block a user