From 22b5a08b102723d2306bf7d02e772437762d55bc Mon Sep 17 00:00:00 2001 From: gtktsc Date: Thu, 10 Feb 2022 20:47:40 +0100 Subject: [PATCH] ascii-website: Add API --- README.md | 7 ++- package.json | 6 +-- pages/_app.tsx | 8 --- pages/api/index.ts | 19 +++++++ pages/index.tsx | 45 +++++++---------- styles/globals.css | 6 --- yarn.lock | 120 ++++++++++++++++++++++----------------------- 7 files changed, 104 insertions(+), 107 deletions(-) delete mode 100644 pages/_app.tsx create mode 100644 pages/api/index.ts delete mode 100644 styles/globals.css diff --git a/README.md b/README.md index 2dc67b5..9ef8109 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ -## Simple ascii chart +## Simple ascii chart website -Website +Either go directly to: +`https://simple-ascii-chart.vercel.app/` +or hit API like: +`https://simple-ascii-chart.vercel.app/api?input=[[1,2],[2,3],[3,4]]&settings={%22width%22:50}` diff --git a/package.json b/package.json index 22acc10..43f7d71 100644 --- a/package.json +++ b/package.json @@ -12,11 +12,11 @@ "next": "12.0.10", "react": "17.0.2", "react-dom": "17.0.2", - "simple-ascii-chart": "^2.0.1" + "simple-ascii-chart": "^2.0.3" }, "devDependencies": { - "@types/node": "17.0.14", - "@types/react": "17.0.38", + "@types/node": "17.0.17", + "@types/react": "17.0.39", "eslint": "8.8.0", "eslint-config-next": "12.0.10", "typescript": "4.5.5" diff --git a/pages/_app.tsx b/pages/_app.tsx deleted file mode 100644 index 3f5c9d5..0000000 --- a/pages/_app.tsx +++ /dev/null @@ -1,8 +0,0 @@ -import '../styles/globals.css' -import type { AppProps } from 'next/app' - -function MyApp({ Component, pageProps }: AppProps) { - return -} - -export default MyApp diff --git a/pages/api/index.ts b/pages/api/index.ts new file mode 100644 index 0000000..7b7ffa6 --- /dev/null +++ b/pages/api/index.ts @@ -0,0 +1,19 @@ +import { NextApiRequest, NextApiResponse } from "next"; +import plot from "simple-ascii-chart"; + +export default function handler( + { query }: NextApiRequest, + res: NextApiResponse +) { + try { + const { input, settings } = query; + const parsedInput = JSON.parse(input as string); + + const parsedSettings = + (settings && JSON.parse(settings as string)) || undefined; + + res.status(200).json(plot(parsedInput, parsedSettings)); + } catch (error) { + res.status(400).send("Unable to parse data"); + } +} diff --git a/pages/index.tsx b/pages/index.tsx index d80d302..2467996 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -3,7 +3,7 @@ import plot from "simple-ascii-chart"; const Home = () => { const [error, setError] = useState(""); - const [size, setSize] = useState({ width: 20, height: 20 }); + const [settings, setSettings] = useState("" as string | void); const [data, setData] = useState([ [1, 1], [2, -20], @@ -13,8 +13,17 @@ const Home = () => { [6, 10], ] as Array<[x: number, y: number]>); - const chart = - (!error && plot(data, { width: size.width, height: size.height })) || ""; + // lord please forgive me + const getSettings = (obj: string) => { + try { + return Function('"use strict";return (' + obj + ")")(); + } catch ({ message }: unknown) { + setError(message as string); + } + }; + + const executed = (settings && getSettings(settings)) || ""; + const chart = (!error && plot(data, executed)) || ""; const runChart = ( event: KeyboardEvent, @@ -50,39 +59,19 @@ const Home = () => { > {JSON.stringify(data)} - {`, { width: `} + {`, `} { runChart(event, () => { const { target } = event; - setSize({ - width: Number( - (target as HTMLSpanElement)?.textContent || undefined - ), - height: size.height, - }); - }); - }} - > - {size.width || "-"} - - {`, height: `} - { - runChart(event, () => { - const { target } = event; - setSize({ - height: Number( - (target as HTMLSpanElement)?.textContent || undefined - ), - width: size.width, - }); + setSettings( + (target as HTMLSpanElement)?.textContent || undefined + ); }); }} > - {size.height || "-"} + {settings?.toString() || "undefined"} {`})`} diff --git a/styles/globals.css b/styles/globals.css deleted file mode 100644 index 0df4aeb..0000000 --- a/styles/globals.css +++ /dev/null @@ -1,6 +0,0 @@ -pre { - background-color: #e7e7e7; - font-family: monospace; - white-space: pre; - display: inline; -} diff --git a/yarn.lock b/yarn.lock index 7bd72b7..8866f36 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3,17 +3,17 @@ "@babel/runtime-corejs3@^7.10.2": - version "7.16.8" - resolved "https://registry.yarnpkg.com/@babel/runtime-corejs3/-/runtime-corejs3-7.16.8.tgz#ea533d96eda6fdc76b1812248e9fbd0c11d4a1a7" - integrity sha512-3fKhuICS1lMz0plI5ktOE/yEtBRMVxplzRkdn6mJQ197XiY0JnrzYV0+Mxozq3JZ8SBV9Ecurmw1XsGbwOf+Sg== + version "7.17.2" + resolved "https://registry.yarnpkg.com/@babel/runtime-corejs3/-/runtime-corejs3-7.17.2.tgz#fdca2cd05fba63388babe85d349b6801b008fd13" + integrity sha512-NcKtr2epxfIrNM4VOmPKO46TvDMCBhgi2CrSHaEarrz+Plk2K5r9QemmOFTGpZaoKnWoGH5MO+CzeRsih/Fcgg== dependencies: core-js-pure "^3.20.2" regenerator-runtime "^0.13.4" "@babel/runtime@^7.10.2", "@babel/runtime@^7.16.3": - version "7.16.7" - resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.16.7.tgz#03ff99f64106588c9c403c6ecb8c3bafbbdff1fa" - integrity sha512-9E9FJowqAsytyOY6LG+1KuueckRL+aQW+mKvXRXnuFGyRAyepJPmEo9vgMfXUA6O9u3IeEdv9MAkppFcaQwogQ== + version "7.17.2" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.17.2.tgz#66f68591605e59da47523c631416b18508779941" + integrity sha512-hzeyJyMA1YGdJTuWU0e/j4wKXrU4OMFvY2MSlaI9B7VQb0r5cxTE3EAIS2Q7Tn2RIcDkRvTA/v2JsAEhxe99uw== dependencies: regenerator-runtime "^0.13.4" @@ -144,20 +144,20 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4= -"@types/node@17.0.14": - version "17.0.14" - resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.14.tgz#33b9b94f789a8fedd30a68efdbca4dbb06b61f20" - integrity sha512-SbjLmERksKOGzWzPNuW7fJM7fk3YXVTFiZWB/Hs99gwhk+/dnrQRPBQjPW9aO+fi1tAffi9PrwFvsmOKmDTyng== +"@types/node@17.0.17": + version "17.0.17" + resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.17.tgz#a8ddf6e0c2341718d74ee3dc413a13a042c45a0c" + integrity sha512-e8PUNQy1HgJGV3iU/Bp2+D/DXh3PYeyli8LgIwsQcs1Ar1LoaWHSIT6Rw+H2rNJmiq6SNWiDytfx8+gYj7wDHw== "@types/prop-types@*": version "15.7.4" resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.4.tgz#fcf7205c25dff795ee79af1e30da2c9790808f11" integrity sha512-rZ5drC/jWjrArrS8BR6SIr4cWpW09RNTYt9AMZo3Jwwif+iacXAqgVjm0B0Bv/S1jhDXKHqRVNCbACkJ89RAnQ== -"@types/react@17.0.38": - version "17.0.38" - resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.38.tgz#f24249fefd89357d5fa71f739a686b8d7c7202bd" - integrity sha512-SI92X1IA+FMnP3qM5m4QReluXzhcmovhZnLNm3pyeQlooi02qI7sLiepEYqT678uNiyc25XfCqxREFpy3W7YhQ== +"@types/react@17.0.39": + version "17.0.39" + resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.39.tgz#d0f4cde092502a6db00a1cded6e6bf2abb7633ce" + integrity sha512-UVavlfAxDd/AgAacMa60Azl7ygyQNRwC/DsHZmKgNvPmRR5p70AJ5Q9EAmL2NWOJmeV+vVUI4IAP7GZrN8h8Ug== dependencies: "@types/prop-types" "*" "@types/scheduler" "*" @@ -169,47 +169,47 @@ integrity sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew== "@typescript-eslint/parser@^5.0.0": - version "5.10.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.10.2.tgz#b6076d27cc5499ce3f2c625f5ccde946ecb7db9a" - integrity sha512-JaNYGkaQVhP6HNF+lkdOr2cAs2wdSZBoalE22uYWq8IEv/OVH0RksSGydk+sW8cLoSeYmC+OHvRyv2i4AQ7Czg== + version "5.11.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.11.0.tgz#b4fcaf65513f9b34bdcbffdda055724a5efb7e04" + integrity sha512-x0DCjetHZYBRovJdr3U0zG9OOdNXUaFLJ82ehr1AlkArljJuwEsgnud+Q7umlGDFLFrs8tU8ybQDFocp/eX8mQ== dependencies: - "@typescript-eslint/scope-manager" "5.10.2" - "@typescript-eslint/types" "5.10.2" - "@typescript-eslint/typescript-estree" "5.10.2" + "@typescript-eslint/scope-manager" "5.11.0" + "@typescript-eslint/types" "5.11.0" + "@typescript-eslint/typescript-estree" "5.11.0" debug "^4.3.2" -"@typescript-eslint/scope-manager@5.10.2": - version "5.10.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.10.2.tgz#92c0bc935ec00f3d8638cdffb3d0e70c9b879639" - integrity sha512-39Tm6f4RoZoVUWBYr3ekS75TYgpr5Y+X0xLZxXqcZNDWZdJdYbKd3q2IR4V9y5NxxiPu/jxJ8XP7EgHiEQtFnw== +"@typescript-eslint/scope-manager@5.11.0": + version "5.11.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.11.0.tgz#f5aef83ff253f457ecbee5f46f762298f0101e4b" + integrity sha512-z+K4LlahDFVMww20t/0zcA7gq/NgOawaLuxgqGRVKS0PiZlCTIUtX0EJbC0BK1JtR4CelmkPK67zuCgpdlF4EA== dependencies: - "@typescript-eslint/types" "5.10.2" - "@typescript-eslint/visitor-keys" "5.10.2" + "@typescript-eslint/types" "5.11.0" + "@typescript-eslint/visitor-keys" "5.11.0" -"@typescript-eslint/types@5.10.2": - version "5.10.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.10.2.tgz#604d15d795c4601fffba6ecb4587ff9fdec68ce8" - integrity sha512-Qfp0qk/5j2Rz3p3/WhWgu4S1JtMcPgFLnmAKAW061uXxKSa7VWKZsDXVaMXh2N60CX9h6YLaBoy9PJAfCOjk3w== +"@typescript-eslint/types@5.11.0": + version "5.11.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.11.0.tgz#ba345818a2540fdf2755c804dc2158517ab61188" + integrity sha512-cxgBFGSRCoBEhvSVLkKw39+kMzUKHlJGVwwMbPcTZX3qEhuXhrjwaZXWMxVfxDgyMm+b5Q5b29Llo2yow8Y7xQ== -"@typescript-eslint/typescript-estree@5.10.2": - version "5.10.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.10.2.tgz#810906056cd3ddcb35aa333fdbbef3713b0fe4a7" - integrity sha512-WHHw6a9vvZls6JkTgGljwCsMkv8wu8XU8WaYKeYhxhWXH/atZeiMW6uDFPLZOvzNOGmuSMvHtZKd6AuC8PrwKQ== +"@typescript-eslint/typescript-estree@5.11.0": + version "5.11.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.11.0.tgz#53f9e09b88368191e52020af77c312a4777ffa43" + integrity sha512-yVH9hKIv3ZN3lw8m/Jy5I4oXO4ZBMqijcXCdA4mY8ull6TPTAoQnKKrcZ0HDXg7Bsl0Unwwx7jcXMuNZc0m4lg== dependencies: - "@typescript-eslint/types" "5.10.2" - "@typescript-eslint/visitor-keys" "5.10.2" + "@typescript-eslint/types" "5.11.0" + "@typescript-eslint/visitor-keys" "5.11.0" debug "^4.3.2" globby "^11.0.4" is-glob "^4.0.3" semver "^7.3.5" tsutils "^3.21.0" -"@typescript-eslint/visitor-keys@5.10.2": - version "5.10.2" - resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.10.2.tgz#fdbf272d8e61c045d865bd6c8b41bea73d222f3d" - integrity sha512-zHIhYGGGrFJvvyfwHk5M08C5B5K4bewkm+rrvNTKk1/S15YHR+SA/QUF8ZWscXSfEaB8Nn2puZj+iHcoxVOD/Q== +"@typescript-eslint/visitor-keys@5.11.0": + version "5.11.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.11.0.tgz#888542381f1a2ac745b06d110c83c0b261487ebb" + integrity sha512-E8w/vJReMGuloGxJDkpPlGwhxocxOpSVgSvjiLO5IxZPmxZF30weOeJYyPSEACwM+X4NziYS9q+WkN/2DHYQwA== dependencies: - "@typescript-eslint/types" "5.10.2" + "@typescript-eslint/types" "5.11.0" eslint-visitor-keys "^3.0.0" acorn-jsx@^5.3.1: @@ -297,9 +297,9 @@ ast-types-flow@^0.0.7: integrity sha1-9wtzXGvKGlycItmCw+Oef+ujva0= axe-core@^4.3.5: - version "4.4.0" - resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.4.0.tgz#f93be7f81017eb8bedeb1859cc8092cc918d2dc8" - integrity sha512-btWy2rze3NnxSSxb7LtNhPYYFrRoFBfjiGzmSc/5Hu47wApO2KNXjP/w7Nv2Uz/Fyr/pfEiwOkcXhDxu0jz5FA== + version "4.4.1" + resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.4.1.tgz#7dbdc25989298f9ad006645cd396782443757413" + integrity sha512-gd1kmb21kwNuWr6BQz8fv6GNECPBnUasepcoLbekws23NVBLODdsClRZ+bQ8+9Uomf3Sm3+Vwn0oYG9NvwnJCw== axobject-query@^2.2.0: version "2.2.0" @@ -340,9 +340,9 @@ callsites@^3.0.0: integrity sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ== caniuse-lite@^1.0.30001283: - version "1.0.30001304" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001304.tgz#38af55ed3fc8220cb13e35e6e7309c8c65a05559" - integrity sha512-bdsfZd6K6ap87AGqSHJP/s1V+U6Z5lyrcbBu3ovbCCf8cSYpwTtGrCBObMpJqwxfTbLW6YTIdbb1jEeTelcpYQ== + version "1.0.30001311" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001311.tgz#682ef3f4e617f1a177ad943de59775ed3032e511" + integrity sha512-mleTFtFKfykEeW34EyfhGIFjGCqzhh38Y0LhdQ9aWF+HorZTtdgKV/1hEE0NlFkG2ubvisPV6l400tlbPys98A== chalk@^4.0.0: version "4.1.2" @@ -370,9 +370,9 @@ concat-map@0.0.1: integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= core-js-pure@^3.20.2: - version "3.20.3" - resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.20.3.tgz#6cc4f36da06c61d95254efc54024fe4797fd5d02" - integrity sha512-Q2H6tQ5MtPtcC7f3HxJ48i4Q7T9ybPKgvWyuH7JXIoNa2pm0KuBnycsET/qw1SLLZYfbsbrZQNMeIOClb+6WIA== + version "3.21.0" + resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.21.0.tgz#819adc8dfb808205ce25b51d50591becd615db7e" + integrity sha512-VaJUunCZLnxuDbo1rNOzwbet9E1K9joiXS5+DQMPtgxd24wfsZbJZMMfQLGYMlCUvSxLfsRUUhoOR2x28mFfeg== cross-spawn@^7.0.2: version "7.0.3" @@ -833,9 +833,9 @@ glob@^7.1.3, glob@^7.1.7: path-is-absolute "^1.0.0" globals@^13.6.0, globals@^13.9.0: - version "13.12.0" - resolved "https://registry.yarnpkg.com/globals/-/globals-13.12.0.tgz#4d733760304230a0082ed96e21e5c565f898089e" - integrity sha512-uS8X6lSKN2JumVoXrbUz+uG4BYG+eiawqm3qFcT7ammfbUHeCBoJMlHcec/S3krSk73/AE/f0szYFmgAA3kYZg== + version "13.12.1" + resolved "https://registry.yarnpkg.com/globals/-/globals-13.12.1.tgz#ec206be932e6c77236677127577aa8e50bf1c5cb" + integrity sha512-317dFlgY2pdJZ9rspXDks7073GpDmXdfbM3vYYp0HAMKGDh1FfWPleI2ljVNLQX5M5lXcAslTcPTrOrMEFOjyw== dependencies: type-fest "^0.20.2" @@ -1125,9 +1125,9 @@ micromatch@^4.0.4: picomatch "^2.2.3" minimatch@^3.0.4: - version "3.0.4" - resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" - integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA== + version "3.0.5" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.5.tgz#4da8f1290ee0f0f8e83d60ca69f8f134068604a3" + integrity sha512-tUpxzX0VAzJHjLu0xUfFv1gwVp9ba3IOuRAVH2EGuRW8a5emA2FlACLqiT/lDVtS1W+TGNwqz3sWaNyLgDJWuw== dependencies: brace-expansion "^1.1.7" @@ -1479,10 +1479,10 @@ side-channel@^1.0.4: get-intrinsic "^1.0.2" object-inspect "^1.9.0" -simple-ascii-chart@^2.0.1: - version "2.0.1" - resolved "https://registry.yarnpkg.com/simple-ascii-chart/-/simple-ascii-chart-2.0.1.tgz#f401469fe3ffa91445a329422e03cd56779b7083" - integrity sha512-rvGaJpBX2MC40J35Mk4fS3IRgeFw6WPnOGW1Kkdwn/1Jl5OLF1TWliu4tELtpknnsRLqURdrvS0W2KD/+qR/mQ== +simple-ascii-chart@^2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/simple-ascii-chart/-/simple-ascii-chart-2.0.3.tgz#ffd71f57e0795abacfc3690d6a7d54e053ce4672" + integrity sha512-aXDJA6rbvtjRWu4reMaJaZqmyntP3Tui0YAOsdWHQQHOOIWp8BXblIatT4Ea+XsoPBpy+WJYe+Fj2ksBNYGATw== slash@^3.0.0: version "3.0.0"