@openstax/ts-utils 1.20.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.
@@ -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,4 +1,3 @@
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';
@@ -7,6 +6,7 @@ import { UnauthorizedError } from '../../errors';
7
6
  import { fetchStatusRetry } from '../../fetch/fetchStatusRetry';
8
7
  import { ifDefined } from '../../guards';
9
8
  import { METHOD } from '../../routing';
9
+ import { addStatementDefaultFields } from "./addStatementDefaultFields";
10
10
  export const lrsGateway = (initializer) => (configProvider) => {
11
11
  const config = configProvider[ifDefined(initializer.configSpace, 'lrs')];
12
12
  const lrsHost = once(() => resolveConfigValue(config.lrsHost));
@@ -16,17 +16,7 @@ export const lrsGateway = (initializer) => (configProvider) => {
16
16
  // Note: This method actually uses POST
17
17
  const putXapiStatements = async (statements) => {
18
18
  const user = assertDefined(await authProvider.getUser(), new UnauthorizedError);
19
- const statementsWithDefaults = statements.map(statement => ({
20
- ...statement,
21
- actor: {
22
- account: {
23
- homePage: 'https://openstax.org',
24
- name: user.uuid,
25
- },
26
- objectType: 'Agent',
27
- },
28
- timestamp: formatISO(new Date())
29
- }));
19
+ const statementsWithDefaults = statements.map(statement => addStatementDefaultFields(statement, user));
30
20
  const response = await fetcher((await lrsHost()).replace(/\/+$/, '') + '/data/xAPI/statements', {
31
21
  body: JSON.stringify(statementsWithDefaults),
32
22
  headers: {
@@ -41,8 +31,8 @@ export const lrsGateway = (initializer) => (configProvider) => {
41
31
 
42
32
  ${await response.text()}`);
43
33
  }
44
- const ids = await response.json();
45
- return ids.map((id, index) => ({ id, ...statementsWithDefaults[index] }));
34
+ await response.json();
35
+ return statementsWithDefaults;
46
36
  };
47
37
  // Note: This code does not currently handle a single statement response,
48
38
  // which can return 404 if the statement is not found