@codejeet/oadm 0.0.3 → 0.0.4

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
@@ -3,8 +3,9 @@ import { Command } from 'commander';
3
3
  import chalk from 'chalk';
4
4
  import { readConfig, writeConfig } from './config.js';
5
5
  import { getJson, postJson } from './http.js';
6
+ import pkg from '../package.json' with { type: 'json' };
6
7
  const program = new Command();
7
- program.name('oadm').description('OADM Inbox CLI').version('0.0.2');
8
+ program.name('oadm').description('OADM Inbox CLI').version(pkg.version);
8
9
  program
9
10
  .option('--api <url>', 'API base URL (or set OADM_API_URL)', '')
10
11
  .hook('preAction', (thisCommand) => {
@@ -112,17 +113,19 @@ program
112
113
  await postJson(`${cfg.apiUrl}/v1/messages/ack/${msgId}`, {}, cfg.token);
113
114
  console.log(chalk.green('✓ acked'), msgId);
114
115
  });
116
+ // Webhooks
115
117
  program
116
118
  .command('webhook:create')
117
119
  .requiredOption('--url <url>')
118
- .option('--secret <secret>')
119
120
  .action(async (opts) => {
120
121
  const cfg = readConfig();
121
122
  if (!cfg.token)
122
123
  throw new Error('not_logged_in');
123
- const data = await postJson(`${cfg.apiUrl}/v1/webhooks`, { url: opts.url, secret: opts.secret }, cfg.token);
124
+ // Server generates the secret. Do NOT send user secret.
125
+ const data = await postJson(`${cfg.apiUrl}/v1/webhooks`, { url: opts.url }, cfg.token);
124
126
  console.log(chalk.green('✓ webhook created'), data.webhook.id);
125
- console.log('secret:', data.secret);
127
+ if (data.secret)
128
+ console.log('secret:', data.secret);
126
129
  });
127
130
  program
128
131
  .command('webhook:list')
@@ -149,7 +152,7 @@ program
149
152
  const cfg = readConfig();
150
153
  if (!cfg.token)
151
154
  throw new Error('not_logged_in');
152
- await postJson(`${cfg.apiUrl}/v1/webhooks/${webhookId}`, {}, cfg.token, 'DELETE');
155
+ await postJson(`${cfg.apiUrl}/v1/webhooks/${webhookId}`, undefined, cfg.token, 'DELETE');
153
156
  console.log(chalk.green('✓ webhook deleted'), webhookId);
154
157
  });
155
158
  program.parseAsync(process.argv).catch((e) => {
package/dist/config.js CHANGED
@@ -1,29 +1,28 @@
1
+ import fs from 'node:fs';
1
2
  import os from 'node:os';
2
3
  import path from 'node:path';
3
- import fs from 'node:fs';
4
- export function configDir() {
5
- return path.join(os.homedir(), '.oadm');
6
- }
7
- export function configPath() {
8
- return path.join(configDir(), 'config.json');
9
- }
4
+ const CONFIG_DIR = path.join(os.homedir(), '.oadm');
5
+ const CONFIG_PATH = path.join(CONFIG_DIR, 'config.json');
10
6
  export function readConfig() {
11
- const p = configPath();
12
- if (!fs.existsSync(p)) {
13
- return { apiUrl: process.env.OADM_API_URL ?? 'http://localhost:3000' };
14
- }
15
- const raw = fs.readFileSync(p, 'utf8');
16
- const cfg = JSON.parse(raw);
17
- cfg.apiUrl = cfg.apiUrl ?? process.env.OADM_API_URL ?? 'http://localhost:3000';
18
- return cfg;
19
- }
20
- export function writeConfig(cfg) {
21
- fs.mkdirSync(configDir(), { recursive: true });
22
- fs.writeFileSync(configPath(), JSON.stringify(cfg, null, 2));
23
7
  try {
24
- fs.chmodSync(configPath(), 0o600);
8
+ const raw = fs.readFileSync(CONFIG_PATH, 'utf8');
9
+ const cfg = JSON.parse(raw);
10
+ const apiUrl = process.env.OADM_API_URL ??
11
+ (typeof cfg.apiUrl === 'string' ? cfg.apiUrl : '') ??
12
+ '';
13
+ return {
14
+ apiUrl: apiUrl || 'https://api-zeta-jet-48.vercel.app',
15
+ name: typeof cfg.name === 'string' ? cfg.name : undefined,
16
+ token: typeof cfg.token === 'string' ? cfg.token : undefined,
17
+ };
25
18
  }
26
19
  catch {
27
- // ignore on platforms that don't support chmod
20
+ const apiUrl = process.env.OADM_API_URL ?? 'https://api-zeta-jet-48.vercel.app';
21
+ return { apiUrl };
28
22
  }
29
23
  }
24
+ export function writeConfig(cfg) {
25
+ if (!fs.existsSync(CONFIG_DIR))
26
+ fs.mkdirSync(CONFIG_DIR, { recursive: true });
27
+ fs.writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 2));
28
+ }
package/dist/http.js CHANGED
@@ -1,31 +1,32 @@
1
- export async function postJson(url, body, token, method = 'POST') {
2
- const r = await fetch(url, {
3
- method,
1
+ import fetch from 'node-fetch';
2
+ export async function getJson(url, token) {
3
+ const res = await fetch(url, {
4
+ method: 'GET',
4
5
  headers: {
5
- 'content-type': 'application/json',
6
- ...(token ? { authorization: `Bearer ${token}` } : {}),
6
+ ...(token ? { Authorization: `Bearer ${token}` } : {}),
7
7
  },
8
- body: JSON.stringify(body),
9
8
  });
10
- const text = await r.text();
11
- const data = text ? JSON.parse(text) : null;
12
- if (!r.ok) {
13
- const msg = data?.error ? `${data.error}` : `http_${r.status}`;
14
- throw new Error(msg);
9
+ if (!res.ok) {
10
+ const text = await res.text().catch(() => '');
11
+ throw new Error(`http_${res.status}${text ? `: ${text}` : ''}`);
15
12
  }
16
- return data;
13
+ return (await res.json());
17
14
  }
18
- export async function getJson(url, token) {
19
- const r = await fetch(url, {
15
+ export async function postJson(url, body, token, method = 'POST') {
16
+ const res = await fetch(url, {
17
+ method,
20
18
  headers: {
21
- ...(token ? { authorization: `Bearer ${token}` } : {}),
19
+ 'Content-Type': 'application/json',
20
+ ...(token ? { Authorization: `Bearer ${token}` } : {}),
22
21
  },
22
+ body: body === undefined ? undefined : JSON.stringify(body),
23
23
  });
24
- const text = await r.text();
25
- const data = text ? JSON.parse(text) : null;
26
- if (!r.ok) {
27
- const msg = data?.error ? `${data.error}` : `http_${r.status}`;
28
- throw new Error(msg);
24
+ if (!res.ok) {
25
+ const text = await res.text().catch(() => '');
26
+ throw new Error(`http_${res.status}${text ? `: ${text}` : ''}`);
29
27
  }
30
- return data;
28
+ const ct = res.headers.get('content-type') || '';
29
+ if (!ct.includes('application/json'))
30
+ return {};
31
+ return (await res.json());
31
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codejeet/oadm",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "oadm": "dist/cli.js"