@openstax/ts-utils 1.2.4 → 1.2.6

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.
@@ -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, attempt?: string | undefined) => Promise<XapiStatement[]>;
23
- export declare const loadStatementsForAttempt: (gateway: LrsGateway, attempt: string) => Promise<XapiStatement[]>;
24
- export declare const loadStatementsForActivity: (gateway: LrsGateway, activityIRI: string, attempt?: string | undefined) => Promise<XapiStatement[]>;
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;
@@ -91,35 +91,42 @@ exports.resolveActivityAttemptInfo = resolveActivityAttemptInfo;
91
91
  * statements would then have to be fetched using `loadStatementsForActivity(gateway, childActivityIRI, childAttemptStatementID)`. this
92
92
  * is because child activities could have multiple attempts under one attempt on the parent activity.
93
93
  */
94
- const loadStatementsForActivityAndFirstChildren = (gateway, activityIRI, attempt) => {
94
+ const loadStatementsForActivityAndFirstChildren = (gateway, activityIRI, options) => {
95
+ const { attempt, ...partialOptions } = options ? options : { attempt: undefined };
96
+ const getOptions = attempt ? { registration: attempt, ...partialOptions } : partialOptions;
95
97
  return gateway.getAllXapiStatements({
96
98
  activity: activityIRI,
97
99
  related_activities: true,
98
- ...(attempt ? { registration: attempt } : {})
100
+ ...getOptions,
99
101
  });
100
102
  };
101
103
  exports.loadStatementsForActivityAndFirstChildren = loadStatementsForActivityAndFirstChildren;
102
104
  /*
103
105
  * loads all statements (for this actor) that have the given parent attempt (registration)
104
106
  */
105
- const loadStatementsForAttempt = (gateway, attempt) => {
107
+ const loadStatementsForAttempt = (gateway, attempt, options) => {
106
108
  return gateway.getAllXapiStatements({
107
- registration: attempt
109
+ registration: attempt,
110
+ ...(options ? options : {})
108
111
  });
109
112
  };
110
113
  exports.loadStatementsForAttempt = loadStatementsForAttempt;
111
114
  /*
112
115
  * loads all statements (for this actor) that have the given activityIRI as the object.id
113
116
  */
114
- const loadStatementsForActivity = (gateway, activityIRI, attempt) => {
117
+ const loadStatementsForActivity = (gateway, activityIRI, options) => {
118
+ const { attempt, ...partialOptions } = options ? options : { attempt: undefined };
119
+ const getOptions = attempt ? { registration: attempt, ...partialOptions } : partialOptions;
115
120
  return gateway.getAllXapiStatements({
116
121
  activity: activityIRI,
117
- ...(attempt ? { registration: attempt } : {})
122
+ ...getOptions,
118
123
  });
119
124
  };
120
125
  exports.loadStatementsForActivity = loadStatementsForActivity;
121
126
  const loadActivityAttemptInfo = async (gateway, activityIRI, options) => {
122
- return (0, exports.resolveActivityAttemptInfo)(await (0, exports.loadStatementsForActivity)(gateway, activityIRI, options === null || options === void 0 ? void 0 : options.parentActivityAttempt), activityIRI, options);
127
+ const { parentActivityAttempt, ...partialOptions } = options ? options : { parentActivityAttempt: undefined };
128
+ const loadOptions = parentActivityAttempt ? { attempt: parentActivityAttempt } : partialOptions;
129
+ return (0, exports.resolveActivityAttemptInfo)(await (0, exports.loadStatementsForActivity)(gateway, activityIRI, loadOptions), activityIRI, options);
123
130
  };
124
131
  exports.loadActivityAttemptInfo = loadActivityAttemptInfo;
125
132
  const createStatement = (verb, activity, attempt, parentActivityIRI) => {
@@ -83,13 +83,15 @@ 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: ({ user, anyUser, ...options }: {
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
91
  user?: string | undefined;
92
92
  anyUser?: boolean | undefined;
93
+ } & {
94
+ ensureSync?: boolean | undefined;
93
95
  }) => Promise<{
94
96
  more: string;
95
97
  statements: XapiStatement[];
@@ -98,13 +100,15 @@ export declare const lrsGateway: <C extends string = "lrs">(initializer: Initial
98
100
  more: string;
99
101
  statements: XapiStatement[];
100
102
  }>;
101
- getAllXapiStatements: (args_0: {
103
+ getAllXapiStatements: (params: {
102
104
  verb?: string | undefined;
103
105
  activity?: string | undefined;
104
106
  registration?: string | undefined;
105
107
  related_activities?: boolean | undefined;
106
108
  user?: string | undefined;
107
109
  anyUser?: boolean | undefined;
110
+ } & {
111
+ ensureSync?: boolean | undefined;
108
112
  }) => Promise<XapiStatement[]>;
109
113
  };
110
114
  export {};
@@ -88,7 +88,7 @@ ${await response.text()}`);
88
88
  'X-Experience-API-Version': '1.0.0',
89
89
  },
90
90
  }));
91
- const getXapiStatements = async ({ user, anyUser, ...options }) => formatGetXapiStatementsResponse(initializer.fetch((await lrsHost()).replace(/\/+$/, '') + '/data/xAPI/statements?' + queryString.stringify({
91
+ const fetchXapiStatements = async ({ user, anyUser, ...options }) => initializer.fetch((await lrsHost()).replace(/\/+$/, '') + '/data/xAPI/statements?' + queryString.stringify({
92
92
  ...options,
93
93
  ...(anyUser === true ? {} : {
94
94
  agent: JSON.stringify({
@@ -104,8 +104,26 @@ ${await response.text()}`);
104
104
  Authorization: await lrsAuthorization(),
105
105
  'X-Experience-API-Version': '1.0.0',
106
106
  },
107
- }));
108
- const getAllXapiStatements = async (...args) => {
107
+ });
108
+ const getXapiStatements = async (params) => {
109
+ const { ensureSync, ...fetchParams } = params;
110
+ if (ensureSync) {
111
+ const date = new Date();
112
+ return (0, __1.retryWithDelay)(async () => {
113
+ const responsePromise = fetchXapiStatements(fetchParams);
114
+ const response = await responsePromise;
115
+ const consistentThrough = response.headers.get('X-Experience-API-Consistent-Through');
116
+ if (!consistentThrough || new Date(consistentThrough) < date) {
117
+ throw new Error(`xAPI consistent through ${consistentThrough}; not in sync with current date ${date}.`);
118
+ }
119
+ return formatGetXapiStatementsResponse(responsePromise);
120
+ });
121
+ }
122
+ else {
123
+ return formatGetXapiStatementsResponse(fetchXapiStatements(fetchParams));
124
+ }
125
+ };
126
+ const getAllXapiStatements = async (params) => {
109
127
  const loadRemaining = async (result) => {
110
128
  if (!result.more) {
111
129
  return result.statements;
@@ -113,7 +131,7 @@ ${await response.text()}`);
113
131
  const { more, statements } = await getMoreXapiStatements(result.more);
114
132
  return loadRemaining({ more, statements: [...result.statements, ...statements] });
115
133
  };
116
- return loadRemaining(await getXapiStatements(...args));
134
+ return loadRemaining(await getXapiStatements(params));
117
135
  };
118
136
  return {
119
137
  putXapiStatements,