neon 7.0.5 → 7.0.6

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.
@@ -1,14 +1,17 @@
1
1
  import { t as __exportAll } from "../_chunks/rolldown-runtime-8H4AJuhK.js";
2
+ import { codeFromBody, isNeonApiError } from "../api.js";
2
3
  import { writer } from "../writer.js";
3
4
  import { describeOperation, getEndpoints, loadSpec } from "../utils/openapi.js";
4
5
  import { readFileSync } from "node:fs";
5
6
  import { resolve } from "node:path";
7
+ import chalk from "chalk";
6
8
  import YAML from "yaml";
7
9
  //#region src/commands/api.ts
8
10
  var api_exports = /* @__PURE__ */ __exportAll({
9
11
  buildBody: () => buildBody,
10
12
  buildQuery: () => buildQuery,
11
13
  builder: () => builder,
14
+ colorizeJson: () => colorizeJson,
12
15
  command: () => command,
13
16
  describe: () => describe,
14
17
  handler: () => handler,
@@ -101,6 +104,23 @@ function parseHeaders(pairs) {
101
104
  function assertMethod(method) {
102
105
  if (!HTTP_METHODS.includes(method)) throw new Error(`Unsupported method "${method}". Use one of: ${HTTP_METHODS.join(", ")}.`);
103
106
  }
107
+ const JSON_TOKEN = /("(?:\\.|[^"\\])*")(\s*:)?|\b(true|false)\b|\bnull\b|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/g;
108
+ /** Color the tokens of serialized JSON without changing a byte of the text itself. */
109
+ function colorizeJson(json) {
110
+ return json.replace(JSON_TOKEN, (token, string, colon, bool) => {
111
+ if (string !== void 0) return colon !== void 0 ? `${chalk.cyan(string)}${colon}` : chalk.green(string);
112
+ if (bool !== void 0) return chalk.magenta(token);
113
+ if (token === "null") return chalk.dim(token);
114
+ return chalk.yellow(token);
115
+ });
116
+ }
117
+ const METHOD_COLORS = {
118
+ GET: chalk.green,
119
+ POST: chalk.cyan,
120
+ PUT: chalk.yellow,
121
+ PATCH: chalk.yellow,
122
+ DELETE: chalk.red
123
+ };
104
124
  async function readStdin() {
105
125
  const chunks = [];
106
126
  for await (const chunk of process.stdin) chunks.push(Buffer.from(chunk));
@@ -138,7 +158,8 @@ async function listEndpoints(args) {
138
158
  "summary"
139
159
  ],
140
160
  title: `Neon API endpoints (${endpoints.length})`,
141
- emptyMessage: "No endpoints found in the spec."
161
+ emptyMessage: "No endpoints found in the spec.",
162
+ renderColumns: { method: (endpoint) => useColor(args) ? (METHOD_COLORS[endpoint.method] ?? String)(endpoint.method) : endpoint.method }
142
163
  });
143
164
  }
144
165
  function isListing(args) {
@@ -242,16 +263,45 @@ async function runRequest(args) {
242
263
  ...hasBody ? { body } : {},
243
264
  ...Object.keys(headers).length > 0 ? { headers } : {}
244
265
  };
245
- const response = await args.apiClient.request(params);
246
- const out = process.stdout;
266
+ let response;
267
+ try {
268
+ response = await args.apiClient.request(params);
269
+ } catch (err) {
270
+ if (args.include && isNeonApiError(err) && err.status !== void 0 && err.status !== 401 && !isSsoCode(codeFromBody(err.data))) await writeResponse(args, {
271
+ status: err.status,
272
+ statusText: err.statusText ?? "",
273
+ headers: err.headers ?? {},
274
+ data: err.data
275
+ });
276
+ throw err;
277
+ }
278
+ await writeResponse(args, response);
279
+ }
280
+ const isSsoCode = (code) => code === "SSO_AUTHORIZATION_REQUIRED" || code === "SSO_ORG_CREDS_ONLY";
281
+ /** Pipes stay plain regardless of FORCE_COLOR, so `neon api … | jq` never sees escapes. */
282
+ const useColor = (args) => process.stdout.isTTY === true && args.color !== false && !process.env.NO_COLOR && chalk.level > 0;
283
+ /** Resolves once stdout has taken the text, so a following `process.exit` can't cut it off. */
284
+ async function writeResponse(args, response) {
285
+ const color = useColor(args);
286
+ let text = "";
247
287
  if (args.include) {
248
- out.write(`HTTP ${response.status} ${response.statusText}\n`);
249
- for (const [key, value] of Object.entries(response.headers)) out.write(`${key}: ${value}\n`);
250
- out.write("\n");
288
+ const status = `HTTP ${response.status} ${response.statusText}`;
289
+ const statusColor = response.status < 400 ? chalk.green : chalk.red;
290
+ text += `${color ? statusColor.bold(status) : status}\n`;
291
+ for (const [key, value] of Object.entries(response.headers)) text += `${color ? chalk.cyan(key) : key}: ${value}\n`;
292
+ text += "\n";
251
293
  }
252
- if (response.data === void 0) return;
253
- if (args.output === "yaml") out.write(YAML.stringify(response.data));
254
- else out.write(`${JSON.stringify(response.data, null, 2)}\n`);
294
+ if (response.data !== void 0) {
295
+ if (args.output === "yaml") text += YAML.stringify(response.data);
296
+ else {
297
+ const json = JSON.stringify(response.data, null, 2);
298
+ text += `${color ? colorizeJson(json) : json}\n`;
299
+ }
300
+ }
301
+ if (text === "") return;
302
+ await new Promise((resolve, reject) => {
303
+ process.stdout.write(text, (err) => err ? reject(err) : resolve());
304
+ });
255
305
  }
256
306
  const command = "api [path]";
257
307
  const describe = "Call any Neon API route directly (authenticated passthrough)";
@@ -335,4 +385,4 @@ const handler = async (args) => {
335
385
  await runRequest(apiArgs);
336
386
  };
337
387
  //#endregion
338
- export { buildBody, buildQuery, builder, command, describe, handler, parseHeaders, parseKeyValue, parseTypedValue, setDeep, api_exports as t };
388
+ export { buildBody, buildQuery, builder, colorizeJson, command, describe, handler, parseHeaders, parseKeyValue, parseTypedValue, setDeep, api_exports as t };
package/dist/index.js CHANGED
@@ -179,6 +179,7 @@ async function handleError(msg, err, canRetry) {
179
179
  } else {
180
180
  const serverMessage = messageFromBody(err.data);
181
181
  if (serverMessage) log.error(serverMessage);
182
+ else log.error([`HTTP ${err.status ?? ""} ${err.statusText ?? ""}`.trim(), err.requestPath].filter(Boolean).join(" | "));
182
183
  log.debug("status: %d %s | path: %s", err.status, err.statusText, err.requestPath);
183
184
  sendError(err, "API_ERROR");
184
185
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neon",
3
- "version": "7.0.5",
3
+ "version": "7.0.6",
4
4
  "description": "CLI tool for Neon, the cloud backend primitives built around Lakebase Postgres",
5
5
  "keywords": [
6
6
  "neon",
@@ -64,9 +64,9 @@
64
64
  "yaml": "^2.9.0",
65
65
  "yargs": "17.7.2",
66
66
  "yoctocolors": "^2.1.2",
67
- "@neon/sdk": "7.0.0",
68
67
  "@neon/config": "1.8.3",
69
- "@neon/config-runtime": "1.7.3"
68
+ "@neon/config-runtime": "1.7.3",
69
+ "@neon/sdk": "7.0.0"
70
70
  },
71
71
  "optionalDependencies": {
72
72
  "@napi-rs/keyring": "1.3.0",