From 54aee58377b0198b6391687f5879933c538e6521 Mon Sep 17 00:00:00 2001 From: Reza Rahemtola Date: Fri, 19 May 2023 20:53:11 +0200 Subject: [PATCH] refactor: GitHub program deployment typed with Zod instead of Joi Useless dependencies removed --- package.json | 6 +-- pages/api/program/create.ts | 34 ++++++++--------- src/lib/middlewares/validation.ts | 7 ---- yarn.lock | 62 +++---------------------------- 4 files changed, 24 insertions(+), 85 deletions(-) delete mode 100644 src/lib/middlewares/validation.ts diff --git a/package.json b/package.json index a2de316d..584c88fd 100644 --- a/package.json +++ b/package.json @@ -19,16 +19,14 @@ "ethers": "^5.7.2", "framer-motion": "^10.12.12", "git-clone": "^0.2.0", - "joi": "^17.9.2", "js-file-download": "^0.4.12", "next": "^12.3.4", "next-auth": "4.10.3", - "next-connect": "^1.0.0-next.3", - "next-joi": "^2.2.1", "react": "^18.2.0", "react-dom": "^18.2.0", "react-icons": "^4.8.0", - "typescript": "^5.0.4" + "typescript": "^5.0.4", + "zod": "^3.21.4" }, "scripts": { "dev": "next dev", diff --git a/pages/api/program/create.ts b/pages/api/program/create.ts index 9cee6800..168bd83c 100644 --- a/pages/api/program/create.ts +++ b/pages/api/program/create.ts @@ -1,24 +1,24 @@ -import Joi from 'joi'; -import type { NextApiRequest, NextApiResponse } from 'next'; -import { createRouter } from 'next-connect'; +import z from 'zod'; -import validate from 'lib/middlewares/validation'; import deploy from 'lib/services/deploy'; import { clone, getProgramName } from 'lib/services/git'; +import { NextApiRequest, NextApiResponse } from 'next'; -const router = createRouter(); - -const postSchema = Joi.object({ +const postSchema = z.object({ // eslint-disable-next-line no-useless-escape - repository: Joi.string().pattern(/((git|http(s)?)|(git@[\w\.]+))(:(\/\/)?)([\w\.@\:\/\-~]+)(\.git)(\/)?/), - entrypoint: Joi.string(), -}); - -router.post(validate({ body: postSchema }), async (req, res) => { - const { repository, entrypoint } = req.body; - const path = await clone(repository); - const itemHash = await deploy(path, entrypoint); - return res.status(200).json({ name: getProgramName(repository), item_hash: itemHash, entrypoint }); + repository: z.string().regex(/((git|http(s)?)|(git@[\w\.]+))(:(\/\/)?)([\w\.@\:\/\-~]+)(\.git)(\/)?/), + entrypoint: z.string(), }); -export default router.handler(); +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + if (req.method !== 'POST') throw new Error('Method not allowed'); + const body = postSchema.parse(req.body); + const { repository, entrypoint } = body; + const path = await clone(repository); + const itemHash = await deploy(path, entrypoint); + return res.status(200).json({ name: getProgramName(repository), item_hash: itemHash, entrypoint }); + } catch (error) { + return res.status(400).end('Bad request'); + } +} diff --git a/src/lib/middlewares/validation.ts b/src/lib/middlewares/validation.ts deleted file mode 100644 index aa5ac8a7..00000000 --- a/src/lib/middlewares/validation.ts +++ /dev/null @@ -1,7 +0,0 @@ -import withJoi from 'next-joi'; - -export default withJoi({ - onValidationError: (_, res) => { - res.status(400).end('Bad request'); - }, -}); diff --git a/yarn.lock b/yarn.lock index a8ed7548..55175e2c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1757,18 +1757,6 @@ "@ethersproject/properties" "^5.7.0" "@ethersproject/strings" "^5.7.0" -"@hapi/hoek@^9.0.0": - version "9.3.0" - resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.3.0.tgz#8368869dcb735be2e7f5cb7647de78e167a251fb" - integrity sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ== - -"@hapi/topo@^5.0.0": - version "5.1.0" - resolved "https://registry.yarnpkg.com/@hapi/topo/-/topo-5.1.0.tgz#dc448e332c6c6e37a4dc02fd84ba8d44b9afb012" - integrity sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg== - dependencies: - "@hapi/hoek" "^9.0.0" - "@humanwhocodes/config-array@^0.11.8": version "0.11.8" resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.11.8.tgz#03595ac2075a4dc0f191cc2131de14fbd7d410b9" @@ -2190,23 +2178,6 @@ "@noble/hashes" "~1.2.0" "@scure/base" "~1.1.0" -"@sideway/address@^4.1.3": - version "4.1.4" - resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.4.tgz#03dccebc6ea47fdc226f7d3d1ad512955d4783f0" - integrity sha512-7vwq+rOHVWjyXxVlR76Agnvhy8I9rpzjosTESvmhNeXOXdZZB15Fl+TI9x1SiHZH5Jv2wTGduSxFDIaq0m3DUw== - dependencies: - "@hapi/hoek" "^9.0.0" - -"@sideway/formula@^3.0.1": - version "3.0.1" - resolved "https://registry.yarnpkg.com/@sideway/formula/-/formula-3.0.1.tgz#80fcbcbaf7ce031e0ef2dd29b1bfc7c3f583611f" - integrity sha512-/poHZJJVjx3L+zVD6g9KgHfYnb443oi7wLu/XKojDviHy6HOEOA6z1Trk5aR1dGcmPenJEgb2sK2I80LeS3MIg== - -"@sideway/pinpoint@^2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@sideway/pinpoint/-/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df" - integrity sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ== - "@solana/buffer-layout@^4.0.0": version "4.0.1" resolved "https://registry.yarnpkg.com/@solana/buffer-layout/-/buffer-layout-4.0.1.tgz#b996235eaec15b1e0b5092a8ed6028df77fa6c15" @@ -5389,17 +5360,6 @@ jayson@^3.4.4: uuid "^8.3.2" ws "^7.4.5" -joi@^17.9.2: - version "17.9.2" - resolved "https://registry.yarnpkg.com/joi/-/joi-17.9.2.tgz#8b2e4724188369f55451aebd1d0b1d9482470690" - integrity sha512-Itk/r+V4Dx0V3c7RLFdRh12IOjySm2/WGPMubBT92cQvRfYZhPM2W0hZlctjj72iES8jsRCwp7S/cRmWBnJ4nw== - dependencies: - "@hapi/hoek" "^9.0.0" - "@hapi/topo" "^5.0.0" - "@sideway/address" "^4.1.3" - "@sideway/formula" "^3.0.1" - "@sideway/pinpoint" "^2.0.0" - jose@^4.14.1, jose@^4.3.7: version "4.14.4" resolved "https://registry.yarnpkg.com/jose/-/jose-4.14.4.tgz#59e09204e2670c3164ee24cbfe7115c6f8bff9ca" @@ -5786,18 +5746,6 @@ next-auth@4.10.3: preact-render-to-string "^5.1.19" uuid "^8.3.2" -next-connect@^1.0.0-next.3: - version "1.0.0-next.3" - resolved "https://registry.yarnpkg.com/next-connect/-/next-connect-1.0.0-next.3.tgz#d7df4ee2c54bb1808f273e1ebec308e2ffb553d2" - integrity sha512-i1kb8rz/3lm6z68Lnh18juHGgbgFVZXXAIiElaASGXxDZ8mJ2EVxdbTXX8NiF9BbGDBeao7u6uKMqw5ZLZg/Kg== - dependencies: - regexparam "^2.0.1" - -next-joi@^2.2.1: - version "2.2.1" - resolved "https://registry.yarnpkg.com/next-joi/-/next-joi-2.2.1.tgz#bd690b5eba02d6d5d8bae76a2f402fb11843eca8" - integrity sha512-m6/rDj9a9sp0CeMGy3np/7T2663QFinfiTY4MuJ9LEicU+6SiDim4wnsqG5CfzI4IQX4tupN6jSCtsv0t2EWnQ== - next@^12.3.4: version "12.3.4" resolved "https://registry.yarnpkg.com/next/-/next-12.3.4.tgz#f2780a6ebbf367e071ce67e24bd8a6e05de2fcb1" @@ -6373,11 +6321,6 @@ regexp.prototype.flags@^1.4.3: define-properties "^1.1.3" functions-have-names "^1.2.2" -regexparam@^2.0.1: - version "2.0.1" - resolved "https://registry.yarnpkg.com/regexparam/-/regexparam-2.0.1.tgz#c912f5dae371e3798100b3c9ce22b7414d0889fa" - integrity sha512-zRgSaYemnNYxUv+/5SeoHI0eJIgTL/A2pUtXUPLHQxUldagouJ9p+K6IbIZ/JiQuCEv2E2B1O11SjVQy3aMCkw== - request-progress@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/request-progress/-/request-progress-3.0.0.tgz#4ca754081c7fec63f505e4faa825aa06cd669dbe" @@ -7254,3 +7197,8 @@ yocto-queue@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b" integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q== + +zod@^3.21.4: + version "3.21.4" + resolved "https://registry.yarnpkg.com/zod/-/zod-3.21.4.tgz#10882231d992519f0a10b5dd58a38c9dabbb64db" + integrity sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==