@baiyibai-antora/atlassian-client 1.0.0-beta.7 → 1.0.0-beta.8
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/lib/index.js +27 -3
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -32,6 +32,30 @@ class AtlassianClient {
|
|
|
32
32
|
return `Bearer ${await this.oauth.getAccessToken()}`
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
// Transient-failure retry: connections intermittently drop mid-run and a
|
|
36
|
+
// whole space retrieval or publish must not abort on one hiccup.
|
|
37
|
+
// Network-level failures retry for any method; gateway 5xx retries are
|
|
38
|
+
// limited to GET, whose repeats are safe by definition. Token rotation
|
|
39
|
+
// is never retried here (a repeated rotation request revokes the chain);
|
|
40
|
+
// it lives in the OAuth session, which keeps its own fetch.
|
|
41
|
+
async fetchRetry (url, opts) {
|
|
42
|
+
const delays = [500, 2000]
|
|
43
|
+
for (let attempt = 0; ; attempt++) {
|
|
44
|
+
try {
|
|
45
|
+
const response = await this.fetch(url, opts)
|
|
46
|
+
const method = (opts && opts.method) || 'GET'
|
|
47
|
+
if (attempt < delays.length && method === 'GET' && [502, 503, 504].includes(response.status)) {
|
|
48
|
+
await new Promise((resolve) => setTimeout(resolve, delays[attempt]))
|
|
49
|
+
continue
|
|
50
|
+
}
|
|
51
|
+
return response
|
|
52
|
+
} catch (err) {
|
|
53
|
+
if (attempt >= delays.length) throw err
|
|
54
|
+
await new Promise((resolve) => setTimeout(resolve, delays[attempt]))
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
35
59
|
async request (method, path, { body, headers, form } = {}) {
|
|
36
60
|
const opts = { method, headers: { Authorization: await this.authHeader(), Accept: 'application/json', ...headers } }
|
|
37
61
|
if (form) {
|
|
@@ -41,12 +65,12 @@ class AtlassianClient {
|
|
|
41
65
|
opts.headers['Content-Type'] = 'application/json'
|
|
42
66
|
opts.body = JSON.stringify(body)
|
|
43
67
|
}
|
|
44
|
-
let response = await this.
|
|
68
|
+
let response = await this.fetchRetry(`${this.baseUrl}${path}`, opts)
|
|
45
69
|
// A 401 on an OAuth session usually means the cached access token was
|
|
46
70
|
// revoked or expired early; refresh once and retry.
|
|
47
71
|
if (response.status === 401 && this.oauth) {
|
|
48
72
|
opts.headers.Authorization = `Bearer ${await this.oauth.refresh()}`
|
|
49
|
-
response = await this.
|
|
73
|
+
response = await this.fetchRetry(`${this.baseUrl}${path}`, opts)
|
|
50
74
|
}
|
|
51
75
|
if (response.status === 404) return null
|
|
52
76
|
if (!response.ok) {
|
|
@@ -59,7 +83,7 @@ class AtlassianClient {
|
|
|
59
83
|
|
|
60
84
|
// Raw body download (attachments, exports); returns a Buffer or null on 404.
|
|
61
85
|
async download (path) {
|
|
62
|
-
const response = await this.
|
|
86
|
+
const response = await this.fetchRetry(`${this.baseUrl}${path}`, {
|
|
63
87
|
headers: { Authorization: await this.authHeader() },
|
|
64
88
|
redirect: 'follow',
|
|
65
89
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baiyibai-antora/atlassian-client",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.8",
|
|
4
4
|
"description": "Minimal zero-dependency Atlassian Cloud REST client (basic auth, JSON, multipart, v2 cursor pagination) shared by the Confluora packages",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "baiyibai",
|