create-oc 0.0.7 → 0.0.10

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": "create-oc",
3
- "version": "0.0.7",
3
+ "version": "0.0.10",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "index.js",
@@ -35,7 +35,7 @@
35
35
  "@types/react": "^18.0.28",
36
36
  "jsdom": "^21.1.1",
37
37
  "oc-server": "*",
38
- "oc-template-react-compiler": "6.0.0",
38
+ "oc-template-react-compiler": "^6.0.0",
39
39
  "react": "18.2.0",
40
40
  "react-dom": "18.2.0",
41
41
  "typescript": "5.1.3",
@@ -1,23 +1,21 @@
1
1
  import { useState } from 'react';
2
- import { useData } from 'oc-template-react-compiler/dist/utils/useData';
3
- import { serverClient } from 'oc-server';
2
+ import { serverClient, InitialData, ActionOutput } from 'oc-server';
4
3
  import styles from './styles.css';
5
4
  import logo from '../public/logo.png';
6
- import type { AdditionalData, ClientProps } from './types';
7
5
 
8
- interface AppProps extends ClientProps {
9
- getMoreData?: boolean;
10
- }
6
+ type AdditionalData = ActionOutput<'getMoreData'>;
11
7
 
12
- const App: React.FC<ClientProps> = () => {
13
- const { firstName, lastName, userId, getData } = useData<AppProps>();
14
- const [additionalData, setAdditionalData] = useState<AdditionalData | null>(null);
8
+ const App: React.FC<InitialData> = (props: InitialData) => {
9
+ const { firstName, lastName, born, hobbies } = props;
10
+ const [additionalData, setAdditionalData] = useState<AdditionalData | null>(
11
+ null
12
+ );
15
13
  const [error, setError] = useState('');
16
14
 
17
15
  const fetchMoreData = async () => {
18
16
  setError('');
19
17
  try {
20
- const data = await serverClient.getMoreData({ userId });
18
+ const data = await serverClient.getMoreData({ year: born });
21
19
  setAdditionalData(data);
22
20
  } catch (err) {
23
21
  setError(String(err));
@@ -32,18 +30,18 @@ const App: React.FC<ClientProps> = () => {
32
30
  <div className={styles.container}>
33
31
  <img width="50" height="50" src={logo} alt="Logo" />
34
32
  <h1 style={{ margin: '0 0 20px 0' }}>
35
- Hello, <span style={{ textDecoration: 'underline' }}>{firstName}</span> {lastName}
33
+ Hello, <span style={{ textDecoration: 'underline' }}>{firstName}</span>{' '}
34
+ {lastName}
36
35
  </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>
36
+ <div className={styles.info}>
37
+ <div className={styles.block}>Born: {born}</div>
38
+ <div className={styles.block}>
39
+ Hobbies: {hobbies.map((x) => x.toLowerCase()).join(', ')}
43
40
  </div>
44
- )}
41
+ </div>
42
+ {additionalData && <div>{additionalData.funFact}</div>}
45
43
  <button className={styles.button} onClick={fetchMoreData}>
46
- Get extra information
44
+ Fun year fact
47
45
  </button>
48
46
  </div>
49
47
  );
@@ -1,32 +1,45 @@
1
1
  import { Server } from 'oc-server';
2
- import { AdditionalData, ClientProps, OcParameters } from './types';
3
2
 
4
- const database = [
5
- { name: 'John Doe', age: 34, hobbies: ['Swimming', 'Basketball'] },
6
- { name: 'Jane Doe', age: 35, hobbies: ['Running', 'Rugby'] }
3
+ const userDatabase = [
4
+ { name: 'John Doe', born: 1986, hobbies: ['Swimming', 'Basketball'] },
5
+ { name: 'Jane Doe', born: 1991, hobbies: ['Running', 'Rugby'] },
7
6
  ];
8
7
 
8
+ const yearsFunFactDatabase: Record<number, string> = {
9
+ 1986: "Halley's Comet made its closest approach to Earth in 1986, visible to the naked eye.",
10
+ 1987: 'The first Final Fantasy game was released in 1987.',
11
+ 1991: 'The first web page was created in 1991 by Tim Berners-Lee.',
12
+ };
13
+
9
14
  async function getUser(userId: number) {
10
- return database[userId];
15
+ return userDatabase[userId];
16
+ }
17
+
18
+ async function getFunFact(year: number) {
19
+ return yearsFunFactDatabase[year];
11
20
  }
12
21
 
13
- export const server = new Server(async (params: OcParameters): Promise<ClientProps> => {
22
+ export const server = new Server(async (params: { userId: number }) => {
14
23
  const { userId } = params;
15
24
  const user = await getUser(userId);
16
25
  const [firstName, lastName] = user.name.split(/\s+/);
17
26
 
27
+ if (firstName === 'Invalid') {
28
+ return;
29
+ }
30
+
18
31
  return {
19
- userId,
20
32
  firstName,
21
- lastName
33
+ lastName,
34
+ born: user.born,
35
+ hobbies: user.hobbies,
22
36
  };
23
- }).action('getMoreData', async (params: OcParameters): Promise<AdditionalData> => {
24
- const { userId } = params;
25
- const user = await getUser(userId);
37
+ }).action('getMoreData', async (params: { year: number }) => {
38
+ const { year } = params;
39
+ const funFact = await getFunFact(year);
26
40
 
27
41
  return {
28
- age: user.age,
29
- hobbies: user.hobbies
42
+ funFact,
30
43
  };
31
44
  });
32
45
 
@@ -4,7 +4,7 @@
4
4
  "version": "1.0.0",
5
5
  "scripts": {
6
6
  "lint": "eslint --max-warnings 0 --ext .js,.jsx,.ts,.tsx src",
7
- "build": "tsc --noEmit && oc package .",
7
+ "build": "vue-tsc --noEmit && oc package .",
8
8
  "test": "vitest"
9
9
  },
10
10
  "oc": {
@@ -1,15 +0,0 @@
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
- }
@@ -1,38 +0,0 @@
1
- <script setup lang="ts">
2
- import { ref } from 'vue'
3
-
4
- defineProps<{ msg: string }>()
5
-
6
- const count = ref(0)
7
- </script>
8
-
9
- <template>
10
- <h1>{{ msg }}</h1>
11
-
12
- <div class="card">
13
- <button type="button" @click="count++">count is {{ count }}</button>
14
- <p>
15
- Edit
16
- <code>components/HelloWorld.vue</code> to test HMR
17
- </p>
18
- </div>
19
-
20
- <p>
21
- Check out
22
- <a href="https://vuejs.org/guide/quick-start.html#local" target="_blank"
23
- >create-vue</a
24
- >, the official Vue + Vite starter
25
- </p>
26
- <p>
27
- Install
28
- <a href="https://github.com/vuejs/language-tools" target="_blank">Volar</a>
29
- in your IDE for a better DX
30
- </p>
31
- <p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
32
- </template>
33
-
34
- <style scoped>
35
- .read-the-docs {
36
- color: #888;
37
- }
38
- </style>