@settlemint/dalp-cli 3.1.32-main.42202049300 → 3.1.32-main.42217736252

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,103 @@
1
+ import { authRequest } from "../lib/auth-request.ts";
2
+ import { Cli, z } from "incur";
3
+ import { saveGlobalConfig } from "../lib/cli-config.ts";
4
+ import { loadCredentials, saveCredentials } from "../lib/credential-store.ts";
5
+ import { runCommand } from "../lib/run-command.ts";
6
+ import { cliVars } from "../lib/vars.ts";
7
+
8
+ const ApiKeyOrganizationSchema = z.object({
9
+ metadata: z.object({ organizationId: z.string().min(1).optional() }).nullish(),
10
+ });
11
+
12
+ export const orgCommand = Cli.create("org", {
13
+ description: "Active organization for organization-scoped requests",
14
+ vars: cliVars,
15
+ });
16
+
17
+ orgCommand.command("list", {
18
+ description: "List organizations available to the current credentials",
19
+ async run(c) {
20
+ const { client, credentials } = c.var;
21
+ if (client === undefined || credentials === undefined) {
22
+ return c.error({
23
+ code: "NOT_AUTHENTICATED",
24
+ message: "Not logged in. Run 'dalp login' to authenticate.",
25
+ retryable: true,
26
+ });
27
+ }
28
+ return runCommand(
29
+ c,
30
+ (async () => {
31
+ const data = await authRequest(credentials.apiUrl, credentials, "/organization/list");
32
+ return {
33
+ organizations: data,
34
+ activeOrgId: credentials?.activeOrgId ?? null,
35
+ };
36
+ })()
37
+ );
38
+ },
39
+ });
40
+
41
+ orgCommand.command("switch", {
42
+ description: "Save the organization of the stored API key as the active organization",
43
+ args: z.object({
44
+ organizationId: z.string().describe("Organization id to activate"),
45
+ }),
46
+ async run(c) {
47
+ const { client, credentials } = c.var;
48
+ if (client === undefined || credentials === undefined) {
49
+ return c.error({
50
+ code: "NOT_AUTHENTICATED",
51
+ message: "Not logged in. Run 'dalp login' to authenticate.",
52
+ retryable: true,
53
+ });
54
+ }
55
+ return runCommand(
56
+ c,
57
+ (async () => {
58
+ const key = ApiKeyOrganizationSchema.safeParse(
59
+ await authRequest(
60
+ credentials.apiUrl,
61
+ credentials,
62
+ `/api-key/get?id=${encodeURIComponent(credentials.apiKeyId)}`
63
+ )
64
+ );
65
+ const keyOrganizationId = key.data?.metadata?.organizationId;
66
+ if (keyOrganizationId === undefined) {
67
+ return c.error({
68
+ code: "KEY_HAS_NO_ORGANIZATION",
69
+ message: "The stored API key names no organization. Run 'dalp login' to get a key for one.",
70
+ retryable: false,
71
+ });
72
+ }
73
+ if (keyOrganizationId !== c.args.organizationId) {
74
+ return c.error({
75
+ code: "ORGANIZATION_NOT_ON_KEY",
76
+ message: `The stored API key belongs to \`${keyOrganizationId}\`, not \`${c.args.organizationId}\`. Run 'dalp login' with \`${c.args.organizationId}\` as the active organization to get a key for it.`,
77
+ retryable: false,
78
+ });
79
+ }
80
+ const latest = (await loadCredentials()) ?? credentials;
81
+ await saveCredentials({
82
+ ...latest,
83
+ activeOrgId: c.args.organizationId,
84
+ });
85
+ await saveGlobalConfig({ defaultOrg: c.args.organizationId });
86
+ return {
87
+ activeOrgId: c.args.organizationId,
88
+ message: "Active organization saved. It matches the organization of the stored API key.",
89
+ };
90
+ })()
91
+ );
92
+ },
93
+ });
94
+
95
+ orgCommand.command("current", {
96
+ description: "Show the active organization id saved with the stored credentials",
97
+ run(c) {
98
+ return {
99
+ activeOrgId: c.var.credentials?.activeOrgId ?? null,
100
+ apiUrl: c.var.credentials?.apiUrl ?? null,
101
+ };
102
+ },
103
+ });
@@ -0,0 +1,29 @@
1
+ import { Cli } from "incur";
2
+ import { runCommand } from "../lib/run-command.ts";
3
+ import { cliVars } from "../lib/vars.ts";
4
+
5
+ export const whoamiCommand = Cli.create("whoami", {
6
+ description: "Show current authenticated user and organization",
7
+ vars: cliVars,
8
+ async run(c) {
9
+ const { client, credentials } = c.var;
10
+ if (client === undefined) {
11
+ return c.error({
12
+ code: "NOT_AUTHENTICATED",
13
+ message: "Not logged in. Run 'dalp login' to authenticate.",
14
+ retryable: true,
15
+ });
16
+ }
17
+ return runCommand(
18
+ c,
19
+ (async () => {
20
+ const data = await client.execute("party.users.me");
21
+ return {
22
+ user: data,
23
+ organizationId: credentials?.activeOrgId ?? null,
24
+ apiUrl: credentials?.apiUrl ?? null,
25
+ };
26
+ })()
27
+ );
28
+ },
29
+ });