@openstax/ts-utils 1.5.4 → 1.5.6

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.
@@ -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 {};
@@ -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
  /**
@@ -18,6 +18,11 @@ export interface ApiUser extends TokenUser {
18
18
  is_verified: boolean;
19
19
  is_guessed_preferred: boolean;
20
20
  }>;
21
+ applications: Array<{
22
+ id: number;
23
+ name: string;
24
+ roles: string[];
25
+ }>;
21
26
  external_ids: string[];
22
27
  is_not_gdpr_location: boolean;
23
28
  signed_contract_names: string[];
@@ -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;
@@ -24,8 +24,10 @@ export declare const resolveAttemptInfo: (statements: XapiStatement[], options?:
24
24
  currentPreference?: "latest" | "oldest" | undefined;
25
25
  } | undefined) => ActivityState;
26
26
  export declare const loadStatementsForActivityAndFirstChildren: (gateway: LrsGateway, activityIRI: string, options?: {
27
+ anyUser?: boolean | undefined;
27
28
  attempt?: string | undefined;
28
29
  ensureSync?: boolean | undefined;
30
+ user?: string | undefined;
29
31
  } | undefined) => Promise<XapiStatement[]>;
30
32
  export declare const loadActivityAttemptInfo: (gateway: LrsGateway, activityIRI: string, options?: {
31
33
  currentAttempt?: string | undefined;
@@ -117,7 +117,7 @@ ${await response.text()}`);
117
117
  throw new Error(`xAPI consistent through ${consistentThrough}; not in sync with current date ${date}.`);
118
118
  }
119
119
  return formatGetXapiStatementsResponse(responsePromise);
120
- });
120
+ }, { retries: 4 });
121
121
  }
122
122
  else {
123
123
  return formatGetXapiStatementsResponse(fetchXapiStatements(fetchParams));