@kyoji2/intercom-cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,112 @@
1
+ import ora from "ora";
2
+ import { createClient, handleIntercomError } from "../client.ts";
3
+ import { CLIError, type GlobalOptions, getTokenAsync, output } from "../utils/index.ts";
4
+
5
+ export interface TagCreateOptions extends GlobalOptions {
6
+ name: string;
7
+ }
8
+
9
+ export interface TagGetOptions extends GlobalOptions {
10
+ id: string;
11
+ }
12
+
13
+ export interface TagDeleteOptions extends GlobalOptions {
14
+ id: string;
15
+ }
16
+
17
+ async function requireToken(): Promise<string> {
18
+ const token = await getTokenAsync();
19
+ if (!token) {
20
+ throw new CLIError("Not logged in", 401, "Run 'intercom login' to authenticate.");
21
+ }
22
+ return token;
23
+ }
24
+
25
+ export async function cmdTagList(options: GlobalOptions): Promise<void> {
26
+ const token = await requireToken();
27
+ const spinner = ora("Fetching tags...").start();
28
+
29
+ try {
30
+ const client = createClient({ token, dryRun: options.dryRun });
31
+ const result = await client.tags.list();
32
+
33
+ spinner.stop();
34
+
35
+ output(
36
+ {
37
+ tags:
38
+ result.data?.map((tag) => ({
39
+ id: tag.id,
40
+ name: tag.name,
41
+ })) ?? [],
42
+ },
43
+ options.format,
44
+ );
45
+ } catch (error) {
46
+ spinner.fail("Failed to fetch tags");
47
+ handleIntercomError(error);
48
+ }
49
+ }
50
+
51
+ export async function cmdTagCreate(options: TagCreateOptions): Promise<void> {
52
+ const token = await requireToken();
53
+ const spinner = ora("Creating tag...").start();
54
+
55
+ try {
56
+ const client = createClient({ token, dryRun: options.dryRun });
57
+ const tag = await client.tags.create({ name: options.name });
58
+
59
+ spinner.succeed("Tag created");
60
+
61
+ output(
62
+ {
63
+ id: tag.id,
64
+ name: tag.name,
65
+ },
66
+ options.format,
67
+ );
68
+ } catch (error) {
69
+ spinner.fail("Failed to create tag");
70
+ handleIntercomError(error);
71
+ }
72
+ }
73
+
74
+ export async function cmdTagGet(options: TagGetOptions): Promise<void> {
75
+ const token = await requireToken();
76
+ const spinner = ora("Fetching tag...").start();
77
+
78
+ try {
79
+ const client = createClient({ token, dryRun: options.dryRun });
80
+ const tag = await client.tags.find({ tag_id: options.id });
81
+
82
+ spinner.stop();
83
+
84
+ output(
85
+ {
86
+ id: tag.id,
87
+ name: tag.name,
88
+ },
89
+ options.format,
90
+ );
91
+ } catch (error) {
92
+ spinner.fail("Failed to fetch tag");
93
+ handleIntercomError(error);
94
+ }
95
+ }
96
+
97
+ export async function cmdTagDelete(options: TagDeleteOptions): Promise<void> {
98
+ const token = await requireToken();
99
+ const spinner = ora("Deleting tag...").start();
100
+
101
+ try {
102
+ const client = createClient({ token, dryRun: options.dryRun });
103
+ await client.tags.delete({ tag_id: options.id });
104
+
105
+ spinner.succeed("Tag deleted");
106
+
107
+ output({ deleted: true, id: options.id }, options.format);
108
+ } catch (error) {
109
+ spinner.fail("Failed to delete tag");
110
+ handleIntercomError(error);
111
+ }
112
+ }