-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate.ts
More file actions
50 lines (44 loc) · 1.47 KB
/
create.ts
File metadata and controls
50 lines (44 loc) · 1.47 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
import { Command } from '@cliffy/command';
import { handleCommandError } from '@/lib/errors.ts';
import { createAuthenticatedMuxClient } from '@/lib/mux.ts';
interface CreateOptions {
date: number;
note: string;
subPropertyId?: string;
json?: boolean;
}
export const createCommand = new Command()
.description('Create a new annotation in Mux Data')
.option('--date <date:number>', 'Unix timestamp for the annotation date', {
required: true,
})
.option('--note <note:string>', 'Note text for the annotation', {
required: true,
})
.option(
'--sub-property-id <subPropertyId:string>',
'Sub-property ID to associate with the annotation',
)
.option('--json', 'Output JSON instead of pretty format')
.action(async (options: CreateOptions) => {
try {
const mux = await createAuthenticatedMuxClient();
const body: Record<string, unknown> = {
date: options.date,
note: options.note,
};
if (options.subPropertyId) {
body.sub_property_id = options.subPropertyId;
}
const annotation = await mux.data.annotations.create(body as never);
if (options.json) {
console.log(JSON.stringify(annotation, null, 2));
} else {
console.log(`Annotation ID: ${annotation.id}`);
console.log(` Date: ${annotation.date}`);
console.log(` Note: ${annotation.note}`);
}
} catch (error) {
await handleCommandError(error, 'annotations', 'create', options);
}
});