@nasebanal/cli 0.12.0 → 0.14.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.
@@ -0,0 +1,88 @@
1
+ /**
2
+ * `nb specs specs push --server-url <url>`: the document a pipeline keeps in its repository is ONE file per API; what differs
3
+ * between environments is where the API is served, which is the document's top-level `servers`. The push replaces `servers`
4
+ * with `[{ url }]` just before it sends the document, so each environment's spec (a spec per environment, filed in a group)
5
+ * carries its own URL. The catalog's contract hash ignores `servers`, so this does not change the contract's identity.
6
+ *
7
+ * How the document is edited:
8
+ * - JSON input: parsed, `servers` set, written back as JSON (2-space indent).
9
+ * - YAML input: a TEXT edit of the top-level `servers` block (or one inserted before `paths:`), so comments and the order of
10
+ * everything else stay as they are. The result is parsed and compared with the original (everything but `servers` must be
11
+ * identical); if the edit cannot be proven safe the document is re-serialised instead (comments are lost) and the method
12
+ * says so.
13
+ */
14
+ import { parse, stringify } from "yaml";
15
+ /** `--server-url`: an absolute http(s) URL (no trailing slash needed). */
16
+ export function checkServerUrl(raw) {
17
+ const v = raw.trim();
18
+ let url;
19
+ try {
20
+ url = new URL(v);
21
+ }
22
+ catch {
23
+ throw new Error(`--server-url '${raw}' is not a URL (e.g. https://api.example.com/v1)`);
24
+ }
25
+ if (url.protocol !== "https:" && url.protocol !== "http:")
26
+ throw new Error(`--server-url must be an http(s) URL, got '${raw}'`);
27
+ return v;
28
+ }
29
+ const withoutServers = (doc) => {
30
+ const { servers: _servers, ...rest } = (doc ?? {});
31
+ void _servers;
32
+ return JSON.stringify(rest);
33
+ };
34
+ /** Set the document's top-level `servers` to `[{ url }]`. */
35
+ export function setServers(text, url) {
36
+ const trimmed = text.trimStart();
37
+ if (trimmed.startsWith("{")) {
38
+ let doc;
39
+ try {
40
+ doc = JSON.parse(text);
41
+ }
42
+ catch {
43
+ throw new Error("--server-url: the file looks like JSON but does not parse");
44
+ }
45
+ return { text: `${JSON.stringify({ ...doc, servers: [{ url }] }, null, 2)}\n`, method: "json" };
46
+ }
47
+ let original;
48
+ try {
49
+ original = parse(text);
50
+ }
51
+ catch (err) {
52
+ throw new Error(`--server-url: the file is not valid YAML or JSON: ${err.message}`);
53
+ }
54
+ if (original === null || typeof original !== "object" || Array.isArray(original))
55
+ throw new Error("--server-url: the document is not an OpenAPI object");
56
+ const block = `servers:\n - url: ${JSON.stringify(url)}`;
57
+ const lines = text.split("\n");
58
+ const isTopKey = (l) => /^[^\s#\-][^\n]*$/.test(l) && !/^---\s*$/.test(l) && !/^\.\.\.\s*$/.test(l);
59
+ const start = lines.findIndex((l) => /^servers\s*:/.test(l));
60
+ let edited = null;
61
+ if (start >= 0) {
62
+ let end = start + 1;
63
+ // The block runs to the next top-level key; comment lines and blank lines after the last item stay with what follows it.
64
+ while (end < lines.length && !isTopKey(lines[end]))
65
+ end++;
66
+ let tail = end;
67
+ while (tail > start + 1 && (lines[tail - 1].trim() === "" || lines[tail - 1].trimStart().startsWith("#")))
68
+ tail--;
69
+ edited = [...lines.slice(0, start), ...block.split("\n"), ...lines.slice(tail)].join("\n");
70
+ }
71
+ else {
72
+ const paths = lines.findIndex((l) => /^paths\s*:/.test(l));
73
+ if (paths >= 0)
74
+ edited = [...lines.slice(0, paths), ...block.split("\n"), ...lines.slice(paths)].join("\n");
75
+ else
76
+ edited = `${text.endsWith("\n") ? text : `${text}\n`}${block}\n`;
77
+ }
78
+ try {
79
+ const after = parse(edited);
80
+ const ok = JSON.stringify(after?.servers) === JSON.stringify([{ url }]) && withoutServers(after) === withoutServers(original);
81
+ if (ok)
82
+ return { text: edited, method: "text" };
83
+ }
84
+ catch {
85
+ /* fall through to re-serialising */
86
+ }
87
+ return { text: stringify({ ...original, servers: [{ url }] }), method: "reserialised" };
88
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * `nb specs specs settings <id>` - a spec's public settings (since 2026-11-10 these are columns of the spec, not of its
3
+ * repository): the quality badge, who may read the report, and the button that issues new public links. One `PATCH /specs/{id}`
4
+ * with only the fields asked for. The Evolution screen's Spec detail has the same fields (public badge and report with their
5
+ * links). Whether a result may carry the original report (`attachments_enabled`) is the REPOSITORY's: `nb specs repositories
6
+ * upsert|update --attachments`. There is no API version rule any more: a spec has ONE API version (v1, v2), so a result needs none.
7
+ */
8
+ import { ApiError, request } from "../http.js";
9
+ import { headersOf, reasonOf } from "./push.js";
10
+ export const REPORT_ACCESS = ["hidden", "login", "public"];
11
+ /** `on|off` (also true/false, yes/no, 1/0) as a boolean; anything else is a mistake worth saying so. */
12
+ export function onOff(flag, raw) {
13
+ const v = raw.trim().toLowerCase();
14
+ if (["on", "true", "yes", "1"].includes(v))
15
+ return true;
16
+ if (["off", "false", "no", "0"].includes(v))
17
+ return false;
18
+ throw new Error(`${flag} '${raw}' must be on or off`);
19
+ }
20
+ /** The flags of `settings` as the PATCH body; at least one is required (a PATCH that changes nothing is a mistake). */
21
+ export function parseSettingsFlags(o) {
22
+ const patch = {};
23
+ if (o.publicBadge !== undefined)
24
+ patch.public_badge = onOff("--public-badge", o.publicBadge);
25
+ if (o.reportAccess !== undefined) {
26
+ const v = o.reportAccess.trim().toLowerCase();
27
+ if (!REPORT_ACCESS.includes(v))
28
+ throw new Error(`--report-access '${o.reportAccess}' must be one of: ${REPORT_ACCESS.join(", ")}`);
29
+ patch.report_access = v;
30
+ }
31
+ if (o.rotateQualityPublicId)
32
+ patch.rotate_quality_public_id = true;
33
+ if (Object.keys(patch).length === 0) {
34
+ throw new Error("Nothing to change: pass at least one of --public-badge, --report-access, --rotate-quality-public-id");
35
+ }
36
+ return patch;
37
+ }
38
+ export async function updateSpecSettings(conn, id, patch) {
39
+ try {
40
+ const res = await request({ ...headersOf(conn), method: "PATCH", path: `/api/v1/specs/${encodeURIComponent(id)}`, body: patch });
41
+ return res.data.data;
42
+ }
43
+ catch (err) {
44
+ if (err instanceof ApiError)
45
+ throw new Error(`The catalog refused the settings: ${reasonOf(err)}`);
46
+ throw err;
47
+ }
48
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nasebanal/cli",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "gcloud-style command-line interface for NASEBANAL APIs, generated from nb-api-specs OpenAPI contracts.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -2,7 +2,7 @@ openapi: 3.0.3
2
2
  info:
3
3
  title: NASEBANAL Account API
4
4
  description: Centralized user and billing management API for NASEBANAL applications. Manages user profiles, per-service subscriptions, and Personal Access Tokens (PAT).
5
- version: 1.7.0
5
+ version: 1.8.0
6
6
  contact:
7
7
  name: NASEBANAL
8
8
  url: https://www.nasebanal.com
@@ -165,6 +165,65 @@ paths:
165
165
  format: date-time
166
166
  "401":
167
167
  $ref: "#/components/responses/Unauthorized"
168
+ /api/v1/me/newsletter:
169
+ get:
170
+ tags:
171
+ - User
172
+ summary: Newsletter subscription
173
+ description: "Whether the caller's own address receives the newsletter: it is in the newsletter segment and has not unsubscribed through a broadcast's own link. Only the caller's own address is ever read. `available` is false when the newsletter is not configured on this API (the switch should be hidden). Requires the API's Resend key to have Full access; a Resend failure answers 502, never a wrong state."
174
+ responses:
175
+ "200":
176
+ description: Current newsletter state
177
+ content:
178
+ application/json:
179
+ schema:
180
+ $ref: "#/components/schemas/NewsletterResponse"
181
+ "401":
182
+ $ref: "#/components/responses/Unauthorized"
183
+ "502":
184
+ description: The newsletter service (Resend) could not be read or updated
185
+ content:
186
+ application/json:
187
+ schema:
188
+ $ref: "#/components/schemas/Error"
189
+ put:
190
+ tags:
191
+ - User
192
+ summary: Subscribe or unsubscribe the newsletter
193
+ description: Subscribes (adds the caller to the newsletter segment and clears `unsubscribed`) or unsubscribes (removes the caller from the newsletter segment only; the Users segment and the contact stay). The address always comes from the token, never from the body.
194
+ requestBody:
195
+ required: true
196
+ content:
197
+ application/json:
198
+ schema:
199
+ $ref: "#/components/schemas/UpdateNewsletterInput"
200
+ responses:
201
+ "200":
202
+ description: Newsletter state after the change
203
+ content:
204
+ application/json:
205
+ schema:
206
+ $ref: "#/components/schemas/NewsletterResponse"
207
+ "400":
208
+ description: "`subscribed` is not a boolean"
209
+ content:
210
+ application/json:
211
+ schema:
212
+ $ref: "#/components/schemas/Error"
213
+ "401":
214
+ $ref: "#/components/responses/Unauthorized"
215
+ "502":
216
+ description: The newsletter service (Resend) could not be read or updated
217
+ content:
218
+ application/json:
219
+ schema:
220
+ $ref: "#/components/schemas/Error"
221
+ "503":
222
+ description: The newsletter is not available for this account
223
+ content:
224
+ application/json:
225
+ schema:
226
+ $ref: "#/components/schemas/Error"
168
227
  /api/v1/me/subscriptions:
169
228
  get:
170
229
  tags:
@@ -1127,6 +1186,36 @@ components:
1127
1186
  details:
1128
1187
  type: object
1129
1188
  additionalProperties: true
1189
+ NewsletterResponse:
1190
+ type: object
1191
+ required:
1192
+ - success
1193
+ - data
1194
+ properties:
1195
+ success:
1196
+ type: boolean
1197
+ enum:
1198
+ - true
1199
+ data:
1200
+ type: object
1201
+ required:
1202
+ - available
1203
+ - subscribed
1204
+ properties:
1205
+ available:
1206
+ type: boolean
1207
+ description: False when the newsletter is not configured on this API (hide the switch).
1208
+ subscribed:
1209
+ type: boolean
1210
+ description: True while the address is in the newsletter segment and has not unsubscribed.
1211
+ UpdateNewsletterInput:
1212
+ type: object
1213
+ required:
1214
+ - subscribed
1215
+ properties:
1216
+ subscribed:
1217
+ type: boolean
1218
+ description: true to subscribe, false to unsubscribe.
1130
1219
  responses:
1131
1220
  Unauthorized:
1132
1221
  description: Authentication required or invalid token