Skip to content

Commit

Permalink
Move template to TypeScript (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyashton authored Jul 22, 2024
1 parent e88ddda commit fe30d2b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ $ /opt/ccf_virtual/bin/sandbox.sh --js-app-bundle ./js/dist/
In another terminal:

```bash
$ curl -X POST https://127.0.0.1:8000/app/log?id=1 --cacert ./workspace/sandbox_common/service_cert.pem -H "Content-Type: application/json" --data '{"msg": "hello world"}'
$ curl https://127.0.0.1:8000/app/log?id=1 --cacert ./workspace/sandbox_common/service_cert.pem
$ curl -X POST "https://127.0.0.1:8000/app/log?id=1" --cacert ./workspace/sandbox_common/service_cert.pem -H "Content-Type: application/json" --data '{"msg": "hello world"}'
$ curl "https://127.0.0.1:8000/app/log?id=1" --cacert ./workspace/sandbox_common/service_cert.pem
hello world
```

Expand Down
5 changes: 4 additions & 1 deletion js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-node-resolve": "^11.2.0",
"@rollup/plugin-typescript": "^11.1.6",
"del-cli": "^3.0.1",
"rollup": "^2.41.0"
"rollup": "^2.41.0",
"tslib": "^2.6.3",
"typescript": "^5.4.5"
}
}
9 changes: 7 additions & 2 deletions js/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";

export default {
input: "src/app.js",
input: "src/app.ts",
output: {
dir: "dist/src",
format: "es",
preserveModules: true,
preserveModulesRoot: "src",
},
plugins: [nodeResolve(), commonjs()],
plugins: [
nodeResolve(),
typescript({ compilerOptions: { noEmitOnError: true } }),
commonjs(),
],
};
39 changes: 0 additions & 39 deletions js/src/app.js

This file was deleted.

41 changes: 41 additions & 0 deletions js/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as ccfapp from "@microsoft/ccf-app";

function parse_request_query(request: ccfapp.Request): {
[key: string]: string;
} {
const elements = request.query.split("&");
const obj = {};
for (const kv of elements) {
const [k, v] = kv.split("=");
obj[k] = v;
}
return obj;
}

const recordsMap = ccfapp.typedKv("records", ccfapp.string, ccfapp.string);

export function write(request: ccfapp.Request): ccfapp.Response {
const parsedQuery = parse_request_query(request);
if (parsedQuery.id === undefined) {
return { body: { error: "Missing query parameter 'id'" } };
}
const params = request.body.json();

recordsMap.set(parsedQuery.id, params.msg);
return {};
}

export function read(request: ccfapp.Request): ccfapp.Response {
const parsedQuery = parse_request_query(request);
if (parsedQuery.id === undefined) {
return { body: { error: "Missing query parameter 'id'" } };
}

const msg = recordsMap.get(parsedQuery.id);
if (msg === undefined) {
return {
body: { error: `Cannot find record for id \"${parsedQuery.id}\".` },
};
}
return { body: msg };
}
14 changes: 14 additions & 0 deletions js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"lib": ["ES2020"],
"target": "ES2020",
"module": "ES2020",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": false
},
"include": ["src/**/*"]
}

0 comments on commit fe30d2b

Please sign in to comment.