@zumino/cli 2.1.0 → 2.2.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/src/users.js ADDED
@@ -0,0 +1,38 @@
1
+ import { api } from "./client.js";
2
+ import { CliError } from "./errors.js";
3
+ import { assertUserRef } from "./flags.js";
4
+
5
+ /*
6
+ * Turning what a person typed into a user id.
7
+ *
8
+ * The shape check — an email refused, `me` and `-` recognised — is
9
+ * `assertUserRef` in `flags.js`, because it is about the argument and needs no
10
+ * credential. What is left here is the one part that does: resolving `me`
11
+ * against `GET /me`, so a script never needs a second command and a `jq`
12
+ * expression to name its own user.
13
+ */
14
+
15
+ /**
16
+ * @param {any} ctx
17
+ * @param {string} value a user id, `me`, or `-`/`none` to clear
18
+ * @param {{flag: string, allowClear?: boolean}} opts
19
+ * @returns {Promise<string|null>} `null` only when clearing is allowed and asked for
20
+ */
21
+ export async function resolveUser(ctx, value, opts) {
22
+ return resolveUserRef(ctx, assertUserRef(value, opts));
23
+ }
24
+
25
+ /** The same, for a caller that has already classified the value. */
26
+ export async function resolveUserRef(ctx, ref) {
27
+ if (ref.kind === "clear") return null;
28
+ if (ref.kind === "id") return ref.id;
29
+
30
+ const me = await api(ctx, "GET", "/me");
31
+ const id = me?.user?.id;
32
+ if (!id) {
33
+ throw new CliError(`This token's owner could not be read, so "me" has no id.`, {
34
+ hint: "Check the credential with: zumino auth status",
35
+ });
36
+ }
37
+ return id;
38
+ }