1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.StrategyCollection = void 0;
|
4 | class StrategyCollection {
|
5 | constructor(strategies) {
|
6 | this.map = {};
|
7 | this.authorityMap = {};
|
8 | this.credentialMap = {};
|
9 | this.queryFields = {};
|
10 | this.mutationFields = {};
|
11 | this.types = [];
|
12 | if (strategies) {
|
13 | for (const strategy of strategies) {
|
14 | this.add(strategy);
|
15 | }
|
16 | }
|
17 | }
|
18 | add(s) {
|
19 | if (this.map[s.name])
|
20 | throw new Error(`INVARIANT: Multiple strategies cannot use the same identifier; "${s.name}" is used twice.`);
|
21 | const queryFields = { ...this.queryFields };
|
22 | for (const f of Object.keys(s.queryFields)) {
|
23 | if (queryFields[f]) {
|
24 | throw new Error(`INVARIANT: Multiple strategies cannot use the query field; "${f}" is used twice.`);
|
25 | }
|
26 | queryFields[f] = s.queryFields[f];
|
27 | }
|
28 | const mutationFields = { ...this.mutationFields };
|
29 | for (const f of Object.keys(s.mutationFields)) {
|
30 | if (mutationFields[f]) {
|
31 | throw new Error(`INVARIANT: Multiple strategies cannot use the mutation field; "${f}" is used twice.`);
|
32 | }
|
33 | mutationFields[f] = s.mutationFields[f];
|
34 | }
|
35 | this.map = { ...this.map, [s.name]: s };
|
36 | this.authorityMap = { ...this.authorityMap, [s.name]: s.authorityModel };
|
37 | this.credentialMap = { ...this.credentialMap, [s.name]: s.credentialModel };
|
38 | this.queryFields = queryFields;
|
39 | this.mutationFields = mutationFields;
|
40 | this.types = [...this.types, ...s.types];
|
41 | return this;
|
42 | }
|
43 | delete(s) {
|
44 | if (!this.map[s.name])
|
45 | return false;
|
46 | const map = { ...this.map };
|
47 | delete map[s.name];
|
48 | const authorityMap = { ...this.authorityMap };
|
49 | delete authorityMap[s.name];
|
50 | const credentialMap = { ...this.credentialMap };
|
51 | delete credentialMap[s.name];
|
52 | const queryFields = { ...this.queryFields };
|
53 | for (const f of Object.keys(s.queryFields)) {
|
54 | delete queryFields[f];
|
55 | }
|
56 | const mutationFields = { ...this.mutationFields };
|
57 | for (const f of Object.keys(s.mutationFields)) {
|
58 | delete mutationFields[f];
|
59 | }
|
60 | this.map = map;
|
61 | this.authorityMap = authorityMap;
|
62 | this.credentialMap = credentialMap;
|
63 | this.queryFields = queryFields;
|
64 | this.mutationFields = mutationFields;
|
65 | this.types = this.types.filter(t => !s.types.includes(t));
|
66 | return true;
|
67 | }
|
68 | }
|
69 | exports.StrategyCollection = StrategyCollection;
|
70 |
|
\ | No newline at end of file |