@superintern/sites 0.1.0 → 0.3.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,34 @@
1
1
  # Changes to @superintern/sites
2
2
 
3
+ ## 0.3.0
4
+
5
+ - `sites media add <file...>`: put large pictures, video, audio and PDFs in
6
+ a site's media files, outside its versions, up to 95 MB each. Each is
7
+ served at `/_media/<name>` on the site straight away; the command prints
8
+ that address. Three files go at a time, a dropped connection is tried once
9
+ more, and a file that is refused says why while the others still go.
10
+ - `sites media list` shows them and the room they take (`--json` too);
11
+ `sites media remove <name...>` deletes them. Inside a site's folder they
12
+ need no name; elsewhere add `--site <slug>`.
13
+ - `sites crm …` and `sites site …`: every tool of SuperIntern's MCP server
14
+ (the tools Claude is given) as a command, read from the service, so the two
15
+ always offer the same things. The CRM: tables, records, saved views and
16
+ automations, with changes, as far as your key's CRM access allows. Websites:
17
+ settings, history, media, redirects, analytics, traffic, search, removed
18
+ sites and more. A tool's arguments are its options; `--json` prints the
19
+ answer as it came. See `sites crm --help` and `sites site --help`.
20
+ - `list`, `status`, `publish`, `unpublish`, `remove`, `open`, `accounts`,
21
+ `new` and `clone` work as before, through the same tools.
22
+
23
+ ## 0.2.0
24
+
25
+ - `sites accounts`: whether customers can sign in to a site, and how
26
+ (customer accounts with a code by email, passwords, Google, Microsoft).
27
+ `--sign-in on`, `--passwords on`, `--google on`, `--microsoft on` (or
28
+ `off`) switch them, the same switches as the website's Settings tab; only
29
+ what is given changes. Needs a key that can change websites. Inside a
30
+ site's folder it needs no name.
31
+
3
32
  ## 0.1.0
4
33
 
5
34
  The first version on npm.
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # sites
2
2
 
3
3
  Make a website for a business on SuperIntern, see how it is doing, and put it
4
- online, from your own computer.
4
+ online, from your own computer. Work on the business's CRM too: its tables
5
+ and records, and everything else SuperIntern's tools for Claude can do.
5
6
 
6
7
  A site is a folder of ordinary files that you edit, with git keeping its
7
8
  history. `sites` makes the site, brings its files to your computer, tells you
@@ -80,22 +81,64 @@ sites remove <name> remove the site for good; you type its
80
81
  sites open <name> open the site's address in your browser
81
82
  sites tables [<name>] what the business's CRM lets the site show
82
83
  sites rows <table> [<name>] the first rows of one of those tables
84
+ sites accounts [<name>] whether customers can sign in, and how
85
+ sites accounts [<name>] --sign-in on --passwords on
86
+ let customers sign in, with a password too
87
+ sites media add <file...> put large pictures, video, audio and PDFs on the site
88
+ sites media list the site's media files and the room they take
89
+ sites media remove <name...> delete media files
83
90
  sites link in a site's folder: tie it to this copy of sites
91
+ sites crm <command> [options] the business's CRM: tables, records, views, automations
92
+ sites site <command> [options] every other website tool: settings, history, media,
93
+ redirects, analytics, traffic, search and more
84
94
  sites --version which version of sites this is
85
95
  sites --help every option
86
96
  ```
87
97
 
88
98
  A site's name is 3 to 40 lowercase letters, digits and single hyphens. It
89
- becomes the site's address. Inside a site's folder, `status`, `tables` and
90
- `rows` need no name.
99
+ becomes the site's address. Inside a site's folder, `status`, `tables`,
100
+ `rows`, `accounts` and `media` need no name.
91
101
 
92
102
  `sites status --wait` ends with exit code 0 when your push was built and is
93
103
  ready to publish, 1 when it failed (what to fix is printed), and 2 when it
94
104
  took too long.
95
105
 
106
+ A site's own files are at most 25 MB each, and 25 MB together once zipped.
107
+ Large pictures, video, audio and PDFs (up to 95 MB each) go in its media
108
+ files instead: `sites media add photos/*.jpg tour.mp4` prints the address
109
+ each file got, such as `/_media/tour.mp4`, which pages use like any other
110
+ file. Media files are not part of a version: they are on the site as soon as
111
+ they are added, and publishing does not change them.
112
+
96
113
  To go back to an earlier version: `sites status` shows the numbers, then
97
114
  `sites publish <name> --version <n>`.
98
115
 
116
+ ## The CRM, and every other tool
117
+
118
+ `sites crm` and `sites site` are SuperIntern's tools for Claude, the same
119
+ ones, read from the service each time: whatever your key may do, these
120
+ commands can do. See them with `sites crm --help` and `sites site --help`,
121
+ and one command's options with `--help` after it.
122
+
123
+ ```bash
124
+ sites crm table-list
125
+ sites crm table-save "Bike sizes" --create --columns '[{"title": "Frame", "type": "text", "visibility": "public"}]'
126
+ sites crm record-save "Bike sizes" --records '[{"name": "Small", "values": {"frame": "48 cm"} }]'
127
+ sites crm record-search "Bike sizes" --json
128
+ sites site history-read my-shop
129
+ sites site redirect-read
130
+ ```
131
+
132
+ Each of a tool's arguments is an option with the same name, hyphens for
133
+ underscores. A list or an object is written as JSON (or `@file.json`), and
134
+ `--input` takes all the arguments as one JSON object. In a site's folder,
135
+ `sites site` commands need no site name. `--json` prints the service's answer
136
+ as it came.
137
+
138
+ What the CRM commands may do is the CRM access chosen on your key. A key that
139
+ may not change the CRM says so; make one that may (Website, Keys) and run
140
+ `sites login` again.
141
+
99
142
  ## When a push is refused
100
143
 
101
144
  | Git says | What to do |
@@ -113,7 +156,7 @@ To go back to an earlier version: `sites status` shows the numbers, then
113
156
  for it: a command line is kept in your shell's history.
114
157
  - It is kept in your macOS keychain, or elsewhere in the file
115
158
  `~/.config/superintern-sites/<service>.key`, which only you can read.
116
- - It is sent to two addresses only, always over https: SuperIntern's site
159
+ - It is sent to two addresses only, always over https: SuperIntern's
117
160
  service and its git address (below). It is never printed.
118
161
  - Those two addresses change only if you set `SUPERINTERN_SITES_API_BASE` or
119
162
  `SUPERINTERN_SITES_GIT_BASE` yourself, which is for running against a
@@ -131,7 +174,7 @@ To go back to an earlier version: `sites status` shows the numbers, then
131
174
 
132
175
  | | Test service (the default today) | Production (`--env prod`, not open yet) |
133
176
  |---|---|---|
134
- | Site service | `https://test-api.superintern.ai` | `https://api.superintern.ai` |
177
+ | Service | `https://test-api.superintern.ai` | `https://api.superintern.ai` |
135
178
  | Git address | `https://git.testforstuff.com` | `https://git.superintern.app` |
136
179
  | A site's address | `https://<name>.testforstuff.com` | `https://<name>.superintern.app` |
137
180
 
package/bin/sites.mjs CHANGED
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env node
2
- // sites: make a site, see how it is doing, and put it online.
3
- // It talks to the site API and to the git address, and to nothing else.
2
+ // sites: make a site, see how it is doing, and put it online; and every tool
3
+ // of SuperIntern's MCP server, the same ones Claude is given (`sites crm …`,
4
+ // `sites site …`). It talks to the service (its MCP server and site API) and
5
+ // to the git address, and to nothing else.
4
6
 
5
7
  import { spawn } from "node:child_process";
6
8
  import { fileURLToPath } from "node:url";
@@ -13,6 +15,7 @@ import { runGit } from "../src/cli/git.mjs";
13
15
  import { defaultKeyStore } from "../src/cli/keys.mjs";
14
16
  import { PACKAGE_VERSION } from "../src/cli/package-info.mjs";
15
17
  import { readLine, readSecret } from "../src/cli/prompt.mjs";
18
+ import { GROUPS } from "../src/cli/tools.mjs";
16
19
 
17
20
  const USAGE = `Usage:
18
21
  sites login Save your key (you are asked for it; it is not shown)
@@ -28,16 +31,39 @@ const USAGE = `Usage:
28
31
  sites clone <slug> Bring an existing site's files here
29
32
  sites tables [<slug>] What the business's CRM lets the site show
30
33
  sites rows <table> [<slug>] The first rows of one of those tables
34
+ sites accounts [<slug>] Whether customers can sign in to the site, and how
35
+ sites accounts [<slug>] --sign-in on|off [--passwords on|off]
36
+ Switch customer accounts (and passwords, --google,
37
+ --microsoft) on or off
38
+ sites media add <file...> Put large pictures, video, audio and PDFs in the
39
+ site's media files, served at /_media/<name>
40
+ sites media list The site's media files, and the room they take
41
+ sites media remove <name...> Delete media files
31
42
  sites link In a site's folder: tie it to this copy of sites
43
+ sites crm <command> [options] The business's CRM: tables, records, saved views and
44
+ automations. See them all: sites crm --help
45
+ sites site <command> [options] Everything else about websites: settings, history,
46
+ media, redirects, analytics, traffic, search and more.
47
+ See them all: sites site --help
32
48
  sites logout Remove your saved key
33
49
  sites --version Which version of sites this is
34
50
 
35
- In a site's folder, status, tables and rows need no <slug>. A table is named by its
51
+ In a site's folder, status, tables, rows, accounts and media need no <slug>. A table is named by its
36
52
  name (in quotes when it has a space), its slug or its id.
37
53
 
54
+ \`sites crm\` and \`sites site\` are SuperIntern's MCP tools, the same ones Claude
55
+ is given, read from the service: what your key may do decides which you can use.
56
+ Each tool's arguments are its options (\`sites crm record-save --help\`); a list or
57
+ an object is given as JSON. --json prints the service's answer as it came.
58
+
38
59
  A push never changes the live site. After a push, the site is built and
39
60
  checked; \`sites publish\` puts the result online.
40
61
 
62
+ A site's own files are at most 25 MB each and 25 MB together, zipped. Large
63
+ pictures, video, audio and PDFs go in its media files instead
64
+ (\`sites media add\`): up to 95 MB each, kept outside its versions, and on the
65
+ site straight away at /_media/<name>.
66
+
41
67
  Options:
42
68
  --env <test|prod> Which service to use. Default: ${DEFAULT_ENVIRONMENT}.
43
69
  --name <name> With new: what the business is called.
@@ -50,8 +76,11 @@ Options:
50
76
  --timeout <seconds> With status --wait: how long to wait. Default: 900.
51
77
  --limit <n> With rows: how many rows to show. Default: 5. At most 50.
52
78
  --site <slug> With tables and rows: the site, the same as naming it
53
- after the command.
54
- --json With list and status: print everything as JSON.
79
+ after the command. With media: the site.
80
+ --sign-in, --passwords, --google, --microsoft <on|off>
81
+ With accounts: what to switch. Only what is given changes.
82
+ --json With list, status, accounts and media list: print everything
83
+ as JSON.
55
84
  With tables and rows: print the service's reply.
56
85
  --no-check With login: save the key without checking it.
57
86
  --yes With remove: don't ask to type the site's name. For scripts.
@@ -72,6 +101,14 @@ async function main() {
72
101
  return 0;
73
102
  }
74
103
 
104
+ // `sites crm …` and `sites site …` take the options of the tool they name.
105
+ const invocation = toolInvocation(typed);
106
+ if (invocation) {
107
+ const environment = resolveEnvironment(invocation.env);
108
+ if (environment.overridden) console.error(`Using ${environment.api} and ${environment.git} (set by SUPERINTERN_SITES_API_BASE / SUPERINTERN_SITES_GIT_BASE).`);
109
+ return commands.tool(context(environment), invocation.group, invocation.rest, { json: invocation.json, help: invocation.help });
110
+ }
111
+
75
112
  const { values, positionals } = parseArgs({
76
113
  allowPositionals: true,
77
114
  options: {
@@ -85,6 +122,10 @@ async function main() {
85
122
  json: { type: "boolean" },
86
123
  limit: { type: "string" },
87
124
  site: { type: "string" },
125
+ "sign-in": { type: "string" },
126
+ passwords: { type: "string" },
127
+ google: { type: "string" },
128
+ microsoft: { type: "string" },
88
129
  "no-check": { type: "boolean" },
89
130
  yes: { type: "boolean" },
90
131
  help: { type: "boolean", short: "h" },
@@ -97,28 +138,12 @@ async function main() {
97
138
  }
98
139
  // Quietly ignoring it would show one site to someone who named another.
99
140
  if (values.site !== undefined && !TAKES_SITE.has(command)) {
100
- throw new Error(`\`sites ${command}\` does not take --site: it goes with tables, rows and data-token only.`);
141
+ throw new Error(`\`sites ${command}\` does not take --site: it goes with tables, rows, media and data-token only.`);
101
142
  }
102
143
 
103
144
  const environment = resolveEnvironment(values.env);
104
- /** @type {commands.Context} */
105
- const ctx = {
106
- environment,
107
- keys: defaultKeyStore(),
108
- api: (key) => createApi({ base: environment.api, key }),
109
- git: runGit,
110
- out: (line = "") => console.log(line),
111
- note: (line = "") => console.error(line),
112
- cwd: process.cwd(),
113
- self: { node: process.execPath, script: fileURLToPath(import.meta.url) },
114
- readSecret: () => readSecret(`Paste your key for ${environment.name} (it will not be shown): `),
115
- ask: readLine,
116
- openUrl,
117
- sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
118
- now: Date.now,
119
- };
145
+ const ctx = context(environment);
120
146
 
121
- // Git calls this many times and reads the answer: nothing else may be printed.
122
147
  if (command === "git-credential") {
123
148
  return commands.gitCredential(ctx, rest[0] ?? "", await readAll());
124
149
  }
@@ -158,6 +183,16 @@ async function main() {
158
183
  return commands.tables(ctx, slug, { json: values.json, site: values.site });
159
184
  case "rows":
160
185
  return commands.rows(ctx, rest[0], rest[1], { json: values.json, limit: values.limit, site: values.site });
186
+ case "accounts":
187
+ return commands.accounts(ctx, slug, {
188
+ signIn: values["sign-in"],
189
+ passwords: values.passwords,
190
+ google: values.google,
191
+ microsoft: values.microsoft,
192
+ json: values.json,
193
+ });
194
+ case "media":
195
+ return commands.media(ctx, rest[0], rest.slice(1), { site: values.site, json: values.json });
161
196
  case "link":
162
197
  return commands.link(ctx);
163
198
  // A site's build runs this and reads the one line it prints.
@@ -168,10 +203,58 @@ async function main() {
168
203
  }
169
204
  }
170
205
 
206
+ /**
207
+ * Everything a command needs.
208
+ * @param {import("../src/cli/environments.mjs").CliEnvironment} environment
209
+ * @returns {commands.Context}
210
+ */
211
+ function context(environment) {
212
+ return {
213
+ environment,
214
+ keys: defaultKeyStore(),
215
+ api: (key) => createApi({ base: environment.api, key }),
216
+ git: runGit,
217
+ out: (line = "") => console.log(line),
218
+ note: (line = "") => console.error(line),
219
+ cwd: process.cwd(),
220
+ self: { node: process.execPath, script: fileURLToPath(import.meta.url) },
221
+ readSecret: () => readSecret(`Paste your key for ${environment.name} (it will not be shown): `),
222
+ ask: readLine,
223
+ openUrl,
224
+ sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
225
+ now: Date.now,
226
+ };
227
+ }
228
+
229
+ /**
230
+ * `sites [--env <name>] crm|site …`: the group, the environment, and the rest
231
+ * as typed. Null for every other command.
232
+ * @param {string[]} typed
233
+ */
234
+ function toolInvocation(typed) {
235
+ /** @type {string | undefined} */
236
+ let env;
237
+ let json = false;
238
+ let help = false;
239
+ /** @type {string[]} */
240
+ const rest = [];
241
+ for (let index = 0; index < typed.length; index++) {
242
+ const word = /** @type {string} */ (typed[index]);
243
+ if (word === "--env") env = typed[++index];
244
+ else if (word.startsWith("--env=")) env = word.slice(6);
245
+ else if (word === "--json") json = true;
246
+ else if (word === "--help" || word === "-h") help = true;
247
+ else rest.push(word);
248
+ }
249
+ const [group, ...after] = rest;
250
+ if (!group || !Object.hasOwn(GROUPS, group)) return null;
251
+ return { group, rest: after, env, json, help };
252
+ }
253
+
171
254
  class UsageError extends Error {}
172
255
 
173
256
  /** The commands that can be told their site with `--site` as well as after the command. */
174
- const TAKES_SITE = new Set(["tables", "rows", "data-token"]);
257
+ const TAKES_SITE = new Set(["tables", "rows", "media", "data-token"]);
175
258
 
176
259
  /** @param {string} value @param {string} flag */
177
260
  function positiveNumber(value, flag) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superintern/sites",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Make a website for a business on SuperIntern, see how it is doing, and put it online.",
5
5
  "keywords": [
6
6
  "superintern",
@@ -24,9 +24,13 @@
24
24
  "src/cli/environments.mjs",
25
25
  "src/cli/git.mjs",
26
26
  "src/cli/keys.mjs",
27
+ "src/cli/mcp-tools.json",
28
+ "src/cli/mcp.mjs",
29
+ "src/cli/media.mjs",
27
30
  "src/cli/names.mjs",
28
31
  "src/cli/package-info.mjs",
29
- "src/cli/prompt.mjs"
32
+ "src/cli/prompt.mjs",
33
+ "src/cli/tools.mjs"
30
34
  ],
31
35
  "engines": {
32
36
  "node": ">=22.12"
package/src/cli/api.mjs CHANGED
@@ -1,7 +1,15 @@
1
- // The site API, as the `sites` command uses it.
1
+ // The service, as the `sites` command uses it.
2
2
  //
3
- // Every address and shape the command relies on is in this one file, and
4
- // follows the service's own description of its API. Change them here.
3
+ // Websites are read and changed through SuperIntern's MCP server (mcp.mjs),
4
+ // the same tools Claude is given, so the command and the server can never
5
+ // offer different things. Three calls have no tool and stay on the site API
6
+ // (`/sites/v1`): whose key this is (`me`), and the pass a site's build uses to
7
+ // read what the business made public in its CRM (`dataPass`, `data`).
8
+ //
9
+ // Every address and shape the command relies on is in this file and mcp.mjs.
10
+ // Change them here.
11
+
12
+ import { createMcp } from "./mcp.mjs";
5
13
 
6
14
  export class ApiError extends Error {
7
15
  /**
@@ -15,6 +23,8 @@ export class ApiError extends Error {
15
23
  /** What code branches on. */
16
24
  this.code = detail.code;
17
25
  this.body = detail.body;
26
+ /** @type {string | undefined} the MCP tool whose own refusal this is */
27
+ this.tool = undefined;
18
28
  }
19
29
  }
20
30
 
@@ -176,6 +186,10 @@ async function send(doFetch, url, { method, bearer, json }) {
176
186
  * @param {{ base: string, key: string, fetch?: typeof fetch }} options
177
187
  */
178
188
  export function createApi({ base, key, fetch: doFetch = fetch }) {
189
+ const mcp = createMcp({ base, key, fetch: doFetch });
190
+ /** `site_read`'s whole answer. @param {string} site its slug or id */
191
+ const readSite = async (site) => (await mcp.call("site_read", { site })).data ?? {};
192
+
179
193
  /**
180
194
  * A website key (scope sites.read or sites.write). The same key is the git address's password.
181
195
  * @param {string} method @param {string} path @param {unknown} [json]
@@ -187,6 +201,9 @@ export function createApi({ base, key, fetch: doFetch = fetch }) {
187
201
  const dataBase = `${base}/sites/v1/data`;
188
202
 
189
203
  return {
204
+ /** Every tool of SuperIntern's MCP server this key may use, and a way to run one. */
205
+ mcp,
206
+
190
207
  /**
191
208
  * Whose key this is, which agents it works on, and whether it may change
192
209
  * anything (`can_write`) or only read.
@@ -198,13 +215,11 @@ export function createApi({ base, key, fetch: doFetch = fetch }) {
198
215
  },
199
216
 
200
217
  /**
201
- * Sites the key may see. A key names its own agent, so none is needed.
202
- * @param {{ agentId?: string }} [options]
218
+ * The business's sites (`site_list`). A key names its own agent.
203
219
  * @returns {Promise<Site[]>}
204
220
  */
205
- async listSites(options = {}) {
206
- const query = options.agentId ? `?agent_id=${encodeURIComponent(options.agentId)}` : "";
207
- return (await call("GET", `/sites${query}`)).sites ?? [];
221
+ async listSites() {
222
+ return (await mcp.call("site_list")).data?.sites ?? [];
208
223
  },
209
224
 
210
225
  /**
@@ -213,83 +228,134 @@ export function createApi({ base, key, fetch: doFetch = fetch }) {
213
228
  * @returns {Promise<Site | null>}
214
229
  */
215
230
  async findSite(slug) {
216
- const sites = (await call("GET", `/sites?slug=${encodeURIComponent(slug)}`)).sites ?? [];
217
- // Never trust the filter alone: a server that ignores it returns every site.
218
- return sites.find((/** @type {Site} */ site) => site.slug === slug) ?? null;
231
+ try {
232
+ const site = (await readSite(slug)).site;
233
+ // Never trust the lookup alone: `site_read` also takes an id.
234
+ return site?.slug === slug ? site : null;
235
+ } catch (error) {
236
+ // The tool found no such site. A server that is not there at all is not that.
237
+ if (error instanceof ApiError && error.code === "not_found" && error.tool) return null;
238
+ throw error;
239
+ }
219
240
  },
220
241
 
221
242
  /**
222
- * The site belongs to the key's own business: no agent is named.
243
+ * The site belongs to the key's own business (`site_create`).
223
244
  * @param {{ slug: string, name?: string }} input
224
245
  * @returns {Promise<Site>}
225
246
  */
226
247
  async createSite(input) {
227
- return (await call("POST", "/sites", { slug: input.slug, name: input.name })).site;
248
+ return (await mcp.call("site_create", { slug: input.slug, name: input.name ?? input.slug })).data.site;
228
249
  },
229
250
 
230
251
  /**
231
252
  * Finish making a site's repository, for a site whose `git.ready` is
232
- * false. Refusals: `repository_in_progress` (wait), `repository_failed`
233
- * (try again), `repository_unavailable`.
253
+ * false: asking for git access to push does it (`site_git_access`).
254
+ * Refusals: `repository_in_progress` (wait), `repository_failed` (try
255
+ * again), `repository_unavailable`.
234
256
  * @param {string} id @returns {Promise<Site>}
235
257
  */
236
258
  async prepareRepository(id) {
237
- return (await call("POST", `/sites/${encodeURIComponent(id)}/repository`)).site;
259
+ await mcp.call("site_git_access", { site: id, access: "write" });
260
+ return (await readSite(id)).site;
238
261
  },
239
262
 
240
263
  /** @param {string} id @returns {Promise<Site>} */
241
264
  async getSite(id) {
242
- return (await call("GET", `/sites/${encodeURIComponent(id)}`)).site;
243
- },
244
-
245
- /**
246
- * Newest first.
247
- * @param {string} id @param {{ limit?: number }} [options]
248
- * @returns {Promise<Version[]>}
249
- */
250
- async versions(id, options = {}) {
251
- return (await call("GET", `/sites/${encodeURIComponent(id)}/versions?limit=${options.limit ?? 20}`)).versions ?? [];
265
+ return (await readSite(id)).site;
252
266
  },
253
267
 
254
268
  /**
255
- * Newest first; null when the service has no build information.
256
- * @param {string} id @param {{ limit?: number }} [options]
257
- * @returns {Promise<Build[] | null>}
269
+ * The site with its latest versions and builds, newest first
270
+ * (`site_read`: the latest few of each).
271
+ * @param {string} id @param {{ wait?: boolean }} [options]
272
+ * @returns {Promise<{ site: Site, versions: Version[], builds: Build[] }>}
258
273
  */
259
- async builds(id, options = {}) {
260
- try {
261
- return (await call("GET", `/sites/${encodeURIComponent(id)}/builds?limit=${options.limit ?? 10}`)).builds ?? [];
262
- } catch (error) {
263
- // The site was found a moment ago, so "not found" here is the route.
264
- if (error instanceof ApiError && error.status === 404) return null;
265
- throw error;
266
- }
274
+ async readSite(id, options = {}) {
275
+ // wait_for_build: the server holds the answer (up to 90 s) until the newest push's build ends, and always lists that build.
276
+ const data = options.wait ? ((await mcp.call("site_read", { site: id, wait_for_build: true })).data ?? {}) : await readSite(id);
277
+ return { site: data.site, versions: Array.isArray(data.versions) ? data.versions : [], builds: Array.isArray(data.builds) ? data.builds : [] };
267
278
  },
268
279
 
269
280
  /**
270
- * Serve a version; the newest ready one when none is named.
271
- * Needs a key that may change sites.
272
- * @param {string} id @param {string} [versionId]
281
+ * Serve a version (`site_publish`): the newest ready one when no number
282
+ * is given. Needs a key that may change sites.
283
+ * @param {string} id @param {number} [number] the version's number, as `site_read` shows it
273
284
  * @returns {Promise<Site>}
274
285
  */
275
- async publish(id, versionId) {
276
- return (await call("POST", `/sites/${encodeURIComponent(id)}/publish`, versionId ? { version_id: versionId } : {})).site;
286
+ async publish(id, number) {
287
+ return (await mcp.call("site_publish", number === undefined ? { site: id } : { site: id, version: number })).data.site;
277
288
  },
278
289
 
279
290
  /** @param {string} id @returns {Promise<Site>} */
280
291
  async unpublish(id) {
281
- return (await call("POST", `/sites/${encodeURIComponent(id)}/unpublish`, {})).site;
292
+ return (await mcp.call("site_unpublish", { site: id })).data.site;
282
293
  },
283
294
 
284
295
  /**
285
- * Remove a site for good. The service asks for its slug again, as the
286
- * person typed it, so an id from the wrong place removes nothing.
287
- * Needs a key that may change sites.
296
+ * Remove a site for good (`site_remove`). The service asks for its slug
297
+ * again, as the person typed it, so an id from the wrong place removes
298
+ * nothing. Needs a key that may change sites.
288
299
  * @param {string} id @param {string} slug
289
300
  * @returns {Promise<RemovedSite>}
290
301
  */
291
302
  async removeSite(id, slug) {
292
- return (await call("POST", `/sites/${encodeURIComponent(id)}/remove`, { confirm: slug })).site;
303
+ return (await mcp.call("site_remove", { site: id, confirm: slug })).data.site;
304
+ },
305
+
306
+ /**
307
+ * Whether customers may sign in to the site, and how: `{enabled,
308
+ * passwords, providers: {google: {enabled, available}, microsoft: …}}`,
309
+ * from `site_read`. A key that may only look is enough.
310
+ * @param {string} id @returns {Promise<SignInSwitches>}
311
+ */
312
+ async signInSwitches(id) {
313
+ const data = await readSite(id);
314
+ if (data.customer_accounts && typeof data.customer_accounts === "object") return data.customer_accounts;
315
+ throw new ApiError("This service doesn't show customer accounts to a key yet.", { status: 404, code: "no_customer_accounts" });
316
+ },
317
+
318
+ /**
319
+ * Switch customer sign-in (`site_update`'s `customer_accounts`): only what
320
+ * is sent changes (`enabled`, `passwords`, `google`, `microsoft`, each
321
+ * true or false). Needs a key that may change sites.
322
+ * @param {string} id @param {Partial<Record<"enabled" | "passwords" | "google" | "microsoft", boolean>>} changes
323
+ * @returns {Promise<SignInSwitches>}
324
+ */
325
+ async switchSignIn(id, changes) {
326
+ return (await mcp.call("site_update", { site: id, customer_accounts: changes })).data.customer_accounts;
327
+ },
328
+
329
+ /**
330
+ * The site's media files, whether it may have them here (`available`),
331
+ * and how much room they take (`usage`) (`site_media_list`). A key that
332
+ * may only look is enough.
333
+ * @param {string} id @returns {Promise<MediaList>}
334
+ */
335
+ async mediaFiles(id) {
336
+ const data = (await mcp.call("site_media_list", { site: id })).data;
337
+ return { available: data?.available !== false, files: Array.isArray(data?.files) ? data.files : [], usage: data?.usage ?? {} };
338
+ },
339
+
340
+ /**
341
+ * Ask for an address to send one media file to (`site_media_upload`): its
342
+ * name on this computer (the service makes the name it is kept under) and
343
+ * its exact size in bytes. Needs a key that may change sites.
344
+ * @param {string} id @param {{ name: string, size: number }} file
345
+ * @returns {Promise<MediaUpload>}
346
+ */
347
+ async startMediaUpload(id, file) {
348
+ return (await mcp.call("site_media_upload", { site: id, name: file.name, size: file.size })).data;
349
+ },
350
+
351
+ /**
352
+ * Stop serving one media file and delete it (`site_media_delete`). Needs
353
+ * a key that may change sites.
354
+ * @param {string} id @param {string} name
355
+ */
356
+ async deleteMedia(id, name) {
357
+ if (!pathWord(name)) throw new Error("That is not a media file's name, so nothing was deleted.");
358
+ return (await mcp.call("site_media_delete", { site: id, name })).data;
293
359
  },
294
360
 
295
361
  /**
@@ -345,4 +411,38 @@ export function createApi({ base, key, fetch: doFetch = fetch }) {
345
411
  * @property {string | null} name_free_for_others_at when another business may take its name
346
412
  */
347
413
 
414
+ /**
415
+ * Customer sign-in on a site.
416
+ * @typedef {object} SignInSwitches
417
+ * @property {boolean} enabled customers may sign in, with a code by email
418
+ * @property {boolean} passwords they may also choose a password
419
+ * @property {Record<string, { enabled?: boolean, available?: boolean }>} [providers] Google and Microsoft
420
+ */
421
+
422
+ /**
423
+ * One media file of a site.
424
+ * @typedef {object} MediaFile
425
+ * @property {string} name what it is kept under, such as `summer-menu.pdf`
426
+ * @property {string} path `/_media/<name>`
427
+ * @property {number} size
428
+ * @property {string} [content_type]
429
+ * @property {string} [state]
430
+ * @property {string | null} [ready_at]
431
+ */
432
+
433
+ /**
434
+ * @typedef {object} MediaList
435
+ * @property {boolean} available false where sites may not keep media files yet
436
+ * @property {MediaFile[]} files
437
+ * @property {{ files?: number, bytes?: number, max_files?: number, max_bytes?: number, max_file_bytes?: number }} usage
438
+ */
439
+
440
+ /**
441
+ * Where and how to send one media file: the file as it will be listed, and
442
+ * an address on the site's free address that takes it for an hour.
443
+ * @typedef {object} MediaUpload
444
+ * @property {MediaFile} media
445
+ * @property {{ url: string, method?: string, headers?: Record<string, string>, expires_at?: string }} upload
446
+ */
447
+
348
448
  /** @typedef {ReturnType<typeof createApi>} Api */