Skip to content

Commit 761fa67

Browse files
authored
Add component boilerplate (#80)
1 parent 67671c9 commit 761fa67

File tree

19 files changed

+1176
-424
lines changed

19 files changed

+1176
-424
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"plugins": ["react", "@typescript-eslint", "react-hooks", "prettier", "simple-import-sort", "jsdoc"],
2121
"settings": {
2222
"react": {
23-
"version": "latest"
23+
"version": "detect"
2424
},
2525
"import/resolver": {
2626
"typescript": {}, // this loads <rootdir>/tsconfig.json to eslint
Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as React from 'react';
22
import { K8sResourceCommon, WatchK8sResource } from '@openshift-console/dynamic-plugin-sdk';
33

4-
const getURL = (props: WatchK8sResource): string => {
5-
const {name, namespace, namespaced, isList, groupVersionKind} = props;
4+
const getResourceURL = (props: WatchK8sResource): string => {
5+
const {name, namespace, namespaced, groupVersionKind} = props;
66
const {group, version, kind} = groupVersionKind;
77
if (!group || !kind || !version) {
88
return;
@@ -11,39 +11,42 @@ const getURL = (props: WatchK8sResource): string => {
1111
const baseURL ='/api/kubernetes/apis/' + group + '/' + version;
1212
const namespaceURL =
1313
namespaced ? '/namespaces/' + (namespace.toString() || 'default') : '';
14-
const resourceKind = kind.toLowerCase();
15-
const pluralize = isList ? 's' : '';
14+
const resourceKind = kind.toLowerCase() + 's';
1615
const resourceName = name ? name.toString() : '';
17-
const url = baseURL + namespaceURL + '/' + resourceKind + pluralize + '/' + resourceName;
16+
const url = baseURL + namespaceURL + '/' + resourceKind + (resourceName ? '/' + resourceName : '');
1817

1918
return url;
2019
};
2120

2221
export const useK8sWatchResource = <R extends K8sResourceCommon | K8sResourceCommon[]>(
2322
props: WatchK8sResource | null,
2423
) => {
25-
const [loaded, setLoaded] = React.useState(false);
26-
const [loadError, setLoadError] = React.useState<string>(null);
27-
const [data, setData] = React.useState<R>();
28-
29-
React.useEffect(() => {
30-
const url = getURL(props);
31-
32-
fetch(url)
33-
.then(response => response.json())
34-
.then((jsonData) => {
24+
const [loaded, setLoaded] = React.useState(false);
25+
const [loadError, setLoadError] = React.useState<string>(null);
26+
const [data, setData] = React.useState<R>();
27+
28+
React.useEffect(() => {
29+
const url = getResourceURL(props);
30+
31+
fetch(url)
32+
.then(response => response.json())
33+
.then((jsonData) => {
34+
if (props.isList) {
35+
setData(jsonData?.items as R);
36+
} else {
3537
setData(jsonData as R);
36-
setLoaded(true);
37-
})
38-
.catch((error) => {
39-
setLoadError(error);
40-
setLoaded(true);
41-
});
42-
43-
// eslint-disable-next-line react-hooks/exhaustive-deps
44-
}, [props?.name, props?.namespace]);
45-
46-
return [data, loaded, loadError];
47-
};
48-
49-
export default useK8sWatchResource;
38+
}
39+
setLoaded(true);
40+
})
41+
.catch((error) => {
42+
setLoadError(error);
43+
setLoaded(true);
44+
});
45+
46+
// eslint-disable-next-line react-hooks/exhaustive-deps
47+
}, [props?.name, props?.namespace]);
48+
49+
return [data, loaded, loadError];
50+
};
51+
52+
export default useK8sWatchResource;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { K8sResourceCommon, WatchK8sResource } from '@openshift-console/dynamic-plugin-sdk';
2+
import fs from 'fs';
3+
4+
const getResourceURL = (props: WatchK8sResource): string => {
5+
const {name, namespace, namespaced, groupVersionKind} = props;
6+
const {group, version, kind} = groupVersionKind;
7+
if (!group || !kind || !version) {
8+
return;
9+
}
10+
11+
const baseURL ='/api/kubernetes/apis/' + group + '/' + version;
12+
const namespaceURL =
13+
namespaced ? '/namespaces/' + (namespace.toString() || 'default') : '';
14+
const resourceKind = kind.toLowerCase() + 's';
15+
const resourceName = name ? name.toString() : '';
16+
const url = baseURL + namespaceURL + '/' + resourceKind + (resourceName ? '/' + resourceName : '');
17+
18+
return url;
19+
};
20+
21+
export const useK8sWatchResourceFS = <R extends K8sResourceCommon | K8sResourceCommon[]>(
22+
props: WatchK8sResource | null,
23+
) => {
24+
let data = {};
25+
26+
const reqPath = 'public' + getResourceURL(props);
27+
if (!fs.existsSync(reqPath)) {
28+
return [{}, true, 'Resource not found (404)'];
29+
}
30+
31+
const response = fs.readFileSync (reqPath).toString('utf-8');
32+
const jsonData = JSON.parse(response);
33+
if (props.isList) {
34+
data = jsonData?.items as R;
35+
} else {
36+
data = jsonData as R;
37+
}
38+
39+
return [data, true, null];
40+
};
41+
42+
export default useK8sWatchResourceFS;

bridge/environment.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ export BRIDGE_AUTH_BEARER_TOKEN
1515
BRIDGE_STYLEGUIDE_ENDPOINT="http://127.0.0.1:6060"
1616
export BRIDGE_STYLEGUIDE_ENDPOINT
1717

18+
BRIDGE_PROXY_REWRITE="true"
19+
export BRIDGE_PROXY_REWRITE
20+
1821
echo "BRIDGE_CLUSTER_ENDPOINT: $BRIDGE_CLUSTER_ENDPOINT"
1922
echo "BRIDGE_CLUSTER_THANOS: $BRIDGE_CLUSTER_THANOS"
2023
echo "BRIDGE_CLUSTER_ALERTMANAGER: $BRIDGE_CLUSTER_ALERTMANAGER"
2124
echo "BRIDGE_STYLEGUIDE_ENDPOINT $BRIDGE_STYLEGUIDE_ENDPOINT"
25+
echo "BRIDGE_PROXY_REWRITE $BRIDGE_PROXY_REWRITE"
2226
echo "BRIDGE_AUTH_BEARER_TOKEN: hidden"

codecov.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
comment: false
1+
comment: false
2+
ignore:
3+
- "bridge"

jest-setup.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import { WatchK8sResource } from '@openshift-console/dynamic-plugin-sdk';
12
import { configure } from '@testing-library/dom';
23

34
import '@testing-library/jest-dom/extend-expect';
45
import 'regenerator-runtime/runtime';
56
import '@testing-library/jest-dom';
67

8+
import { useK8sWatchResourceFS } from './bridge/dynamic-plugin-sdk-mocks/useK8sWatchResourceFS';
9+
710
configure({
811
testIdAttribute: 'data-test-id',
912
});
13+
14+
jest.doMock('@openshift-console/dynamic-plugin-sdk/lib/lib-core', () => ({
15+
useK8sWatchResource: jest.fn((props: WatchK8sResource | null) => {
16+
const response = useK8sWatchResourceFS(props);
17+
18+
return response;
19+
}),
20+
}));

jest.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const config: Config.InitialOptions = {
44
testEnvironment: 'jsdom',
55
setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],
66
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/dist/', '<rootDir>/bridge/'],
7-
transformIgnorePatterns: ['<rootDir>/node_modules/(?!(@patternfly|@openshift-console\\S*?)/.*)'],
7+
transformIgnorePatterns: [
8+
'<rootDir>/node_modules/(?!(@patternfly|@kubevirt-ui|@openshift-console\\S*?)/.*)',
9+
],
810
moduleNameMapper: {
911
'\\.(css|less|svg)$': '<rootDir>/__mocks__/dummy.ts',
1012
},

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
"@babel/preset-env": "^7.16.8",
4040
"@babel/preset-react": "^7.16.7",
4141
"@babel/preset-typescript": "^7.16.7",
42-
"@openshift-console/dynamic-plugin-sdk": "0.0.5",
43-
"@openshift-console/dynamic-plugin-sdk-internal": "^0.0.4",
42+
"@openshift-console/dynamic-plugin-sdk": "0.0.6",
43+
"@openshift-console/dynamic-plugin-sdk-internal": "^0.0.5",
4444
"@rollup/plugin-commonjs": "^21.0.1",
4545
"@rollup/plugin-node-resolve": "^13.1.3",
4646
"@rollup/plugin-typescript": "^8.3.0",
@@ -52,6 +52,7 @@
5252
"@types/webpack-env": "^1.16.3",
5353
"@typescript-eslint/eslint-plugin": "^5.9.1",
5454
"@typescript-eslint/parser": "^5.9.1",
55+
"Express": "^3.0.1",
5556
"babel-jest": "^27.4.6",
5657
"babel-loader": "^8.2.3",
5758
"css-loader": "^6.5.1",
@@ -65,6 +66,7 @@
6566
"eslint-plugin-react-hooks": "^4.3.0",
6667
"eslint-plugin-simple-import-sort": "^7.0.0",
6768
"file-loader": "^6.2.0",
69+
"http": "^0.0.1-security",
6870
"http-proxy-middleware": "^2.0.3",
6971
"jest": "^27.4.7",
7072
"postcss": "^8.4.5",
@@ -80,6 +82,7 @@
8082
"sass-loader": "^12.4.0",
8183
"style-loader": "^3.3.1",
8284
"svg-url-loader": "^7.1.1",
85+
"ts-jest": "^27.1.3",
8386
"ts-loader": "^9.2.6",
8487
"ts-node": "^10.4.0",
8588
"tsconfig-paths-webpack-plugin": "^3.5.2",

public/api/kubernetes/apis/README.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)