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