Skip to content

Commit 5c34e1d

Browse files
authored
Feature/domain container getters (#418)
* Added domain mutations. Added domain getDefaultState. * Added domain getters * Added domain getter to store
1 parent 65c2e92 commit 5c34e1d

File tree

11 files changed

+309
-1
lines changed

11 files changed

+309
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
import { connect } from 'vuex-connect';
23+
import {
24+
ROUTE_PARAMS_CLUSTER_NAME,
25+
ROUTE_PARAMS_DOMAIN,
26+
} from '../route/getter-types';
27+
import {
28+
DOMAIN_ERROR,
29+
DOMAIN_IS_LOADING,
30+
DOMAIN_IS_READY,
31+
DOMAIN_CROSS_ORIGIN,
32+
} from './getter-types';
33+
34+
const gettersToProps = {
35+
clusterName: ROUTE_PARAMS_CLUSTER_NAME,
36+
domainName: ROUTE_PARAMS_DOMAIN,
37+
error: DOMAIN_ERROR,
38+
isLoading: DOMAIN_IS_LOADING,
39+
isReady: DOMAIN_IS_READY,
40+
origin: DOMAIN_CROSS_ORIGIN,
41+
};
42+
43+
export default connect({
44+
gettersToProps,
45+
});

client/containers/domain/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
// THE SOFTWARE.
2121

22+
export const DOMAIN_STATE_PREFIX = 'domain';
2223
export const DOMAIN_TYPE_PREFIX = 'DOMAIN';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
import { typePrefix } from './helpers';
23+
24+
export const DOMAIN_CROSS_ORIGIN = typePrefix('CROSS_ORIGIN');
25+
export const DOMAIN_CURRENT = typePrefix('CURRENT');
26+
export const DOMAIN_ERROR = typePrefix('ERROR');
27+
export const DOMAIN_HASH = typePrefix('HASH');
28+
export const DOMAIN_IS_LOADING = typePrefix('IS_LOADING');
29+
export const DOMAIN_IS_READY = typePrefix('IS_READY');
30+
export const DOMAIN_NAMESPACE = typePrefix('NAMESPACE');
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
import { get } from 'lodash-es';
23+
import {
24+
ROUTE_PARAMS_CLUSTER_NAME,
25+
ROUTE_PARAMS_DOMAIN,
26+
} from '../route/getter-types';
27+
import {
28+
CROSS_REGION,
29+
CROSS_REGION_ALLOWED_CROSS_ORIGIN,
30+
CROSS_REGION_CLUSTER_ORIGIN_LIST,
31+
} from '../cross-region/getter-types';
32+
import {
33+
DOMAIN_CROSS_ORIGIN,
34+
DOMAIN_CURRENT,
35+
DOMAIN_ERROR,
36+
DOMAIN_HASH,
37+
DOMAIN_IS_LOADING,
38+
DOMAIN_IS_READY,
39+
DOMAIN_NAMESPACE,
40+
} from './getter-types';
41+
import { statePrefix, getDomain, getCrossOrigin } from './helpers';
42+
import { hasExpired } from '~helpers';
43+
44+
const getters = {
45+
[DOMAIN_CURRENT]: (_, getters) => {
46+
const clusterName = getters[ROUTE_PARAMS_CLUSTER_NAME];
47+
const domainNamespace = getters[DOMAIN_NAMESPACE];
48+
49+
return getDomain({ clusterName, domainNamespace });
50+
},
51+
[DOMAIN_NAMESPACE]: (_, getters) => {
52+
const domainName = getters[ROUTE_PARAMS_DOMAIN];
53+
const domainHash = getters[DOMAIN_HASH];
54+
55+
return domainHash[domainName];
56+
},
57+
[DOMAIN_ERROR]: (_, getters) => {
58+
const domainNamespace = getters[DOMAIN_NAMESPACE];
59+
60+
return domainNamespace && domainNamespace.error;
61+
},
62+
[DOMAIN_HASH]: state => get(state, statePrefix('domainHash')),
63+
[DOMAIN_IS_LOADING]: (_, getters) =>
64+
getters[CROSS_REGION] &&
65+
getters[ROUTE_PARAMS_DOMAIN] &&
66+
!getters[DOMAIN_ERROR] &&
67+
hasExpired(get(getters[DOMAIN_CURRENT], 'expiryDateTime')),
68+
[DOMAIN_IS_READY]: (_, getters) =>
69+
!getters[DOMAIN_IS_LOADING] && !getters[DOMAIN_ERROR],
70+
[DOMAIN_CROSS_ORIGIN]: (_, getters) => {
71+
const allowedCrossOrigin = getters[CROSS_REGION_ALLOWED_CROSS_ORIGIN];
72+
const clusterName = getters[ROUTE_PARAMS_CLUSTER_NAME];
73+
const clusterOriginList = getters[CROSS_REGION_CLUSTER_ORIGIN_LIST];
74+
const crossRegion = getters[CROSS_REGION];
75+
const domain = getters[DOMAIN_CURRENT];
76+
77+
return getCrossOrigin({
78+
allowedCrossOrigin,
79+
clusterName,
80+
clusterOriginList,
81+
crossRegion,
82+
domain,
83+
});
84+
},
85+
};
86+
87+
export default getters;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
const getCrossOrigin = ({
23+
allowedCrossOrigin,
24+
clusterName,
25+
clusterOriginList,
26+
crossRegion,
27+
domain,
28+
}) => {
29+
if (
30+
!domain ||
31+
!crossRegion ||
32+
!allowedCrossOrigin ||
33+
!clusterOriginList ||
34+
!clusterOriginList.length
35+
) {
36+
return window.location.origin;
37+
}
38+
39+
const {
40+
replicationConfiguration: { activeClusterName, clusters },
41+
} = domain;
42+
43+
const matchedActiveClusterOrigin = clusterOriginList.find(
44+
({ clusterName: matchClusterName }) =>
45+
matchClusterName === activeClusterName
46+
);
47+
const matchedCluster =
48+
clusterName &&
49+
clusters.find(
50+
({ clusterName: matchClusterName }) => matchClusterName === clusterName
51+
);
52+
const matchedClusterOrigin = clusterOriginList.find(
53+
({ clusterName: matchClusterName }) =>
54+
matchedCluster && matchClusterName === clusterName
55+
);
56+
const origin =
57+
(matchedClusterOrigin && matchedClusterOrigin.origin) ||
58+
(matchedActiveClusterOrigin && matchedActiveClusterOrigin.origin);
59+
60+
if (!origin) {
61+
console.warn(
62+
`could not find clusterName "${clusterName}" or "${activeClusterName}" in crossRegion.clusterOriginList.`
63+
);
64+
}
65+
66+
return origin || window.location.origin;
67+
};
68+
69+
export default getCrossOrigin;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
const getDomain = ({ clusterName, domainNamespace }) => {
23+
if (!domainNamespace) {
24+
return;
25+
}
26+
27+
if (domainNamespace.global) {
28+
return domainNamespace.global;
29+
}
30+
31+
if (domainNamespace.local) {
32+
if (clusterName) {
33+
return domainNamespace.local.find(
34+
({ replicationConfiguration: { activeClusterName } }) =>
35+
activeClusterName === clusterName
36+
);
37+
}
38+
39+
return domainNamespace.local[0];
40+
}
41+
};
42+
43+
export default getDomain;

client/containers/domain/helpers/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
// THE SOFTWARE.
2121

22+
export { default as getCrossOrigin } from './get-cross-origin';
23+
export { default as getDomain } from './get-domain';
24+
export { default as statePrefix } from './state-prefix';
2225
export { default as typePrefix } from './type-prefix';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
import { DOMAIN_STATE_PREFIX } from '../constants';
23+
24+
const statePrefix = term => `${DOMAIN_STATE_PREFIX}.${term}`;
25+
26+
export default statePrefix;

client/containers/domain/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// THE SOFTWARE.
2121

2222
import getDefaultState from './get-default-state';
23+
import getters from './getters';
2324
import mutations from './mutations';
2425

25-
export { getDefaultState, mutations };
26+
export { getDefaultState, getters, mutations };

client/containers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export {
3434
} from './cross-region';
3535
export {
3636
getDefaultState as getDomainDefaultState,
37+
getters as domainGetters,
3738
mutations as domainMutations,
3839
} from './domain';
3940
export {

0 commit comments

Comments
 (0)