Skip to content

Commit

Permalink
Merge pull request #11 from teleporthq/feat/upload-project-uidl
Browse files Browse the repository at this point in the history
Feature to upload project uidl
  • Loading branch information
Utwo committed Mar 18, 2021
2 parents a8a6459 + 758add4 commit ebf03f1
Show file tree
Hide file tree
Showing 6 changed files with 730 additions and 454 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## REPL BACK-END
## REPL BACKEND

Helps in sharing [UIDL](https://docs.teleporthq.io/#uidl) that is generated from [teleport-repl](https://repl.teleporthq.io/).
Helps in sharing [UIDL](https://docs.teleporthq.io/#uidl) that is generated from [teleport-repl](https://repl.teleporthq.io/).

## Running

Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
"build": "rimraf dist && tsc"
},
"dependencies": {
"@google-cloud/storage": "^3.0.2",
"body-parser": "^1.19.0",
"express": "^4.17.1"
"@google-cloud/storage": "^5.8.1",
"express": "^4.17.1",
"uuid": "^8.3.2",
"@teleporthq/teleport-uidl-validator": "^0.16.0"
},
"devDependencies": {
"rimraf": "^2.6.3",
"ts-node-dev": "^1.0.0-pre.40",
"rimraf": "^3.0.2",
"ts-node-dev": "^1.1.6",
"tslint-config-prettier": "^1.18.0",
"tslint": "^5.18.0",
"@types/express": "^4.17.0",
"typescript": "^3.5.2"
"tslint": "^6.1.3",
"@types/express": "^4.17.11",
"typescript": "^4.2.3"
}
}
6 changes: 4 additions & 2 deletions src/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class GoogleCloud {
const bufferStream = Buffer.from(uidl);
await file.save(bufferStream, {
metadata: {
gzip: true,
contentType: APPLICATION_TYPE,
cacheControl: CACHE_CONTROL
}
cacheControl: CACHE_CONTROL,
},
resumable: false,
});
return file;
} catch (e) {
Expand Down
7 changes: 0 additions & 7 deletions src/helper.ts

This file was deleted.

47 changes: 29 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import express from "express";
import bodyParser from "body-parser";
import { v4 as uuidv4 } from "uuid";
import GoogleCloud from "./cloud";
import { getFileName } from "./helper";
import { Validator } from "@teleporthq/teleport-uidl-validator";

const port = process.env.PORT || 8080;
const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true, limit: "2mb" }));
app.use(express.json());
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
Expand All @@ -20,38 +19,50 @@ app.use((req, res, next) => {
const googleCloud = new GoogleCloud();

app.post("/upload-uidl", async (req, res) => {
const { uidl } = req.body;
const {
type,
uidl,
}: {
type: "component" | "project";
uidl: Record<string, unknown>;
} = req.body;

if (!["project", "component"].includes(type)) {
return res
.status(400)
.json({ message: "Type must be project or component" });
}
if (!uidl) {
return res.status(400).json({ message: "UIDL missing from the request" });
}

const validator = new Validator();
const validateFunction =
type === "project"
? validator.validateProjectSchema
: validator.validateComponentSchema;

try {
JSON.parse(uidl);
if (typeof uidl !== "string") {
throw new Error("Requested UIDL is not a string");
}
} catch (e) {
console.error(e);
return res
.status(400)
.json({ message: "Please send a properly structured UIDL" });
validateFunction(uidl);
} catch (error) {
return res.status(400).json({ message: error.message });
}

const fileName = getFileName();
try {
await googleCloud.uploadUIDL(uidl, fileName);
const fileName = uuidv4();
await googleCloud.uploadUIDL(JSON.stringify(uidl), fileName);
return res
.status(200)
.json({ message: "UIDL saved successfully", fileName });
} catch (e) {
console.error(e);
} catch (error) {
console.error(error);
return res.status(500).json({ message: "Failed in saving UIDL" });
}
});

app.get("/fetch-uidl/:fileName", async (req, res) => {
const { fileName } = req.params;

if (!fileName) {
return res
.status(400)
Expand Down

0 comments on commit ebf03f1

Please sign in to comment.