@openstax/ts-utils 1.5.5 → 1.5.7
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/assertions.d.ts +2 -2
- package/dist/cjs/assertions.js +8 -7
- package/dist/cjs/services/authProvider/utils/userRoleValidator.d.ts +13 -0
- package/dist/cjs/services/authProvider/utils/userRoleValidator.js +37 -0
- package/dist/cjs/services/lrsGateway/xapiUtils.js +3 -1
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/assertions.d.ts +2 -2
- package/dist/esm/assertions.js +1 -1
- package/dist/esm/services/authProvider/utils/userRoleValidator.d.ts +13 -0
- package/dist/esm/services/authProvider/utils/userRoleValidator.js +33 -0
- package/dist/esm/services/lrsGateway/xapiUtils.js +3 -1
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +57 -57
- package/script/.build-dist.swo +0 -0
- package/script/.build-dist.swp +0 -0
- package/script/bin/.copy-from-template.bash.swp +0 -0
- package/script/bin/update-utils.bash +9 -0
package/dist/cjs/assertions.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
declare type AssertionFailed = string | Error | (() => never) | undefined;
|
|
1
|
+
export declare type AssertionFailed = string | Error | (() => never) | undefined;
|
|
2
|
+
export declare const doThrow: (failed: AssertionFailed) => never;
|
|
2
3
|
/**
|
|
3
4
|
* Asserts that the given value is true.
|
|
4
5
|
*
|
|
@@ -82,4 +83,3 @@ export declare const assertInstanceOf: <T>(thing: any, constructable: Function &
|
|
|
82
83
|
* @see assertInstanceOf
|
|
83
84
|
*/
|
|
84
85
|
export declare const assertErrorInstanceOf: <T extends Error>(thing: unknown, constructable: Function & (new (...args: any[]) => T)) => T;
|
|
85
|
-
export {};
|
package/dist/cjs/assertions.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* that might be handled differently.
|
|
14
14
|
*/
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.assertErrorInstanceOf = exports.assertInstanceOf = exports.notNaN = exports.assertNotNaN = exports.assertString = exports.assertDefined = exports.assertFalse = exports.assertTrue = void 0;
|
|
16
|
+
exports.assertErrorInstanceOf = exports.assertInstanceOf = exports.notNaN = exports.assertNotNaN = exports.assertString = exports.assertDefined = exports.assertFalse = exports.assertTrue = exports.doThrow = void 0;
|
|
17
17
|
const doThrow = (failed) => {
|
|
18
18
|
if (typeof failed === 'string') {
|
|
19
19
|
throw new Error(failed);
|
|
@@ -26,6 +26,7 @@ const doThrow = (failed) => {
|
|
|
26
26
|
}
|
|
27
27
|
return failed();
|
|
28
28
|
};
|
|
29
|
+
exports.doThrow = doThrow;
|
|
29
30
|
/**
|
|
30
31
|
* Asserts that the given value is true.
|
|
31
32
|
*
|
|
@@ -38,7 +39,7 @@ const doThrow = (failed) => {
|
|
|
38
39
|
*/
|
|
39
40
|
const assertTrue = (x, failed) => {
|
|
40
41
|
if (typeof x !== 'boolean' || x !== true) {
|
|
41
|
-
return doThrow(failed);
|
|
42
|
+
return (0, exports.doThrow)(failed);
|
|
42
43
|
}
|
|
43
44
|
return x;
|
|
44
45
|
};
|
|
@@ -55,7 +56,7 @@ exports.assertTrue = assertTrue;
|
|
|
55
56
|
*/
|
|
56
57
|
const assertFalse = (x, failed) => {
|
|
57
58
|
if (typeof x !== 'boolean' || x !== false) {
|
|
58
|
-
return doThrow(failed);
|
|
59
|
+
return (0, exports.doThrow)(failed);
|
|
59
60
|
}
|
|
60
61
|
return x;
|
|
61
62
|
};
|
|
@@ -72,7 +73,7 @@ exports.assertFalse = assertFalse;
|
|
|
72
73
|
*/
|
|
73
74
|
const assertDefined = (x, failed) => {
|
|
74
75
|
if (x === undefined) {
|
|
75
|
-
return doThrow(failed);
|
|
76
|
+
return (0, exports.doThrow)(failed);
|
|
76
77
|
}
|
|
77
78
|
return x;
|
|
78
79
|
};
|
|
@@ -89,7 +90,7 @@ exports.assertDefined = assertDefined;
|
|
|
89
90
|
*/
|
|
90
91
|
const assertString = (x, failed) => {
|
|
91
92
|
if (typeof x !== 'string') {
|
|
92
|
-
return doThrow(failed);
|
|
93
|
+
return (0, exports.doThrow)(failed);
|
|
93
94
|
}
|
|
94
95
|
return x;
|
|
95
96
|
};
|
|
@@ -106,7 +107,7 @@ exports.assertString = assertString;
|
|
|
106
107
|
*/
|
|
107
108
|
const assertNotNaN = (thing, failed) => {
|
|
108
109
|
if (typeof thing === 'number' && isNaN(thing)) {
|
|
109
|
-
return doThrow(failed);
|
|
110
|
+
return (0, exports.doThrow)(failed);
|
|
110
111
|
}
|
|
111
112
|
return thing;
|
|
112
113
|
};
|
|
@@ -130,7 +131,7 @@ const assertInstanceOf = (thing, constructable, failed) => {
|
|
|
130
131
|
if (thing instanceof constructable) {
|
|
131
132
|
return thing;
|
|
132
133
|
}
|
|
133
|
-
return doThrow(failed);
|
|
134
|
+
return (0, exports.doThrow)(failed);
|
|
134
135
|
};
|
|
135
136
|
exports.assertInstanceOf = assertInstanceOf;
|
|
136
137
|
/**
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AuthProvider } from '..';
|
|
2
|
+
import { AssertionFailed } from '../../../assertions';
|
|
3
|
+
import { ConfigProviderForConfig } from '../../../config';
|
|
4
|
+
declare type Config = {
|
|
5
|
+
application: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const createUserRoleValidator: (auth: AuthProvider, config: ConfigProviderForConfig<Config>) => {
|
|
8
|
+
getUserRoles: () => Promise<string[]>;
|
|
9
|
+
userHasRole: (role: string[]) => Promise<boolean>;
|
|
10
|
+
assertUserRole: (role: string[], fail?: AssertionFailed) => Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
export declare type UserRoleValidator = ReturnType<typeof createUserRoleValidator>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createUserRoleValidator = void 0;
|
|
4
|
+
const assertions_1 = require("../../../assertions");
|
|
5
|
+
const resolveConfigValue_1 = require("../../../config/resolveConfigValue");
|
|
6
|
+
const errors_1 = require("../../../errors");
|
|
7
|
+
const helpers_1 = require("../../../misc/helpers");
|
|
8
|
+
const createUserRoleValidator = (auth, config) => {
|
|
9
|
+
const application = (0, helpers_1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.application));
|
|
10
|
+
const getUserRoles = async () => {
|
|
11
|
+
var _a;
|
|
12
|
+
const user = await auth.getUser();
|
|
13
|
+
const appName = await application();
|
|
14
|
+
if (!user || !('applications' in user)) {
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
return ((_a = user.applications.find(a => a.name === appName)) === null || _a === void 0 ? void 0 : _a.roles) || [];
|
|
18
|
+
};
|
|
19
|
+
const userHasRole = async (role) => {
|
|
20
|
+
const roles = await getUserRoles();
|
|
21
|
+
if (!roles.some(r => role.includes(r))) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
};
|
|
26
|
+
const assertUserRole = async (role, fail = new errors_1.UnauthorizedError()) => {
|
|
27
|
+
if (!await userHasRole(role)) {
|
|
28
|
+
return (0, assertions_1.doThrow)(fail);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
getUserRoles,
|
|
33
|
+
userHasRole,
|
|
34
|
+
assertUserRole
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
exports.createUserRoleValidator = createUserRoleValidator;
|
|
@@ -40,7 +40,9 @@ const getScoreGrade = (score, completed, userId, maxScore) => {
|
|
|
40
40
|
return {
|
|
41
41
|
userId,
|
|
42
42
|
activityProgress: completed ? 'Completed' : 'Started',
|
|
43
|
-
|
|
43
|
+
// canvas assumes that anything that isn't 'FullyGraded' requires manual grading and displays a "needs grading" icon.
|
|
44
|
+
// if you warp your mind you can consider the portion of the assignment which is completed to be fully graded.
|
|
45
|
+
gradingProgress: 'FullyGraded',
|
|
44
46
|
scoreMaximum,
|
|
45
47
|
scoreGiven: (0, __1.roundToPrecision)(scoreGiven, -2),
|
|
46
48
|
};
|