@faable/faable 4.48.0 → 4.49.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,50 @@
1
+ import { withAuthHints, requireAuthAdmin } from '../../../api/auth_admin.js';
2
+ import { log } from '../../../log.js';
3
+ import { tenant_options } from '../options.js';
4
+ import { json_option, print_json } from '../../../lib/listing.js';
5
+ import { EMAIL_EVENTS, assertRecipients, assertEmailEvents } from './shared.js';
6
+
7
+ const email_subscriptions_create = {
8
+ command: 'create',
9
+ describe: 'Email these addresses on tenant events (e.g. a new user)',
10
+ builder: yargs => json_option(tenant_options(yargs))
11
+ .option('to', {
12
+ type: 'string',
13
+ array: true,
14
+ demandOption: true,
15
+ description: 'Recipient email address — repeatable'
16
+ })
17
+ .option('event', {
18
+ alias: 'e',
19
+ type: 'string',
20
+ array: true,
21
+ demandOption: true,
22
+ description: `Event to email about — repeatable. One of: ${EMAIL_EVENTS.join(', ')}`
23
+ })
24
+ .option('disabled', {
25
+ type: 'boolean',
26
+ default: false,
27
+ description: 'Create it inactive: nothing is sent until enabled'
28
+ })
29
+ .example('$0 auth email-subscriptions create --to ops@example.com -e user.created -e user.updated', 'Get an email (with the form fields) every time a user is created or updated')
30
+ .showHelpOnFail(false),
31
+ handler: withAuthHints(async (args) => {
32
+ assertRecipients(args.to);
33
+ assertEmailEvents(args.event);
34
+ const api = await requireAuthAdmin(args);
35
+ const created = (await api.notificationsubscriptionCreate({
36
+ channel: 'email',
37
+ events: args.event,
38
+ active: !args.disabled,
39
+ config: { recipients: args.to }
40
+ }));
41
+ if (args.json)
42
+ return print_json(created);
43
+ log.info(`✅ Created email subscription ${created.id}`);
44
+ log.info(` To: ${(created.config?.recipients ?? []).join(', ')}`);
45
+ log.info(` Events: ${(created.events ?? []).join(', ')}`);
46
+ log.info(` Active: ${created.active ? 'yes' : 'no'}`);
47
+ })
48
+ };
49
+
50
+ export { email_subscriptions_create };
@@ -0,0 +1,23 @@
1
+ import { email_subscriptions_create } from './create.js';
2
+ import { email_subscriptions_list } from './list.js';
3
+ import { email_subscriptions_rm } from './rm.js';
4
+ import { email_subscriptions_update } from './update.js';
5
+
6
+ // Emails to the tenant's own people on its events (user.created,
7
+ // user.updated, credentials.exported), e.g. to hear about every form that
8
+ // lands as a user. Same resource as webhooks, `channel: 'email'`.
9
+ const email_subscriptions = {
10
+ command: 'email-subscriptions',
11
+ aliases: ['emails'],
12
+ describe: 'Manage email subscriptions (emails to your team on tenant events)',
13
+ builder: yargs => yargs
14
+ .command(email_subscriptions_list)
15
+ .command(email_subscriptions_create)
16
+ .command(email_subscriptions_update)
17
+ .command(email_subscriptions_rm)
18
+ .demandCommand(1)
19
+ .showHelpOnFail(false),
20
+ handler: () => { }
21
+ };
22
+
23
+ export { email_subscriptions };
@@ -0,0 +1,39 @@
1
+ import { withAuthHints, requireAuthAdmin } from '../../../api/auth_admin.js';
2
+ import { list_options, fetch_page, from_paginator, print_json, table_lines, print, more_hint } from '../../../lib/listing.js';
3
+ import { log } from '../../../log.js';
4
+ import { tenant_options } from '../options.js';
5
+ import { yes_no } from '../render.js';
6
+ import { isEmail } from './shared.js';
7
+
8
+ const email_subscriptions_list = {
9
+ command: 'list',
10
+ describe: 'List email subscriptions',
11
+ builder: yargs => list_options(tenant_options(yargs)).showHelpOnFail(false),
12
+ handler: withAuthHints(async (args) => {
13
+ const api = await requireAuthAdmin(args);
14
+ const page = await fetch_page(from_paginator(api.notificationsubscriptionList({})), args);
15
+ // The resource also holds webhooks; this command is email.
16
+ const data = page.data.filter(isEmail);
17
+ if (args.json)
18
+ return print_json({ ...page, data });
19
+ if (data.length === 0) {
20
+ log.info('📭 No email subscriptions. Create one with `faable auth email-subscriptions create`.');
21
+ return;
22
+ }
23
+ log.info(`📧 ${data.length} email subscription(s):`);
24
+ const rows = data.map(s => [
25
+ s.id,
26
+ (s.config?.recipients ?? []).join(','),
27
+ s.events.join(','),
28
+ yes_no(s.active)
29
+ ]);
30
+ for (const line of table_lines(['ID', 'RECIPIENTS', 'EVENTS', 'ACTIVE'], rows)) {
31
+ print(line);
32
+ }
33
+ const hint = more_hint(page);
34
+ if (hint)
35
+ log.info(hint);
36
+ })
37
+ };
38
+
39
+ export { email_subscriptions_list };
@@ -0,0 +1,45 @@
1
+ import { withAuthHints, requireAuthAdmin } from '../../../api/auth_admin.js';
2
+ import { confirm } from '../../../lib/interactive.js';
3
+ import { log } from '../../../log.js';
4
+ import { tenant_options } from '../options.js';
5
+ import { json_option, print_json } from '../../../lib/listing.js';
6
+ import { isEmail } from './shared.js';
7
+
8
+ const email_subscriptions_rm = {
9
+ command: 'rm <subscription_id>',
10
+ describe: 'Delete an email subscription',
11
+ builder: yargs => json_option(tenant_options(yargs))
12
+ .positional('subscription_id', {
13
+ type: 'string',
14
+ demandOption: true,
15
+ description: 'Subscription id (notificationsubscription_…)'
16
+ })
17
+ .option('yes', {
18
+ alias: 'y',
19
+ type: 'boolean',
20
+ default: false,
21
+ description: 'Skip the confirmation prompt'
22
+ })
23
+ .showHelpOnFail(false),
24
+ handler: withAuthHints(async (args) => {
25
+ const api = await requireAuthAdmin(args);
26
+ const s = (await api.notificationsubscriptionGet(args.subscription_id));
27
+ if (!isEmail(s)) {
28
+ throw new Error(`${s.id} is a webhook, not an email subscription: use \`faable auth webhooks rm\`.`);
29
+ }
30
+ const go = await confirm({
31
+ message: `Delete email subscription ${s.id} → ${(s.config?.recipients ?? []).join(', ')}? Those emails stop.`,
32
+ yes: args.yes
33
+ });
34
+ if (!go) {
35
+ log.info('Cancelled.');
36
+ return;
37
+ }
38
+ await api.notificationsubscriptionDelete(s.id);
39
+ if (args.json)
40
+ return print_json({ deleted: true, id: s.id });
41
+ log.info(`🗑️ Deleted email subscription ${s.id}.`);
42
+ })
43
+ };
44
+
45
+ export { email_subscriptions_rm };
@@ -0,0 +1,29 @@
1
+ // Pieces shared by the `faable auth email-subscriptions` commands.
2
+ //
3
+ // An email subscription is a notification subscription with `channel: 'email'`
4
+ // (the same resource as webhooks): Faable Auth emails the tenant's own people
5
+ // on an event — e.g. every time a form creates or updates a user, the
6
+ // recipients get an email with the user's `faable.com/fields`.
7
+ const isEmail = (s) => s.channel === 'email';
8
+ // The only events with an email template in Faable Auth
9
+ // (auth: EMAIL_SUBSCRIPTION_EVENTS). Any other event would be stored and never
10
+ // sent, so it is refused here, before the request.
11
+ const EMAIL_EVENTS = [
12
+ 'user.created',
13
+ 'user.updated',
14
+ 'credentials.exported'
15
+ ];
16
+ const assertEmailEvents = (events) => {
17
+ const unknown = events.filter(e => !EMAIL_EVENTS.includes(e));
18
+ if (unknown.length) {
19
+ throw new Error(`Email subscriptions can only send ${EMAIL_EVENTS.join(', ')} (no email template for: ${unknown.join(', ')}). For other events use \`faable auth webhooks\`.`);
20
+ }
21
+ };
22
+ const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
23
+ const assertRecipients = (to) => {
24
+ const bad = to.filter(r => !EMAIL.test(r));
25
+ if (bad.length)
26
+ throw new Error(`Not an email address: ${bad.join(', ')}`);
27
+ };
28
+
29
+ export { EMAIL_EVENTS, assertEmailEvents, assertRecipients, isEmail };
@@ -0,0 +1,54 @@
1
+ import { withAuthHints, requireAuthAdmin } from '../../../api/auth_admin.js';
2
+ import { log } from '../../../log.js';
3
+ import { tenant_options } from '../options.js';
4
+ import { yes_no } from '../render.js';
5
+ import { assertRecipients, assertEmailEvents } from './shared.js';
6
+ import { json_option, print_json } from '../../../lib/listing.js';
7
+
8
+ const email_subscriptions_update = {
9
+ command: 'update <subscription_id>',
10
+ describe: 'Change an email subscription (recipients, events, enabled)',
11
+ builder: yargs => json_option(tenant_options(yargs))
12
+ .positional('subscription_id', {
13
+ type: 'string',
14
+ demandOption: true,
15
+ description: 'Subscription id (notificationsubscription_…)'
16
+ })
17
+ .option('to', {
18
+ type: 'string',
19
+ array: true,
20
+ description: 'Replace the recipients — repeatable'
21
+ })
22
+ .option('event', {
23
+ alias: 'e',
24
+ type: 'string',
25
+ array: true,
26
+ description: 'Replace the events — repeatable'
27
+ })
28
+ .option('enabled', {
29
+ type: 'boolean',
30
+ description: 'Enable (--enabled) or pause (--no-enabled) the emails'
31
+ })
32
+ .showHelpOnFail(false),
33
+ handler: withAuthHints(async (args) => {
34
+ if (args.to?.length)
35
+ assertRecipients(args.to);
36
+ if (args.event?.length)
37
+ assertEmailEvents(args.event);
38
+ const patch = {
39
+ ...(args.to?.length ? { config: { recipients: args.to } } : {}),
40
+ ...(args.event?.length ? { events: args.event } : {}),
41
+ ...(args.enabled !== undefined ? { active: args.enabled } : {})
42
+ };
43
+ if (Object.keys(patch).length === 0) {
44
+ throw new Error('Nothing to update — pass at least one of --to, --event, --enabled');
45
+ }
46
+ const api = await requireAuthAdmin(args);
47
+ const s = (await api.notificationsubscriptionUpdate(args.subscription_id, patch));
48
+ if (args.json)
49
+ return print_json(s);
50
+ log.info(`✅ Updated email subscription ${s.id} (${(s.config?.recipients ?? []).join(', ')}, ${(s.events ?? []).join(', ')}, active ${yes_no(s.active)})`);
51
+ })
52
+ };
53
+
54
+ export { email_subscriptions_update };
@@ -8,6 +8,7 @@ import { roles } from './roles/index.js';
8
8
  import { sessions } from './sessions/index.js';
9
9
  import { users } from './users/index.js';
10
10
  import { webhooks } from './webhooks/index.js';
11
+ import { email_subscriptions } from './email_subscriptions/index.js';
11
12
 
12
13
  // `faable auth` — management commands for a Faable Auth tenant. Auth: reuses
13
14
  // the `faable login` session (or FAABLE_TOKEN); tenant: --account /
@@ -15,7 +16,7 @@ import { webhooks } from './webhooks/index.js';
15
16
  // resolveTenant in src/api/auth_admin.ts.
16
17
  const auth = {
17
18
  command: 'auth',
18
- describe: 'Manage Faable Auth (users, roles, APIs, sessions, logins, actions, clients, webhooks, audit logs)',
19
+ describe: 'Manage Faable Auth (users, roles, APIs, sessions, logins, actions, clients, webhooks, email subscriptions, audit logs)',
19
20
  builder: yargs => yargs
20
21
  .command(users)
21
22
  .command(roles)
@@ -26,6 +27,7 @@ const auth = {
26
27
  .command(sessions)
27
28
  .command(connections)
28
29
  .command(webhooks)
30
+ .command(email_subscriptions)
29
31
  .command(accounts)
30
32
  .command(auth_use)
31
33
  .demandCommand(1)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faable/faable",
3
- "version": "4.48.0",
3
+ "version": "4.49.0",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "author": "Marc Pomar <marc@faable.com>",