@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/esm/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/esm/assertions.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* stack trace more useful, and allows you to use specific error types
|
|
12
12
|
* that might be handled differently.
|
|
13
13
|
*/
|
|
14
|
-
const doThrow = (failed) => {
|
|
14
|
+
export const doThrow = (failed) => {
|
|
15
15
|
if (typeof failed === 'string') {
|
|
16
16
|
throw new Error(failed);
|
|
17
17
|
}
|
|
@@ -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,33 @@
|
|
|
1
|
+
import { doThrow } from '../../../assertions';
|
|
2
|
+
import { resolveConfigValue } from '../../../config/resolveConfigValue';
|
|
3
|
+
import { UnauthorizedError } from '../../../errors';
|
|
4
|
+
import { once } from '../../../misc/helpers';
|
|
5
|
+
export const createUserRoleValidator = (auth, config) => {
|
|
6
|
+
const application = once(() => resolveConfigValue(config.application));
|
|
7
|
+
const getUserRoles = async () => {
|
|
8
|
+
var _a;
|
|
9
|
+
const user = await auth.getUser();
|
|
10
|
+
const appName = await application();
|
|
11
|
+
if (!user || !('applications' in user)) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
return ((_a = user.applications.find(a => a.name === appName)) === null || _a === void 0 ? void 0 : _a.roles) || [];
|
|
15
|
+
};
|
|
16
|
+
const userHasRole = async (role) => {
|
|
17
|
+
const roles = await getUserRoles();
|
|
18
|
+
if (!roles.some(r => role.includes(r))) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
};
|
|
23
|
+
const assertUserRole = async (role, fail = new UnauthorizedError()) => {
|
|
24
|
+
if (!await userHasRole(role)) {
|
|
25
|
+
return doThrow(fail);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
getUserRoles,
|
|
30
|
+
userHasRole,
|
|
31
|
+
assertUserRole
|
|
32
|
+
};
|
|
33
|
+
};
|
|
@@ -33,7 +33,9 @@ export const getScoreGrade = (score, completed, userId, maxScore) => {
|
|
|
33
33
|
return {
|
|
34
34
|
userId,
|
|
35
35
|
activityProgress: completed ? 'Completed' : 'Started',
|
|
36
|
-
|
|
36
|
+
// canvas assumes that anything that isn't 'FullyGraded' requires manual grading and displays a "needs grading" icon.
|
|
37
|
+
// if you warp your mind you can consider the portion of the assignment which is completed to be fully graded.
|
|
38
|
+
gradingProgress: 'FullyGraded',
|
|
37
39
|
scoreMaximum,
|
|
38
40
|
scoreGiven: roundToPrecision(scoreGiven, -2),
|
|
39
41
|
};
|