-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathbookmarks.ts
More file actions
98 lines (86 loc) · 3.67 KB
/
bookmarks.ts
File metadata and controls
98 lines (86 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import type { MutationOptions } from '../write/write.js';
import { normalizeMutationOptions } from '../write/write.js';
import { DocumentApiValidationError } from '../errors.js';
import { assertTargetPresent } from '../validation-primitives.js';
import { validateStoryLocator } from '../validation/story-validator.js';
import type {
BookmarkAddress,
BookmarkGetInput,
BookmarkInfo,
BookmarkInsertInput,
BookmarkRenameInput,
BookmarkRemoveInput,
BookmarkMutationResult,
BookmarkListInput,
BookmarksListResult,
} from './bookmarks.types.js';
// ---------------------------------------------------------------------------
// Adapter / API interface
// ---------------------------------------------------------------------------
export interface BookmarksApi {
list(query?: BookmarkListInput): BookmarksListResult;
get(input: BookmarkGetInput): BookmarkInfo;
insert(input: BookmarkInsertInput, options?: MutationOptions): BookmarkMutationResult;
rename(input: BookmarkRenameInput, options?: MutationOptions): BookmarkMutationResult;
remove(input: BookmarkRemoveInput, options?: MutationOptions): BookmarkMutationResult;
}
export type BookmarksAdapter = BookmarksApi;
// ---------------------------------------------------------------------------
// Target validation
// ---------------------------------------------------------------------------
function validateBookmarkTarget(target: unknown, operationName: string): asserts target is BookmarkAddress {
assertTargetPresent(target, operationName);
const t = target as Record<string, unknown>;
if (t.kind !== 'entity' || t.entityType !== 'bookmark' || typeof t.name !== 'string') {
throw new DocumentApiValidationError(
'INVALID_TARGET',
`${operationName} target must be a BookmarkAddress with kind 'entity', entityType 'bookmark', and a string name.`,
{ target },
);
}
if (t.story !== undefined) {
validateStoryLocator(t.story, `${operationName}.target.story`);
}
}
// ---------------------------------------------------------------------------
// Execute wrappers
// ---------------------------------------------------------------------------
export function executeBookmarksList(adapter: BookmarksAdapter, query?: BookmarkListInput): BookmarksListResult {
if (query?.in !== undefined) {
validateStoryLocator(query.in, 'bookmarks.list.in');
}
return adapter.list(query);
}
export function executeBookmarksGet(adapter: BookmarksAdapter, input: BookmarkGetInput): BookmarkInfo {
validateBookmarkTarget(input.target, 'bookmarks.get');
return adapter.get(input);
}
export function executeBookmarksInsert(
adapter: BookmarksAdapter,
input: BookmarkInsertInput,
options?: MutationOptions,
): BookmarkMutationResult {
if (!input.name || typeof input.name !== 'string') {
throw new DocumentApiValidationError('INVALID_INPUT', 'bookmarks.insert requires a non-empty name string.');
}
return adapter.insert(input, normalizeMutationOptions(options));
}
export function executeBookmarksRename(
adapter: BookmarksAdapter,
input: BookmarkRenameInput,
options?: MutationOptions,
): BookmarkMutationResult {
validateBookmarkTarget(input.target, 'bookmarks.rename');
if (!input.newName || typeof input.newName !== 'string') {
throw new DocumentApiValidationError('INVALID_INPUT', 'bookmarks.rename requires a non-empty newName string.');
}
return adapter.rename(input, normalizeMutationOptions(options));
}
export function executeBookmarksRemove(
adapter: BookmarksAdapter,
input: BookmarkRemoveInput,
options?: MutationOptions,
): BookmarkMutationResult {
validateBookmarkTarget(input.target, 'bookmarks.remove');
return adapter.remove(input, normalizeMutationOptions(options));
}