@argos-ci/api-client 0.16.0 → 0.17.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.
package/dist/index.js DELETED
@@ -1,259 +0,0 @@
1
- // src/index.ts
2
- import createFetchClient from "openapi-fetch";
3
-
4
- // ../../node_modules/.pnpm/is-network-error@1.3.0/node_modules/is-network-error/index.js
5
- var objectToString = Object.prototype.toString;
6
- var isError = (value) => objectToString.call(value) === "[object Error]";
7
- var errorMessages = /* @__PURE__ */ new Set([
8
- "network error",
9
- // Chrome
10
- "Failed to fetch",
11
- // Chrome
12
- "NetworkError when attempting to fetch resource.",
13
- // Firefox
14
- "The Internet connection appears to be offline.",
15
- // Safari 16
16
- "Network request failed",
17
- // `cross-fetch`
18
- "fetch failed",
19
- // Undici (Node.js)
20
- "terminated",
21
- // Undici (Node.js)
22
- " A network error occurred.",
23
- // Bun (WebKit)
24
- "Network connection lost"
25
- // Cloudflare Workers (fetch)
26
- ]);
27
- function isNetworkError(error) {
28
- const isValid = error && isError(error) && error.name === "TypeError" && typeof error.message === "string";
29
- if (!isValid) {
30
- return false;
31
- }
32
- const { message, stack } = error;
33
- if (message === "Load failed") {
34
- return stack === void 0 || "__sentry_captured__" in error;
35
- }
36
- if (message.startsWith("error sending request for url")) {
37
- return true;
38
- }
39
- return errorMessages.has(message);
40
- }
41
-
42
- // ../../node_modules/.pnpm/p-retry@7.1.1/node_modules/p-retry/index.js
43
- function validateRetries(retries) {
44
- if (typeof retries === "number") {
45
- if (retries < 0) {
46
- throw new TypeError("Expected `retries` to be a non-negative number.");
47
- }
48
- if (Number.isNaN(retries)) {
49
- throw new TypeError("Expected `retries` to be a valid number or Infinity, got NaN.");
50
- }
51
- } else if (retries !== void 0) {
52
- throw new TypeError("Expected `retries` to be a number or Infinity.");
53
- }
54
- }
55
- function validateNumberOption(name, value, { min = 0, allowInfinity = false } = {}) {
56
- if (value === void 0) {
57
- return;
58
- }
59
- if (typeof value !== "number" || Number.isNaN(value)) {
60
- throw new TypeError(`Expected \`${name}\` to be a number${allowInfinity ? " or Infinity" : ""}.`);
61
- }
62
- if (!allowInfinity && !Number.isFinite(value)) {
63
- throw new TypeError(`Expected \`${name}\` to be a finite number.`);
64
- }
65
- if (value < min) {
66
- throw new TypeError(`Expected \`${name}\` to be \u2265 ${min}.`);
67
- }
68
- }
69
- var AbortError = class extends Error {
70
- constructor(message) {
71
- super();
72
- if (message instanceof Error) {
73
- this.originalError = message;
74
- ({ message } = message);
75
- } else {
76
- this.originalError = new Error(message);
77
- this.originalError.stack = this.stack;
78
- }
79
- this.name = "AbortError";
80
- this.message = message;
81
- }
82
- };
83
- function calculateDelay(retriesConsumed, options) {
84
- const attempt = Math.max(1, retriesConsumed + 1);
85
- const random = options.randomize ? Math.random() + 1 : 1;
86
- let timeout = Math.round(random * options.minTimeout * options.factor ** (attempt - 1));
87
- timeout = Math.min(timeout, options.maxTimeout);
88
- return timeout;
89
- }
90
- function calculateRemainingTime(start, max) {
91
- if (!Number.isFinite(max)) {
92
- return max;
93
- }
94
- return max - (performance.now() - start);
95
- }
96
- async function onAttemptFailure({ error, attemptNumber, retriesConsumed, startTime, options }) {
97
- const normalizedError = error instanceof Error ? error : new TypeError(`Non-error was thrown: "${error}". You should only throw errors.`);
98
- if (normalizedError instanceof AbortError) {
99
- throw normalizedError.originalError;
100
- }
101
- const retriesLeft = Number.isFinite(options.retries) ? Math.max(0, options.retries - retriesConsumed) : options.retries;
102
- const maxRetryTime = options.maxRetryTime ?? Number.POSITIVE_INFINITY;
103
- const context = Object.freeze({
104
- error: normalizedError,
105
- attemptNumber,
106
- retriesLeft,
107
- retriesConsumed
108
- });
109
- await options.onFailedAttempt(context);
110
- if (calculateRemainingTime(startTime, maxRetryTime) <= 0) {
111
- throw normalizedError;
112
- }
113
- const consumeRetry = await options.shouldConsumeRetry(context);
114
- const remainingTime = calculateRemainingTime(startTime, maxRetryTime);
115
- if (remainingTime <= 0 || retriesLeft <= 0) {
116
- throw normalizedError;
117
- }
118
- if (normalizedError instanceof TypeError && !isNetworkError(normalizedError)) {
119
- if (consumeRetry) {
120
- throw normalizedError;
121
- }
122
- options.signal?.throwIfAborted();
123
- return false;
124
- }
125
- if (!await options.shouldRetry(context)) {
126
- throw normalizedError;
127
- }
128
- if (!consumeRetry) {
129
- options.signal?.throwIfAborted();
130
- return false;
131
- }
132
- const delayTime = calculateDelay(retriesConsumed, options);
133
- const finalDelay = Math.min(delayTime, remainingTime);
134
- options.signal?.throwIfAborted();
135
- if (finalDelay > 0) {
136
- await new Promise((resolve, reject) => {
137
- const onAbort = () => {
138
- clearTimeout(timeoutToken);
139
- options.signal?.removeEventListener("abort", onAbort);
140
- reject(options.signal.reason);
141
- };
142
- const timeoutToken = setTimeout(() => {
143
- options.signal?.removeEventListener("abort", onAbort);
144
- resolve();
145
- }, finalDelay);
146
- if (options.unref) {
147
- timeoutToken.unref?.();
148
- }
149
- options.signal?.addEventListener("abort", onAbort, { once: true });
150
- });
151
- }
152
- options.signal?.throwIfAborted();
153
- return true;
154
- }
155
- async function pRetry(input, options = {}) {
156
- options = { ...options };
157
- validateRetries(options.retries);
158
- if (Object.hasOwn(options, "forever")) {
159
- throw new Error("The `forever` option is no longer supported. For many use-cases, you can set `retries: Infinity` instead.");
160
- }
161
- options.retries ??= 10;
162
- options.factor ??= 2;
163
- options.minTimeout ??= 1e3;
164
- options.maxTimeout ??= Number.POSITIVE_INFINITY;
165
- options.maxRetryTime ??= Number.POSITIVE_INFINITY;
166
- options.randomize ??= false;
167
- options.onFailedAttempt ??= () => {
168
- };
169
- options.shouldRetry ??= () => true;
170
- options.shouldConsumeRetry ??= () => true;
171
- validateNumberOption("factor", options.factor, { min: 0, allowInfinity: false });
172
- validateNumberOption("minTimeout", options.minTimeout, { min: 0, allowInfinity: false });
173
- validateNumberOption("maxTimeout", options.maxTimeout, { min: 0, allowInfinity: true });
174
- validateNumberOption("maxRetryTime", options.maxRetryTime, { min: 0, allowInfinity: true });
175
- if (!(options.factor > 0)) {
176
- options.factor = 1;
177
- }
178
- options.signal?.throwIfAborted();
179
- let attemptNumber = 0;
180
- let retriesConsumed = 0;
181
- const startTime = performance.now();
182
- while (Number.isFinite(options.retries) ? retriesConsumed <= options.retries : true) {
183
- attemptNumber++;
184
- try {
185
- options.signal?.throwIfAborted();
186
- const result = await input(attemptNumber);
187
- options.signal?.throwIfAborted();
188
- return result;
189
- } catch (error) {
190
- if (await onAttemptFailure({
191
- error,
192
- attemptNumber,
193
- retriesConsumed,
194
- startTime,
195
- options
196
- })) {
197
- retriesConsumed++;
198
- }
199
- }
200
- }
201
- throw new Error("Retry attempts exhausted without throwing an error.");
202
- }
203
-
204
- // src/debug.ts
205
- import createDebug from "debug";
206
- var KEY = "@argos-ci/api-client";
207
- var debug = createDebug(KEY);
208
-
209
- // src/schema.ts
210
- var schema_exports = {};
211
-
212
- // src/index.ts
213
- function createClient(options) {
214
- const { baseUrl } = options || {};
215
- return createFetchClient({
216
- baseUrl: baseUrl || "https://api.argos-ci.com/v2/",
217
- headers: {
218
- Authorization: `Bearer ${options.authToken}`
219
- },
220
- fetch: (input) => {
221
- return pRetry(
222
- async () => {
223
- const response = await fetch(input.clone());
224
- if (response.status >= 500) {
225
- throw new APIError("Internal Server Error");
226
- }
227
- return response;
228
- },
229
- {
230
- retries: 3,
231
- onFailedAttempt: (context) => {
232
- debug("API request failed", context.error.message);
233
- if (context.retriesLeft > 0) {
234
- debug(`Retrying API request... (${context.retriesLeft} left)`);
235
- }
236
- }
237
- }
238
- );
239
- }
240
- });
241
- }
242
- var APIError = class extends Error {
243
- constructor(message) {
244
- super(message);
245
- }
246
- };
247
- function throwAPIError(error) {
248
- debug("API error", error);
249
- const detailMessage = error.details?.map((detail) => detail.message).join(", ");
250
- throw new APIError(
251
- detailMessage ? `${error.error}: ${detailMessage}` : error.error
252
- );
253
- }
254
- export {
255
- APIError,
256
- schema_exports as ArgosAPISchema,
257
- createClient,
258
- throwAPIError
259
- };