@openstax/ts-utils 1.18.0 → 1.20.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/cjs/fetch/fetchStatusRetry.d.ts +8 -0
- package/dist/cjs/fetch/fetchStatusRetry.js +24 -0
- package/dist/cjs/services/apiGateway/index.d.ts +2 -2
- package/dist/cjs/services/apiGateway/index.js +5 -3
- package/dist/cjs/services/lrsGateway/index.js +5 -3
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/fetch/fetchStatusRetry.d.ts +8 -0
- package/dist/esm/fetch/fetchStatusRetry.js +20 -0
- package/dist/esm/services/apiGateway/index.d.ts +2 -2
- package/dist/esm/services/apiGateway/index.js +5 -3
- package/dist/esm/services/lrsGateway/index.js +5 -3
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchStatusRetry = void 0;
|
|
4
|
+
const fetchStatusRetry = (base, options) => {
|
|
5
|
+
const fetchTry = (retries, ...params) => {
|
|
6
|
+
return base(...params)
|
|
7
|
+
.catch(e => options.onFail && retries > 0
|
|
8
|
+
? fetchTry(retries - 1, ...params)
|
|
9
|
+
: Promise.reject(e))
|
|
10
|
+
.then(async (r) => {
|
|
11
|
+
var _a;
|
|
12
|
+
const shouldHandleStatus = (_a = options.status) === null || _a === void 0 ? void 0 : _a.includes(r.status);
|
|
13
|
+
if (shouldHandleStatus && retries > 0) {
|
|
14
|
+
return fetchTry(retries - 1, ...params);
|
|
15
|
+
}
|
|
16
|
+
else if (shouldHandleStatus) {
|
|
17
|
+
throw new Error(`fetch failed after ${options.retries} retries. ${params[0]} -- ${r.status}: ${await r.text()}`);
|
|
18
|
+
}
|
|
19
|
+
return r;
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
return (...params) => fetchTry(options.retries, ...params);
|
|
23
|
+
};
|
|
24
|
+
exports.fetchStatusRetry = fetchStatusRetry;
|
|
@@ -21,8 +21,8 @@ declare type RouteClient<R> = {
|
|
|
21
21
|
})) => Promise<string>;
|
|
22
22
|
};
|
|
23
23
|
interface AcceptStatus<Ro> {
|
|
24
|
-
<S extends TResponseStatus<Ro>[]>(...args: S): ApiClientResponse<Extract<Ro, Record<'statusCode', S[number]
|
|
25
|
-
<S extends number[]>(...args: S): ApiClientResponse<any
|
|
24
|
+
<S extends TResponseStatus<Ro>[]>(...args: S): Promise<ApiClientResponse<Extract<Ro, Record<'statusCode', S[number]>>>>;
|
|
25
|
+
<S extends number[]>(...args: S): Promise<ApiClientResponse<any>>;
|
|
26
26
|
}
|
|
27
27
|
declare type UnsafeApiClientResponse<Ro> = {
|
|
28
28
|
headers: Headers;
|
|
@@ -32,6 +32,7 @@ const query_string_1 = __importDefault(require("query-string"));
|
|
|
32
32
|
const __1 = require("../..");
|
|
33
33
|
const config_1 = require("../../config");
|
|
34
34
|
const errors_1 = require("../../errors");
|
|
35
|
+
const fetchStatusRetry_1 = require("../../fetch/fetchStatusRetry");
|
|
35
36
|
/** Pulls the content out of a response based on the content type */
|
|
36
37
|
const loadResponse = (response) => () => {
|
|
37
38
|
const [contentType] = (response.headers.get('content-type') || '').split(';');
|
|
@@ -57,7 +58,8 @@ const makeRouteClient = (initializer, config, route, authProvider) => {
|
|
|
57
58
|
const url = await renderUrl({ params, query });
|
|
58
59
|
const body = payload ? JSON.stringify(payload) : undefined;
|
|
59
60
|
const baseOptions = (0, __1.merge)((await (authProvider === null || authProvider === void 0 ? void 0 : authProvider.getAuthorizedFetchConfig())) || {}, fetchConfig || {});
|
|
60
|
-
|
|
61
|
+
const fetcher = (0, fetchStatusRetry_1.fetchStatusRetry)(initializer.fetch, { retries: 1, status: [502], onFail: true });
|
|
62
|
+
return fetcher(url, (0, __1.merge)(baseOptions, {
|
|
61
63
|
method: route.method,
|
|
62
64
|
body,
|
|
63
65
|
headers: {
|
|
@@ -73,9 +75,9 @@ const makeRouteClient = (initializer, config, route, authProvider) => {
|
|
|
73
75
|
}
|
|
74
76
|
return {
|
|
75
77
|
status: response.status,
|
|
76
|
-
acceptStatus: (...status) => {
|
|
78
|
+
acceptStatus: async (...status) => {
|
|
77
79
|
if (!status.includes(response.status)) {
|
|
78
|
-
throw new Error(`unexpected HTTP ${response.status} response from api`);
|
|
80
|
+
throw new Error(`unexpected HTTP ${response.status} response from api: ${await response.text()}`);
|
|
79
81
|
}
|
|
80
82
|
return { status: response.status, headers: response.headers, load: (0, exports.loadResponse)(response) };
|
|
81
83
|
},
|
|
@@ -33,12 +33,14 @@ const __1 = require("../..");
|
|
|
33
33
|
const assertions_1 = require("../../assertions");
|
|
34
34
|
const config_1 = require("../../config");
|
|
35
35
|
const errors_1 = require("../../errors");
|
|
36
|
+
const fetchStatusRetry_1 = require("../../fetch/fetchStatusRetry");
|
|
36
37
|
const guards_1 = require("../../guards");
|
|
37
38
|
const routing_1 = require("../../routing");
|
|
38
39
|
const lrsGateway = (initializer) => (configProvider) => {
|
|
39
40
|
const config = configProvider[(0, guards_1.ifDefined)(initializer.configSpace, 'lrs')];
|
|
40
41
|
const lrsHost = (0, __1.once)(() => (0, config_1.resolveConfigValue)(config.lrsHost));
|
|
41
42
|
const lrsAuthorization = (0, __1.once)(() => (0, config_1.resolveConfigValue)(config.lrsAuthorization));
|
|
43
|
+
const fetcher = (0, fetchStatusRetry_1.fetchStatusRetry)(initializer.fetch, { retries: 1, status: [502], onFail: true });
|
|
42
44
|
return (authProvider) => {
|
|
43
45
|
// Note: This method actually uses POST
|
|
44
46
|
const putXapiStatements = async (statements) => {
|
|
@@ -54,7 +56,7 @@ const lrsGateway = (initializer) => (configProvider) => {
|
|
|
54
56
|
},
|
|
55
57
|
timestamp: (0, formatISO_1.default)(new Date())
|
|
56
58
|
}));
|
|
57
|
-
const response = await
|
|
59
|
+
const response = await fetcher((await lrsHost()).replace(/\/+$/, '') + '/data/xAPI/statements', {
|
|
58
60
|
body: JSON.stringify(statementsWithDefaults),
|
|
59
61
|
headers: {
|
|
60
62
|
Authorization: await lrsAuthorization(),
|
|
@@ -82,13 +84,13 @@ ${await response.text()}`);
|
|
|
82
84
|
}
|
|
83
85
|
return response.json();
|
|
84
86
|
};
|
|
85
|
-
const getMoreXapiStatements = async (more) => formatGetXapiStatementsResponse(
|
|
87
|
+
const getMoreXapiStatements = async (more) => formatGetXapiStatementsResponse(fetcher((await lrsHost()).replace(/\/+$/, '') + more, {
|
|
86
88
|
headers: {
|
|
87
89
|
Authorization: await lrsAuthorization(),
|
|
88
90
|
'X-Experience-API-Version': '1.0.0',
|
|
89
91
|
},
|
|
90
92
|
}));
|
|
91
|
-
const fetchXapiStatements = async ({ user, anyUser, ...options }) =>
|
|
93
|
+
const fetchXapiStatements = async ({ user, anyUser, ...options }) => fetcher((await lrsHost()).replace(/\/+$/, '') + '/data/xAPI/statements?' + queryString.stringify({
|
|
92
94
|
...options,
|
|
93
95
|
...(anyUser === true ? {} : {
|
|
94
96
|
agent: JSON.stringify({
|