@codejeet/oadm 0.0.1 → 0.0.3

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.
package/dist/cli.js CHANGED
@@ -4,7 +4,7 @@ import chalk from 'chalk';
4
4
  import { readConfig, writeConfig } from './config.js';
5
5
  import { getJson, postJson } from './http.js';
6
6
  const program = new Command();
7
- program.name('oadm').description('OADM Inbox CLI').version('0.0.1');
7
+ program.name('oadm').description('OADM Inbox CLI').version('0.0.2');
8
8
  program
9
9
  .option('--api <url>', 'API base URL (or set OADM_API_URL)', '')
10
10
  .hook('preAction', (thisCommand) => {
@@ -21,7 +21,11 @@ program
21
21
  .requiredOption('--password <password>')
22
22
  .action(async (opts) => {
23
23
  const cfg = readConfig();
24
- await postJson(`${cfg.apiUrl}/v1/register`, { name: opts.name, password: opts.password, inviteCode: process.env.OADM_INVITE_CODE });
24
+ await postJson(`${cfg.apiUrl}/v1/register`, {
25
+ name: opts.name,
26
+ password: opts.password,
27
+ inviteCode: process.env.OADM_INVITE_CODE,
28
+ });
25
29
  console.log(chalk.green('✓ registered'));
26
30
  console.log('Next: oadm login --name <name> --password <pw>');
27
31
  });
@@ -31,7 +35,10 @@ program
31
35
  .requiredOption('--password <password>')
32
36
  .action(async (opts) => {
33
37
  const cfg = readConfig();
34
- const data = await postJson(`${cfg.apiUrl}/v1/login`, { name: opts.name, password: opts.password });
38
+ const data = await postJson(`${cfg.apiUrl}/v1/login`, {
39
+ name: opts.name,
40
+ password: opts.password,
41
+ });
35
42
  cfg.name = opts.name;
36
43
  cfg.token = data.token;
37
44
  writeConfig(cfg);
@@ -51,22 +58,36 @@ program
51
58
  program
52
59
  .command('inbox')
53
60
  .option('--unread', 'Only unread', false)
61
+ .option('--sent', 'Show sent messages (outbox)', false)
62
+ .option('--all', 'Show both received and sent', false)
63
+ .option('--since <timestamp>', 'Only messages since timestamp (ISO 8601 or unix)', '')
54
64
  .option('--json', 'JSON output', false)
55
65
  .option('--ack', 'Ack returned messages', false)
56
66
  .action(async (opts) => {
57
67
  const cfg = readConfig();
58
68
  if (!cfg.token)
59
69
  throw new Error('not_logged_in');
70
+ if (opts.sent && opts.all)
71
+ throw new Error('conflicting_flags_sent_all');
60
72
  const q = new URLSearchParams();
61
73
  if (opts.unread)
62
74
  q.set('unread', '1');
75
+ if (opts.sent)
76
+ q.set('sent', '1');
77
+ if (opts.all)
78
+ q.set('all', '1');
79
+ if (opts.since)
80
+ q.set('since', opts.since);
63
81
  const data = await getJson(`${cfg.apiUrl}/v1/messages/inbox?${q.toString()}`, cfg.token);
64
82
  if (opts.json) {
65
83
  console.log(JSON.stringify(data, null, 2));
66
84
  }
67
85
  else {
68
86
  for (const m of data.messages) {
69
- console.log(`${m.id} from:${m.fromName} ${new Date(m.createdAt).toLocaleString()}`);
87
+ const direction = m.direction ?? (opts.sent ? 'out' : 'in');
88
+ const peer = direction === 'out' ? `to:${m.toName}` : `from:${m.fromName}`;
89
+ const ackedAt = m.ackedAt ? ` acked:${new Date(m.ackedAt).toLocaleString()}` : '';
90
+ console.log(`${m.id} ${peer} ${new Date(m.createdAt).toLocaleString()}${direction === 'out' ? ackedAt : ''}`);
70
91
  console.log(m.text);
71
92
  console.log('---');
72
93
  }
@@ -75,6 +96,8 @@ program
75
96
  }
76
97
  if (opts.ack) {
77
98
  for (const m of data.messages) {
99
+ if (m.direction === 'out')
100
+ continue;
78
101
  await postJson(`${cfg.apiUrl}/v1/messages/ack/${m.id}`, {}, cfg.token);
79
102
  }
80
103
  }
@@ -89,6 +112,46 @@ program
89
112
  await postJson(`${cfg.apiUrl}/v1/messages/ack/${msgId}`, {}, cfg.token);
90
113
  console.log(chalk.green('✓ acked'), msgId);
91
114
  });
115
+ program
116
+ .command('webhook:create')
117
+ .requiredOption('--url <url>')
118
+ .option('--secret <secret>')
119
+ .action(async (opts) => {
120
+ const cfg = readConfig();
121
+ if (!cfg.token)
122
+ throw new Error('not_logged_in');
123
+ const data = await postJson(`${cfg.apiUrl}/v1/webhooks`, { url: opts.url, secret: opts.secret }, cfg.token);
124
+ console.log(chalk.green('✓ webhook created'), data.webhook.id);
125
+ console.log('secret:', data.secret);
126
+ });
127
+ program
128
+ .command('webhook:list')
129
+ .option('--json', 'JSON output', false)
130
+ .action(async (opts) => {
131
+ const cfg = readConfig();
132
+ if (!cfg.token)
133
+ throw new Error('not_logged_in');
134
+ const data = await getJson(`${cfg.apiUrl}/v1/webhooks`, cfg.token);
135
+ if (opts.json) {
136
+ console.log(JSON.stringify(data, null, 2));
137
+ return;
138
+ }
139
+ for (const hook of data.webhooks) {
140
+ console.log(`${hook.id} ${hook.url} enabled:${hook.enabled}`);
141
+ }
142
+ if (!data.webhooks.length)
143
+ console.log('(empty)');
144
+ });
145
+ program
146
+ .command('webhook:delete')
147
+ .argument('<webhookId>')
148
+ .action(async (webhookId) => {
149
+ const cfg = readConfig();
150
+ if (!cfg.token)
151
+ throw new Error('not_logged_in');
152
+ await postJson(`${cfg.apiUrl}/v1/webhooks/${webhookId}`, {}, cfg.token, 'DELETE');
153
+ console.log(chalk.green('✓ webhook deleted'), webhookId);
154
+ });
92
155
  program.parseAsync(process.argv).catch((e) => {
93
156
  console.error(chalk.red('error:'), e?.message ?? String(e));
94
157
  process.exit(1);
package/dist/http.js CHANGED
@@ -1,6 +1,6 @@
1
- export async function postJson(url, body, token) {
1
+ export async function postJson(url, body, token, method = 'POST') {
2
2
  const r = await fetch(url, {
3
- method: 'POST',
3
+ method,
4
4
  headers: {
5
5
  'content-type': 'application/json',
6
6
  ...(token ? { authorization: `Bearer ${token}` } : {}),
package/package.json CHANGED
@@ -1,14 +1,17 @@
1
1
  {
2
2
  "name": "@codejeet/oadm",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "oadm": "dist/cli.js"
7
7
  },
8
- "files": ["dist"],
8
+ "files": [
9
+ "dist"
10
+ ],
9
11
  "scripts": {
10
12
  "dev": "tsx src/cli.ts",
11
13
  "build": "tsc -p tsconfig.json",
14
+ "prepack": "tsc -p tsconfig.json",
12
15
  "typecheck": "tsc -p tsconfig.json --noEmit"
13
16
  },
14
17
  "dependencies": {