@faable/faable 4.37.0 → 4.38.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.
@@ -495,8 +495,10 @@ class FaableApi {
495
495
  headers: projectHeader(team)
496
496
  }));
497
497
  }
498
- // Relink a domain to another app of its project (or `null` to unlink it).
499
- // Keeps the row: same id, same CNAME target, same certificate.
498
+ // Relink a domain to another app of its project (`app_id`, or `null` to
499
+ // unlink it), or make it redirect to another domain of the project
500
+ // (`redirect`, `null` to serve again). Keeps the row: same id, same CNAME
501
+ // target, same certificate.
500
502
  async updateDomain(domain_id, team, params) {
501
503
  return data(this.client.post(`/domain/${domain_id}`, params, {
502
504
  headers: projectHeader(team)
@@ -36,6 +36,9 @@ const domains_check = {
36
36
  if (!domain.app_id) {
37
37
  log.warn(` Linked to no app: it answers DOMAIN_NOT_LINKED. Serve it with: faable deploy domains attach ${domain.fqdn} --app <app>`);
38
38
  }
39
+ if (domain.redirect) {
40
+ log.info(` Redirects to: https://${domain.redirect} (path and query kept). Serve it again with: faable deploy domains redirect ${domain.fqdn} --off`);
41
+ }
39
42
  log.info(` Expected CNAME: ${domain.fqdn} → ${cname_target(domain)}`);
40
43
  if (status?.dns_observed?.length) {
41
44
  log.info(` Observed: ${status.dns_observed.join(', ')}`);
@@ -47,5 +47,13 @@ const find_by_fqdn = (domains, fqdn) => domains.find(d => d.fqdn.toLowerCase() =
47
47
  * the counterpart to look it up.
48
48
  */
49
49
  const pair_fqdn = (fqdn) => fqdn.startsWith('www.') ? fqdn.slice(4) : `www.${fqdn}`;
50
+ /** ` ↪ example.com` for a domain that redirects, '' for one that serves. */
51
+ const redirect_badge = (domain) => domain.redirect ? `↪ ${domain.redirect}` : '';
52
+ /**
53
+ * The domains of `all` that redirect to `domain`. They are part of it: deleting
54
+ * `domain` deletes them too (the api cascades; same project only).
55
+ */
56
+ const redirects_to = (all, domain) => all.filter(d => d.id !== domain.id &&
57
+ d.redirect?.toLowerCase() === domain.fqdn.toLowerCase());
50
58
 
51
- export { acme_record, cname_target, dns_badge, find_by_fqdn, pair_fqdn, tls_line };
59
+ export { acme_record, cname_target, dns_badge, find_by_fqdn, pair_fqdn, redirect_badge, redirects_to, tls_line };
@@ -2,6 +2,7 @@ import { domains_add } from './add.js';
2
2
  import { domains_attach } from './attach.js';
3
3
  import { domains_check } from './check.js';
4
4
  import { domains_list } from './list.js';
5
+ import { domains_redirect } from './redirect.js';
5
6
  import { domains_rm } from './rm.js';
6
7
 
7
8
  const domains = {
@@ -12,8 +13,9 @@ const domains = {
12
13
  .command(domains_add)
13
14
  .command(domains_attach)
14
15
  .command(domains_check)
16
+ .command(domains_redirect)
15
17
  .command(domains_rm)
16
- .demandCommand(1, 'Specify a domains command: list, add, attach, check or rm'),
18
+ .demandCommand(1, 'Specify a domains command: list, add, attach, check, redirect or rm'),
17
19
  handler: () => {
18
20
  // Unreachable: demandCommand(1) either routes to a subcommand or fails
19
21
  // through the global .fail() in src/index.ts.
@@ -3,7 +3,7 @@ import { fetch_page, print_json, print, more_hint, list_options } from '../../..
3
3
  import { log } from '../../../log.js';
4
4
  import { requireProject } from '../../../api/project.js';
5
5
  import { find_app_id } from '../resolve_app_id.js';
6
- import { dns_badge, cname_target } from './format.js';
6
+ import { dns_badge, redirect_badge, cname_target } from './format.js';
7
7
 
8
8
  const domains_list = {
9
9
  command: 'list',
@@ -35,7 +35,7 @@ const domains_list = {
35
35
  const width = Math.max(...domains.map(d => d.fqdn.length));
36
36
  for (const domain of domains) {
37
37
  const tls = domain.tls ? 'tls' : 'no-tls';
38
- print(` ${domain.fqdn.padEnd(width)} ${dns_badge(domain)} (${tls})`);
38
+ print(` ${domain.fqdn.padEnd(width)} ${dns_badge(domain)} (${tls}) ${redirect_badge(domain)}`.trimEnd());
39
39
  }
40
40
  const hint = more_hint(page);
41
41
  if (hint)
@@ -75,7 +75,7 @@ const list_project_domains = async (api, args) => {
75
75
  const app_width = Math.max(...domains.map(d => label(d).length));
76
76
  for (const domain of domains) {
77
77
  const tls = domain.tls ? 'tls' : 'no-tls';
78
- print(` ${domain.fqdn.padEnd(width)} ${label(domain).padEnd(app_width)} ${dns_badge(domain)} (${tls})`);
78
+ print(` ${domain.fqdn.padEnd(width)} ${label(domain).padEnd(app_width)} ${dns_badge(domain)} (${tls}) ${redirect_badge(domain)}`.trimEnd());
79
79
  }
80
80
  const hint = more_hint(page);
81
81
  if (hint)
@@ -0,0 +1,77 @@
1
+ import { requireApi } from '../../../api/context.js';
2
+ import { CliError } from '../../../lib/errors.js';
3
+ import { print_json, json_option } from '../../../lib/listing.js';
4
+ import { log } from '../../../log.js';
5
+ import { find_by_fqdn } from './format.js';
6
+ import { domain_scope, project_domains, not_in_project } from './locate.js';
7
+
8
+ const domains_redirect = {
9
+ command: 'redirect <fqdn> [target]',
10
+ describe: 'Make a domain redirect every request to another domain of the project (or serve again with --off)',
11
+ builder: yargs => json_option(yargs)
12
+ .positional('fqdn', {
13
+ type: 'string',
14
+ demandOption: true,
15
+ description: 'The domain that redirects'
16
+ })
17
+ .positional('target', {
18
+ type: 'string',
19
+ description: 'The domain it redirects to: same project, and one that serves'
20
+ })
21
+ .option('off', {
22
+ type: 'boolean',
23
+ default: false,
24
+ description: 'Stop redirecting: the domain serves its app again'
25
+ })
26
+ .option('app', {
27
+ alias: 'a',
28
+ type: 'string',
29
+ description: 'App id, name or slug. Optional: the domain is looked up in the project'
30
+ })
31
+ .example('$0 deploy domains redirect www.example.com example.com', 'www.example.com/path?q → https://example.com/path?q')
32
+ .example('$0 deploy domains redirect www.example.com --off', 'www.example.com serves its app again')
33
+ .epilog('GET gets a 301, every other method (HEAD included) a 308: the path and the query are kept. A domain redirects to the one it names, never further: no chains. Removing the target removes the domains that redirect to it.')
34
+ .showHelpOnFail(false),
35
+ handler: async (args) => {
36
+ if (!args.off && !args.target) {
37
+ throw new CliError('usage', 'Name the domain to redirect to, or pass --off to stop redirecting.');
38
+ }
39
+ if (args.off && args.target) {
40
+ throw new CliError('usage', 'Pass either a target or --off, not both.');
41
+ }
42
+ const ctx = await requireApi();
43
+ const scope = await domain_scope(ctx.api, args.app, ctx.appId);
44
+ const all = await project_domains(ctx.api, scope.team);
45
+ const domain = find_by_fqdn(all, args.fqdn);
46
+ if (!domain)
47
+ throw not_in_project(args.fqdn, scope.team);
48
+ const target = args.off ? null : args.target.toLowerCase();
49
+ if ((domain.redirect ?? null) === target) {
50
+ if (args.json)
51
+ return print_json(domain);
52
+ log.info(target
53
+ ? `↪ ${domain.fqdn} already redirects to ${target}.`
54
+ : `🌐 ${domain.fqdn} already serves its app: it redirects nowhere.`);
55
+ return;
56
+ }
57
+ // The api checks the target (same project, serving, no chains) and says
58
+ // why in its error; this only spares the round trip for the plain typo.
59
+ if (target && !find_by_fqdn(all, target)) {
60
+ throw new CliError('domain_redirect_invalid', `${target} is not a domain of this project. Add it first with "faable deploy domains add ${target}": a domain can only redirect to another domain of the same project.`);
61
+ }
62
+ const updated = await ctx.api.updateDomain(domain.id, scope.team, {
63
+ redirect: target
64
+ });
65
+ if (args.json)
66
+ return print_json(updated);
67
+ if (target) {
68
+ log.info(`↪ ${updated.fqdn} now redirects to ${target}: GET → 301, other methods → 308, path and query kept.`);
69
+ log.info(`Removing ${target} will remove ${updated.fqdn} too. Its CNAME and certificate stay as they are.`);
70
+ }
71
+ else {
72
+ log.info(`🌐 ${updated.fqdn} no longer redirects: it serves ${updated.app_id ?? 'no app (link one with "faable deploy domains attach")'}.`);
73
+ }
74
+ }
75
+ };
76
+
77
+ export { domains_redirect };
@@ -2,7 +2,8 @@ import { requireApi } from '../../../api/context.js';
2
2
  import { confirm } from '../../../lib/interactive.js';
3
3
  import { print_json, json_option } from '../../../lib/listing.js';
4
4
  import { log } from '../../../log.js';
5
- import { domain_scope, locate_domain } from './locate.js';
5
+ import { redirects_to } from './format.js';
6
+ import { domain_scope, locate_domain, project_domains } from './locate.js';
6
7
 
7
8
  const domains_rm = {
8
9
  command: 'rm <fqdn>',
@@ -36,10 +37,16 @@ const domains_rm = {
36
37
  ? `${scope.app.name} (${domain.app_id})`
37
38
  : domain.app_id
38
39
  : null;
40
+ // The domains that redirect to it go with it (the api cascades): say so
41
+ // before, not after.
42
+ const dependents = redirects_to(await project_domains(ctx.api, scope.team), domain);
43
+ const also = dependents.length
44
+ ? ` ${dependents.map(d => d.fqdn).join(', ')} redirect${dependents.length === 1 ? 's' : ''} to it and will be removed too.`
45
+ : '';
39
46
  const go = await confirm({
40
47
  message: serves
41
- ? `Remove domain "${domain.fqdn}" from ${serves}? Traffic to it will stop being served.`
42
- : `Remove domain "${domain.fqdn}"? It is linked to no app. To serve it again with the same CNAME and certificate, use "faable deploy domains attach" instead.`,
48
+ ? `Remove domain "${domain.fqdn}" from ${serves}? Traffic to it will stop being served.${also}`
49
+ : `Remove domain "${domain.fqdn}"? It is linked to no app. To serve it again with the same CNAME and certificate, use "faable deploy domains attach" instead.${also}`,
43
50
  yes: args.yes
44
51
  });
45
52
  if (!go) {
@@ -48,10 +55,15 @@ const domains_rm = {
48
55
  }
49
56
  await ctx.api.deleteDomain(domain.id, scope.team);
50
57
  if (args.json) {
51
- return print_json({ deleted: true, id: domain.id, fqdn: domain.fqdn });
58
+ return print_json({
59
+ deleted: true,
60
+ id: domain.id,
61
+ fqdn: domain.fqdn,
62
+ also_deleted: dependents.map(d => ({ id: d.id, fqdn: d.fqdn }))
63
+ });
52
64
  }
53
- log.info(`🗑️ Removed domain ${domain.fqdn}${serves ? ` from ${serves}` : ''}.`);
54
- log.info(`Remember to delete the CNAME at your DNS provider.`);
65
+ log.info(`🗑️ Removed domain ${domain.fqdn}${serves ? ` from ${serves}` : ''}${dependents.length ? `, and ${dependents.map(d => d.fqdn).join(', ')} that redirected to it` : ''}.`);
66
+ log.info(`Remember to delete the CNAME${dependents.length ? 's' : ''} at your DNS provider.`);
55
67
  }
56
68
  };
57
69
 
package/dist/mcp/tools.js CHANGED
@@ -420,7 +420,7 @@ const TOOLS = [
420
420
  {
421
421
  name: 'list_domains',
422
422
  title: 'List custom domains',
423
- description: 'Custom domains of an app and whether their DNS is verified. Without `app`, every domain of the project with the app it serves: `app_id: null` is a domain linked to no app (its app was deleted), which answers DOMAIN_NOT_LINKED until attach_domain links it.',
423
+ description: 'Custom domains of an app and whether their DNS is verified. Without `app`, every domain of the project with the app it serves: `app_id: null` is a domain linked to no app (its app was deleted), which answers DOMAIN_NOT_LINKED until attach_domain links it. `redirect` names the domain it sends every request to (the www half of a pair, or an alias); null when it serves its app.',
424
424
  input: { app: optional_app, project },
425
425
  command: ['deploy', 'domains', 'list'],
426
426
  flags: app_or_project,
@@ -430,6 +430,7 @@ const TOOLS = [
430
430
  app_id: d.app_id ?? null,
431
431
  verified: d.verified,
432
432
  tls: d.tls,
433
+ redirect: d.redirect ?? null,
433
434
  dns_message: d.status?.dns_message
434
435
  })),
435
436
  annotations: READ,
@@ -450,6 +451,7 @@ const TOOLS = [
450
451
  fqdn: d.fqdn,
451
452
  app_id: d.app_id ?? null,
452
453
  verified: d.verified,
454
+ redirect: d.redirect ?? null,
453
455
  expected_cname: d.expected_cname,
454
456
  observed: d.status?.dns_observed ?? [],
455
457
  diagnostic: d.status?.dns_message,
@@ -1047,6 +1049,40 @@ const TOOLS = [
1047
1049
  },
1048
1050
  example: { app: 'web', fqdn: 'app.example.com' }
1049
1051
  },
1052
+ {
1053
+ name: 'set_domain_redirect',
1054
+ title: 'Redirect a domain to another',
1055
+ description: 'Make a custom domain send every request to another domain of the same project (GET → 301, other methods → 308, path and query kept), e.g. www.example.com to example.com, or an old name to the new one. `target: null` stops it: the domain serves its app again. The target must be a domain of the project that serves (no chains). Deleting the target later deletes the domains that redirect to it. DNS and certificate do not change.',
1056
+ input: {
1057
+ app: optional_app,
1058
+ project,
1059
+ fqdn: z.string().describe('The domain that redirects, e.g. www.example.com'),
1060
+ target: z
1061
+ .string()
1062
+ .nullable()
1063
+ .describe('The domain it redirects to, e.g. example.com; null to stop redirecting')
1064
+ },
1065
+ command: ['deploy', 'domains', 'redirect'],
1066
+ flags: a => [
1067
+ a.fqdn,
1068
+ ...(a.target ? [a.target] : ['--off']),
1069
+ ...app_or_project(a)
1070
+ ],
1071
+ write: true,
1072
+ shape: (d) => ({
1073
+ id: d.id,
1074
+ fqdn: d.fqdn,
1075
+ app_id: d.app_id ?? null,
1076
+ redirect: d.redirect ?? null
1077
+ }),
1078
+ annotations: {
1079
+ readOnlyHint: false,
1080
+ destructiveHint: false,
1081
+ idempotentHint: true,
1082
+ openWorldHint: true
1083
+ },
1084
+ example: { fqdn: 'www.example.com', target: 'example.com' }
1085
+ },
1050
1086
  {
1051
1087
  name: 'configure_repo',
1052
1088
  title: 'Configure how an app deploys',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faable/faable",
3
- "version": "4.37.0",
3
+ "version": "4.38.0",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "author": "Marc Pomar <marc@faable.com>",