From c49af943d89e615e5ade910e004013afc273c5f2 Mon Sep 17 00:00:00 2001 From: Bart Huijgen Date: Thu, 22 Oct 2020 16:02:35 +0200 Subject: [PATCH] Change bluepritns app to typescript strict mode --- .../pages/[blueprint]/[blueprintId].tsx | 4 +- apps/blueprints/pages/api/image-to-gen.ts | 7 ++- apps/blueprints/pages/api/post-image.ts | 43 ------------------- apps/blueprints/src/CopyButton.tsx | 2 +- apps/blueprints/src/Pagination.tsx | 2 +- apps/blueprints/tsconfig.json | 1 - libs/web-utils/src/lib/web-utils.ts | 6 +-- 7 files changed, 12 insertions(+), 53 deletions(-) delete mode 100644 apps/blueprints/pages/api/post-image.ts diff --git a/apps/blueprints/pages/[blueprint]/[blueprintId].tsx b/apps/blueprints/pages/[blueprint]/[blueprintId].tsx index 267abbe..aad0af0 100644 --- a/apps/blueprints/pages/[blueprint]/[blueprintId].tsx +++ b/apps/blueprints/pages/[blueprint]/[blueprintId].tsx @@ -142,7 +142,7 @@ export const Index: NextPage = ({ {renderImage()} @@ -153,7 +153,7 @@ export const Index: NextPage = ({ > {blueprint_page.description_markdown} - {selected.type === "blueprint" && data && ( + {selected.type === "blueprint" && data?.blueprint && ( Entities for {BBCode.toReact(data.blueprint.label)}) as any} gridColumn={chakraResponsive({ mobile: "1", desktop: "1 / span 2" })} diff --git a/apps/blueprints/pages/api/image-to-gen.ts b/apps/blueprints/pages/api/image-to-gen.ts index 7728404..1a52a0d 100644 --- a/apps/blueprints/pages/api/image-to-gen.ts +++ b/apps/blueprints/pages/api/image-to-gen.ts @@ -25,6 +25,8 @@ const getOneMessage = async (): Promise => { const data = JSON.parse(message.data.toString()); const blueprint = await getBlueprintById(data.blueprintId); + if (!blueprint) return message.ack(); + if (await hasBlueprintImage(blueprint.image_hash)) { console.log(`Blueprint ${data.blueprintId} image already exists ${blueprint.image_hash}`); return message.ack(); @@ -43,8 +45,9 @@ const getOneMessage = async (): Promise => { const handler: NextApiHandler = async (req, res) => { // Allow the url to be used in the blueprint editor if ( - req.headers.origin === "https://teoxoy.github.io" || - req.headers.origin.startsWith("http://localhost") + req.headers.origin && + (req.headers.origin === "https://teoxoy.github.io" || + req.headers.origin.startsWith("http://localhost")) ) { res.setHeader("Access-Control-Allow-Origin", req.headers.origin); } diff --git a/apps/blueprints/pages/api/post-image.ts b/apps/blueprints/pages/api/post-image.ts deleted file mode 100644 index 3538d80..0000000 --- a/apps/blueprints/pages/api/post-image.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { getBlueprintById, saveBlueprintImage } from "@factorio-sites/database"; -// import imagemin from "imagemin"; -// import imageminWebp from "imagemin-webp"; -import { NextApiHandler } from "next"; - -const handler: NextApiHandler = async (req, res) => { - // Allow the url to be used in the blueprint editor - if ( - (req.method === "OPTIONS" || req.method === "POST") && - req.headers.origin === "https://teoxoy.github.io" - ) { - res.setHeader("Access-Control-Allow-Origin", "https://teoxoy.github.io"); - res.setHeader("Access-Control-Allow-Headers", "content-type"); - } - if (req.method === "OPTIONS") return res.status(200).end(); - else if (req.method !== "POST") { - return res.status(400).end("Only accepts POST"); - } - - if (req.body.blueprintId && req.body.image) { - console.log(`store image for blueprint ${req.body.blueprintId}`); - const buffer = Buffer.from(req.body.image, "base64"); - const buffermin = buffer; - // const buffermin = await imagemin.buffer(buffer, { - // plugins: [imageminWebp({ quality: 50 })], - // }); - - console.log("minified image buffer length", buffermin.byteLength); - - const blueprint = await getBlueprintById(req.body.blueprintId); - await saveBlueprintImage(blueprint.image_hash, buffermin); - console.log(`Successfuly saved image ${blueprint.image_hash}`); - - res.setHeader("Content-Type", "text/plain"); - res.status(200).end("ok"); - return; - } - - res.setHeader("Content-Type", "text/plain"); - res.status(400).end("no post body"); -}; - -export default handler; diff --git a/apps/blueprints/src/CopyButton.tsx b/apps/blueprints/src/CopyButton.tsx index 7f34f92..b5eaec4 100644 --- a/apps/blueprints/src/CopyButton.tsx +++ b/apps/blueprints/src/CopyButton.tsx @@ -12,7 +12,7 @@ export const CopyButton: React.FC & { content: str