@knolo/cli 5.2.1 → 5.2.3

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,15 +1,31 @@
1
- import { RegistryError, RegistryNetworkError, registryErrorMessage } from './errors.mjs';
1
+ import {
2
+ RegistryError,
3
+ RegistryNetworkError,
4
+ registryErrorMessage,
5
+ } from './errors.mjs';
2
6
 
3
- export async function getJson(url, { fetchImpl = globalThis.fetch, headers = {} } = {}) {
7
+ export async function getJson(
8
+ url,
9
+ { fetchImpl = globalThis.fetch, headers = {} } = {}
10
+ ) {
4
11
  return requestJson(url, { fetchImpl, headers, method: 'GET' });
5
12
  }
6
13
 
7
- export async function postJson(url, body, { fetchImpl = globalThis.fetch, headers = {} } = {}) {
14
+ export async function postJson(
15
+ url,
16
+ body,
17
+ { fetchImpl = globalThis.fetch, headers = {} } = {}
18
+ ) {
8
19
  return requestJson(url, { fetchImpl, headers, method: 'POST', body });
9
20
  }
10
21
 
11
- export async function putBytes(url, bytes, { fetchImpl = globalThis.fetch, headers = {} } = {}) {
12
- if (typeof fetchImpl !== 'function') throw new Error('This Node runtime does not provide fetch.');
22
+ export async function putBytes(
23
+ url,
24
+ bytes,
25
+ { fetchImpl = globalThis.fetch, headers = {} } = {}
26
+ ) {
27
+ if (typeof fetchImpl !== 'function')
28
+ throw new Error('This Node runtime does not provide fetch.');
13
29
 
14
30
  let response;
15
31
  try {
@@ -20,34 +36,83 @@ export async function putBytes(url, bytes, { fetchImpl = globalThis.fetch, heade
20
36
  body: bytes,
21
37
  });
22
38
  } catch (error) {
23
- throw new RegistryNetworkError(`Could not upload artifact to ${new URL(url).origin}.`, { url: String(url), cause: error });
39
+ throw new RegistryNetworkError(
40
+ `Could not upload artifact to ${new URL(url).origin}.`,
41
+ { url: String(url), cause: error }
42
+ );
43
+ }
44
+
45
+ const text = await readResponseText(response, url);
46
+ let parsed;
47
+ if (text) {
48
+ try {
49
+ parsed = JSON.parse(text);
50
+ } catch {
51
+ parsed = undefined;
52
+ }
24
53
  }
25
54
 
26
55
  if (!response.ok) {
27
- const detail = await readResponseText(response, url);
28
- throw new RegistryError(`Blob upload failed with HTTP ${response.status}${detail ? `: ${detail}` : '.'}`, {
29
- status: response.status,
30
- url: String(url),
31
- body: detail,
32
- });
56
+ const detail =
57
+ (parsed && typeof parsed.error === 'object' && parsed.error?.message) ||
58
+ (typeof parsed?.error === 'string' && parsed.error) ||
59
+ (typeof parsed?.message === 'string' && parsed.message) ||
60
+ (text ? text.slice(0, 240) : '');
61
+ throw new RegistryError(
62
+ `Blob upload failed with HTTP ${response.status}${detail ? `: ${detail}` : '.'}`,
63
+ {
64
+ status: response.status,
65
+ url: String(url),
66
+ body: parsed ?? text,
67
+ }
68
+ );
33
69
  }
34
70
 
35
- return { status: response.status, url: response.url || String(url) };
71
+ const publicUrl =
72
+ (parsed && typeof parsed.url === 'string' && parsed.url) ||
73
+ response.url ||
74
+ String(url);
75
+ return {
76
+ status: response.status,
77
+ url: publicUrl,
78
+ pathname: parsed && typeof parsed.pathname === 'string' ? parsed.pathname : undefined,
79
+ body: parsed,
80
+ };
36
81
  }
37
82
 
38
- async function requestJson(url, { fetchImpl = globalThis.fetch, headers = {}, method = 'GET', body: requestBody } = {}) {
39
- if (typeof fetchImpl !== 'function') throw new Error('This Node runtime does not provide fetch.');
83
+ async function requestJson(
84
+ url,
85
+ {
86
+ fetchImpl = globalThis.fetch,
87
+ headers = {},
88
+ method = 'GET',
89
+ body: requestBody,
90
+ } = {}
91
+ ) {
92
+ if (typeof fetchImpl !== 'function')
93
+ throw new Error('This Node runtime does not provide fetch.');
40
94
 
41
95
  let response;
42
96
  try {
43
97
  response = await fetchImpl(url, {
44
98
  method,
45
99
  redirect: 'follow',
46
- headers: { accept: 'application/json', ...(requestBody !== undefined ? { 'content-type': 'application/json' } : {}), ...headers },
47
- ...(requestBody !== undefined ? { body: JSON.stringify(requestBody) } : {}),
100
+ headers: {
101
+ accept: 'application/json',
102
+ ...(requestBody !== undefined
103
+ ? { 'content-type': 'application/json' }
104
+ : {}),
105
+ ...headers,
106
+ },
107
+ ...(requestBody !== undefined
108
+ ? { body: JSON.stringify(requestBody) }
109
+ : {}),
48
110
  });
49
111
  } catch (error) {
50
- throw new RegistryNetworkError(`Could not reach registry at ${new URL(url).origin}.`, { url: String(url), cause: error });
112
+ throw new RegistryNetworkError(
113
+ `Could not reach registry at ${new URL(url).origin}.`,
114
+ { url: String(url), cause: error }
115
+ );
51
116
  }
52
117
 
53
118
  const text = await readResponseText(response, url);
@@ -57,11 +122,14 @@ async function requestJson(url, { fetchImpl = globalThis.fetch, headers = {}, me
57
122
  body = JSON.parse(text);
58
123
  } catch {
59
124
  if (!response.ok) {
60
- throw new RegistryError(`Registry returned HTTP ${response.status} with a non-JSON response.`, {
61
- status: response.status,
62
- url: String(url),
63
- body: text.slice(0, 240),
64
- });
125
+ throw new RegistryError(
126
+ `Registry returned HTTP ${response.status} with a non-JSON response.`,
127
+ {
128
+ status: response.status,
129
+ url: String(url),
130
+ body: text.slice(0, 240),
131
+ }
132
+ );
65
133
  }
66
134
  throw new RegistryError('Registry returned invalid JSON.', {
67
135
  status: response.status,
@@ -74,12 +142,20 @@ async function requestJson(url, { fetchImpl = globalThis.fetch, headers = {}, me
74
142
 
75
143
  if (!response.ok) {
76
144
  const code = typeof body?.code === 'string' ? body.code : undefined;
77
- throw new RegistryError(registryErrorMessage({ status: response.status, code, body, url: String(url) }), {
78
- status: response.status,
79
- code,
80
- url: String(url),
81
- body,
82
- });
145
+ throw new RegistryError(
146
+ registryErrorMessage({
147
+ status: response.status,
148
+ code,
149
+ body,
150
+ url: String(url),
151
+ }),
152
+ {
153
+ status: response.status,
154
+ code,
155
+ url: String(url),
156
+ body,
157
+ }
158
+ );
83
159
  }
84
160
 
85
161
  return body;
@@ -89,6 +165,9 @@ async function readResponseText(response, url) {
89
165
  try {
90
166
  return await response.text();
91
167
  } catch (error) {
92
- throw new RegistryNetworkError(`Could not read registry response from ${new URL(url).origin}.`, { url: String(url), cause: error });
168
+ throw new RegistryNetworkError(
169
+ `Could not read registry response from ${new URL(url).origin}.`,
170
+ { url: String(url), cause: error }
171
+ );
93
172
  }
94
173
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knolo/cli",
3
- "version": "5.2.1",
3
+ "version": "5.2.3",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "knolo": "bin/knolo.mjs"