@openstax/ts-utils 1.5.7 → 1.6.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.
@@ -15,8 +15,7 @@ export declare type EventHandler = (e: {
15
15
  }) => void;
16
16
  export interface Window {
17
17
  fetch: GenericFetch;
18
- top: {} | null;
19
- parent: Pick<Window, 'postMessage'> | null;
18
+ parent: Pick<Window, 'postMessage'>;
20
19
  location: {
21
20
  search: string;
22
21
  };
@@ -4,7 +4,9 @@ exports.browserAuthProvider = void 0;
4
4
  const __1 = require("../..");
5
5
  const config_1 = require("../../config");
6
6
  const guards_1 = require("../../guards");
7
+ const helpers_1 = require("../../routing/helpers");
7
8
  const embeddedAuthProvider_1 = require("./utils/embeddedAuthProvider");
9
+ const isUserData = (0, helpers_1.unsafePayloadValidator)();
8
10
  const browserAuthProvider = ({ window, configSpace }) => (configProvider) => {
9
11
  const config = configProvider[(0, guards_1.ifDefined)(configSpace, 'auth')];
10
12
  const accountsBase = (0, __1.once)(() => (0, config_1.resolveConfigValue)(config.accountsBase));
@@ -12,7 +14,7 @@ const browserAuthProvider = ({ window, configSpace }) => (configProvider) => {
12
14
  const queryKey = 'auth';
13
15
  const authQuery = new URLSearchParams(queryString).get(queryKey);
14
16
  const referrer = window.document.referrer ? new URL(window.document.referrer) : undefined;
15
- const isEmbedded = window.top && window.top !== window;
17
+ const isEmbedded = window.parent !== window;
16
18
  const trustedParent = isEmbedded && referrer && referrer.hostname.match(/^(openstax\.org|((.*)(\.openstax\.org|local|localhost)))$/) ? referrer : undefined;
17
19
  const { embeddedQueryValue, getAuthorizedEmbedUrl } = (0, embeddedAuthProvider_1.embeddedAuthProvider)(() => getUserData(), { queryKey, window });
18
20
  let userData = {
@@ -71,12 +73,11 @@ const browserAuthProvider = ({ window, configSpace }) => (configProvider) => {
71
73
  * requests user identity from accounts api using given token or cookie
72
74
  */
73
75
  const getFetchUser = async () => {
74
- const response = await window.fetch((await accountsBase()).replace(/\/+$/, '') + '/api/user', getAuthorizedFetchConfigFromData(userData));
76
+ const response = await window.fetch((await accountsBase()).replace(/\/+$/, '') + '/api/user?always_200=true', getAuthorizedFetchConfigFromData(userData));
75
77
  if (response.status === 200) {
76
- return { ...userData, user: await response.json() };
77
- }
78
- if (response.status === 403) {
79
- return { ...userData, user: undefined };
78
+ const body = await response.json();
79
+ const user = isUserData(body) ? body : undefined;
80
+ return { ...userData, user };
80
81
  }
81
82
  const message = await response.text();
82
83
  throw new Error(`Error response from Accounts ${response.status}: ${message}`);
@@ -12,8 +12,10 @@ export interface Grade {
12
12
  userId: string;
13
13
  }
14
14
  export declare const getRegistrationAttemptInfo: (lrs: LrsGateway, registration: string, options?: {
15
+ activity?: string | undefined;
15
16
  anyUser?: boolean | undefined;
16
17
  currentPreference?: "latest" | "oldest" | undefined;
18
+ ensureSync?: boolean | undefined;
17
19
  user?: string | undefined;
18
20
  } | undefined) => Promise<{
19
21
  [key: string]: ActivityState;
@@ -47,7 +49,7 @@ export declare const getAssignmentGrades: (services: {
47
49
  accountsGateway: AccountsGateway;
48
50
  lrs: LrsGateway;
49
51
  logger: Logger;
50
- }, registration: string, options?: {
52
+ }, assignmentIRI: string, registration: string, options?: {
51
53
  anyUser?: boolean | undefined;
52
54
  currentPreference?: "latest" | "oldest" | undefined;
53
55
  incompleteAttemptsCallback?: ((mappedInfo: UserActivityInfo[]) => Promise<GradeAndProgress[]>) | undefined;
@@ -9,7 +9,7 @@ const __1 = require("../..");
9
9
  const attempt_utils_1 = require("./attempt-utils");
10
10
  const getRegistrationAttemptInfo = async (lrs, registration, options) => {
11
11
  const { currentPreference, ...xapiOptions } = options !== null && options !== void 0 ? options : {};
12
- const allStatements = await lrs.getAllXapiStatements({ ...xapiOptions, registration, ensureSync: true });
12
+ const allStatements = await lrs.getAllXapiStatements({ ...xapiOptions, registration });
13
13
  // Partition statements for each user
14
14
  const statementsPerUser = {};
15
15
  allStatements.forEach((statement) => {
@@ -78,9 +78,9 @@ const getCurrentGrade = async (services, registration, options) => {
78
78
  return incompleteAttemptCallback(userInfo);
79
79
  };
80
80
  exports.getCurrentGrade = getCurrentGrade;
81
- const getAssignmentGrades = async (services, registration, options) => {
81
+ const getAssignmentGrades = async (services, assignmentIRI, registration, options) => {
82
82
  const { anyUser, currentPreference, incompleteAttemptsCallback, platformId, scoreMaximum, user } = options !== null && options !== void 0 ? options : {};
83
- const infoPerUserUuid = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration, { anyUser, currentPreference, user });
83
+ const infoPerUserUuid = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration, { activity: assignmentIRI, anyUser, currentPreference, user });
84
84
  const mappedInfo = await services.accountsGateway.mapUserUuids(infoPerUserUuid, services.logger, platformId);
85
85
  if (!incompleteAttemptsCallback) {
86
86
  return getCompletedUserInfosGradeAndProgress(mappedInfo, scoreMaximum);