oc-template-react-compiler 10.0.1 → 10.1.1
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oc-template-react-compiler",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.1.1",
|
|
4
4
|
"description": "Compiler for the React OC template",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@vitejs/plugin-react": "^4.3.1",
|
|
32
32
|
"oc-template-react": "^4.2.0",
|
|
33
|
-
"oc-vite-compiler": "^5.
|
|
33
|
+
"oc-vite-compiler": "^5.1.1",
|
|
34
34
|
"react": "18.2.0",
|
|
35
35
|
"react-dom": "18.2.0"
|
|
36
36
|
},
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"@testing-library/user-event": "^14.4.3",
|
|
27
27
|
"@types/react": "^18.0.28",
|
|
28
28
|
"jsdom": "^21.1.1",
|
|
29
|
-
"oc-server": "^3.
|
|
30
|
-
"oc-template-react-compiler": "^10.
|
|
29
|
+
"oc-server": "^3.1.0",
|
|
30
|
+
"oc-template-react-compiler": "^10.1.1",
|
|
31
31
|
"react": "18.3.1",
|
|
32
32
|
"react-dom": "18.3.1",
|
|
33
33
|
"typescript": "5.1.3",
|
package/scaffold/src/src/App.tsx
CHANGED
|
@@ -6,6 +6,7 @@ import logo from '../public/logo.png';
|
|
|
6
6
|
type AdditionalData = ActionOutput<'funFact'>;
|
|
7
7
|
|
|
8
8
|
const App: React.FC<InitialData> = (props: InitialData) => {
|
|
9
|
+
// These come from the server.ts handler return value
|
|
9
10
|
const { firstName, lastName, born, hobbies } = props;
|
|
10
11
|
const [additionalData, setAdditionalData] = useState<AdditionalData | null>(
|
|
11
12
|
null
|
|
@@ -15,6 +16,8 @@ const App: React.FC<InitialData> = (props: InitialData) => {
|
|
|
15
16
|
const getFunFact = async () => {
|
|
16
17
|
setError('');
|
|
17
18
|
try {
|
|
19
|
+
// Call server action with type safety
|
|
20
|
+
// serverClient provides autocomplete and type checking
|
|
18
21
|
const data = await serverClient.funFact({ year: born });
|
|
19
22
|
setAdditionalData(data);
|
|
20
23
|
} catch (err) {
|
|
@@ -28,6 +31,7 @@ const App: React.FC<InitialData> = (props: InitialData) => {
|
|
|
28
31
|
|
|
29
32
|
return (
|
|
30
33
|
<div className={styles.container}>
|
|
34
|
+
{/* Static assets are imported from ../public/ */}
|
|
31
35
|
<img width="50" height="50" src={logo} alt="Logo" />
|
|
32
36
|
<h1 style={{ margin: '0 0 20px 0' }}>
|
|
33
37
|
Hello, <span style={{ textDecoration: 'underline' }}>{firstName}</span>{' '}
|
|
@@ -47,4 +51,5 @@ const App: React.FC<InitialData> = (props: InitialData) => {
|
|
|
47
51
|
);
|
|
48
52
|
};
|
|
49
53
|
|
|
54
|
+
// Default export is required for OC build process
|
|
50
55
|
export default App;
|
|
@@ -12,19 +12,24 @@ const yearsFunFactDatabase: Record<number, string> = {
|
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
async function getUser(userId: number) {
|
|
15
|
+
if (userId < 0 || userId >= userDatabase.length) {
|
|
16
|
+
throw new Error(`User with ID ${userId} not found`);
|
|
17
|
+
}
|
|
15
18
|
return userDatabase[userId];
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
async function getFunFact(year: number) {
|
|
19
|
-
return yearsFunFactDatabase[year]
|
|
22
|
+
return yearsFunFactDatabase[year] || `No fun fact available for year ${year}`;
|
|
20
23
|
}
|
|
21
24
|
|
|
25
|
+
// Export the server instance - this is required for OC
|
|
22
26
|
export const server = new Server({
|
|
23
27
|
development: {
|
|
24
|
-
//
|
|
28
|
+
// Enable console logs from browser to server for debugging
|
|
25
29
|
console: true,
|
|
26
30
|
},
|
|
27
31
|
})
|
|
32
|
+
//Define URL parameters that can be passed to your component
|
|
28
33
|
.withParameters({
|
|
29
34
|
userId: {
|
|
30
35
|
default: 1,
|
|
@@ -34,15 +39,23 @@ export const server = new Server({
|
|
|
34
39
|
type: 'number',
|
|
35
40
|
},
|
|
36
41
|
})
|
|
37
|
-
|
|
42
|
+
// Main handler - returns initial data for component rendering
|
|
43
|
+
// This runs when the component is first loaded
|
|
44
|
+
.handler(async (params, ctx) => {
|
|
38
45
|
const { userId } = params;
|
|
39
46
|
const user = await getUser(userId);
|
|
40
47
|
const [firstName, lastName] = user.name.split(/\s+/);
|
|
41
48
|
|
|
42
49
|
if (firstName === 'Invalid') {
|
|
50
|
+
// Return undefined to prevent rendering
|
|
43
51
|
return;
|
|
44
52
|
}
|
|
45
53
|
|
|
54
|
+
// Set cache headers when same params produce same result
|
|
55
|
+
// This improves performance by reducing server load
|
|
56
|
+
ctx.setHeader('Cache-Control', 'max-age=300'); // Cache for 5 minutes
|
|
57
|
+
|
|
58
|
+
// Return data that will be passed as props to React component
|
|
46
59
|
return {
|
|
47
60
|
firstName,
|
|
48
61
|
lastName,
|
|
@@ -50,15 +63,17 @@ export const server = new Server({
|
|
|
50
63
|
hobbies: user.hobbies,
|
|
51
64
|
};
|
|
52
65
|
})
|
|
66
|
+
// Actions are server endpoints that the client can call
|
|
67
|
+
// Use these for user interactions, form submissions, etc.
|
|
53
68
|
.action('funFact', async (params: { year: number }) => {
|
|
54
69
|
const { year } = params;
|
|
55
70
|
const funFact = await getFunFact(year);
|
|
56
|
-
|
|
57
71
|
return {
|
|
58
72
|
funFact,
|
|
59
73
|
};
|
|
60
74
|
});
|
|
61
75
|
|
|
76
|
+
// This enables automatic type inference between server and client
|
|
62
77
|
declare module 'oc-server' {
|
|
63
78
|
interface Register {
|
|
64
79
|
server: typeof server;
|