49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import {
|
|
type PageSubtreeProjection,
|
|
} from "@/lib/documents/page-subtree";
|
|
|
|
export type DocumentContentResponseLike = {
|
|
content?: unknown;
|
|
revision?: number | null;
|
|
conflict_detection_key?: string | null;
|
|
conflictDetectionKey?: string | null;
|
|
page_subtree?: PageSubtreeProjection | null;
|
|
pageSubtree?: PageSubtreeProjection | null;
|
|
title?: string | null;
|
|
};
|
|
|
|
export type NormalizedDocumentContentResponse = {
|
|
content: unknown;
|
|
revision: number;
|
|
conflictDetectionKey: string;
|
|
pageSubtree: PageSubtreeProjection | null;
|
|
};
|
|
|
|
export function normalizeDocumentContentResponse(input: {
|
|
documentId: string;
|
|
title?: string | null;
|
|
payload?: DocumentContentResponseLike | null;
|
|
}): NormalizedDocumentContentResponse {
|
|
const payload = input.payload ?? null;
|
|
const content = payload?.content ?? null;
|
|
const revision =
|
|
typeof payload?.revision === "number" && Number.isInteger(payload.revision)
|
|
? payload.revision
|
|
: 0;
|
|
const conflictDetectionKey = (
|
|
typeof payload?.conflictDetectionKey === "string" && payload.conflictDetectionKey.trim()
|
|
? payload.conflictDetectionKey
|
|
: typeof payload?.conflict_detection_key === "string" && payload.conflict_detection_key.trim()
|
|
? payload.conflict_detection_key
|
|
: `${input.documentId}:0`
|
|
);
|
|
const pageSubtree = payload?.pageSubtree ?? payload?.page_subtree ?? null;
|
|
|
|
return {
|
|
content,
|
|
revision,
|
|
conflictDetectionKey,
|
|
pageSubtree,
|
|
};
|
|
}
|