@jsnote-zeina/local-api 1.0.0
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/dist/index.d.ts +1 -0
- package/dist/index.js +37 -0
- package/dist/routes/cells.d.ts +1 -0
- package/dist/routes/cells.js +59 -0
- package/package.json +32 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const serve: (port: number, filename: string, dir: string, useProxy: boolean) => Promise<void>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.serve = void 0;
|
|
7
|
+
const express_1 = __importDefault(require("express"));
|
|
8
|
+
const http_proxy_middleware_1 = require("http-proxy-middleware");
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const cells_1 = require("./routes/cells");
|
|
11
|
+
const serve = (port, filename, dir, useProxy) => {
|
|
12
|
+
const app = (0, express_1.default)();
|
|
13
|
+
app.use((0, cells_1.createCellsRouter)(filename, dir));
|
|
14
|
+
if (useProxy) {
|
|
15
|
+
app.use((0, http_proxy_middleware_1.createProxyMiddleware)({
|
|
16
|
+
target: 'http://127.0.0.1:3000',
|
|
17
|
+
ws: true, //websocket support
|
|
18
|
+
}));
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
const packagePath = require.resolve('@jsnote-zeina/local-client/build/index.html'); // gets absolute path on user's local machine
|
|
22
|
+
app.use(express_1.default.static(path_1.default.dirname(packagePath)));
|
|
23
|
+
}
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
app.listen(port, resolve).on('error', reject);
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
exports.serve = serve;
|
|
29
|
+
/* if express server starts successfully, resolve function is called and promise is finished
|
|
30
|
+
if something fails, reject function will be called with an error state
|
|
31
|
+
|
|
32
|
+
added useProxy boolean to seperate the two cases where our execution environment is being used
|
|
33
|
+
case 1: we are in development mode, constantly developing the app, in this case, we want to see
|
|
34
|
+
the changes done on the app instantly so we use the proxy method
|
|
35
|
+
case 2: we are in production mode, user won't have access to a current react app running so we
|
|
36
|
+
reference production build using express static
|
|
37
|
+
*/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const createCellsRouter: (filename: string, dir: string) => import("express-serve-static-core").Router;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.createCellsRouter = void 0;
|
|
16
|
+
const express_1 = __importDefault(require("express"));
|
|
17
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
18
|
+
const path_1 = __importDefault(require("path"));
|
|
19
|
+
const createCellsRouter = (filename, dir) => {
|
|
20
|
+
const router = express_1.default.Router();
|
|
21
|
+
router.use(express_1.default.json());
|
|
22
|
+
const fullPath = path_1.default.join(dir, filename);
|
|
23
|
+
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
|
+
try {
|
|
28
|
+
// Read the file
|
|
29
|
+
const result = yield promises_1.default.readFile(fullPath, { encoding: 'utf-8' });
|
|
30
|
+
res.send(JSON.parse(result));
|
|
31
|
+
}
|
|
32
|
+
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([]);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
throw err;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
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
|
+
}));
|
|
49
|
+
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' });
|
|
56
|
+
}));
|
|
57
|
+
return router;
|
|
58
|
+
};
|
|
59
|
+
exports.createCellsRouter = createCellsRouter;
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jsnote-zeina/local-api",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"types": "dist/index.d.ts",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"start": "tsc --watch --preserveWatchOutput",
|
|
15
|
+
"prepublishOnly": "tsc"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [],
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/cors": "^2.8.17",
|
|
22
|
+
"@types/express": "^4.17.21",
|
|
23
|
+
"typescript": "^5.4.5"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@jsnote-zeina/local-client": "^1.0.0",
|
|
27
|
+
"cors": "^2.8.5",
|
|
28
|
+
"express": "^4.19.2",
|
|
29
|
+
"http-proxy-middleware": "^3.0.0"
|
|
30
|
+
},
|
|
31
|
+
"gitHead": "f1b8bb3eeaac9d9800122679a99164ffbb655bbe"
|
|
32
|
+
}
|