Skip to content
This repository was archived by the owner on Dec 14, 2020. It is now read-only.

Commit fe6eadd

Browse files
committed
feat: Adds insertStatements.
1 parent 6cdaab8 commit fe6eadd

File tree

6 files changed

+119
-2
lines changed

6 files changed

+119
-2
lines changed

package-lock.json

Lines changed: 44 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
"check-coverage": true
2424
},
2525
"dependencies": {
26+
"axios": "^0.18.0",
27+
"base-64": "^0.1.0",
2628
"lodash": "^4.17.11"
2729
},
2830
"devDependencies": {
2931
"@ht2-labs/semantic-release": "1.1.27",
3032
"@ht2-labs/typescript-project": "2.0.2",
33+
"@types/base-64": "0.1.2",
3134
"@types/lodash": "4.14.116",
3235
"@types/mocha": "5.2.5",
3336
"@types/source-map-support": "0.4.1",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import createBasicAuthToken from '../statementUtils/createBasicAuthToken';
2+
import insertStatements from '../statementUtils/insertStatements';
3+
import actionOnSiteActivity from './actionOnSiteActivity';
4+
5+
const main = async () => {
6+
try {
7+
await insertStatements({
8+
statements: [actionOnSiteActivity],
9+
endpoint: 'https://example.org/lrs/xAPI',
10+
authToken: createBasicAuthToken({
11+
key: 'demo-key',
12+
secret: 'demo-secret',
13+
}),
14+
});
15+
// tslint:disable-next-line:no-console
16+
console.log('Successfully inserted statements');
17+
} catch {
18+
// tslint:disable-next-line:no-console
19+
console.log('Failed inserting statements');
20+
}
21+
};
22+
23+
main();

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import loggedOutOfSite from './statementCreators/loggedOutOfSite';
1010
import registeredToSite from './statementCreators/registeredToSite';
1111
import createActivity from './statementUtils/createActivity';
1212
import createAgent from './statementUtils/createAgent';
13+
import createBasicAuthToken from './statementUtils/createBasicAuthToken';
1314
import createTimestamp from './statementUtils/createTimestamp';
15+
import insertStatements from './statementUtils/insertStatements';
1416
import * as types from './statementUtils/types';
1517

1618
export const statementCreators = {
@@ -26,7 +28,9 @@ export const statementCreators = {
2628
export const statementUtils = {
2729
createActivity,
2830
createAgent,
31+
createBasicAuthToken,
2932
createTimestamp,
33+
insertStatements,
3034
types,
3135
};
3236

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { encode } from 'base-64';
2+
3+
export interface BasicAuthOpts {
4+
readonly key: string;
5+
readonly secret: string;
6+
}
7+
8+
export default function createBasicAuthToken(opts: BasicAuthOpts) {
9+
const encodedCredentials = encode(`${opts.key}:${opts.secret})`);
10+
return `Basic ${encodedCredentials}`;
11+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import axios from 'axios';
2+
import { Statement } from './types';
3+
4+
interface StatementInsertionOpts {
5+
/** An array of statements to be inserted to the LRS. */
6+
readonly statements: Statement[];
7+
8+
/**
9+
* The URL that provides access to the LRS (e.g. https://example.org/xAPI).
10+
* Do not use a trailing slash (e.g. https://example.org/xAPI/).
11+
* Do not add `/statements` (e.g. https://example.org/xAPI/statements).
12+
*/
13+
readonly endpoint: string;
14+
15+
/**
16+
* The authorization header value that provides access to the LRS.
17+
* Use `createBasicAuthToken` from `statementUtils` for basic authentication tokens.
18+
*/
19+
readonly authToken: string;
20+
}
21+
22+
/**
23+
* Makes a HTTP POST request to the LRS to insert the statements.
24+
*/
25+
export default async function insertStatements(opts: StatementInsertionOpts) {
26+
const url = `${opts.endpoint}/statements`;
27+
const headers = {
28+
Authorization: opts.authToken,
29+
'X-Experience-API-Version': '1.0.3',
30+
'Content-Type': 'application/json',
31+
};
32+
const body = JSON.stringify(opts.statements);
33+
return axios.post(url, body, { headers });
34+
}

0 commit comments

Comments
 (0)