@ascendkit/cli 0.2.6 → 0.3.1

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.
@@ -13,6 +13,7 @@ export declare class AscendKitClient {
13
13
  });
14
14
  get configured(): boolean;
15
15
  get currentPublicKey(): string | null;
16
+ get currentApiUrl(): string;
16
17
  get platformConfigured(): boolean;
17
18
  configure(publicKey: string, apiUrl?: string): void;
18
19
  configurePlatform(token: string, apiUrl?: string): void;
@@ -5,6 +5,7 @@ import { readFileSync } from "fs";
5
5
  import { fileURLToPath } from "url";
6
6
  import { dirname, resolve } from "path";
7
7
  import { DEFAULT_API_URL } from "../constants.js";
8
+ import { correlationHeaders } from "../utils/correlation.js";
8
9
  const __filename = fileURLToPath(import.meta.url);
9
10
  const __dirname = dirname(__filename);
10
11
  function readPackageVersion() {
@@ -17,6 +18,30 @@ function readPackageVersion() {
17
18
  }
18
19
  }
19
20
  export const CLI_VERSION = readPackageVersion();
21
+ async function extractErrorMessage(response) {
22
+ const text = await response.text();
23
+ if (!text) {
24
+ return `AscendKit API error ${response.status}: ${response.statusText}`;
25
+ }
26
+ try {
27
+ const parsed = JSON.parse(text);
28
+ if (typeof parsed.error === "string") {
29
+ return parsed.error;
30
+ }
31
+ if (typeof parsed.detail === "string") {
32
+ return parsed.detail;
33
+ }
34
+ if (parsed.detail
35
+ && typeof parsed.detail === "object"
36
+ && typeof parsed.detail.message === "string") {
37
+ return parsed.detail.message;
38
+ }
39
+ }
40
+ catch {
41
+ // Fall back to the raw response body below.
42
+ }
43
+ return `AscendKit API error ${response.status}: ${text}`;
44
+ }
20
45
  export class AscendKitClient {
21
46
  baseUrl;
22
47
  publicKey;
@@ -33,6 +58,9 @@ export class AscendKitClient {
33
58
  get currentPublicKey() {
34
59
  return this.publicKey;
35
60
  }
61
+ get currentApiUrl() {
62
+ return this.baseUrl;
63
+ }
36
64
  get platformConfigured() {
37
65
  return this.platformToken !== null;
38
66
  }
@@ -47,7 +75,10 @@ export class AscendKitClient {
47
75
  this.baseUrl = apiUrl;
48
76
  }
49
77
  get versionHeader() {
50
- return { "X-AscendKit-Client-Version": `cli/${CLI_VERSION}` };
78
+ return {
79
+ "X-AscendKit-Client-Version": `cli/${CLI_VERSION}`,
80
+ ...correlationHeaders(),
81
+ };
51
82
  }
52
83
  get headers() {
53
84
  if (!this.publicKey) {
@@ -105,8 +136,7 @@ export class AscendKitClient {
105
136
  const response = await fetch(url, init);
106
137
  this.checkUpgradeHeader(response);
107
138
  if (!response.ok) {
108
- const text = await response.text();
109
- throw new Error(`AscendKit API error ${response.status}: ${text}`);
139
+ throw new Error(await extractErrorMessage(response));
110
140
  }
111
141
  const json = (await response.json());
112
142
  return json.data;
@@ -123,8 +153,7 @@ export class AscendKitClient {
123
153
  const response = await fetch(url, init);
124
154
  this.checkUpgradeHeader(response);
125
155
  if (!response.ok) {
126
- const text = await response.text();
127
- throw new Error(`AscendKit API error ${response.status}: ${text}`);
156
+ throw new Error(await extractErrorMessage(response));
128
157
  }
129
158
  const json = (await response.json());
130
159
  return json.data;
@@ -142,8 +171,7 @@ export class AscendKitClient {
142
171
  const response = await fetch(url, init);
143
172
  this.checkUpgradeHeader(response);
144
173
  if (!response.ok) {
145
- const text = await response.text();
146
- throw new Error(`AscendKit API error ${response.status}: ${text}`);
174
+ throw new Error(await extractErrorMessage(response));
147
175
  }
148
176
  const json = (await response.json());
149
177
  return json.data;
@@ -173,8 +201,7 @@ export class AscendKitClient {
173
201
  const response = await fetch(url, init);
174
202
  this.checkUpgradeHeader(response);
175
203
  if (!response.ok) {
176
- const text = await response.text();
177
- throw new Error(`AscendKit API error ${response.status}: ${text}`);
204
+ throw new Error(await extractErrorMessage(response));
178
205
  }
179
206
  const json = (await response.json());
180
207
  return json.data;