oc-template-react-compiler 5.2.1 → 6.0.0
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.
- package/README.md +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/lib/compile.d.ts +3 -0
- package/dist/lib/compile.js +36 -0
- package/dist/lib/htmlTemplate.d.ts +10 -0
- package/dist/lib/htmlTemplate.js +51 -0
- package/dist/lib/reactOCProviderTemplate.d.ts +3 -0
- package/dist/lib/reactOCProviderTemplate.js +56 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/useData.d.ts +7 -0
- package/dist/utils/useData.js +31 -0
- package/package.json +37 -47
- package/scaffold/src/.eslintrc.json +3 -0
- package/scaffold/src/package.json +29 -11
- package/scaffold/src/public/logo.png +0 -0
- package/scaffold/src/src/App.test.tsx +39 -0
- package/scaffold/src/src/App.tsx +52 -0
- package/scaffold/src/src/oc-app.d.ts +1 -0
- package/scaffold/src/src/server.ts +37 -0
- package/scaffold/src/src/setupTests.js +1 -0
- package/scaffold/src/src/styles.css +30 -0
- package/scaffold/src/src/types.ts +15 -0
- package/scaffold/src/tsconfig.json +26 -0
- package/scaffold/src/vite.config.ts +11 -0
- package/index.js +0 -11
- package/lib/compile.js +0 -26
- package/lib/compileServer.js +0 -100
- package/lib/compileView.js +0 -135
- package/lib/higherOrderServerTemplate.js +0 -32
- package/lib/reactOCProviderTemplate.js +0 -54
- package/lib/to-abstract-base-template-utils/font-family-unicode-parser.js +0 -25
- package/lib/to-abstract-base-template-utils/oc-webpack/index.js +0 -9
- package/lib/to-abstract-base-template-utils/oc-webpack/lib/compiler/index.js +0 -42
- package/lib/to-abstract-base-template-utils/oc-webpack/lib/configurator/client/index.js +0 -120
- package/lib/to-abstract-base-template-utils/oc-webpack/lib/configurator/createExcludeRegex.js +0 -6
- package/lib/to-abstract-base-template-utils/oc-webpack/lib/configurator/index.js +0 -9
- package/lib/to-abstract-base-template-utils/oc-webpack/lib/configurator/server/index.js +0 -93
- package/lib/viewTemplate.js +0 -35
- package/scaffold/src/app.js +0 -11
- package/scaffold/src/server.js +0 -4
- package/scaffold/src/styles.css +0 -4
- package/utils/index.js +0 -4
- package/utils/withDataProvider.js +0 -21
- package/utils/withSettingProvider.js +0 -21
package/README.md
CHANGED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./lib/compile"), exports);
|
|
18
|
+
__exportStar(require("oc-template-react"), exports);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.compile = void 0;
|
|
7
|
+
const oc_generic_template_compiler_1 = require("oc-generic-template-compiler");
|
|
8
|
+
const oc_statics_compiler_1 = __importDefault(require("oc-statics-compiler"));
|
|
9
|
+
const oc_template_react_1 = require("oc-template-react");
|
|
10
|
+
const oc_vite_compiler_1 = require("oc-vite-compiler");
|
|
11
|
+
const plugin_react_1 = __importDefault(require("@vitejs/plugin-react"));
|
|
12
|
+
const reactOCProviderTemplate_1 = __importDefault(require("./reactOCProviderTemplate"));
|
|
13
|
+
const htmlTemplate_1 = __importDefault(require("./htmlTemplate"));
|
|
14
|
+
exports.compile = (0, oc_generic_template_compiler_1.createCompile)({
|
|
15
|
+
compileView: (options, cb) => (0, oc_vite_compiler_1.viteView)({
|
|
16
|
+
...options,
|
|
17
|
+
plugins: [plugin_react_1.default()],
|
|
18
|
+
viewWrapper: ({ viewPath }) => (0, reactOCProviderTemplate_1.default)({ viewPath }),
|
|
19
|
+
htmlTemplate: htmlTemplate_1.default,
|
|
20
|
+
externals: (0, oc_template_react_1.getInfo)().externals
|
|
21
|
+
}, cb),
|
|
22
|
+
compileServer: oc_vite_compiler_1.viteServer,
|
|
23
|
+
compileStatics: oc_statics_compiler_1.default,
|
|
24
|
+
getInfo: oc_template_react_1.getInfo
|
|
25
|
+
});
|
|
26
|
+
// OPTIONS
|
|
27
|
+
// =======
|
|
28
|
+
// componentPath
|
|
29
|
+
// componentPackage,
|
|
30
|
+
// logger,
|
|
31
|
+
// minify
|
|
32
|
+
// ocPackage
|
|
33
|
+
// publishPath
|
|
34
|
+
// verbose,
|
|
35
|
+
// watch,
|
|
36
|
+
// production
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const cssesc_1 = __importDefault(require("cssesc"));
|
|
7
|
+
function viewTemplate({ templateId, css, externals, bundle, hash }) {
|
|
8
|
+
return `function(model){
|
|
9
|
+
oc.reactComponents = oc.reactComponents || {};
|
|
10
|
+
oc.reactComponents['${hash}'] = oc.reactComponents['${hash}'] || (${bundle});
|
|
11
|
+
if (!model) return;
|
|
12
|
+
var modelHTML = model.__html ? model.__html : '';
|
|
13
|
+
var staticPath = model.component.props._staticPath;
|
|
14
|
+
var props = JSON.stringify(model.component.props);
|
|
15
|
+
oc = oc || {};
|
|
16
|
+
oc.__typescriptReactTemplate = oc.__typescriptReactTemplate || { count: 0 };
|
|
17
|
+
var count = oc.__typescriptReactTemplate.count;
|
|
18
|
+
var templateId = "${templateId}-" + count;
|
|
19
|
+
oc.__typescriptReactTemplate.count++;
|
|
20
|
+
return '<div id="' + templateId + '" class="${templateId}">' + modelHTML + '</div>' +
|
|
21
|
+
'${css ? '<style>' + (0, cssesc_1.default)(css) + '</style>' : ''}' +
|
|
22
|
+
'<script>' +
|
|
23
|
+
'oc = oc || {};' +
|
|
24
|
+
'oc.cmd = oc.cmd || [];' +
|
|
25
|
+
'oc.cmd.push(function(oc){' +
|
|
26
|
+
'${css ? "oc.events.fire(\\'oc:cssDidMount\\', \\'" + css + "\\');" : ''}' +
|
|
27
|
+
'oc.requireSeries(${JSON.stringify(externals)}, function(){' +
|
|
28
|
+
'var targetNode = document.getElementById("' + templateId + '");' +
|
|
29
|
+
'targetNode.setAttribute("id","");' +
|
|
30
|
+
'var reactElement = React.createElement(oc.reactComponents["${hash}"],' + props + ');' +
|
|
31
|
+
'if (ReactDOM.createRoot) {' +
|
|
32
|
+
'if (' + !!modelHTML + ') {' +
|
|
33
|
+
'var root = ReactDOM.hydrateRoot(targetNode, reactElement);' +
|
|
34
|
+
'root.render(reactElement);' +
|
|
35
|
+
'} else {' +
|
|
36
|
+
'var root = ReactDOM.createRoot(targetNode);' +
|
|
37
|
+
'root.render(reactElement);' +
|
|
38
|
+
'}' +
|
|
39
|
+
'} else {' +
|
|
40
|
+
'if (' + !!modelHTML + ') {' +
|
|
41
|
+
'ReactDOM.hydrate(reactElement, targetNode);' +
|
|
42
|
+
'} else {' +
|
|
43
|
+
'ReactDOM.render(reactElement, targetNode);' +
|
|
44
|
+
'}' +
|
|
45
|
+
'}' +
|
|
46
|
+
'});' +
|
|
47
|
+
'});' +
|
|
48
|
+
'</script>'
|
|
49
|
+
}`;
|
|
50
|
+
}
|
|
51
|
+
exports.default = viewTemplate;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const removeExtension = (path) => path.replace(/\.(t|j)sx?$/, '');
|
|
4
|
+
function reactOCProviderTemplate({ viewPath }) {
|
|
5
|
+
return `
|
|
6
|
+
import React from 'react';
|
|
7
|
+
import View from '${removeExtension(viewPath)}';
|
|
8
|
+
import { DataProvider } from 'oc-template-react-compiler/dist/utils/useData'
|
|
9
|
+
|
|
10
|
+
class OCProvider extends React.Component {
|
|
11
|
+
componentDidMount(){
|
|
12
|
+
const { _staticPath, _baseUrl, _componentName, _componentVersion, ...rest } = (this.props as any);
|
|
13
|
+
(window as any).oc.events.fire('oc:componentDidMount', rest);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getData(providerProps: any, parameters: any, cb: (error: any, parameters?: any, props?: any) => void) {
|
|
17
|
+
return (window as any).oc.getData({
|
|
18
|
+
name: providerProps._componentName,
|
|
19
|
+
version: providerProps._componentVersion,
|
|
20
|
+
baseUrl: providerProps._baseUrl,
|
|
21
|
+
parameters
|
|
22
|
+
}, (err: any, data: any) => {
|
|
23
|
+
if (err) {
|
|
24
|
+
return cb(err);
|
|
25
|
+
}
|
|
26
|
+
const { _staticPath, _baseUrl, _componentName, _componentVersion, ...rest } = (data.component.props as any);
|
|
27
|
+
cb(null, rest, data.component.props);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getSetting(providerProps: any, setting: string) {
|
|
32
|
+
const settingHash = {
|
|
33
|
+
name: providerProps._componentName,
|
|
34
|
+
version: providerProps._componentVersion,
|
|
35
|
+
baseUrl: providerProps._baseUrl,
|
|
36
|
+
staticPath: providerProps._staticPath
|
|
37
|
+
};
|
|
38
|
+
return (settingHash as any)[setting];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
render() {
|
|
42
|
+
const { _staticPath, _baseUrl, _componentName, _componentVersion, ...rest } = (this.props as any);
|
|
43
|
+
(rest as any).getData = (parameters: any, cb: (error: any, parameters?: any, props?: any) => void) => this.getData(this.props, parameters, cb);
|
|
44
|
+
(rest as any).getSetting = (setting: string) => this.getSetting(this.props, setting);
|
|
45
|
+
return (
|
|
46
|
+
<DataProvider value={{...rest}}>
|
|
47
|
+
<View { ...rest } />
|
|
48
|
+
</DataProvider>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default OCProvider
|
|
54
|
+
`;
|
|
55
|
+
}
|
|
56
|
+
exports.default = reactOCProviderTemplate;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useData';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./useData"), exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
type PromiseData<T = any> = T & {
|
|
2
|
+
getData<O = any>(parameters?: Partial<T>): Promise<O>;
|
|
3
|
+
getSetting(setting: 'name' | 'version' | 'baseUrl' | 'staticPath'): string;
|
|
4
|
+
};
|
|
5
|
+
export declare const DataProvider: ({ children, ...props }: any) => import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
export declare function useData<T = any>(): PromiseData<T>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useData = exports.DataProvider = void 0;
|
|
7
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
+
const react_1 = __importDefault(require("react"));
|
|
9
|
+
const DataContext = react_1.default.createContext({});
|
|
10
|
+
const DataProvider = ({ children, ...props }) => {
|
|
11
|
+
return (0, jsx_runtime_1.jsx)(DataContext.Provider, { value: props, children: children });
|
|
12
|
+
};
|
|
13
|
+
exports.DataProvider = DataProvider;
|
|
14
|
+
function useData() {
|
|
15
|
+
const { value: { getData, ...rest } } = react_1.default.useContext(DataContext);
|
|
16
|
+
const asyncGetData = react_1.default.useCallback((data) => {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
getData({ ...rest, ...data }, (err, newData) => {
|
|
20
|
+
if (err) {
|
|
21
|
+
reject(err);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
resolve(newData);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}, []);
|
|
29
|
+
return { getData: asyncGetData, ...rest };
|
|
30
|
+
}
|
|
31
|
+
exports.useData = useData;
|
package/package.json
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oc-template-react-compiler",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "index.js",
|
|
3
|
+
"version": "6.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prepare": "npm run build",
|
|
10
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
11
|
+
},
|
|
6
12
|
"repository": {
|
|
7
13
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/opencomponents/
|
|
14
|
+
"url": "https://github.com/opencomponents/vite-templates.git"
|
|
9
15
|
},
|
|
10
16
|
"bugs": {
|
|
11
|
-
"url": "https://github.com/opencomponents/
|
|
17
|
+
"url": "https://github.com/opencomponents/vite-templates/issues"
|
|
12
18
|
},
|
|
13
|
-
"homepage": "https://github.com/opencomponents/
|
|
19
|
+
"homepage": "https://github.com/opencomponents/vite-templates#readme",
|
|
14
20
|
"keywords": [
|
|
15
21
|
"oc",
|
|
16
22
|
"opencomponents",
|
|
@@ -18,49 +24,33 @@
|
|
|
18
24
|
"oc-template"
|
|
19
25
|
],
|
|
20
26
|
"author": {
|
|
21
|
-
"name": "
|
|
22
|
-
"email": "
|
|
27
|
+
"name": "Ricardo Devis Agullo",
|
|
28
|
+
"email": "mkredz@gmail.com"
|
|
23
29
|
},
|
|
24
30
|
"license": "MIT",
|
|
25
31
|
"dependencies": {
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"memory-fs": "0.4.1",
|
|
38
|
-
"mini-css-extract-plugin": "0.8.0",
|
|
39
|
-
"node-dir": "0.1.17",
|
|
40
|
-
"oc-external-dependencies-handler": "1.0.8",
|
|
41
|
-
"oc-generic-template-compiler": "2.0.5",
|
|
42
|
-
"oc-get-unix-utc-timestamp": "1.0.2",
|
|
43
|
-
"oc-hash-builder": "1.0.2",
|
|
44
|
-
"oc-minify-file": "1.0.15",
|
|
45
|
-
"oc-react-component-wrapper": "1.0.2",
|
|
46
|
-
"oc-statics-compiler": "2.0.11",
|
|
47
|
-
"oc-template-react": "2.1.0",
|
|
48
|
-
"oc-templates-messages": "1.0.2",
|
|
49
|
-
"oc-view-wrapper": "1.0.3",
|
|
50
|
-
"postcss-extend": "1.0.5",
|
|
51
|
-
"postcss-icss-values": "2.0.2",
|
|
52
|
-
"postcss-import": "12.0.1",
|
|
53
|
-
"postcss-loader": "3.0.0",
|
|
54
|
-
"prop-types": "15.7.2",
|
|
55
|
-
"react": "16.9.0",
|
|
56
|
-
"webpack": "4.41.0"
|
|
32
|
+
"@vitejs/plugin-react": "^4.0.0",
|
|
33
|
+
"chalk": "^3.0.0",
|
|
34
|
+
"cssesc": "^3.0.0",
|
|
35
|
+
"immer": "^9.0.6",
|
|
36
|
+
"oc-generic-template-compiler": "2.1.0",
|
|
37
|
+
"oc-statics-compiler": "2.1.1",
|
|
38
|
+
"oc-template-react": "4.0.0",
|
|
39
|
+
"oc-vite-compiler": "3.0.0",
|
|
40
|
+
"react": "^18.2.0",
|
|
41
|
+
"react-dom": "^18.2.0",
|
|
42
|
+
"resolve": "^1.20.0"
|
|
57
43
|
},
|
|
58
44
|
"files": [
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
|
|
66
|
-
|
|
45
|
+
"dist/**",
|
|
46
|
+
"scaffold"
|
|
47
|
+
],
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/cssesc": "^3.0.2",
|
|
50
|
+
"@types/react": "^18.0.28",
|
|
51
|
+
"@types/react-dom": "^18.0.11",
|
|
52
|
+
"lodash": "^4.17.4",
|
|
53
|
+
"prettier": "^2.3.2",
|
|
54
|
+
"prettier-config-guestline": "2.0.0"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -2,25 +2,43 @@
|
|
|
2
2
|
"name": "base-component-react",
|
|
3
3
|
"description": "",
|
|
4
4
|
"version": "1.0.0",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"lint": "eslint --max-warnings 0 --ext .js,.jsx,.ts,.tsx src",
|
|
7
|
+
"build": "tsc --noEmit && oc package .",
|
|
8
|
+
"test": "vitest"
|
|
9
|
+
},
|
|
5
10
|
"oc": {
|
|
6
11
|
"files": {
|
|
7
|
-
"data": "server.
|
|
12
|
+
"data": "src/server.ts",
|
|
8
13
|
"template": {
|
|
9
|
-
"src": "
|
|
14
|
+
"src": "src/App.tsx",
|
|
10
15
|
"type": "oc-template-react"
|
|
11
|
-
}
|
|
16
|
+
},
|
|
17
|
+
"static": [
|
|
18
|
+
"public"
|
|
19
|
+
]
|
|
12
20
|
},
|
|
13
21
|
"parameters": {
|
|
14
|
-
"
|
|
15
|
-
"default":
|
|
16
|
-
"description": "
|
|
17
|
-
"example":
|
|
18
|
-
"mandatory":
|
|
19
|
-
"type": "
|
|
22
|
+
"userId": {
|
|
23
|
+
"default": 1,
|
|
24
|
+
"description": "The user id from the user database",
|
|
25
|
+
"example": 1,
|
|
26
|
+
"mandatory": true,
|
|
27
|
+
"type": "number"
|
|
20
28
|
}
|
|
21
29
|
}
|
|
22
30
|
},
|
|
31
|
+
"dependencies": {},
|
|
23
32
|
"devDependencies": {
|
|
24
|
-
"
|
|
33
|
+
"@testing-library/react": "^14.0.0",
|
|
34
|
+
"@testing-library/user-event": "^14.4.3",
|
|
35
|
+
"@types/react": "^18.0.28",
|
|
36
|
+
"jsdom": "^21.1.1",
|
|
37
|
+
"oc-server": "*",
|
|
38
|
+
"oc-template-react-compiler": "6.0.0",
|
|
39
|
+
"react": "18.2.0",
|
|
40
|
+
"react-dom": "18.2.0",
|
|
41
|
+
"typescript": "5.1.3",
|
|
42
|
+
"vitest": "^0.32.2"
|
|
25
43
|
}
|
|
26
|
-
}
|
|
44
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { describe, it, afterEach, beforeEach, expect, vi } from 'vitest';
|
|
3
|
+
import { render, screen } from '@testing-library/react';
|
|
4
|
+
import { DataProvider } from 'oc-template-react-compiler/dist/utils/useData';
|
|
5
|
+
|
|
6
|
+
import App from './App';
|
|
7
|
+
|
|
8
|
+
const getData = vi.fn();
|
|
9
|
+
const getSetting = vi.fn();
|
|
10
|
+
|
|
11
|
+
describe('App - Page', () => {
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
window.oc = { events: { on: vi.fn(), fire: vi.fn() } } as any;
|
|
14
|
+
});
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
vi.restoreAllMocks();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('Gets more data when clicking the button', () => {
|
|
20
|
+
getData.mockImplementationOnce(() => Promise.resolve({}));
|
|
21
|
+
getSetting.mockImplementationOnce(() => ({}));
|
|
22
|
+
render(
|
|
23
|
+
<DataProvider
|
|
24
|
+
value={{
|
|
25
|
+
firstName: 'firstName',
|
|
26
|
+
lastName: 'lastName',
|
|
27
|
+
userId: 0,
|
|
28
|
+
getData,
|
|
29
|
+
getSetting
|
|
30
|
+
}}
|
|
31
|
+
>
|
|
32
|
+
<App firstName="firstName" lastName="lastName" userId={0} />
|
|
33
|
+
</DataProvider>
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const extraInfoButton = screen.getByText(/Get extra information/i);
|
|
37
|
+
expect(extraInfoButton).toBeTruthy();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { useData } from 'oc-template-react-compiler/dist/utils/useData';
|
|
3
|
+
import { serverClient } from 'oc-server';
|
|
4
|
+
import styles from './styles.css';
|
|
5
|
+
import logo from '../public/logo.png';
|
|
6
|
+
import type { AdditionalData, ClientProps } from './types';
|
|
7
|
+
|
|
8
|
+
interface AppProps extends ClientProps {
|
|
9
|
+
getMoreData?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const App: React.FC<ClientProps> = () => {
|
|
13
|
+
const { firstName, lastName, userId, getData } = useData<AppProps>();
|
|
14
|
+
const [additionalData, setAdditionalData] = useState<AdditionalData | null>(null);
|
|
15
|
+
const [error, setError] = useState('');
|
|
16
|
+
|
|
17
|
+
const fetchMoreData = async () => {
|
|
18
|
+
setError('');
|
|
19
|
+
try {
|
|
20
|
+
const data = await serverClient.getMoreData({ userId });
|
|
21
|
+
setAdditionalData(data);
|
|
22
|
+
} catch (err) {
|
|
23
|
+
setError(String(err));
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
if (error) {
|
|
28
|
+
return <div>Something wrong happened!</div>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<div className={styles.container}>
|
|
33
|
+
<img width="50" height="50" src={logo} alt="Logo" />
|
|
34
|
+
<h1 style={{ margin: '0 0 20px 0' }}>
|
|
35
|
+
Hello, <span style={{ textDecoration: 'underline' }}>{firstName}</span> {lastName}
|
|
36
|
+
</h1>
|
|
37
|
+
{additionalData && (
|
|
38
|
+
<div className={styles.info}>
|
|
39
|
+
<div className={styles.block}>Age: {additionalData.age}</div>
|
|
40
|
+
<div className={styles.block}>
|
|
41
|
+
Hobbies: {additionalData.hobbies.map((x) => x.toLowerCase()).join(', ')}
|
|
42
|
+
</div>
|
|
43
|
+
</div>
|
|
44
|
+
)}
|
|
45
|
+
<button className={styles.button} onClick={fetchMoreData}>
|
|
46
|
+
Get extra information
|
|
47
|
+
</button>
|
|
48
|
+
</div>
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export default App;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="oc-vite/client" />
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Server } from 'oc-server';
|
|
2
|
+
import { AdditionalData, ClientProps, OcParameters } from './types';
|
|
3
|
+
|
|
4
|
+
const database = [
|
|
5
|
+
{ name: 'John Doe', age: 34, hobbies: ['Swimming', 'Basketball'] },
|
|
6
|
+
{ name: 'Jane Doe', age: 35, hobbies: ['Running', 'Rugby'] }
|
|
7
|
+
];
|
|
8
|
+
|
|
9
|
+
async function getUser(userId: number) {
|
|
10
|
+
return database[userId];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const server = new Server(async (params: OcParameters): Promise<ClientProps> => {
|
|
14
|
+
const { userId } = params;
|
|
15
|
+
const user = await getUser(userId);
|
|
16
|
+
const [firstName, lastName] = user.name.split(/\s+/);
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
userId,
|
|
20
|
+
firstName,
|
|
21
|
+
lastName
|
|
22
|
+
};
|
|
23
|
+
}).action('getMoreData', async (params: OcParameters): Promise<AdditionalData> => {
|
|
24
|
+
const { userId } = params;
|
|
25
|
+
const user = await getUser(userId);
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
age: user.age,
|
|
29
|
+
hobbies: user.hobbies
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
declare module 'oc-server' {
|
|
34
|
+
interface Register {
|
|
35
|
+
server: typeof server;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
.container {
|
|
2
|
+
background-color: #3b246c;
|
|
3
|
+
color: #fff;
|
|
4
|
+
font-family: sans-serif;
|
|
5
|
+
padding: 40px;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.button {
|
|
9
|
+
background-color: #a97613;
|
|
10
|
+
border: none;
|
|
11
|
+
padding: 15px 32px;
|
|
12
|
+
text-align: center;
|
|
13
|
+
font-size: 16px;
|
|
14
|
+
text-decoration: none;
|
|
15
|
+
display: inline-block;
|
|
16
|
+
color: inherit;
|
|
17
|
+
cursor: pointer;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.info {
|
|
21
|
+
margin-bottom: 20px;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.block {
|
|
25
|
+
margin: 6px 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.button:hover {
|
|
29
|
+
background-color: #c79535;
|
|
30
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface OcParameters {
|
|
2
|
+
userId: number;
|
|
3
|
+
getMoreData?: boolean;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface AdditionalData {
|
|
7
|
+
hobbies: string[];
|
|
8
|
+
age: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ClientProps extends Partial<AdditionalData> {
|
|
12
|
+
userId: number;
|
|
13
|
+
firstName: string;
|
|
14
|
+
lastName: string;
|
|
15
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// These are suggested values and will be set when not present in the
|
|
4
|
+
// tsconfig.json
|
|
5
|
+
// 'parsedValue' matches the output value from ts.parseJsonConfigFileContent()
|
|
6
|
+
"target": "es5",
|
|
7
|
+
"lib": ["dom", "dom.iterable", "esnext"],
|
|
8
|
+
"strict": true,
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"allowSyntheticDefaultImports": true,
|
|
13
|
+
"forceConsistentCasingInFileNames": true,
|
|
14
|
+
"noFallthroughCasesInSwitch": true,
|
|
15
|
+
|
|
16
|
+
// These values are required and cannot be changed by the user
|
|
17
|
+
// Keep this in sync with the rollup config
|
|
18
|
+
"module": "ESNext",
|
|
19
|
+
"moduleResolution": "Node",
|
|
20
|
+
"resolveJsonModule": true,
|
|
21
|
+
"isolatedModules": true,
|
|
22
|
+
"jsx": "react-jsx"
|
|
23
|
+
},
|
|
24
|
+
"include": ["src"],
|
|
25
|
+
"exclude": ["src/_package"]
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defineConfig } from 'vite';
|
|
2
|
+
|
|
3
|
+
// https://vitejs.dev/config/
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
// Add your own plugins here for thinkgs like eslint or typecheckers
|
|
6
|
+
plugins: [],
|
|
7
|
+
// @ts-ignore Missing test property
|
|
8
|
+
test: {
|
|
9
|
+
environment: 'jsdom'
|
|
10
|
+
}
|
|
11
|
+
});
|