@openstax/ts-utils 1.19.0 → 1.20.1
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.js +3 -1
- package/dist/cjs/services/lrsGateway/addStatementDefaultFields.d.ts +5 -0
- package/dist/cjs/services/lrsGateway/addStatementDefaultFields.js +21 -0
- package/dist/cjs/services/lrsGateway/index.d.ts +2 -2
- package/dist/cjs/services/lrsGateway/index.js +9 -20
- 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.js +3 -1
- package/dist/esm/services/lrsGateway/addStatementDefaultFields.d.ts +5 -0
- package/dist/esm/services/lrsGateway/addStatementDefaultFields.js +14 -0
- package/dist/esm/services/lrsGateway/index.d.ts +2 -2
- package/dist/esm/services/lrsGateway/index.js +9 -17
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const fetchStatusRetry = (base, options) => {
|
|
2
|
+
const fetchTry = (retries, ...params) => {
|
|
3
|
+
return base(...params)
|
|
4
|
+
.catch(e => options.onFail && retries > 0
|
|
5
|
+
? fetchTry(retries - 1, ...params)
|
|
6
|
+
: Promise.reject(e))
|
|
7
|
+
.then(async (r) => {
|
|
8
|
+
var _a;
|
|
9
|
+
const shouldHandleStatus = (_a = options.status) === null || _a === void 0 ? void 0 : _a.includes(r.status);
|
|
10
|
+
if (shouldHandleStatus && retries > 0) {
|
|
11
|
+
return fetchTry(retries - 1, ...params);
|
|
12
|
+
}
|
|
13
|
+
else if (shouldHandleStatus) {
|
|
14
|
+
throw new Error(`fetch failed after ${options.retries} retries. ${params[0]} -- ${r.status}: ${await r.text()}`);
|
|
15
|
+
}
|
|
16
|
+
return r;
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
return (...params) => fetchTry(options.retries, ...params);
|
|
20
|
+
};
|
|
@@ -3,6 +3,7 @@ import queryString from 'query-string';
|
|
|
3
3
|
import { merge } from '../..';
|
|
4
4
|
import { resolveConfigValue } from '../../config';
|
|
5
5
|
import { SessionExpiredError, UnauthorizedError } from '../../errors';
|
|
6
|
+
import { fetchStatusRetry } from '../../fetch/fetchStatusRetry';
|
|
6
7
|
/** Pulls the content out of a response based on the content type */
|
|
7
8
|
export const loadResponse = (response) => () => {
|
|
8
9
|
const [contentType] = (response.headers.get('content-type') || '').split(';');
|
|
@@ -27,7 +28,8 @@ const makeRouteClient = (initializer, config, route, authProvider) => {
|
|
|
27
28
|
const url = await renderUrl({ params, query });
|
|
28
29
|
const body = payload ? JSON.stringify(payload) : undefined;
|
|
29
30
|
const baseOptions = merge((await (authProvider === null || authProvider === void 0 ? void 0 : authProvider.getAuthorizedFetchConfig())) || {}, fetchConfig || {});
|
|
30
|
-
|
|
31
|
+
const fetcher = fetchStatusRetry(initializer.fetch, { retries: 1, status: [502], onFail: true });
|
|
32
|
+
return fetcher(url, merge(baseOptions, {
|
|
31
33
|
method: route.method,
|
|
32
34
|
body,
|
|
33
35
|
headers: {
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { XapiStatement, EagerXapiStatement } from ".";
|
|
2
|
+
import { User } from "../authProvider";
|
|
3
|
+
export declare const addStatementDefaultFields: (statement: Pick<XapiStatement, 'object' | 'verb' | 'context' | 'result'> & {
|
|
4
|
+
id?: string;
|
|
5
|
+
}, user: User) => EagerXapiStatement;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import formatISO from 'date-fns/formatISO';
|
|
2
|
+
import { v4 as uuid } from 'uuid';
|
|
3
|
+
export const addStatementDefaultFields = (statement, user) => ({
|
|
4
|
+
id: uuid(),
|
|
5
|
+
...statement,
|
|
6
|
+
actor: {
|
|
7
|
+
account: {
|
|
8
|
+
homePage: 'https://openstax.org',
|
|
9
|
+
name: user.uuid,
|
|
10
|
+
},
|
|
11
|
+
objectType: 'Agent',
|
|
12
|
+
},
|
|
13
|
+
timestamp: formatISO(new Date())
|
|
14
|
+
});
|
|
@@ -80,9 +80,9 @@ export declare const lrsGateway: <C extends string = "lrs">(initializer: Initial
|
|
|
80
80
|
lrsHost: import("../../config").ConfigValueProvider<string>;
|
|
81
81
|
lrsAuthorization: import("../../config").ConfigValueProvider<string>;
|
|
82
82
|
}; }) => (authProvider: AuthProvider) => {
|
|
83
|
-
putXapiStatements: (statements: Array<Pick<XapiStatement, 'object' | 'verb' | 'context' | 'result'> & {
|
|
83
|
+
putXapiStatements: (statements: Array<(Pick<XapiStatement, 'object' | 'verb' | 'context' | 'result'> & {
|
|
84
84
|
id?: string;
|
|
85
|
-
}>) => Promise<EagerXapiStatement[]>;
|
|
85
|
+
}) | EagerXapiStatement>) => Promise<EagerXapiStatement[]>;
|
|
86
86
|
getXapiStatements: (params: {
|
|
87
87
|
verb?: string | undefined;
|
|
88
88
|
activity?: string | undefined;
|
|
@@ -1,31 +1,23 @@
|
|
|
1
|
-
import formatISO from 'date-fns/formatISO';
|
|
2
1
|
import * as queryString from 'query-string';
|
|
3
2
|
import { once, retryWithDelay } from '../..';
|
|
4
3
|
import { assertDefined } from '../../assertions';
|
|
5
4
|
import { resolveConfigValue } from '../../config';
|
|
6
5
|
import { UnauthorizedError } from '../../errors';
|
|
6
|
+
import { fetchStatusRetry } from '../../fetch/fetchStatusRetry';
|
|
7
7
|
import { ifDefined } from '../../guards';
|
|
8
8
|
import { METHOD } from '../../routing';
|
|
9
|
+
import { addStatementDefaultFields } from "./addStatementDefaultFields";
|
|
9
10
|
export const lrsGateway = (initializer) => (configProvider) => {
|
|
10
11
|
const config = configProvider[ifDefined(initializer.configSpace, 'lrs')];
|
|
11
12
|
const lrsHost = once(() => resolveConfigValue(config.lrsHost));
|
|
12
13
|
const lrsAuthorization = once(() => resolveConfigValue(config.lrsAuthorization));
|
|
14
|
+
const fetcher = fetchStatusRetry(initializer.fetch, { retries: 1, status: [502], onFail: true });
|
|
13
15
|
return (authProvider) => {
|
|
14
16
|
// Note: This method actually uses POST
|
|
15
17
|
const putXapiStatements = async (statements) => {
|
|
16
18
|
const user = assertDefined(await authProvider.getUser(), new UnauthorizedError);
|
|
17
|
-
const statementsWithDefaults = statements.map(statement => (
|
|
18
|
-
|
|
19
|
-
actor: {
|
|
20
|
-
account: {
|
|
21
|
-
homePage: 'https://openstax.org',
|
|
22
|
-
name: user.uuid,
|
|
23
|
-
},
|
|
24
|
-
objectType: 'Agent',
|
|
25
|
-
},
|
|
26
|
-
timestamp: formatISO(new Date())
|
|
27
|
-
}));
|
|
28
|
-
const response = await initializer.fetch((await lrsHost()).replace(/\/+$/, '') + '/data/xAPI/statements', {
|
|
19
|
+
const statementsWithDefaults = statements.map(statement => addStatementDefaultFields(statement, user));
|
|
20
|
+
const response = await fetcher((await lrsHost()).replace(/\/+$/, '') + '/data/xAPI/statements', {
|
|
29
21
|
body: JSON.stringify(statementsWithDefaults),
|
|
30
22
|
headers: {
|
|
31
23
|
Authorization: await lrsAuthorization(),
|
|
@@ -39,8 +31,8 @@ export const lrsGateway = (initializer) => (configProvider) => {
|
|
|
39
31
|
|
|
40
32
|
${await response.text()}`);
|
|
41
33
|
}
|
|
42
|
-
|
|
43
|
-
return
|
|
34
|
+
await response.json();
|
|
35
|
+
return statementsWithDefaults;
|
|
44
36
|
};
|
|
45
37
|
// Note: This code does not currently handle a single statement response,
|
|
46
38
|
// which can return 404 if the statement is not found
|
|
@@ -53,13 +45,13 @@ ${await response.text()}`);
|
|
|
53
45
|
}
|
|
54
46
|
return response.json();
|
|
55
47
|
};
|
|
56
|
-
const getMoreXapiStatements = async (more) => formatGetXapiStatementsResponse(
|
|
48
|
+
const getMoreXapiStatements = async (more) => formatGetXapiStatementsResponse(fetcher((await lrsHost()).replace(/\/+$/, '') + more, {
|
|
57
49
|
headers: {
|
|
58
50
|
Authorization: await lrsAuthorization(),
|
|
59
51
|
'X-Experience-API-Version': '1.0.0',
|
|
60
52
|
},
|
|
61
53
|
}));
|
|
62
|
-
const fetchXapiStatements = async ({ user, anyUser, ...options }) =>
|
|
54
|
+
const fetchXapiStatements = async ({ user, anyUser, ...options }) => fetcher((await lrsHost()).replace(/\/+$/, '') + '/data/xAPI/statements?' + queryString.stringify({
|
|
63
55
|
...options,
|
|
64
56
|
...(anyUser === true ? {} : {
|
|
65
57
|
agent: JSON.stringify({
|