api-spec-cli 0.2.3 → 0.2.4

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/resolve.js CHANGED
@@ -1,65 +1,64 @@
1
- import { getEntry, getCachedSpec, saveCachedSpec } from "./registry.js";
2
- import { fetchSpec, inlineEntryFromFlags } from "./commands/fetch.js";
3
- import { getConfig } from "./store.js";
4
- import { parseKV } from "./args.js";
5
-
6
- /**
7
- * Resolve the active spec from flags.
8
- * Priority:
9
- * 1. --spec <name> → registry (auto-caches on first use)
10
- * 2. Inline flags → ad-hoc, no caching
11
- * 3. Error → no spec source given
12
- */
13
- export async function resolveSpec(flags) {
14
- if (flags.spec) {
15
- const entry = getEntry(flags.spec); // throws if missing or disabled
16
- let spec = getCachedSpec(flags.spec);
17
- if (!spec) {
18
- spec = await fetchSpec(entry);
19
- saveCachedSpec(flags.spec, spec);
20
- }
21
- return { spec, entry };
22
- }
23
-
24
- const inlineEntry = inlineEntryFromFlags(flags);
25
- if (inlineEntry) {
26
- const spec = await fetchSpec(inlineEntry);
27
- return { spec, entry: inlineEntry };
28
- }
29
-
30
- throw new Error(
31
- "No spec source. Pass --spec <name> (registered) or an inline flag:\n" +
32
- " --openapi <url-or-file>\n" +
33
- " --graphql <url>\n" +
34
- " --mcp-http <url>\n" +
35
- " --mcp-sse <url>\n" +
36
- ' --mcp-stdio "<cmd args>"'
37
- );
38
- }
39
-
40
- /**
41
- * Build the effective config for a command.
42
- * Precedence (highest → lowest):
43
- * 1. Call-time flags: --auth, --base-url, --header k=v
44
- * 2. Registry entry config
45
- * 3. .spec-cli/config.json
46
- */
47
- export function resolveConfig(flags, entry) {
48
- const global = getConfig();
49
- const entryConfig = entry?.config || {};
50
- const callHeaders = parseKV(flags.header);
51
-
52
- const auth = flags.auth || entryConfig.auth || global.auth;
53
- const baseUrl = flags["base-url"] || entryConfig.baseUrl || global.baseUrl;
54
- const headers = { ...global.headers, ...(entryConfig.headers || {}), ...callHeaders };
55
-
56
- // Apply auth as Authorization header if not already there (case-insensitive check)
57
- const hasAuthHeader = Object.keys(headers).some((k) => k.toLowerCase() === "authorization");
58
- if (auth && !hasAuthHeader) {
59
- headers["Authorization"] = auth.startsWith("Bearer ") || auth.startsWith("Basic ")
60
- ? auth
61
- : `Bearer ${auth}`;
62
- }
63
-
64
- return { auth, baseUrl, headers };
65
- }
1
+ import { getEntry, getCachedSpec, saveCachedSpec } from "./registry.js";
2
+ import { fetchSpec, inlineEntryFromFlags } from "./commands/fetch.js";
3
+ import { getConfig } from "./store.js";
4
+ import { parseKV } from "./args.js";
5
+
6
+ /**
7
+ * Resolve the active spec from flags.
8
+ * Priority:
9
+ * 1. --spec <name> → registry (auto-caches on first use)
10
+ * 2. Inline flags → ad-hoc, no caching
11
+ * 3. Error → no spec source given
12
+ */
13
+ export async function resolveSpec(flags) {
14
+ if (flags.spec) {
15
+ const entry = getEntry(flags.spec); // throws if missing or disabled
16
+ let spec = getCachedSpec(flags.spec);
17
+ if (!spec) {
18
+ spec = await fetchSpec(entry);
19
+ saveCachedSpec(flags.spec, spec);
20
+ }
21
+ return { spec, entry };
22
+ }
23
+
24
+ const inlineEntry = inlineEntryFromFlags(flags);
25
+ if (inlineEntry) {
26
+ const spec = await fetchSpec(inlineEntry);
27
+ return { spec, entry: inlineEntry };
28
+ }
29
+
30
+ throw new Error(
31
+ "No spec source. Pass --spec <name> (registered) or an inline flag:\n" +
32
+ " --openapi <url-or-file>\n" +
33
+ " --graphql <url>\n" +
34
+ " --mcp-http <url>\n" +
35
+ " --mcp-sse <url>\n" +
36
+ ' --mcp-stdio "<cmd args>"'
37
+ );
38
+ }
39
+
40
+ /**
41
+ * Build the effective config for a command.
42
+ * Precedence (highest → lowest):
43
+ * 1. Call-time flags: --auth, --base-url, --header k=v
44
+ * 2. Registry entry config
45
+ * 3. .spec-cli/config.json
46
+ */
47
+ export function resolveConfig(flags, entry) {
48
+ const global = getConfig();
49
+ const entryConfig = entry?.config || {};
50
+ const callHeaders = parseKV(flags.header);
51
+
52
+ const auth = flags.auth || entryConfig.auth || global.auth;
53
+ const baseUrl = flags["base-url"] || entryConfig.baseUrl || global.baseUrl;
54
+ const headers = { ...global.headers, ...(entryConfig.headers || {}), ...callHeaders };
55
+
56
+ // Apply auth as Authorization header if not already there (case-insensitive check)
57
+ const hasAuthHeader = Object.keys(headers).some((k) => k.toLowerCase() === "authorization");
58
+ if (auth && !hasAuthHeader) {
59
+ headers["Authorization"] =
60
+ auth.startsWith("Bearer ") || auth.startsWith("Basic ") ? auth : `Bearer ${auth}`;
61
+ }
62
+
63
+ return { auth, baseUrl, headers };
64
+ }