CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/lib/api/schema/bookmarks.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2024 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { z } from "../framework";
7
8
import {
9
LoadStarredFilesBookmarksProps,
10
SaveStarredFilesBoookmarksProps,
11
} from "@cocalc/server/bookmarks/starred";
12
import { MAX_STARS, STARRED_FILES } from "@cocalc/util/consts/bookmarks";
13
import {
14
GetStarredBookmarks,
15
GetStarredBookmarksPayload,
16
SetStarredBookmarks,
17
} from "@cocalc/util/types/bookmarks";
18
import { ProjectIdSchema } from "./projects/common";
19
20
const ERROR = z.object({
21
status: z.literal("error"),
22
error: z.string(),
23
});
24
25
const COMMON_STARS = z.object({
26
project_id: ProjectIdSchema,
27
type: z.literal(STARRED_FILES),
28
});
29
30
export const BookmarkSetInputSchema = COMMON_STARS.extend({
31
stars: z
32
.string()
33
.array()
34
.max(MAX_STARS)
35
.describe("List of file paths or IDs"),
36
}).describe("Set the list of starred items to the given list of stars.");
37
38
export const BookmarkSetOutputSchema = z.union([
39
COMMON_STARS.merge(z.object({ status: z.literal("success") })),
40
ERROR,
41
]);
42
43
export const BookmarkAddInputSchema = BookmarkSetInputSchema.describe(
44
"Add a list of starred items to the list of bookmarks.",
45
);
46
export const BookmarkAddOutputSchema = BookmarkSetOutputSchema;
47
48
export const BookmarkRemoveInputSchema = BookmarkSetInputSchema.describe(
49
"Remove a list of starred items from the given list of bookmarks.",
50
);
51
export const BookmarkRemoveOutputSchema = BookmarkSetOutputSchema;
52
53
export const BookmarkGetInputSchema = COMMON_STARS.describe(
54
"Get the list of starred items for the given project ID and your account.",
55
);
56
export const BookmarkGetOutputSchema = z.union([
57
z
58
.object({
59
status: z.literal("success"),
60
stars: z
61
.array(z.string())
62
.describe(
63
"Array of IDs or file path strings, as they are in the starred tabs flyout",
64
),
65
last_edited: z
66
.number()
67
.optional()
68
.describe("UNIX epoch timestamp, when bookmark was last edited"),
69
})
70
.merge(COMMON_STARS),
71
ERROR.merge(COMMON_STARS),
72
]);
73
74
export type BookmarkSetInputType = z.infer<typeof BookmarkSetInputSchema>;
75
export type BookmarkSetOutputType = z.infer<typeof BookmarkSetOutputSchema>;
76
export type BookmarkAddInputType = z.infer<typeof BookmarkAddInputSchema>;
77
export type BookmarkAddOutputType = z.infer<typeof BookmarkRemoveOutputSchema>;
78
export type BookmarkRemoveInputType = z.infer<typeof BookmarkRemoveInputSchema>;
79
export type BookmarkRemoveOutputType = z.infer<typeof BookmarkAddOutputSchema>;
80
export type BookmarkGetInputType = z.infer<typeof BookmarkGetInputSchema>;
81
export type BookmarkGetOutputType = z.infer<typeof BookmarkGetOutputSchema>;
82
83
// consistency checks
84
export const _1: Omit<SaveStarredFilesBoookmarksProps, "mode" | "account_id"> =
85
{} as Omit<BookmarkSetInputType, typeof STARRED_FILES>;
86
87
export const _2: Omit<LoadStarredFilesBookmarksProps, "account_id"> =
88
{} as Omit<BookmarkGetInputType, typeof STARRED_FILES>;
89
90
export const _3: BookmarkGetOutputType = {} as GetStarredBookmarks;
91
92
export const _4: BookmarkSetInputType = {} as SetStarredBookmarks;
93
94
export const _5: BookmarkGetInputType = {} as GetStarredBookmarksPayload;
95
96