@myapihq/cli 2.25.1 → 2.26.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/dist/commands/init.d.ts +7 -0
- package/dist/commands/init.js +134 -0
- package/dist/commands/org-delete-confirm.test.d.ts +1 -0
- package/dist/commands/org-delete-confirm.test.js +121 -0
- package/dist/commands/org.js +29 -1
- package/dist/completion.js +1 -1
- package/dist/config.d.ts +11 -0
- package/dist/config.js +34 -0
- package/dist/exposes.test.js +1 -0
- package/dist/helpers.d.ts +16 -0
- package/dist/helpers.js +50 -5
- package/dist/index.js +5 -0
- package/dist/org-resolution.test.d.ts +1 -0
- package/dist/org-resolution.test.js +157 -0
- package/dist/project.d.ts +20 -0
- package/dist/project.js +59 -0
- package/dist/skills/my-api-hq/SKILL.md +13 -10
- package/package.json +2 -2
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Flags } from '../helpers.js';
|
|
2
|
+
import type { FlagSchema } from '../flags.js';
|
|
3
|
+
import type { Exposes } from '../exposes.js';
|
|
4
|
+
export declare const SCHEMA: FlagSchema;
|
|
5
|
+
export declare const EXPOSES: Exposes;
|
|
6
|
+
export declare const HELP = "Usage: myapi init --org <org_id> [--name <label>] [--spend-cap <usd>]\n\nBind THIS DIRECTORY to one org, so commands run here can never target another.\n\nIt mints an API key locked to that org, stores it, and writes .myapi.json\nnaming the org. The file holds the org id and never the key.\n\n --org <id> The org this project works in (required)\n --name <label> Key name; defaults to the directory name\n --spend-cap <usd> Ceiling for this key, e.g. --spend-cap 50\n --grant <list> Narrow what it can touch, e.g. --grant crm:write,email:read\n --force Re-bind a directory that already names an org\n\nWhy this exists: `myapi config set-org` writes a default into ~/.myapi/config.json,\none file for the whole machine. Two projects on one laptop overwrite each other's,\nand commands then land in the wrong org while still succeeding. A key that the\nserver has locked to one org cannot do that \u2014 a wrong target is refused (403),\nnot silently accepted.";
|
|
7
|
+
export declare function run(_sub: string | undefined, _args: string[], flags: Flags): Promise<void>;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { hq } from '@myapihq/sdk';
|
|
2
|
+
import { requireConfig, saveConfig, accountLockedTo, loadConfig } from '../config.js';
|
|
3
|
+
import { success, error, info, printJson } from '../output.js';
|
|
4
|
+
import { writeProjectOrg, findProjectOrg, PROJECT_FILE } from '../project.js';
|
|
5
|
+
import { _parseGrants, _dollarsToCents } from './keys.js';
|
|
6
|
+
export const SCHEMA = {
|
|
7
|
+
org: 'string',
|
|
8
|
+
name: 'string',
|
|
9
|
+
// string + _dollarsToCents, matching `keys create` exactly.
|
|
10
|
+
'spend-cap': 'string',
|
|
11
|
+
grant: 'string',
|
|
12
|
+
force: 'boolean',
|
|
13
|
+
};
|
|
14
|
+
export const EXPOSES = ['POST /hq/account/create/key', 'GET /hq/orgs/{id}'];
|
|
15
|
+
export const HELP = `Usage: myapi init --org <org_id> [--name <label>] [--spend-cap <usd>]
|
|
16
|
+
|
|
17
|
+
Bind THIS DIRECTORY to one org, so commands run here can never target another.
|
|
18
|
+
|
|
19
|
+
It mints an API key locked to that org, stores it, and writes ${PROJECT_FILE}
|
|
20
|
+
naming the org. The file holds the org id and never the key.
|
|
21
|
+
|
|
22
|
+
--org <id> The org this project works in (required)
|
|
23
|
+
--name <label> Key name; defaults to the directory name
|
|
24
|
+
--spend-cap <usd> Ceiling for this key, e.g. --spend-cap 50
|
|
25
|
+
--grant <list> Narrow what it can touch, e.g. --grant crm:write,email:read
|
|
26
|
+
--force Re-bind a directory that already names an org
|
|
27
|
+
|
|
28
|
+
Why this exists: \`myapi config set-org\` writes a default into ~/.myapi/config.json,
|
|
29
|
+
one file for the whole machine. Two projects on one laptop overwrite each other's,
|
|
30
|
+
and commands then land in the wrong org while still succeeding. A key that the
|
|
31
|
+
server has locked to one org cannot do that — a wrong target is refused (403),
|
|
32
|
+
not silently accepted.`;
|
|
33
|
+
// keys.ts owns the grant syntax; this reuses its pure parser rather than
|
|
34
|
+
// writing a second one. Two parsers for one flag is exactly how `--org` and
|
|
35
|
+
// the campaign source flags drifted into disagreeing.
|
|
36
|
+
function centsOrDie(raw) {
|
|
37
|
+
const parsed = _dollarsToCents(raw);
|
|
38
|
+
if (typeof parsed === 'string')
|
|
39
|
+
error(parsed);
|
|
40
|
+
return parsed;
|
|
41
|
+
}
|
|
42
|
+
function grantsOrDie(raw) {
|
|
43
|
+
const parsed = _parseGrants(raw);
|
|
44
|
+
if (typeof parsed === 'string')
|
|
45
|
+
error(parsed);
|
|
46
|
+
return parsed;
|
|
47
|
+
}
|
|
48
|
+
export async function run(_sub, _args, flags) {
|
|
49
|
+
if (flags.help) {
|
|
50
|
+
info(HELP);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const config = requireConfig();
|
|
54
|
+
const orgId = typeof flags.org === 'string' ? flags.org : '';
|
|
55
|
+
if (!orgId) {
|
|
56
|
+
error('Missing --org <org_id>.\nWhich org does this project work in? List them with: myapi org list\n\n' + HELP);
|
|
57
|
+
}
|
|
58
|
+
const existing = findProjectOrg();
|
|
59
|
+
if (existing && !flags.force) {
|
|
60
|
+
if (existing.org === orgId) {
|
|
61
|
+
info(`Already bound: ${existing.file} names org ${orgId}.`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
error(`${existing.file} already binds this directory to org ${existing.org}.\n` +
|
|
65
|
+
`Re-bind to ${orgId} with --force, or edit the file directly.`);
|
|
66
|
+
}
|
|
67
|
+
// Name the org before touching anything, so a mistyped id fails here rather
|
|
68
|
+
// than after a key has been minted. It also puts the NAME in front of the
|
|
69
|
+
// user: a wrong UUID is unverifiable at a glance, a wrong name is obvious.
|
|
70
|
+
let orgName;
|
|
71
|
+
try {
|
|
72
|
+
orgName = (await hq.getOrg(config.api_key, orgId))?.name;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
error(`Could not read org ${orgId}. Check the id with: myapi org list`);
|
|
76
|
+
}
|
|
77
|
+
// Reuse a key already locked to this org rather than minting another. Keys
|
|
78
|
+
// are a revocation surface; one per project per org is enough, and silently
|
|
79
|
+
// accumulating them makes the list unreadable exactly when someone is trying
|
|
80
|
+
// to revoke the right one in a hurry.
|
|
81
|
+
const already = accountLockedTo(orgId);
|
|
82
|
+
if (already) {
|
|
83
|
+
const file = writeProjectOrg(process.cwd(), orgId);
|
|
84
|
+
success(`Bound this directory to ${orgName ?? orgId}`);
|
|
85
|
+
info(` ${file}`);
|
|
86
|
+
info(` using the key already locked to this org (${already.email ?? already.account_id})`);
|
|
87
|
+
if (flags.json)
|
|
88
|
+
printJson({ org_id: orgId, org_name: orgName, project_file: file, minted: false });
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const label = typeof flags.name === 'string' && flags.name
|
|
92
|
+
? flags.name
|
|
93
|
+
: (process.cwd().split('/').filter(Boolean).pop() ?? 'project');
|
|
94
|
+
// Same type and same parser as `keys create --spend-cap`. When one command
|
|
95
|
+
// declares a flag a string and another a number, the merged schema silently
|
|
96
|
+
// picks one and the loser is parsed wrong — that is how `--ttl` broke.
|
|
97
|
+
const capUsd = typeof flags['spend-cap'] === 'string' && flags['spend-cap']
|
|
98
|
+
? centsOrDie(flags['spend-cap'])
|
|
99
|
+
: undefined;
|
|
100
|
+
const grant = typeof flags.grant === 'string' ? flags.grant : undefined;
|
|
101
|
+
const minted = await hq.createApiKey(config.api_key, label, {
|
|
102
|
+
orgId,
|
|
103
|
+
...(capUsd !== undefined ? { spendCapCents: capUsd } : {}),
|
|
104
|
+
...(grant ? { grants: grantsOrDie(grant) } : {}),
|
|
105
|
+
});
|
|
106
|
+
const value = minted.api_key
|
|
107
|
+
?? minted.key;
|
|
108
|
+
if (!value) {
|
|
109
|
+
error('The API was asked for a key and returned none. Nothing was written; no project file was created.');
|
|
110
|
+
}
|
|
111
|
+
// Stored as its own account entry — saveConfig upserts by api_key, and
|
|
112
|
+
// deliberately does NOT make it active. A project credential must not become
|
|
113
|
+
// the identity every other terminal on this machine signs in as.
|
|
114
|
+
saveConfig({
|
|
115
|
+
...(loadConfig() ?? { api_key: value, account_id: config.account_id }),
|
|
116
|
+
api_key: value,
|
|
117
|
+
account_id: config.account_id,
|
|
118
|
+
email: config.email,
|
|
119
|
+
key_org_id: orgId,
|
|
120
|
+
default_org: orgId,
|
|
121
|
+
org_names: { ...(config.org_names ?? {}), ...(orgName ? { [orgId]: orgName } : {}) },
|
|
122
|
+
});
|
|
123
|
+
const file = writeProjectOrg(process.cwd(), orgId);
|
|
124
|
+
if (flags.json) {
|
|
125
|
+
printJson({ org_id: orgId, org_name: orgName, project_file: file, key_id: minted.id, minted: true });
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
success(`Bound this directory to ${orgName ?? orgId}`);
|
|
129
|
+
info(` ${file} names the org (no key in it — safe to commit)`);
|
|
130
|
+
info(` key "${label}" locked to this org${capUsd !== undefined ? `, capped at $${(capUsd / 100).toFixed(2)}` : ''}`);
|
|
131
|
+
info('');
|
|
132
|
+
info('Commands run here now target this org without --org, and a command that');
|
|
133
|
+
info('names a different one is refused before it is sent.');
|
|
134
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// `myapi org delete --yes` could not delete any org that owned anything.
|
|
2
|
+
//
|
|
3
|
+
// The API hard-deletes an org across 36 tables with no undo, so it refuses one
|
|
4
|
+
// that still owns resources unless the caller names it back: `?confirm=<id>`.
|
|
5
|
+
// That design is right. The SDK never sent the parameter, and the CLI's `--yes`
|
|
6
|
+
// only skipped its own local prompt — so the call came back 409 CONFIRM_REQUIRED
|
|
7
|
+
// with a message telling the user to "repeat the call with ?confirm=", which is
|
|
8
|
+
// HTTP-shaped advice a CLI user cannot act on.
|
|
9
|
+
//
|
|
10
|
+
// Creating an org provisions a funnel, so an org is non-empty from birth. This
|
|
11
|
+
// was not an edge case: the verb could not delete anything, while its own help
|
|
12
|
+
// said `--yes` was all a non-interactive run needed.
|
|
13
|
+
//
|
|
14
|
+
// Found by the probe, on its own cleanup step, on its first run — which is the
|
|
15
|
+
// argument for having a probe. It is invisible to the reverse request-field
|
|
16
|
+
// linter because `confirm` is a query parameter, and that linter reads request
|
|
17
|
+
// BODIES only; it says so in its own limits.
|
|
18
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
19
|
+
const ORG = '11111111-1111-4111-8111-111111111111';
|
|
20
|
+
const sdk = vi.hoisted(() => ({
|
|
21
|
+
hq: { getOrg: vi.fn(), deleteOrg: vi.fn() },
|
|
22
|
+
MyApiError: class MyApiError extends Error {
|
|
23
|
+
code = '';
|
|
24
|
+
status = 0;
|
|
25
|
+
},
|
|
26
|
+
}));
|
|
27
|
+
vi.mock('@myapihq/sdk', () => sdk);
|
|
28
|
+
vi.mock('../config.js', () => ({
|
|
29
|
+
requireConfig: () => ({ api_key: 'hq_live_test', default_org: ORG }),
|
|
30
|
+
loadConfig: () => ({ api_key: 'hq_live_test', default_org: ORG }),
|
|
31
|
+
CONFIG_DIR: '/tmp/nowhere',
|
|
32
|
+
}));
|
|
33
|
+
// Non-interactive, so `--yes` is the only way through and no prompt can hang.
|
|
34
|
+
vi.mock('../prompt.js', () => ({
|
|
35
|
+
confirm: vi.fn(async () => false),
|
|
36
|
+
isNonInteractive: () => true,
|
|
37
|
+
}));
|
|
38
|
+
function confirmRequired(owns) {
|
|
39
|
+
const e = new Error('CONFIRM_REQUIRED');
|
|
40
|
+
e.code = 'CONFIRM_REQUIRED';
|
|
41
|
+
e.status = 409;
|
|
42
|
+
e.body = { code: 'CONFIRM_REQUIRED', owns };
|
|
43
|
+
return e;
|
|
44
|
+
}
|
|
45
|
+
let printed;
|
|
46
|
+
let exitError;
|
|
47
|
+
beforeEach(async () => {
|
|
48
|
+
printed = [];
|
|
49
|
+
exitError = null;
|
|
50
|
+
// resetAllMocks, not clearAllMocks: `clear` resets call counts but leaves
|
|
51
|
+
// queued mockRejectedValueOnce values in place. The "refuses without --yes"
|
|
52
|
+
// case never consumes its queued rejection — it stops at the local guard —
|
|
53
|
+
// so with `clear` that rejection leaked into the next test and made it see a
|
|
54
|
+
// retry that its own setup never asked for.
|
|
55
|
+
vi.resetAllMocks();
|
|
56
|
+
sdk.hq.getOrg.mockResolvedValue({ id: ORG, name: 'Probe' });
|
|
57
|
+
const output = await import('../output.js');
|
|
58
|
+
vi.spyOn(output, 'info').mockImplementation(((m) => { printed.push(String(m)); }));
|
|
59
|
+
vi.spyOn(output, 'success').mockImplementation(((m) => { printed.push(String(m)); }));
|
|
60
|
+
vi.spyOn(output, 'error').mockImplementation(((m) => {
|
|
61
|
+
exitError = String(m);
|
|
62
|
+
throw new Error('__EXIT__');
|
|
63
|
+
}));
|
|
64
|
+
});
|
|
65
|
+
afterEach(() => vi.restoreAllMocks());
|
|
66
|
+
async function run(fn) {
|
|
67
|
+
try {
|
|
68
|
+
await fn();
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
if (e?.message !== '__EXIT__')
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
describe('org delete — the confirm gate', () => {
|
|
76
|
+
it('retries with confirm when the API asks, so --yes can actually delete', async () => {
|
|
77
|
+
sdk.hq.deleteOrg
|
|
78
|
+
.mockRejectedValueOnce(confirmRequired(['1 funnel', '1 CRM contact']))
|
|
79
|
+
.mockResolvedValueOnce(undefined);
|
|
80
|
+
const { del } = await import('./org.js');
|
|
81
|
+
await run(() => del(ORG, { yes: true }));
|
|
82
|
+
expect(sdk.hq.deleteOrg).toHaveBeenCalledTimes(2);
|
|
83
|
+
expect(sdk.hq.deleteOrg).toHaveBeenNthCalledWith(1, 'hq_live_test', ORG);
|
|
84
|
+
expect(sdk.hq.deleteOrg).toHaveBeenNthCalledWith(2, 'hq_live_test', ORG, { confirm: true });
|
|
85
|
+
expect(printed.join('\n')).toMatch(/Org .*deleted/);
|
|
86
|
+
});
|
|
87
|
+
it('shows what would be destroyed before destroying it', async () => {
|
|
88
|
+
// The refusal carries the inventory, and it is the ONLY place it can be
|
|
89
|
+
// seen — after the delete there is nothing left to look at.
|
|
90
|
+
sdk.hq.deleteOrg
|
|
91
|
+
.mockRejectedValueOnce(confirmRequired(['3 containers', '1 domain']))
|
|
92
|
+
.mockResolvedValueOnce(undefined);
|
|
93
|
+
const { del } = await import('./org.js');
|
|
94
|
+
await run(() => del(ORG, { yes: true }));
|
|
95
|
+
expect(printed.join('\n')).toContain('3 containers, 1 domain');
|
|
96
|
+
});
|
|
97
|
+
it('does not send confirm on the first attempt', async () => {
|
|
98
|
+
// An empty org deletes without ceremony; sending confirm unconditionally
|
|
99
|
+
// would turn the gate into decoration for every org.
|
|
100
|
+
sdk.hq.deleteOrg.mockResolvedValueOnce(undefined);
|
|
101
|
+
const { del } = await import('./org.js');
|
|
102
|
+
await run(() => del(ORG, { yes: true }));
|
|
103
|
+
expect(sdk.hq.deleteOrg).toHaveBeenCalledTimes(1);
|
|
104
|
+
expect(sdk.hq.deleteOrg).toHaveBeenCalledWith('hq_live_test', ORG);
|
|
105
|
+
});
|
|
106
|
+
it('refuses without --yes rather than confirming on the user behalf', async () => {
|
|
107
|
+
sdk.hq.deleteOrg.mockRejectedValueOnce(confirmRequired(['1 funnel']));
|
|
108
|
+
const { del } = await import('./org.js');
|
|
109
|
+
await run(() => del(ORG, {}));
|
|
110
|
+
// Non-interactive without --yes stops at the local guard, before any call.
|
|
111
|
+
expect(exitError).toMatch(/--yes/);
|
|
112
|
+
});
|
|
113
|
+
it('passes other errors through instead of retrying blind', async () => {
|
|
114
|
+
const boom = new Error('NOT_FOUND');
|
|
115
|
+
boom.code = 'NOT_FOUND';
|
|
116
|
+
sdk.hq.deleteOrg.mockRejectedValueOnce(boom);
|
|
117
|
+
const { del } = await import('./org.js');
|
|
118
|
+
await expect(del(ORG, { yes: true })).rejects.toThrow('NOT_FOUND');
|
|
119
|
+
expect(sdk.hq.deleteOrg).toHaveBeenCalledTimes(1);
|
|
120
|
+
});
|
|
121
|
+
});
|
package/dist/commands/org.js
CHANGED
|
@@ -135,7 +135,35 @@ export async function del(id, flags) {
|
|
|
135
135
|
if (!ok)
|
|
136
136
|
error('Aborted.');
|
|
137
137
|
}
|
|
138
|
-
|
|
138
|
+
// First without confirm. The API refuses a non-empty org with an inventory of
|
|
139
|
+
// what would be destroyed, and that inventory is the only place it can be
|
|
140
|
+
// seen — after the delete there is nothing left to look at. So the refusal is
|
|
141
|
+
// not an error to route around; it is the thing worth showing.
|
|
142
|
+
try {
|
|
143
|
+
await hq.deleteOrg(config.api_key, id);
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
const err = e;
|
|
147
|
+
if (err?.code !== 'CONFIRM_REQUIRED')
|
|
148
|
+
throw e;
|
|
149
|
+
// `body` is the backend's error object verbatim; CONFIRM_REQUIRED carries
|
|
150
|
+
// the inventory in `owns`.
|
|
151
|
+
const owns = Array.isArray(err.body?.owns) ? err.body.owns : undefined;
|
|
152
|
+
info(owns?.length
|
|
153
|
+
? `This org still owns ${owns.join(', ')}. Deleting it destroys all of that permanently.`
|
|
154
|
+
: 'This org still owns resources. Deleting it destroys all of them permanently.');
|
|
155
|
+
if (!flags.yes) {
|
|
156
|
+
const ok = await confirm(`› Delete ${label} and everything it owns? (y/N) `, false);
|
|
157
|
+
if (!ok)
|
|
158
|
+
error('Aborted. Nothing was deleted.');
|
|
159
|
+
}
|
|
160
|
+
// --yes already means "I am not being asked again", and the inventory has
|
|
161
|
+
// been printed either way. Previously this path did not exist at all: the
|
|
162
|
+
// API told the caller to "repeat the call with ?confirm=", advice a CLI
|
|
163
|
+
// user could not act on, so `org delete --yes` could not delete any org
|
|
164
|
+
// that owned anything — which is every org, since create provisions a funnel.
|
|
165
|
+
await hq.deleteOrg(config.api_key, id, { confirm: true });
|
|
166
|
+
}
|
|
139
167
|
success(`Org ${label} deleted`);
|
|
140
168
|
}
|
|
141
169
|
// Update one or more fields of an org. At least one --flag must be supplied;
|
package/dist/completion.js
CHANGED
|
@@ -27,7 +27,7 @@ const BLOCK_END = `# end ${PROGRAM} completion`;
|
|
|
27
27
|
// is missing here.
|
|
28
28
|
export const COMMANDS = [
|
|
29
29
|
'account', 'audience', 'auth', 'billing', 'company', 'completion', 'config', 'container',
|
|
30
|
-
'crm', 'database', 'domain', 'email', 'fn', 'funnel', 'git', 'help', 'image',
|
|
30
|
+
'crm', 'database', 'domain', 'email', 'fn', 'funnel', 'git', 'help', 'image', 'init',
|
|
31
31
|
'doctor', 'install-skills', 'keys', 'llm', 'login', 'org', 'payments', 'people', 'pixel',
|
|
32
32
|
'feedback', 'queue', 'setup', 'status', 'storage', 'task', 'update', 'url', 'webhook', 'whoami',
|
|
33
33
|
'workflow',
|
package/dist/config.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export interface AccountEntry {
|
|
|
9
9
|
registrant?: sdkDomain.Registrant;
|
|
10
10
|
is_anonymous?: boolean;
|
|
11
11
|
skills_installed?: boolean;
|
|
12
|
+
key_org_id?: string;
|
|
12
13
|
last_org_used?: string;
|
|
13
14
|
org_names?: Record<string, string>;
|
|
14
15
|
}
|
|
@@ -24,6 +25,16 @@ export declare const CONFIG_DIR: string;
|
|
|
24
25
|
export declare const CONFIG_FILE: string;
|
|
25
26
|
export declare function loadFullConfig(): FullConfig | null;
|
|
26
27
|
export declare function loadConfig(): Config | null;
|
|
28
|
+
/**
|
|
29
|
+
* The stored account whose key is locked to `orgId`, if there is one.
|
|
30
|
+
*
|
|
31
|
+
* This is what lets a project directory name an org and get the right
|
|
32
|
+
* credential without keeping a secret in the repo: the project file says which
|
|
33
|
+
* org, the config says which key belongs to it.
|
|
34
|
+
*/
|
|
35
|
+
export declare function accountLockedTo(orgId: string): AccountEntry | undefined;
|
|
36
|
+
/** Every distinct org this machine holds an org-locked key for. */
|
|
37
|
+
export declare function lockedOrgs(): string[];
|
|
27
38
|
export declare function saveConfig(config: Config): void;
|
|
28
39
|
export declare function addAccount(account: AccountEntry): number;
|
|
29
40
|
export declare function switchAccount(index: number): boolean;
|
package/dist/config.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as fs from 'fs';
|
|
2
2
|
import * as path from 'path';
|
|
3
3
|
import * as os from 'os';
|
|
4
|
+
import { findProjectOrg } from './project.js';
|
|
4
5
|
// MYAPI_CONFIG_DIR redirects the whole config surface — key, accounts,
|
|
5
6
|
// defaults — somewhere other than ~/.myapi.
|
|
6
7
|
//
|
|
@@ -79,11 +80,44 @@ export function loadConfig() {
|
|
|
79
80
|
}
|
|
80
81
|
if (!active)
|
|
81
82
|
return null;
|
|
83
|
+
// The directory picks the CREDENTIAL, not just the org.
|
|
84
|
+
//
|
|
85
|
+
// Resolving the org from .myapi.json while still authenticating with the
|
|
86
|
+
// account-wide key gets the right answer by client-side convention only —
|
|
87
|
+
// the server has nothing to enforce, because an account-wide key may touch
|
|
88
|
+
// every org. Selecting the key that is LOCKED to this project's org is what
|
|
89
|
+
// turns the whole thing from a default into a boundary: from here on a
|
|
90
|
+
// mistake is a 403, not a write into the wrong tenant.
|
|
91
|
+
//
|
|
92
|
+
// Falls through to the active account when the directory names no org, or
|
|
93
|
+
// names one this machine holds no locked key for.
|
|
94
|
+
const dirOrg = process.env.MYAPI_ORG || findProjectOrg()?.org;
|
|
95
|
+
if (dirOrg) {
|
|
96
|
+
const locked = full.accounts.find(a => a.key_org_id === dirOrg);
|
|
97
|
+
if (locked)
|
|
98
|
+
return { ...locked, autocomplete_setup: full.autocomplete_setup };
|
|
99
|
+
}
|
|
82
100
|
return {
|
|
83
101
|
...active,
|
|
84
102
|
autocomplete_setup: full.autocomplete_setup,
|
|
85
103
|
};
|
|
86
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* The stored account whose key is locked to `orgId`, if there is one.
|
|
107
|
+
*
|
|
108
|
+
* This is what lets a project directory name an org and get the right
|
|
109
|
+
* credential without keeping a secret in the repo: the project file says which
|
|
110
|
+
* org, the config says which key belongs to it.
|
|
111
|
+
*/
|
|
112
|
+
export function accountLockedTo(orgId) {
|
|
113
|
+
const full = loadFullConfig();
|
|
114
|
+
return full?.accounts.find(a => a.key_org_id === orgId);
|
|
115
|
+
}
|
|
116
|
+
/** Every distinct org this machine holds an org-locked key for. */
|
|
117
|
+
export function lockedOrgs() {
|
|
118
|
+
const full = loadFullConfig();
|
|
119
|
+
return [...new Set((full?.accounts ?? []).map(a => a.key_org_id).filter(Boolean))];
|
|
120
|
+
}
|
|
87
121
|
// Single point of truth for writes. Ensures CONFIG_DIR exists and that the
|
|
88
122
|
// file is created with mode 0o600 (owner read/write only) — no caller has
|
|
89
123
|
// to remember either.
|
package/dist/exposes.test.js
CHANGED
package/dist/helpers.d.ts
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
import { type Config } from './config.js';
|
|
2
2
|
export type Flags = Record<string, string | boolean | number>;
|
|
3
|
+
export type OrgSource = 'flag' | 'env' | 'project' | 'key' | 'default';
|
|
4
|
+
export interface ResolvedOrg {
|
|
5
|
+
org: string;
|
|
6
|
+
source: OrgSource;
|
|
7
|
+
/** For 'project', the file it came from, so output can name it. */
|
|
8
|
+
from?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Resolves the org without side effects, so it can be tested and reported on
|
|
12
|
+
* (`myapi status`) rather than only observed by running a real command.
|
|
13
|
+
*
|
|
14
|
+
* Precedence: --org > MYAPI_ORG > nearest .myapi.json > the key's own lock >
|
|
15
|
+
* default_org. The first four are per-invocation or per-directory. The last is
|
|
16
|
+
* the machine-global one that two projects fight over.
|
|
17
|
+
*/
|
|
18
|
+
export declare function resolveOrg(flags: Flags, config: Config): ResolvedOrg | undefined;
|
|
3
19
|
export declare function requireOrg(flags: Flags, config: Config, usage: string): string;
|
|
4
20
|
export declare function orgLine(orgId: string, name: string | undefined, changedFrom?: string): string;
|
|
5
21
|
/** The org this invocation resolved to, for `--json` consumers. */
|
package/dist/helpers.js
CHANGED
|
@@ -1,8 +1,33 @@
|
|
|
1
1
|
import { error, banner } from './output.js';
|
|
2
|
+
import { findProjectOrg } from './project.js';
|
|
2
3
|
import { loadConfig, saveConfig } from './config.js';
|
|
3
4
|
import { confirm, isNonInteractive } from './prompt.js';
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Resolves the org without side effects, so it can be tested and reported on
|
|
7
|
+
* (`myapi status`) rather than only observed by running a real command.
|
|
8
|
+
*
|
|
9
|
+
* Precedence: --org > MYAPI_ORG > nearest .myapi.json > the key's own lock >
|
|
10
|
+
* default_org. The first four are per-invocation or per-directory. The last is
|
|
11
|
+
* the machine-global one that two projects fight over.
|
|
12
|
+
*/
|
|
13
|
+
export function resolveOrg(flags, config) {
|
|
14
|
+
if (typeof flags.org === 'string' && flags.org)
|
|
15
|
+
return { org: flags.org, source: 'flag' };
|
|
16
|
+
const env = process.env.MYAPI_ORG;
|
|
17
|
+
if (env)
|
|
18
|
+
return { org: env, source: 'env' };
|
|
19
|
+
const project = findProjectOrg();
|
|
20
|
+
if (project)
|
|
21
|
+
return { org: project.org, source: 'project', from: project.file };
|
|
22
|
+
// A locked key does not need to be told which org it is for — the server will
|
|
23
|
+
// only accept one. Ranking it above default_org means an org-locked key makes
|
|
24
|
+
// the machine-global default irrelevant, which is the whole point.
|
|
25
|
+
if (config.key_org_id)
|
|
26
|
+
return { org: config.key_org_id, source: 'key' };
|
|
27
|
+
if (config.default_org)
|
|
28
|
+
return { org: config.default_org, source: 'default' };
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
6
31
|
export function requireOrg(flags, config, usage) {
|
|
7
32
|
// A present-but-empty --org (e.g. --org "$ORG" with $ORG unset, or a bare
|
|
8
33
|
// --org that swallowed no value) must never degrade to the default org —
|
|
@@ -10,14 +35,34 @@ export function requireOrg(flags, config, usage) {
|
|
|
10
35
|
if ('org' in flags && (flags.org === '' || flags.org === true)) {
|
|
11
36
|
error(`--org was passed without a value. Refusing to fall back to the default org.\nUsage: ${usage}`);
|
|
12
37
|
}
|
|
13
|
-
const
|
|
14
|
-
if (!
|
|
15
|
-
error(`Missing required arguments.\nUsage: ${usage}\n
|
|
38
|
+
const resolved = resolveOrg(flags, config);
|
|
39
|
+
if (!resolved) {
|
|
40
|
+
error(`Missing required arguments.\nUsage: ${usage}\n` +
|
|
41
|
+
`(This directory has no org. Set one up with: myapi init --org <id>)`);
|
|
42
|
+
}
|
|
43
|
+
const orgId = resolved.org;
|
|
44
|
+
// The key is locked server-side; a mismatch is a guaranteed 403. Saying so
|
|
45
|
+
// here costs nothing and can explain WHICH org the key belongs to, which the
|
|
46
|
+
// server's refusal cannot: it will not name another tenant's id back to a
|
|
47
|
+
// caller that has no authority over it.
|
|
48
|
+
if (config.key_org_id && orgId !== config.key_org_id) {
|
|
49
|
+
error(`This API key is locked to org ${config.key_org_id}, but ${describeSource(resolved)} says ${orgId}.\n` +
|
|
50
|
+
`The server would refuse this (403 SCOPE_FORBIDDEN), so nothing was sent.\n` +
|
|
51
|
+
`Use a key for ${orgId}, or drop the override to work in ${config.key_org_id}.`);
|
|
16
52
|
}
|
|
17
53
|
announceOrg(orgId, config);
|
|
18
54
|
recordResolvedOrg(orgId, config);
|
|
19
55
|
return orgId;
|
|
20
56
|
}
|
|
57
|
+
function describeSource(r) {
|
|
58
|
+
switch (r.source) {
|
|
59
|
+
case 'flag': return '--org';
|
|
60
|
+
case 'env': return 'MYAPI_ORG';
|
|
61
|
+
case 'project': return r.from ?? '.myapi.json';
|
|
62
|
+
case 'key': return 'the key';
|
|
63
|
+
case 'default': return 'your machine-wide default org';
|
|
64
|
+
}
|
|
65
|
+
}
|
|
21
66
|
// ── Which org am I in? ───────────────────────────────────────────────────────
|
|
22
67
|
//
|
|
23
68
|
// The org is ambient, sticky and invisible: it comes from a saved default that
|
package/dist/index.js
CHANGED
|
@@ -35,6 +35,7 @@ import * as urlCmd from './commands/url.js';
|
|
|
35
35
|
import * as authProductCmd from './commands/authproduct.js';
|
|
36
36
|
import * as configCmd from './commands/config.js';
|
|
37
37
|
import * as statusCmd from './commands/status.js';
|
|
38
|
+
import * as initCmd from './commands/init.js';
|
|
38
39
|
import * as peopleCmd from './commands/people.js';
|
|
39
40
|
import * as companyCmd from './commands/company.js';
|
|
40
41
|
import * as audienceCmd from './commands/audience.js';
|
|
@@ -317,6 +318,9 @@ async function main() {
|
|
|
317
318
|
case 'status':
|
|
318
319
|
await statusCmd.run(subcommand, restArgs, flags);
|
|
319
320
|
break;
|
|
321
|
+
case 'init':
|
|
322
|
+
await initCmd.run(subcommand, restArgs, flags);
|
|
323
|
+
break;
|
|
320
324
|
case 'keys':
|
|
321
325
|
await keysCmd.run(subcommand, restArgs, flags);
|
|
322
326
|
break;
|
|
@@ -510,6 +514,7 @@ Commands:
|
|
|
510
514
|
funnel Manage websites (publish pages, custom domains, funnels)
|
|
511
515
|
git Hosted git repositories — repos, commits, branches, history
|
|
512
516
|
image Generate AI images and manage them in storage
|
|
517
|
+
init Bind this directory to one org (locked key + .myapi.json)
|
|
513
518
|
install-skills Install or update the MyAPI skills pack for AI agents
|
|
514
519
|
llm Run LLM completions and embeddings (chat + embed, with usage/cost)
|
|
515
520
|
login Sign in via your browser — Google or email code (preview: --mock)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// Which org does a command target, and can a second project on the same
|
|
2
|
+
// machine change the answer?
|
|
3
|
+
//
|
|
4
|
+
// `myapi config set-org` writes `default_org` into ~/.myapi/config.json — one
|
|
5
|
+
// file per MACHINE. Two checkouts share it: project A sets A, project B sets B,
|
|
6
|
+
// and every later bare command in A targets B. It succeeds, against the wrong
|
|
7
|
+
// tenant. That is the most-reported problem with the CLI and it is a scope
|
|
8
|
+
// mismatch, not a bug: the org is a property of the work, stored per machine.
|
|
9
|
+
//
|
|
10
|
+
// The chain here fixes it by putting four per-invocation or per-directory
|
|
11
|
+
// sources ahead of the machine-global one, and by refusing outright when the
|
|
12
|
+
// key's server-side lock disagrees with any of them.
|
|
13
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
14
|
+
import * as fs from 'node:fs';
|
|
15
|
+
import * as os from 'node:os';
|
|
16
|
+
import * as path from 'node:path';
|
|
17
|
+
import { resolveOrg } from './helpers.js';
|
|
18
|
+
import { findProjectOrg, writeProjectOrg } from './project.js';
|
|
19
|
+
const A = 'aaaaaaaa-1111-4111-8111-aaaaaaaaaaaa';
|
|
20
|
+
const B = 'bbbbbbbb-2222-4222-8222-bbbbbbbbbbbb';
|
|
21
|
+
const base = { api_key: 'hq_live_test', account_id: 'acct' };
|
|
22
|
+
let tmp;
|
|
23
|
+
let cwdSpy;
|
|
24
|
+
function chdirTo(dir) {
|
|
25
|
+
cwdSpy?.mockRestore();
|
|
26
|
+
cwdSpy = vi.spyOn(process, 'cwd').mockReturnValue(dir);
|
|
27
|
+
}
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'myapi-org-'));
|
|
30
|
+
delete process.env.MYAPI_ORG;
|
|
31
|
+
chdirTo(tmp);
|
|
32
|
+
});
|
|
33
|
+
afterEach(() => {
|
|
34
|
+
cwdSpy?.mockRestore();
|
|
35
|
+
delete process.env.MYAPI_ORG;
|
|
36
|
+
fs.rmSync(tmp, { recursive: true, force: true });
|
|
37
|
+
});
|
|
38
|
+
describe('org resolution order', () => {
|
|
39
|
+
it('--org beats everything', () => {
|
|
40
|
+
process.env.MYAPI_ORG = B;
|
|
41
|
+
writeProjectOrg(tmp, B);
|
|
42
|
+
const r = resolveOrg({ org: A }, { ...base, key_org_id: B, default_org: B });
|
|
43
|
+
expect(r).toMatchObject({ org: A, source: 'flag' });
|
|
44
|
+
});
|
|
45
|
+
it('MYAPI_ORG beats the project file and the default', () => {
|
|
46
|
+
process.env.MYAPI_ORG = A;
|
|
47
|
+
writeProjectOrg(tmp, B);
|
|
48
|
+
expect(resolveOrg({}, { ...base, default_org: B })).toMatchObject({ org: A, source: 'env' });
|
|
49
|
+
});
|
|
50
|
+
it('the project file beats the machine-wide default — the whole point', () => {
|
|
51
|
+
// Project B set the global default to B. This directory is project A.
|
|
52
|
+
writeProjectOrg(tmp, A);
|
|
53
|
+
expect(resolveOrg({}, { ...base, default_org: B })).toMatchObject({ org: A, source: 'project' });
|
|
54
|
+
});
|
|
55
|
+
it("a locked key beats the machine-wide default, so the default stops mattering", () => {
|
|
56
|
+
expect(resolveOrg({}, { ...base, key_org_id: A, default_org: B }))
|
|
57
|
+
.toMatchObject({ org: A, source: 'key' });
|
|
58
|
+
});
|
|
59
|
+
it('falls back to the default when nothing else says', () => {
|
|
60
|
+
expect(resolveOrg({}, { ...base, default_org: B })).toMatchObject({ org: B, source: 'default' });
|
|
61
|
+
});
|
|
62
|
+
it('resolves to nothing when there is nothing to resolve', () => {
|
|
63
|
+
expect(resolveOrg({}, base)).toBeUndefined();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe('the project file', () => {
|
|
67
|
+
it('is found by walking up, like .git', () => {
|
|
68
|
+
const nested = path.join(tmp, 'src', 'deep', 'nested');
|
|
69
|
+
fs.mkdirSync(nested, { recursive: true });
|
|
70
|
+
writeProjectOrg(tmp, A);
|
|
71
|
+
chdirTo(nested);
|
|
72
|
+
expect(findProjectOrg()).toMatchObject({ org: A });
|
|
73
|
+
});
|
|
74
|
+
it('takes the NEAREST one, so a nested project wins over its parent', () => {
|
|
75
|
+
const inner = path.join(tmp, 'inner');
|
|
76
|
+
fs.mkdirSync(inner);
|
|
77
|
+
writeProjectOrg(tmp, A);
|
|
78
|
+
writeProjectOrg(inner, B);
|
|
79
|
+
chdirTo(inner);
|
|
80
|
+
expect(findProjectOrg()).toMatchObject({ org: B });
|
|
81
|
+
});
|
|
82
|
+
it('never carries a key', () => {
|
|
83
|
+
// A file named in a repo gets committed eventually. Trading a wrong-org
|
|
84
|
+
// write for a leaked credential would be a worse deal than the bug.
|
|
85
|
+
const file = writeProjectOrg(tmp, A);
|
|
86
|
+
const raw = JSON.parse(fs.readFileSync(file, 'utf-8'));
|
|
87
|
+
expect(Object.keys(raw)).toEqual(['org']);
|
|
88
|
+
expect(JSON.stringify(raw)).not.toMatch(/hq_live|api_key|key/i);
|
|
89
|
+
});
|
|
90
|
+
it('preserves keys it does not own', () => {
|
|
91
|
+
const file = path.join(tmp, '.myapi.json');
|
|
92
|
+
fs.writeFileSync(file, JSON.stringify({ note: 'mine', org: B }));
|
|
93
|
+
writeProjectOrg(tmp, A);
|
|
94
|
+
expect(JSON.parse(fs.readFileSync(file, 'utf-8'))).toEqual({ note: 'mine', org: A });
|
|
95
|
+
});
|
|
96
|
+
it('ignores a malformed file rather than bricking every command below it', () => {
|
|
97
|
+
fs.writeFileSync(path.join(tmp, '.myapi.json'), '{ not json');
|
|
98
|
+
expect(findProjectOrg()).toBeUndefined();
|
|
99
|
+
expect(resolveOrg({}, { ...base, default_org: B })).toMatchObject({ org: B, source: 'default' });
|
|
100
|
+
});
|
|
101
|
+
it('ignores a file that names no org', () => {
|
|
102
|
+
fs.writeFileSync(path.join(tmp, '.myapi.json'), JSON.stringify({ note: 'hi' }));
|
|
103
|
+
expect(findProjectOrg()).toBeUndefined();
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
describe('a locked key refuses a mismatched target', () => {
|
|
107
|
+
// The server enforces this (403 SCOPE_FORBIDDEN, verified against production
|
|
108
|
+
// 2026-08-23). Refusing locally costs nothing and can say WHICH org the key
|
|
109
|
+
// belongs to — which the server's refusal deliberately will not, since it
|
|
110
|
+
// must not name one tenant's id to a caller with no authority over it.
|
|
111
|
+
let exitError;
|
|
112
|
+
beforeEach(async () => {
|
|
113
|
+
exitError = null;
|
|
114
|
+
const output = await import('./output.js');
|
|
115
|
+
vi.spyOn(output, 'error').mockImplementation(((m) => {
|
|
116
|
+
exitError = String(m);
|
|
117
|
+
throw new Error('__EXIT__');
|
|
118
|
+
}));
|
|
119
|
+
vi.spyOn(output, 'banner').mockImplementation(() => { });
|
|
120
|
+
});
|
|
121
|
+
afterEach(() => vi.restoreAllMocks());
|
|
122
|
+
async function requireOrgSafely(flags, config) {
|
|
123
|
+
const { requireOrg } = await import('./helpers.js');
|
|
124
|
+
try {
|
|
125
|
+
return requireOrg(flags, config, 'usage');
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
if (e?.message !== '__EXIT__')
|
|
129
|
+
throw e;
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
it('refuses --org pointing somewhere the key cannot go, before sending', async () => {
|
|
134
|
+
await requireOrgSafely({ org: B }, { ...base, key_org_id: A });
|
|
135
|
+
expect(exitError).toContain(A);
|
|
136
|
+
expect(exitError).toContain(B);
|
|
137
|
+
expect(exitError).toMatch(/nothing was sent/i);
|
|
138
|
+
});
|
|
139
|
+
it('names the source of the conflicting org, not just the conflict', async () => {
|
|
140
|
+
writeProjectOrg(tmp, B);
|
|
141
|
+
await requireOrgSafely({}, { ...base, key_org_id: A });
|
|
142
|
+
expect(exitError).toContain('.myapi.json');
|
|
143
|
+
});
|
|
144
|
+
it('allows an --org that agrees with the lock', async () => {
|
|
145
|
+
const got = await requireOrgSafely({ org: A }, { ...base, key_org_id: A });
|
|
146
|
+
expect(got).toBe(A);
|
|
147
|
+
expect(exitError).toBeNull();
|
|
148
|
+
});
|
|
149
|
+
it('needs no --org at all when the key is locked', async () => {
|
|
150
|
+
const got = await requireOrgSafely({}, { ...base, key_org_id: A });
|
|
151
|
+
expect(got).toBe(A);
|
|
152
|
+
});
|
|
153
|
+
it('still refuses a --org that arrived empty', async () => {
|
|
154
|
+
await requireOrgSafely({ org: '' }, { ...base, key_org_id: A, default_org: A });
|
|
155
|
+
expect(exitError).toMatch(/without a value/);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare const PROJECT_FILE = ".myapi.json";
|
|
2
|
+
export interface ProjectConfig {
|
|
3
|
+
org?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface FoundProject {
|
|
6
|
+
org: string;
|
|
7
|
+
/** Absolute path of the file it came from, so output can name it. */
|
|
8
|
+
file: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Walks up from `startDir` looking for the nearest `.myapi.json` that names an
|
|
12
|
+
* org. Stops at the filesystem root.
|
|
13
|
+
*
|
|
14
|
+
* A malformed file is ignored rather than fatal: it must not be able to brick
|
|
15
|
+
* every command in a directory tree, and the resolution chain has other rungs.
|
|
16
|
+
* It is reported by `myapi status` instead.
|
|
17
|
+
*/
|
|
18
|
+
export declare function findProjectOrg(startDir?: string): FoundProject | undefined;
|
|
19
|
+
/** Writes `.myapi.json` in `dir`, preserving any keys we do not own. */
|
|
20
|
+
export declare function writeProjectOrg(dir: string, orgId: string): string;
|
package/dist/project.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
// Which org does this directory belong to?
|
|
4
|
+
//
|
|
5
|
+
// `myapi config set-org` writes `default_org` into ~/.myapi/config.json — one
|
|
6
|
+
// file per MACHINE. Two checkouts on one laptop share it, so project A sets it
|
|
7
|
+
// to A, project B sets it to B, and every later bare command in A quietly
|
|
8
|
+
// targets B. The command succeeds; it just lands in the wrong tenant. That is
|
|
9
|
+
// the single most-reported problem with the CLI, and it is a scope mismatch:
|
|
10
|
+
// the org is a property of the work, stored per machine.
|
|
11
|
+
//
|
|
12
|
+
// So: the nearest `.myapi.json` walking up from cwd, the way git finds .git and
|
|
13
|
+
// npm finds package.json. Two projects get two files and stop fighting.
|
|
14
|
+
//
|
|
15
|
+
// It holds the ORG ID AND NEVER A KEY. A file named in a repo gets committed
|
|
16
|
+
// eventually — that is not a prediction, it is what happens — and trading a
|
|
17
|
+
// wrong-org write for a leaked credential would be a worse deal than the bug it
|
|
18
|
+
// fixes. The org id is not a secret: it is useless without a key that the
|
|
19
|
+
// server has already locked to it.
|
|
20
|
+
export const PROJECT_FILE = '.myapi.json';
|
|
21
|
+
/**
|
|
22
|
+
* Walks up from `startDir` looking for the nearest `.myapi.json` that names an
|
|
23
|
+
* org. Stops at the filesystem root.
|
|
24
|
+
*
|
|
25
|
+
* A malformed file is ignored rather than fatal: it must not be able to brick
|
|
26
|
+
* every command in a directory tree, and the resolution chain has other rungs.
|
|
27
|
+
* It is reported by `myapi status` instead.
|
|
28
|
+
*/
|
|
29
|
+
export function findProjectOrg(startDir = process.cwd()) {
|
|
30
|
+
let dir = path.resolve(startDir);
|
|
31
|
+
for (;;) {
|
|
32
|
+
const candidate = path.join(dir, PROJECT_FILE);
|
|
33
|
+
try {
|
|
34
|
+
if (fs.existsSync(candidate)) {
|
|
35
|
+
const parsed = JSON.parse(fs.readFileSync(candidate, 'utf-8'));
|
|
36
|
+
if (typeof parsed?.org === 'string' && parsed.org) {
|
|
37
|
+
return { org: parsed.org, file: candidate };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch { /* unreadable or malformed — keep walking */ }
|
|
42
|
+
const parent = path.dirname(dir);
|
|
43
|
+
if (parent === dir)
|
|
44
|
+
return undefined;
|
|
45
|
+
dir = parent;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Writes `.myapi.json` in `dir`, preserving any keys we do not own. */
|
|
49
|
+
export function writeProjectOrg(dir, orgId) {
|
|
50
|
+
const file = path.join(path.resolve(dir), PROJECT_FILE);
|
|
51
|
+
let existing = {};
|
|
52
|
+
try {
|
|
53
|
+
if (fs.existsSync(file))
|
|
54
|
+
existing = JSON.parse(fs.readFileSync(file, 'utf-8'));
|
|
55
|
+
}
|
|
56
|
+
catch { /* replace an unreadable file rather than fail the command */ }
|
|
57
|
+
fs.writeFileSync(file, `${JSON.stringify({ ...existing, org: orgId }, null, 2)}\n`);
|
|
58
|
+
return file;
|
|
59
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: my-api-hq
|
|
3
|
-
version: 1.3.
|
|
3
|
+
version: 1.3.1
|
|
4
4
|
description: >
|
|
5
5
|
Auth, organizations, and billing hub. Start here to get an api_key and org_id — every other service depends on both.
|
|
6
6
|
triggers: [api key, account, organization, org, billing, balance, topup, credits, setup, defaults, brand, sync brand, doctor, health check, is my org healthy]
|
|
7
|
-
checksum: sha256-
|
|
7
|
+
checksum: sha256-f8e308fd3cd4b0ed4097e6f54c620e49cfe59c5b4275cfea4c1a0e18dd78f5e4
|
|
8
8
|
---
|
|
9
9
|
|
|
10
10
|
# MyApiHQ
|
|
@@ -13,20 +13,20 @@ The root service. It manages accounts, API keys, organizations, and billing. No
|
|
|
13
13
|
|
|
14
14
|
## Capabilities
|
|
15
15
|
<!-- llm:start -->
|
|
16
|
-
MyApiHQ is the platform's foundation
|
|
16
|
+
MyApiHQ is the platform's foundation: every other service needs an `api_key` and (for org-scoped resources) an `org_id` minted here. `myapi account setup` provisions an account, key and first org into `~/.myapi/config.json`, and later commands pick those up.
|
|
17
17
|
|
|
18
18
|
### Anonymous vs registered accounts
|
|
19
19
|
|
|
20
20
|
Two tiers, chosen at setup time:
|
|
21
21
|
|
|
22
|
-
- **Anonymous** (`myapi account setup --anonymous`): zero
|
|
23
|
-
- **Registered** (
|
|
22
|
+
- **Anonymous** (`myapi account setup --anonymous`): zero friction, **$0 credit.** Fine for browsing the catalog and reading help.
|
|
23
|
+
- **Registered** (verify email via `myapi account link <email>`): unlocks $5 free credit and the paid surface (LLM, image, email, domain register).
|
|
24
24
|
|
|
25
|
-
An anonymous account
|
|
25
|
+
An anonymous account upgrades any time with `myapi account link <email>`; the credit grants on verification.
|
|
26
26
|
|
|
27
27
|
### Health check
|
|
28
28
|
|
|
29
|
-
`myapi doctor` runs an org-wide consistency check across every slot
|
|
29
|
+
`myapi doctor` runs an org-wide consistency check across every slot, plus customer-perspective DNS/HTTP probes from your machine. It returns per-section findings (`✓` pass / `⚠` warning / `✗` critical) with remediation hints; add `--json` for machine output. The exit code is non-zero **only** on customer-actionable criticals — platform-side issues the MyAPI team is already handling are surfaced with an `ℹ` marker but don't fail the run. Run it to self-check before building (is the org set up?) and after (did everything wire up?).
|
|
30
30
|
<!-- llm:end -->
|
|
31
31
|
|
|
32
32
|
## Commands
|
|
@@ -55,7 +55,8 @@ An anonymous account can upgrade at any time via `myapi account link <email>`
|
|
|
55
55
|
| `myapi billing auto-recharge [show \| set \| disable]` | Keep the wallet funded — off-session refill when balance drops below a threshold, capped monthly |
|
|
56
56
|
| `myapi account sending` / `myapi account resume-sending` | Is sending paused (bounces), and turn it on |
|
|
57
57
|
| `myapi account mailing-address ["<address>"]` | Get or set the account's CAN-SPAM mailing address (required for email send) |
|
|
58
|
-
| `myapi
|
|
58
|
+
| `myapi init --org <id>` | Bind this directory to one org: locked key + `.myapi.json` (`--force` re-binds). Use instead of `config set-org` when a machine has more than one project |
|
|
59
|
+
| `myapi config set-org <id>` / `set-funnel <id>` / `set-domain <name>` | Set machine-wide CLI defaults (single-project machines only) |
|
|
59
60
|
| `myapi install-skills` | Install agent skills into ~/.claude/, ~/.gemini/, ~/.cursor/ |
|
|
60
61
|
| `myapi doctor [--verbose] [--json]` | Org-wide health check: config/integrity findings + DNS/HTTP probes |
|
|
61
62
|
<!-- generated:end -->
|
|
@@ -94,7 +95,7 @@ Each org gets a free preview subdomain (`*.makeautonomous.com`) usable before re
|
|
|
94
95
|
|
|
95
96
|
## Notes
|
|
96
97
|
|
|
97
|
-
-
|
|
98
|
+
- **Bind a directory to an org with `myapi init --org <id>`**, not `config set-org` — `set-org` writes ONE default for the whole machine, so two projects overwrite each other's and commands land in the wrong org while still succeeding. `init` writes `.myapi.json` (org id, never a key) plus a key the server locks to that org.
|
|
98
99
|
- API keys have format `hq_live_...` and are sent as `Authorization: Bearer <key>`.
|
|
99
100
|
- `org sync-brand` is async (scrapes the site, polls the job).
|
|
100
101
|
|
|
@@ -108,7 +109,9 @@ myapi keys create --name agent --org <id> --grant '*' # locked to ONE org
|
|
|
108
109
|
myapi keys create --name ci --grant funnel:write,storage:read --spend-cap 25
|
|
109
110
|
```
|
|
110
111
|
|
|
111
|
-
- `--org <id>` — **lock it to one org.** Omit for account-wide.
|
|
112
|
+
- `--org <id>` — **lock it to one org.** Omit for account-wide. A locked key is
|
|
113
|
+
refused (403) on every other org — the only scoping a stale default cannot
|
|
114
|
+
talk past. `myapi init` sets it up per directory.
|
|
112
115
|
- `--grant <list>` — `slot:read` / `slot:write`; a bare slot means write, `*`
|
|
113
116
|
means all. **Omitting `--grant` mints an unrestricted key.**
|
|
114
117
|
- `--spend-cap <usd>` — hard ceiling; `0` means the key cannot spend at all.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@myapihq/cli",
|
|
3
3
|
"license": "Apache-2.0",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.26.0",
|
|
5
5
|
"description": "MyAPI command-line interface",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"lint:skills:strict": "node scripts/copy-skills.js && node scripts/lint-skills.js --strict"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@myapihq/sdk": "^2.
|
|
49
|
+
"@myapihq/sdk": "^2.26.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/node": "^25.6.0",
|