@openstax/ts-utils 1.3.0 → 1.3.2
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/services/lrsGateway/file-system.js +17 -3
- package/dist/cjs/services/lrsGateway/index.d.ts +7 -1
- package/dist/cjs/services/lrsGateway/index.js +2 -2
- package/dist/cjs/services/searchProvider/index.d.ts +10 -2
- package/dist/cjs/services/searchProvider/memorySearchTheBadWay.js +32 -5
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/services/lrsGateway/file-system.js +17 -3
- package/dist/esm/services/lrsGateway/index.d.ts +7 -1
- package/dist/esm/services/lrsGateway/index.js +2 -2
- package/dist/esm/services/searchProvider/index.d.ts +10 -2
- package/dist/esm/services/searchProvider/memorySearchTheBadWay.js +32 -5
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -57,19 +57,33 @@ export const fileSystemLrsGateway = (initializer) => (configProvider) => (authPr
|
|
|
57
57
|
await save;
|
|
58
58
|
return statementsWithDefaults;
|
|
59
59
|
};
|
|
60
|
-
const getAllXapiStatements = async ({ user, anyUser, ...options }) => {
|
|
60
|
+
const getAllXapiStatements = async ({ user, anyUser, fetchUntil, ...options }) => {
|
|
61
61
|
const authUser = await authProvider.getUser();
|
|
62
62
|
await load;
|
|
63
|
-
|
|
63
|
+
let filteredData = (data || []).filter(statement => {
|
|
64
64
|
var _a, _b, _c, _d;
|
|
65
|
+
const statementDate = new Date(statement.timestamp);
|
|
66
|
+
const sinceDate = options.since ? new Date(options.since) : null;
|
|
67
|
+
const untilDate = options.until ? new Date(options.until) : null;
|
|
65
68
|
return (anyUser === true || statement.actor.account.name === (user || assertDefined(authUser, new UnauthorizedError()).uuid))
|
|
66
69
|
&& (!options.verb || statement.verb.id === options.verb)
|
|
67
70
|
&& (!options.registration || ((_a = statement.context) === null || _a === void 0 ? void 0 : _a.registration) === options.registration)
|
|
68
71
|
&& (!options.activity || (options.related_activities
|
|
69
72
|
? ((statement.object.id === options.activity && statement.object.objectType === 'Activity')
|
|
70
73
|
|| (!!((_d = (_c = (_b = statement.context) === null || _b === void 0 ? void 0 : _b.contextActivities) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.find(parent => parent.id === options.activity && parent.objectType === 'Activity'))))
|
|
71
|
-
: (statement.object.id === options.activity && statement.object.objectType === 'Activity')))
|
|
74
|
+
: (statement.object.id === options.activity && statement.object.objectType === 'Activity')))
|
|
75
|
+
&& (!sinceDate || statementDate >= sinceDate)
|
|
76
|
+
&& (!untilDate || statementDate <= untilDate);
|
|
72
77
|
});
|
|
78
|
+
if (fetchUntil) {
|
|
79
|
+
for (let i = 0; i < filteredData.length; i += pageSize) {
|
|
80
|
+
if (fetchUntil(filteredData.slice(0, i + pageSize))) {
|
|
81
|
+
filteredData = filteredData.slice(0, i + pageSize);
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return filteredData;
|
|
73
87
|
};
|
|
74
88
|
const getMoreXapiStatements = async (more) => {
|
|
75
89
|
const { args, offset } = JSON.parse(more);
|
|
@@ -90,6 +90,8 @@ export declare const lrsGateway: <C extends string = "lrs">(initializer: Initial
|
|
|
90
90
|
related_activities?: boolean | undefined;
|
|
91
91
|
user?: string | undefined;
|
|
92
92
|
anyUser?: boolean | undefined;
|
|
93
|
+
since?: string | undefined;
|
|
94
|
+
until?: string | undefined;
|
|
93
95
|
} & {
|
|
94
96
|
ensureSync?: boolean | undefined;
|
|
95
97
|
}) => Promise<{
|
|
@@ -100,15 +102,19 @@ export declare const lrsGateway: <C extends string = "lrs">(initializer: Initial
|
|
|
100
102
|
more: string;
|
|
101
103
|
statements: XapiStatement[];
|
|
102
104
|
}>;
|
|
103
|
-
getAllXapiStatements: (params: {
|
|
105
|
+
getAllXapiStatements: ({ fetchUntil, ...params }: {
|
|
104
106
|
verb?: string | undefined;
|
|
105
107
|
activity?: string | undefined;
|
|
106
108
|
registration?: string | undefined;
|
|
107
109
|
related_activities?: boolean | undefined;
|
|
108
110
|
user?: string | undefined;
|
|
109
111
|
anyUser?: boolean | undefined;
|
|
112
|
+
since?: string | undefined;
|
|
113
|
+
until?: string | undefined;
|
|
110
114
|
} & {
|
|
111
115
|
ensureSync?: boolean | undefined;
|
|
116
|
+
} & {
|
|
117
|
+
fetchUntil?: ((statements: XapiStatement[]) => boolean) | undefined;
|
|
112
118
|
}) => Promise<XapiStatement[]>;
|
|
113
119
|
};
|
|
114
120
|
export {};
|
|
@@ -94,9 +94,9 @@ ${await response.text()}`);
|
|
|
94
94
|
return formatGetXapiStatementsResponse(fetchXapiStatements(fetchParams));
|
|
95
95
|
}
|
|
96
96
|
};
|
|
97
|
-
const getAllXapiStatements = async (params) => {
|
|
97
|
+
const getAllXapiStatements = async ({ fetchUntil, ...params }) => {
|
|
98
98
|
const loadRemaining = async (result) => {
|
|
99
|
-
if (!result.more) {
|
|
99
|
+
if (!result.more || (fetchUntil && fetchUntil(result.statements))) {
|
|
100
100
|
return result.statements;
|
|
101
101
|
}
|
|
102
102
|
const { more, statements } = await getMoreXapiStatements(result.more);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
declare type Filter = {
|
|
1
|
+
export declare type Filter = {
|
|
2
2
|
key: string;
|
|
3
|
-
value: string | string[];
|
|
3
|
+
value: string | string[] | boolean;
|
|
4
4
|
};
|
|
5
5
|
declare type Field = {
|
|
6
6
|
key: string;
|
|
@@ -9,6 +9,9 @@ declare type Field = {
|
|
|
9
9
|
} | {
|
|
10
10
|
key: string;
|
|
11
11
|
type: 'keyword';
|
|
12
|
+
} | {
|
|
13
|
+
key: string;
|
|
14
|
+
type: 'boolean';
|
|
12
15
|
};
|
|
13
16
|
export interface IndexOptions<T> {
|
|
14
17
|
body: T;
|
|
@@ -18,6 +21,11 @@ export interface SearchOptions {
|
|
|
18
21
|
page?: number;
|
|
19
22
|
query: string | undefined;
|
|
20
23
|
fields: Field[];
|
|
24
|
+
/**
|
|
25
|
+
* @deprecated use `must` instead
|
|
26
|
+
*/
|
|
21
27
|
filter?: Filter[];
|
|
28
|
+
must?: Filter[];
|
|
29
|
+
must_not?: Filter[];
|
|
22
30
|
}
|
|
23
31
|
export {};
|
|
@@ -1,16 +1,41 @@
|
|
|
1
1
|
import { coerceArray } from '../..';
|
|
2
|
+
import { InvalidRequestError } from '../../errors';
|
|
2
3
|
import { isDefined } from '../../guards';
|
|
3
4
|
const MIN_MATCH = 0.5;
|
|
4
5
|
const MAX_RESULTS = 10;
|
|
6
|
+
var MatchType;
|
|
7
|
+
(function (MatchType) {
|
|
8
|
+
MatchType[MatchType["Must"] = 0] = "Must";
|
|
9
|
+
MatchType[MatchType["MustNot"] = 1] = "MustNot";
|
|
10
|
+
})(MatchType || (MatchType = {}));
|
|
5
11
|
const resolveField = (document, field) => field.key.toString().split('.').reduce((result, key) => result[key], document);
|
|
6
12
|
export const memorySearchTheBadWay = ({ loadAllDocumentsTheBadWay }) => {
|
|
7
13
|
return {
|
|
8
14
|
ensureIndexCreated: async () => undefined,
|
|
9
15
|
index: async (_options) => undefined,
|
|
10
16
|
search: async (options) => {
|
|
17
|
+
const getFieldType = (field) => { var _a; return (_a = options.fields.find(f => f.key == field.key)) === null || _a === void 0 ? void 0 : _a.type; };
|
|
11
18
|
const results = (await loadAllDocumentsTheBadWay())
|
|
12
19
|
.map(document => {
|
|
13
20
|
let weight = 0;
|
|
21
|
+
const matchFilters = (filters, mustMatch) => {
|
|
22
|
+
for (const field of filters) {
|
|
23
|
+
const docValues = coerceArray(resolveField(document, field));
|
|
24
|
+
const coerceValue = getFieldType(field) === 'boolean'
|
|
25
|
+
? (input) => {
|
|
26
|
+
if ([true, 'true', '1', 1].includes(input)) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
if ([false, 'false', '0', 0, ''].includes(input)) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
throw new InvalidRequestError('input is not a valid boolean filter');
|
|
33
|
+
}
|
|
34
|
+
: (x) => x;
|
|
35
|
+
const hasMatch = coerceArray(field.value).map(coerceValue).some(v => docValues.includes(v));
|
|
36
|
+
return !((mustMatch === MatchType.Must && !hasMatch) || (mustMatch === MatchType.MustNot && hasMatch));
|
|
37
|
+
}
|
|
38
|
+
};
|
|
14
39
|
if (options.query !== undefined) {
|
|
15
40
|
for (const field of options.fields) {
|
|
16
41
|
if (field.type !== undefined && field.type !== 'text') {
|
|
@@ -30,11 +55,13 @@ export const memorySearchTheBadWay = ({ loadAllDocumentsTheBadWay }) => {
|
|
|
30
55
|
}
|
|
31
56
|
}
|
|
32
57
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
58
|
+
const { must_not } = options;
|
|
59
|
+
const must = 'filter' in options ? options.filter : options.must;
|
|
60
|
+
if ((must === null || must === void 0 ? void 0 : must.length) && !matchFilters(must, MatchType.Must)) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
if ((must_not === null || must_not === void 0 ? void 0 : must_not.length) && !matchFilters(must_not, MatchType.MustNot)) {
|
|
64
|
+
return undefined;
|
|
38
65
|
}
|
|
39
66
|
return { document, weight };
|
|
40
67
|
})
|