oc-template-preact-compiler 3.0.1 → 3.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-preact-compiler",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "Compiler for the Preact OC template",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@preact/preset-vite": "^2.7.0",
|
|
32
32
|
"oc-template-preact": "^0.1.1",
|
|
33
|
-
"oc-vite-compiler": "^5.
|
|
33
|
+
"oc-vite-compiler": "^5.1.1",
|
|
34
34
|
"preact": "^10"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"@testing-library/react": "^14.0.0",
|
|
27
27
|
"@testing-library/user-event": "^14.4.3",
|
|
28
28
|
"jsdom": "^21.1.1",
|
|
29
|
-
"oc-server": "^3.
|
|
30
|
-
"oc-template-preact-compiler": "^3.
|
|
29
|
+
"oc-server": "^3.1.0",
|
|
30
|
+
"oc-template-preact-compiler": "^3.1.1",
|
|
31
31
|
"preact": "^10.13.2",
|
|
32
32
|
"typescript": "5.0.2",
|
|
33
33
|
"vitest": "^0.29.7"
|
|
@@ -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;
|