@argos-ci/api-client 0.9.0 → 0.11.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.d.ts CHANGED
@@ -133,13 +133,29 @@ interface components {
133
133
  repeat?: number | null;
134
134
  /** @description The location of the test in the source code */
135
135
  location?: {
136
- /** @description The file where the test is located */
136
+ /** @description The located file */
137
137
  file: string;
138
138
  /** @description The line number in the file */
139
139
  line: number;
140
140
  /** @description The column number in the file */
141
141
  column: number;
142
142
  };
143
+ /** @description Annotations associated to the test */
144
+ annotations?: {
145
+ /** @description The type of annotation */
146
+ type: string;
147
+ /** @description The description of the annotation */
148
+ description?: string;
149
+ /** @description The location of the annotation in the source code */
150
+ location?: {
151
+ /** @description The located file */
152
+ file: string;
153
+ /** @description The line number in the file */
154
+ line: number;
155
+ /** @description The column number in the file */
156
+ column: number;
157
+ };
158
+ }[];
143
159
  } | null) | null;
144
160
  browser?: {
145
161
  /** @description The name of the browser */
@@ -615,11 +631,6 @@ declare class APIError extends Error {
615
631
  /**
616
632
  * Handle API errors.
617
633
  */
618
- declare function throwAPIError(error: {
619
- error: string;
620
- details?: {
621
- message: string;
622
- }[];
623
- }): never;
634
+ declare function throwAPIError(error: components["schemas"]["Error"]): never;
624
635
 
625
636
  export { APIError, type ArgosAPIClient, schema as ArgosAPISchema, createClient, throwAPIError };
package/dist/index.js CHANGED
@@ -1,6 +1,175 @@
1
1
  // src/index.ts
2
2
  import createFetchClient from "openapi-fetch";
3
3
 
4
+ // ../../node_modules/.pnpm/is-network-error@1.1.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
+ "Load failed",
17
+ // Safari 17+
18
+ "Network request failed",
19
+ // `cross-fetch`
20
+ "fetch failed",
21
+ // Undici (Node.js)
22
+ "terminated"
23
+ // Undici (Node.js)
24
+ ]);
25
+ function isNetworkError(error) {
26
+ const isValid = error && isError(error) && error.name === "TypeError" && typeof error.message === "string";
27
+ if (!isValid) {
28
+ return false;
29
+ }
30
+ if (error.message === "Load failed") {
31
+ return error.stack === void 0;
32
+ }
33
+ return errorMessages.has(error.message);
34
+ }
35
+
36
+ // ../../node_modules/.pnpm/p-retry@7.0.0/node_modules/p-retry/index.js
37
+ function validateRetries(retries) {
38
+ if (typeof retries === "number") {
39
+ if (retries < 0) {
40
+ throw new TypeError("Expected `retries` to be a non-negative number.");
41
+ }
42
+ if (Number.isNaN(retries)) {
43
+ throw new TypeError("Expected `retries` to be a valid number or Infinity, got NaN.");
44
+ }
45
+ } else if (retries !== void 0) {
46
+ throw new TypeError("Expected `retries` to be a number or Infinity.");
47
+ }
48
+ }
49
+ function validateNumberOption(name, value, { min = 0, allowInfinity = false } = {}) {
50
+ if (value === void 0) {
51
+ return;
52
+ }
53
+ if (typeof value !== "number" || Number.isNaN(value)) {
54
+ throw new TypeError(`Expected \`${name}\` to be a number${allowInfinity ? " or Infinity" : ""}.`);
55
+ }
56
+ if (!allowInfinity && !Number.isFinite(value)) {
57
+ throw new TypeError(`Expected \`${name}\` to be a finite number.`);
58
+ }
59
+ if (value < min) {
60
+ throw new TypeError(`Expected \`${name}\` to be \u2265 ${min}.`);
61
+ }
62
+ }
63
+ var AbortError = class extends Error {
64
+ constructor(message) {
65
+ super();
66
+ if (message instanceof Error) {
67
+ this.originalError = message;
68
+ ({ message } = message);
69
+ } else {
70
+ this.originalError = new Error(message);
71
+ this.originalError.stack = this.stack;
72
+ }
73
+ this.name = "AbortError";
74
+ this.message = message;
75
+ }
76
+ };
77
+ var createRetryContext = (error, attemptNumber, options) => {
78
+ const retriesLeft = options.retries - (attemptNumber - 1);
79
+ return Object.freeze({
80
+ error,
81
+ attemptNumber,
82
+ retriesLeft
83
+ });
84
+ };
85
+ function calculateDelay(attempt, options) {
86
+ const random = options.randomize ? Math.random() + 1 : 1;
87
+ let timeout = Math.round(random * Math.max(options.minTimeout, 1) * options.factor ** (attempt - 1));
88
+ timeout = Math.min(timeout, options.maxTimeout);
89
+ return timeout;
90
+ }
91
+ async function onAttemptFailure(error, attemptNumber, options, startTime, maxRetryTime) {
92
+ let normalizedError = error;
93
+ if (!(normalizedError instanceof Error)) {
94
+ normalizedError = new TypeError(`Non-error was thrown: "${normalizedError}". You should only throw errors.`);
95
+ }
96
+ if (normalizedError instanceof AbortError) {
97
+ throw normalizedError.originalError;
98
+ }
99
+ if (normalizedError instanceof TypeError && !isNetworkError(normalizedError)) {
100
+ throw normalizedError;
101
+ }
102
+ const context = createRetryContext(normalizedError, attemptNumber, options);
103
+ await options.onFailedAttempt(context);
104
+ const currentTime = Date.now();
105
+ if (currentTime - startTime >= maxRetryTime || attemptNumber >= options.retries + 1 || !await options.shouldRetry(context)) {
106
+ throw normalizedError;
107
+ }
108
+ const delayTime = calculateDelay(attemptNumber, options);
109
+ const timeLeft = maxRetryTime - (currentTime - startTime);
110
+ if (timeLeft <= 0) {
111
+ throw normalizedError;
112
+ }
113
+ const finalDelay = Math.min(delayTime, timeLeft);
114
+ if (finalDelay > 0) {
115
+ await new Promise((resolve, reject) => {
116
+ const onAbort = () => {
117
+ clearTimeout(timeoutToken);
118
+ options.signal?.removeEventListener("abort", onAbort);
119
+ reject(options.signal.reason);
120
+ };
121
+ const timeoutToken = setTimeout(() => {
122
+ options.signal?.removeEventListener("abort", onAbort);
123
+ resolve();
124
+ }, finalDelay);
125
+ if (options.unref) {
126
+ timeoutToken.unref?.();
127
+ }
128
+ options.signal?.addEventListener("abort", onAbort, { once: true });
129
+ });
130
+ }
131
+ options.signal?.throwIfAborted();
132
+ }
133
+ async function pRetry(input, options = {}) {
134
+ options = { ...options };
135
+ validateRetries(options.retries);
136
+ if (Object.hasOwn(options, "forever")) {
137
+ throw new Error("The `forever` option is no longer supported. For many use-cases, you can set `retries: Infinity` instead.");
138
+ }
139
+ options.retries ??= 10;
140
+ options.factor ??= 2;
141
+ options.minTimeout ??= 1e3;
142
+ options.maxTimeout ??= Number.POSITIVE_INFINITY;
143
+ options.randomize ??= false;
144
+ options.onFailedAttempt ??= () => {
145
+ };
146
+ options.shouldRetry ??= () => true;
147
+ validateNumberOption("factor", options.factor, { min: 0, allowInfinity: false });
148
+ validateNumberOption("minTimeout", options.minTimeout, { min: 0, allowInfinity: false });
149
+ validateNumberOption("maxTimeout", options.maxTimeout, { min: 0, allowInfinity: true });
150
+ const resolvedMaxRetryTime = options.maxRetryTime ?? Number.POSITIVE_INFINITY;
151
+ validateNumberOption("maxRetryTime", resolvedMaxRetryTime, { min: 0, allowInfinity: true });
152
+ if (!(options.factor > 0)) {
153
+ options.factor = 1;
154
+ }
155
+ options.signal?.throwIfAborted();
156
+ let attemptNumber = 0;
157
+ const startTime = Date.now();
158
+ const maxRetryTime = resolvedMaxRetryTime;
159
+ while (attemptNumber < options.retries + 1) {
160
+ attemptNumber++;
161
+ try {
162
+ options.signal?.throwIfAborted();
163
+ const result = await input(attemptNumber);
164
+ options.signal?.throwIfAborted();
165
+ return result;
166
+ } catch (error) {
167
+ await onAttemptFailure(error, attemptNumber, options, startTime, maxRetryTime);
168
+ }
169
+ }
170
+ throw new Error("Retry attempts exhausted without throwing an error.");
171
+ }
172
+
4
173
  // src/debug.ts
5
174
  import createDebug from "debug";
6
175
  var KEY = "@argos-ci/api-client";
@@ -16,6 +185,26 @@ function createClient(options) {
16
185
  baseUrl: baseUrl || "https://api.argos-ci.com/v2/",
17
186
  headers: {
18
187
  Authorization: `Bearer ${options.authToken}`
188
+ },
189
+ fetch: (input) => {
190
+ return pRetry(
191
+ async () => {
192
+ const response = await fetch(input.clone());
193
+ if (response.status >= 500) {
194
+ throw new APIError("Internal Server Error");
195
+ }
196
+ return response;
197
+ },
198
+ {
199
+ retries: 3,
200
+ onFailedAttempt: (context) => {
201
+ debug("API request failed", context.error.message);
202
+ if (context.retriesLeft > 0) {
203
+ debug(`Retrying API request... (${context.retriesLeft} left)`);
204
+ }
205
+ }
206
+ }
207
+ );
19
208
  }
20
209
  });
21
210
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@argos-ci/api-client",
3
3
  "description": "Argos API Client library.",
4
- "version": "0.9.0",
4
+ "version": "0.11.0",
5
5
  "author": "Smooth Code",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -45,11 +45,12 @@
45
45
  "devDependencies": {
46
46
  "@types/debug": "^4.1.12",
47
47
  "@types/node": "catalog:",
48
- "openapi-typescript": "^7.8.0"
48
+ "openapi-typescript": "^7.8.0",
49
+ "p-retry": "^7.0.0"
49
50
  },
50
51
  "dependencies": {
51
52
  "debug": "^4.4.0",
52
53
  "openapi-fetch": "^0.14.0"
53
54
  },
54
- "gitHead": "807e4a9f3c4bfddd4197ed7394b032a39bc1c58e"
55
+ "gitHead": "74818a1d3fe21574d0884c828ee709e62605d974"
55
56
  }