@openstax/ts-utils 1.2.3 → 1.2.5
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/middleware/lambdaCorsResponseMiddleware.js +1 -1
- package/dist/cjs/services/authProvider/browser.d.ts +3 -1
- package/dist/cjs/services/authProvider/utils/embeddedAuthProvider.d.ts +3 -1
- package/dist/cjs/services/authProvider/utils/embeddedAuthProvider.js +7 -2
- package/dist/cjs/services/lrsGateway/attempt-utils.d.ts +12 -3
- package/dist/cjs/services/lrsGateway/attempt-utils.js +14 -7
- package/dist/cjs/services/lrsGateway/index.d.ts +4 -2
- package/dist/cjs/services/lrsGateway/index.js +21 -4
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/middleware/lambdaCorsResponseMiddleware.js +1 -1
- package/dist/esm/services/authProvider/browser.d.ts +3 -1
- package/dist/esm/services/authProvider/utils/embeddedAuthProvider.d.ts +3 -1
- package/dist/esm/services/authProvider/utils/embeddedAuthProvider.js +4 -2
- package/dist/esm/services/lrsGateway/attempt-utils.d.ts +12 -3
- package/dist/esm/services/lrsGateway/attempt-utils.js +14 -7
- package/dist/esm/services/lrsGateway/index.d.ts +4 -2
- package/dist/esm/services/lrsGateway/index.js +22 -5
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -3,7 +3,7 @@ import { apiTextResponse } from '../routing';
|
|
|
3
3
|
export const createLambdaCorsResponseMiddleware = (config) => (responsePromise, { request }) => {
|
|
4
4
|
const cors = async () => {
|
|
5
5
|
const allowedHost = await resolveConfigValue(config.corsAllowedHostRegex);
|
|
6
|
-
if (request.headers.origin && new URL(request.headers.origin).hostname.match(new RegExp(allowedHost))) {
|
|
6
|
+
if (request.headers.origin && request.headers.origin !== 'null' && new URL(request.headers.origin).hostname.match(new RegExp(allowedHost))) {
|
|
7
7
|
return {
|
|
8
8
|
'Access-Control-Allow-Origin': request.headers.origin,
|
|
9
9
|
'Access-Control-Allow-Credentials': 'true',
|
|
@@ -48,7 +48,9 @@ export declare const browserAuthProvider: <C extends string = "auth">({ window,
|
|
|
48
48
|
* gets an authorized url for an iframe src. sets params on the url and saves its
|
|
49
49
|
* origin to trust releasing user identity to it
|
|
50
50
|
*/
|
|
51
|
-
getAuthorizedEmbedUrl: (urlString: string
|
|
51
|
+
getAuthorizedEmbedUrl: (urlString: string, extraParams?: {
|
|
52
|
+
[key: string]: string;
|
|
53
|
+
} | undefined) => string;
|
|
52
54
|
/**
|
|
53
55
|
* gets second argument for `fetch` that has authentication token or cookie
|
|
54
56
|
*/
|
|
@@ -14,7 +14,9 @@ export declare const embeddedAuthProvider: (getUserData: UserDataLoader, { query
|
|
|
14
14
|
window: Window;
|
|
15
15
|
}) => {
|
|
16
16
|
embeddedQueryValue: string;
|
|
17
|
-
getAuthorizedEmbedUrl: (urlString: string
|
|
17
|
+
getAuthorizedEmbedUrl: (urlString: string, extraParams?: {
|
|
18
|
+
[key: string]: string;
|
|
19
|
+
} | undefined) => string;
|
|
18
20
|
unmount: () => void;
|
|
19
21
|
};
|
|
20
22
|
export {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import queryString from 'query-string';
|
|
1
2
|
export var PostMessageTypes;
|
|
2
3
|
(function (PostMessageTypes) {
|
|
3
4
|
PostMessageTypes["ReceiveUser"] = "receive-user";
|
|
@@ -14,10 +15,11 @@ export const embeddedAuthProvider = (getUserData, { queryKey = 'auth', window })
|
|
|
14
15
|
}
|
|
15
16
|
};
|
|
16
17
|
window.addEventListener('message', messageHandler);
|
|
17
|
-
const getAuthorizedEmbedUrl = (urlString) => {
|
|
18
|
+
const getAuthorizedEmbedUrl = (urlString, extraParams) => {
|
|
18
19
|
const url = new URL(urlString);
|
|
19
20
|
trustedEmbeds.add(url.origin);
|
|
20
|
-
url.
|
|
21
|
+
const params = queryString.parse(url.search);
|
|
22
|
+
url.search = queryString.stringify({ ...params, ...extraParams, [queryKey]: embeddedQueryValue });
|
|
21
23
|
return url.href;
|
|
22
24
|
};
|
|
23
25
|
return {
|
|
@@ -19,12 +19,21 @@ export declare const resolveActivityAttemptInfo: (statements: XapiStatement[], a
|
|
|
19
19
|
parentActivityAttempt?: string | undefined;
|
|
20
20
|
currentPreference?: "latest" | "oldest" | undefined;
|
|
21
21
|
} | undefined) => ActivityState;
|
|
22
|
-
export declare const loadStatementsForActivityAndFirstChildren: (gateway: LrsGateway, activityIRI: string,
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
export declare const loadStatementsForActivityAndFirstChildren: (gateway: LrsGateway, activityIRI: string, options?: {
|
|
23
|
+
attempt?: string | undefined;
|
|
24
|
+
ensureSync?: boolean | undefined;
|
|
25
|
+
} | undefined) => Promise<XapiStatement[]>;
|
|
26
|
+
export declare const loadStatementsForAttempt: (gateway: LrsGateway, attempt: string, options?: {
|
|
27
|
+
ensureSync?: boolean | undefined;
|
|
28
|
+
} | undefined) => Promise<XapiStatement[]>;
|
|
29
|
+
export declare const loadStatementsForActivity: (gateway: LrsGateway, activityIRI: string, options?: {
|
|
30
|
+
attempt?: string | undefined;
|
|
31
|
+
ensureSync?: boolean | undefined;
|
|
32
|
+
} | undefined) => Promise<XapiStatement[]>;
|
|
25
33
|
export declare const loadActivityAttemptInfo: (gateway: LrsGateway, activityIRI: string, options?: {
|
|
26
34
|
currentAttempt?: string | undefined;
|
|
27
35
|
parentActivityAttempt?: string | undefined;
|
|
36
|
+
ensureSync?: boolean | undefined;
|
|
28
37
|
} | undefined) => Promise<ActivityState>;
|
|
29
38
|
export declare const createStatement: (verb: XapiStatement['verb'], activity: {
|
|
30
39
|
iri: string;
|
|
@@ -78,32 +78,39 @@ export const resolveActivityAttemptInfo = (statements, activityIRI, options) =>
|
|
|
78
78
|
* statements would then have to be fetched using `loadStatementsForActivity(gateway, childActivityIRI, childAttemptStatementID)`. this
|
|
79
79
|
* is because child activities could have multiple attempts under one attempt on the parent activity.
|
|
80
80
|
*/
|
|
81
|
-
export const loadStatementsForActivityAndFirstChildren = (gateway, activityIRI,
|
|
81
|
+
export const loadStatementsForActivityAndFirstChildren = (gateway, activityIRI, options) => {
|
|
82
|
+
const { attempt, ...partialOptions } = options ? options : { attempt: undefined };
|
|
83
|
+
const getOptions = attempt ? { registration: attempt, ...partialOptions } : partialOptions;
|
|
82
84
|
return gateway.getAllXapiStatements({
|
|
83
85
|
activity: activityIRI,
|
|
84
86
|
related_activities: true,
|
|
85
|
-
...
|
|
87
|
+
...getOptions,
|
|
86
88
|
});
|
|
87
89
|
};
|
|
88
90
|
/*
|
|
89
91
|
* loads all statements (for this actor) that have the given parent attempt (registration)
|
|
90
92
|
*/
|
|
91
|
-
export const loadStatementsForAttempt = (gateway, attempt) => {
|
|
93
|
+
export const loadStatementsForAttempt = (gateway, attempt, options) => {
|
|
92
94
|
return gateway.getAllXapiStatements({
|
|
93
|
-
registration: attempt
|
|
95
|
+
registration: attempt,
|
|
96
|
+
...(options ? options : {})
|
|
94
97
|
});
|
|
95
98
|
};
|
|
96
99
|
/*
|
|
97
100
|
* loads all statements (for this actor) that have the given activityIRI as the object.id
|
|
98
101
|
*/
|
|
99
|
-
export const loadStatementsForActivity = (gateway, activityIRI,
|
|
102
|
+
export const loadStatementsForActivity = (gateway, activityIRI, options) => {
|
|
103
|
+
const { attempt, ...partialOptions } = options ? options : { attempt: undefined };
|
|
104
|
+
const getOptions = attempt ? { registration: attempt, ...partialOptions } : partialOptions;
|
|
100
105
|
return gateway.getAllXapiStatements({
|
|
101
106
|
activity: activityIRI,
|
|
102
|
-
...
|
|
107
|
+
...getOptions,
|
|
103
108
|
});
|
|
104
109
|
};
|
|
105
110
|
export const loadActivityAttemptInfo = async (gateway, activityIRI, options) => {
|
|
106
|
-
|
|
111
|
+
const { parentActivityAttempt, ...partialOptions } = options ? options : { parentActivityAttempt: undefined };
|
|
112
|
+
const loadOptions = parentActivityAttempt ? { attempt: parentActivityAttempt } : partialOptions;
|
|
113
|
+
return resolveActivityAttemptInfo(await loadStatementsForActivity(gateway, activityIRI, loadOptions), activityIRI, options);
|
|
107
114
|
};
|
|
108
115
|
export const createStatement = (verb, activity, attempt, parentActivityIRI) => {
|
|
109
116
|
return {
|
|
@@ -83,11 +83,12 @@ export declare const lrsGateway: <C extends string = "lrs">(initializer: Initial
|
|
|
83
83
|
putXapiStatements: (statements: Array<Pick<XapiStatement, 'object' | 'verb' | 'context' | 'result'> & {
|
|
84
84
|
id?: string;
|
|
85
85
|
}>) => Promise<EagerXapiStatement[]>;
|
|
86
|
-
getXapiStatements: (
|
|
86
|
+
getXapiStatements: (params: {
|
|
87
87
|
verb?: string | undefined;
|
|
88
88
|
activity?: string | undefined;
|
|
89
89
|
registration?: string | undefined;
|
|
90
90
|
related_activities?: boolean | undefined;
|
|
91
|
+
ensureSync?: boolean | undefined;
|
|
91
92
|
user?: string | undefined;
|
|
92
93
|
anyUser?: boolean | undefined;
|
|
93
94
|
}) => Promise<{
|
|
@@ -98,11 +99,12 @@ export declare const lrsGateway: <C extends string = "lrs">(initializer: Initial
|
|
|
98
99
|
more: string;
|
|
99
100
|
statements: XapiStatement[];
|
|
100
101
|
}>;
|
|
101
|
-
getAllXapiStatements: (
|
|
102
|
+
getAllXapiStatements: (params: {
|
|
102
103
|
verb?: string | undefined;
|
|
103
104
|
activity?: string | undefined;
|
|
104
105
|
registration?: string | undefined;
|
|
105
106
|
related_activities?: boolean | undefined;
|
|
107
|
+
ensureSync?: boolean | undefined;
|
|
106
108
|
user?: string | undefined;
|
|
107
109
|
anyUser?: boolean | undefined;
|
|
108
110
|
}) => Promise<XapiStatement[]>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import formatISO from 'date-fns/formatISO';
|
|
2
2
|
import * as queryString from 'query-string';
|
|
3
|
-
import { once } from '../..';
|
|
3
|
+
import { once, retryWithDelay } from '../..';
|
|
4
4
|
import { assertDefined } from '../../assertions';
|
|
5
5
|
import { resolveConfigValue } from '../../config';
|
|
6
6
|
import { UnauthorizedError } from '../../errors';
|
|
@@ -59,7 +59,7 @@ ${await response.text()}`);
|
|
|
59
59
|
'X-Experience-API-Version': '1.0.0',
|
|
60
60
|
},
|
|
61
61
|
}));
|
|
62
|
-
const
|
|
62
|
+
const fetchXapiStatements = async ({ user, anyUser, ...options }) => initializer.fetch((await lrsHost()).replace(/\/+$/, '') + '/data/xAPI/statements?' + queryString.stringify({
|
|
63
63
|
...options,
|
|
64
64
|
...(anyUser === true ? {} : {
|
|
65
65
|
agent: JSON.stringify({
|
|
@@ -75,8 +75,25 @@ ${await response.text()}`);
|
|
|
75
75
|
Authorization: await lrsAuthorization(),
|
|
76
76
|
'X-Experience-API-Version': '1.0.0',
|
|
77
77
|
},
|
|
78
|
-
})
|
|
79
|
-
const
|
|
78
|
+
});
|
|
79
|
+
const getXapiStatements = async (params) => {
|
|
80
|
+
if (params.ensureSync) {
|
|
81
|
+
const date = new Date();
|
|
82
|
+
return retryWithDelay(async () => {
|
|
83
|
+
const responsePromise = fetchXapiStatements(params);
|
|
84
|
+
const response = await responsePromise;
|
|
85
|
+
const consistentThrough = response.headers.get('X-Experience-API-Consistent-Through');
|
|
86
|
+
if (!consistentThrough || new Date(consistentThrough) < date) {
|
|
87
|
+
throw new Error(`xAPI consistent through ${consistentThrough}; not in sync with current date ${date}.`);
|
|
88
|
+
}
|
|
89
|
+
return formatGetXapiStatementsResponse(responsePromise);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
return formatGetXapiStatementsResponse(fetchXapiStatements(params));
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const getAllXapiStatements = async (params) => {
|
|
80
97
|
const loadRemaining = async (result) => {
|
|
81
98
|
if (!result.more) {
|
|
82
99
|
return result.statements;
|
|
@@ -84,7 +101,7 @@ ${await response.text()}`);
|
|
|
84
101
|
const { more, statements } = await getMoreXapiStatements(result.more);
|
|
85
102
|
return loadRemaining({ more, statements: [...result.statements, ...statements] });
|
|
86
103
|
};
|
|
87
|
-
return loadRemaining(await getXapiStatements(
|
|
104
|
+
return loadRemaining(await getXapiStatements(params));
|
|
88
105
|
};
|
|
89
106
|
return {
|
|
90
107
|
putXapiStatements,
|