@jsnote-zeina/local-api 1.0.0 → 1.0.2

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.
@@ -16,43 +16,61 @@ exports.createCellsRouter = void 0;
16
16
  const express_1 = __importDefault(require("express"));
17
17
  const promises_1 = __importDefault(require("fs/promises"));
18
18
  const path_1 = __importDefault(require("path"));
19
+ const defaultCells = [
20
+ {
21
+ content: 'JSNote-Zeina\n----------\nThis is an interactive coding environment. You can write Javascript, see it executed, and write coprehensive documentation using markdown.\n\n- Click any text cell (including this one) to edit it\n- The code in each code editor is all joined into one file. If you define a variable in cell #1, you can refer to it in any following code cell!\n- You can show any React component, string, number, or anything else by calling the `show `function. This is a function built into this environment. Call show multiple times to show multiple values\n- Re-order or delete cells using the buttons on the top right \n- Add new cells by hovering on the divider between each cell\n\nAll of your changes get saved to the file you opened Jbook with. So if you ran `npx jsnote-zeina serve test.js` , all of the text and code you write will be saved to the `test.js` file.',
22
+ type: 'text',
23
+ id: 'ohbrr',
24
+ },
25
+ {
26
+ content: "import { useState } from 'react';\r\nconst Counter = () => {\r\n const [count, setCount] = useState(0);\r\n return (\r\n <div>\r\n <button onClick={() => setCount(count + 1)}>Click </button>\r\n <h3>Count: {count} </h3>\r\n </div>\r\n );\r\n};\r\n\r\nshow(<Counter />);",
27
+ type: 'code',
28
+ id: 'yvorb',
29
+ },
30
+ {
31
+ content: 'const App = () => {\r\n return (\r\n <div>\r\n <h3> App says Hello </h3>\r\n <i> Counter component will be rendered below... </i>\r\n <hr />\r\n {/* Counter was declared in an earlier cell -\r\n We can reference it here! */}\r\n <Counter />\r\n </div>\r\n );\r\n};\r\n\r\nshow(<App />);',
32
+ type: 'code',
33
+ id: 'toeeq',
34
+ },
35
+ ];
19
36
  const createCellsRouter = (filename, dir) => {
20
37
  const router = express_1.default.Router();
21
38
  router.use(express_1.default.json());
22
39
  const fullPath = path_1.default.join(dir, filename);
40
+ const isLocalApiError = (err) => {
41
+ return typeof err === 'object' && err !== null && typeof err.code === 'string';
42
+ };
23
43
  router.get('/cells', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
24
- const isLocalApiError = (err) => {
25
- return typeof err.code === 'string';
26
- };
27
44
  try {
28
- // Read the file
29
45
  const result = yield promises_1.default.readFile(fullPath, { encoding: 'utf-8' });
30
46
  res.send(JSON.parse(result));
31
47
  }
32
48
  catch (err) {
33
- if (isLocalApiError(err)) {
34
- if (err.code === 'ENOENT') {
35
- // Create a file and add default cells
36
- yield promises_1.default.writeFile(fullPath, '[]', 'utf-8');
37
- res.send([]);
49
+ if (isLocalApiError(err) && err.code === 'ENOENT') {
50
+ try {
51
+ yield promises_1.default.writeFile(fullPath, JSON.stringify(defaultCells), 'utf-8');
52
+ res.send(defaultCells);
38
53
  }
39
- else {
40
- throw err;
54
+ catch (writeErr) {
55
+ console.error('Failed to seed notebook file:', writeErr);
56
+ res.status(500).send({ error: 'Failed to create notebook file' });
41
57
  }
58
+ return;
42
59
  }
60
+ console.error('Failed to read notebook file:', err);
61
+ res.status(500).send({ error: 'Failed to read notebook file' });
43
62
  }
44
- // If read throws an error
45
- // Insprect the error, see if it says that file doesn't exist
46
- // Parse a list of cells out of it
47
- // Send list of cells back to browser
48
63
  }));
49
64
  router.post('/cells', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
50
- // Take the list of cells from the request obj
51
- // Serialize them
52
- const { cells } = req.body;
53
- // Write cells into the file
54
- yield promises_1.default.writeFile(fullPath, JSON.stringify(cells), 'utf-8');
55
- res.send({ status: 'ok' });
65
+ try {
66
+ const { cells } = req.body;
67
+ yield promises_1.default.writeFile(fullPath, JSON.stringify(cells), 'utf-8');
68
+ res.send({ status: 'ok' });
69
+ }
70
+ catch (err) {
71
+ console.error('Failed to write notebook file:', err);
72
+ res.status(500).send({ error: 'Failed to save notebook file' });
73
+ }
56
74
  }));
57
75
  return router;
58
76
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsnote-zeina/local-api",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "files": [
6
6
  "dist"
@@ -23,10 +23,9 @@
23
23
  "typescript": "^5.4.5"
24
24
  },
25
25
  "dependencies": {
26
- "@jsnote-zeina/local-client": "^1.0.0",
26
+ "@jsnote-zeina/local-client": "^1.0.1",
27
27
  "cors": "^2.8.5",
28
28
  "express": "^4.19.2",
29
29
  "http-proxy-middleware": "^3.0.0"
30
- },
31
- "gitHead": "f1b8bb3eeaac9d9800122679a99164ffbb655bbe"
30
+ }
32
31
  }