瀏覽代碼

convert-to-typescript

kunickyd 3 年之前
父節點
當前提交
8bfe1cbbbc
共有 4 個文件被更改,包括 31 次插入13 次删除
  1. 4 1
      .gitignore
  2. 9 7
      app.ts
  3. 11 5
      package.json
  4. 7 0
      tsconfig.json

+ 4 - 1
.gitignore

@@ -128,4 +128,7 @@ dist
 
 package-lock.json
 .vscode
-.vs
+.vs
+
+#Build folder
+build

+ 9 - 7
app.js → app.ts

@@ -6,10 +6,12 @@ import basicAuth from "express-basic-auth"
 const app = express();
 app.use(bodyParser.json());
 app.use(basicAuth({
-    users : { 'admin' : 'superPasswd' },
-    challange : true
+    users: { admin: 'superPasswd' },
+    challenge: true
 }));
 
+
+
 const CHAIN4ALL_SERVICE_PORT = process.env.CHAIN4ALL_SERVICE_PORT || 3000;
 const IROHA_API_HOST = process.env.IROHA_API_HOST || "http://localhost";
 const IROHA_API_PORT = process.env.IROHA_API_PORT || 5000;
@@ -17,7 +19,7 @@ const IROHA_API_PORT = process.env.IROHA_API_PORT || 5000;
 const IROHA_DOMAIN = process.env.IROHA_DOMAIN || "test";
 const IROHA_ASSET = process.env.IROHA_ASSET || "coin";
 const DATA_OWNER = process.env.DATA_OWNER || "admin"
-const PRICE_MODIFIER = process.env.PRICE_MODIFIER || 0.5;
+const PRICE_MODIFIER: number = parseFloat(process.env.PRICE_MODIFIER || "0.5");
 
 app.get("/", (req, res) => {
     res.send("Chain4All Blockchain service");
@@ -44,14 +46,14 @@ app.post("/buy", async (req, res, next) => {
         if (!req.body.txHash) {
             res.status(400);
             throw Error(JSON.stringify({ error: { name: "Error, request body has no \"txHash\" property!" } }));
-        }                
+        }
     }
     catch (err) {
         next(err);
     }
 });
 
-function getArea(extent) {
+function getArea(extent: Array<Array<number>>) : number {
     let y1 = extent[0][1];
     let y4 = extent[3][1];
 
@@ -64,11 +66,11 @@ function getArea(extent) {
     return height * width;
 }
 
-function getPrice(extent) {
+function getPrice(extent: Array<Array<number>>) : number{
     return getArea(extent) * PRICE_MODIFIER;
 }
 
-function errorMiddleware(err, req, res, next) { //TODO: add custom Exception class
+function errorMiddleware(err: any, req: any, res: any, next: any): void { //TODO: add custom Exception class
     console.log(err);
     res.status(500);
     res.send(err.message);

+ 11 - 5
package.json

@@ -2,11 +2,11 @@
   "name": "chain4all",
   "version": "1.0.0",
   "description": "",
-  "main": "app.js",
-  "type": "module",
+  "main": "build/app.js",
   "scripts": {
-    "dev": "nodemon app.js",
-    "start": "node app.js"
+    "dev": "nodemon app.ts",
+    "start": "node build/app.js",
+    "build": "tsc --project ./"
   },
   "author": "Daniel Kunický",
   "license": "ISC",
@@ -17,7 +17,13 @@
     "express-basic-auth": "^1.2.1"
   },
   "devDependencies": {
-    "nodemon": "^2.0.15"
+    "@tsconfig/node10": "^1.0.8",
+    "@types/body-parser": "^1.19.2",
+    "@types/express": "^4.17.13",
+    "@types/node": "^17.0.16",
+    "nodemon": "^2.0.15",
+    "ts-node": "^10.5.0",
+    "typescript": "^4.5.5"
   },
   "nodemonConfig": {
     "ext": "js",

+ 7 - 0
tsconfig.json

@@ -0,0 +1,7 @@
+{
+    "extends": "@tsconfig/node10/tsconfig.json",    
+    "compilerOptions": {        
+        "rootDir": "./",
+        "outDir": "./build",       
+    }
+}