oc-template-svelte-compiler 2.1.0 → 2.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-svelte-compiler",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Compiler for the Svelte OC template",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -30,7 +30,7 @@
30
30
  "dependencies": {
31
31
  "@sveltejs/vite-plugin-svelte": "^5.0.3",
32
32
  "oc-template-svelte": "^0.0.2",
33
- "oc-vite-compiler": "^5.1.0",
33
+ "oc-vite-compiler": "^5.1.1",
34
34
  "svelte": "^5.23.0"
35
35
  },
36
36
  "files": [
@@ -27,8 +27,8 @@
27
27
  "@tsconfig/svelte": "^5.0.4",
28
28
  "@types/testing-library__jest-dom": "^5.14.5",
29
29
  "jsdom": "^21.1.1",
30
- "oc-server": "^3.0.0",
31
- "oc-template-svelte-compiler": "^2.1.0",
30
+ "oc-server": "^3.1.0",
31
+ "oc-template-svelte-compiler": "^2.1.1",
32
32
  "svelte": "^5.23.0",
33
33
  "svelte-check": "^4.0.0",
34
34
  "typescript": "5.0.2",
@@ -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
- // Echo console logs from the browser to the server
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
- .handler(async (params) => {
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;
@@ -1,4 +0,0 @@
1
- export default function vueOCProviderTemplate({ viewPath, providerFunctions }: {
2
- viewPath: string;
3
- providerFunctions: string;
4
- }): string;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- function vueOCProviderTemplate({ viewPath, providerFunctions }) {
4
- return `
5
- import { createApp } from 'vue'
6
- import View from '${viewPath}';
7
-
8
- function renderer(props, element, ssr) {
9
- const { _staticPath, _baseUrl, _componentName, _componentVersion, ...rest } = props;
10
- ${providerFunctions}
11
-
12
- rest.getData = getData;
13
- rest.getSetting = getSetting;
14
- createApp(View, rest).mount(element, ssr);
15
- }
16
-
17
- renderer.component = View;
18
-
19
- export default renderer;
20
- `;
21
- }
22
- exports.default = vueOCProviderTemplate;