From 71f539d495d1a772729c10bbba5c3cd2bc61cc74 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Sun, 29 Dec 2019 00:08:04 -0800 Subject: [PATCH 01/15] add todos --- src/global.ts | 1 + src/index.ts | 1 + src/params.ts | 1 + src/query.ts | 1 + src/route.ts | 3 +++ src/router.ts | 2 ++ src/server.ts | 2 ++ src/test.ts | 1 + 8 files changed, 12 insertions(+) diff --git a/src/global.ts b/src/global.ts index 5efb2511..0d843564 100644 --- a/src/global.ts +++ b/src/global.ts @@ -1,3 +1,4 @@ +// TODO: properly take into account the routes folder import { existsSync } from 'fs'; import { join } from 'path'; diff --git a/src/index.ts b/src/index.ts index 0287059f..2f67cf03 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ // exports = light.default; // export { default, default as light } from './light'; +// TODO: Ensure micro and micro boom are updated export { default as server } from './server'; export { default as params } from './params'; export { default as query } from './query'; diff --git a/src/params.ts b/src/params.ts index bcbd4b36..74c28a88 100644 --- a/src/params.ts +++ b/src/params.ts @@ -1,5 +1,6 @@ import Path from 'path-parser'; +// TODO: ensure tests cover this export default async (path: string, url: string): Promise => { const parser = new Path(path); return parser.test(url); diff --git a/src/query.ts b/src/query.ts index 0abc70ae..8374c778 100644 --- a/src/query.ts +++ b/src/query.ts @@ -1,5 +1,6 @@ import { URL } from 'url'; +// TODO: ensure tests cover this export default async (url: string): Promise => { const { searchParams } = new URL(url, 'http://localhost'); diff --git a/src/route.ts b/src/route.ts index c0262881..50f60b0b 100644 --- a/src/route.ts +++ b/src/route.ts @@ -9,6 +9,9 @@ import forTerminal from 'youch-terminal'; import pinoPretty from './helpers/pino-pretty'; +// TODO: clean up interfaces +// TODO: abstract out more stuff + type IM = IncomingMessage; type SR = ServerResponse; type AP = Promise; diff --git a/src/router.ts b/src/router.ts index d2b08749..387d7736 100644 --- a/src/router.ts +++ b/src/router.ts @@ -2,6 +2,8 @@ import { METHODS } from 'http'; import join from 'url-join'; import camelCase from 'lodash.camelcase'; +// TODO: clean up interfaces +// TODO: write tests interface RouterRouteType { method: string; path: string; diff --git a/src/server.ts b/src/server.ts index 55720fc3..efb3c704 100644 --- a/src/server.ts +++ b/src/server.ts @@ -8,6 +8,8 @@ import importRoutes from './utils/import-routes'; import addRoute from './utils/add-route'; import glob from './global'; +// TODO: move interfaces to global file +// TODO: clean up interface Light { server: Server; router: any; diff --git a/src/test.ts b/src/test.ts index 83a28cd3..33b557b6 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,6 +1,7 @@ import listen from 'test-listen'; import { server } from './index'; +// TODO: redo with hooks // TODO: support multiple routes with a given route object export default async (route: any, opts?: any): Promise => { // generate a server with only the route provided From e292afa8455da6811dfecbab7b112444b0c80bca Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Sun, 29 Dec 2019 00:35:40 -0800 Subject: [PATCH 02/15] update packages --- package-lock.json | 415 ++++++++++++++++++++++++++++++++++------------ package.json | 28 ++-- src/index.ts | 1 - 3 files changed, 320 insertions(+), 124 deletions(-) diff --git a/package-lock.json b/package-lock.json index f03ab09c..efbb20d2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -469,9 +469,9 @@ } }, "@types/braces": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/@types/braces/-/braces-2.3.0.tgz", - "integrity": "sha512-A3MV5EsLHgShHoJ/XES/fQAnwNISKLrFuH9eNBZY5OkTQB7JPIwbRoExvRpDsNABvkMojnKqKWS8x0m2rLYi+A==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@types/braces/-/braces-3.0.0.tgz", + "integrity": "sha512-TbH79tcyi9FHwbyboOKeRachRq63mSuWYXOflsNO9ZyE5ClQ/JaozNKl+aWUq87qPNsXasXxi2AbgfwIJ+8GQw==", "dev": true }, "@types/eslint-visitor-keys": { @@ -506,20 +506,14 @@ } }, "@types/jest": { - "version": "24.0.18", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-24.0.18.tgz", - "integrity": "sha512-jcDDXdjTcrQzdN06+TSVsPPqxvsZA/5QkYfIZlq1JMw7FdP5AZylbOc+6B/cuDurctRe+MziUMtQ3xQdrbjqyQ==", + "version": "24.0.25", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-24.0.25.tgz", + "integrity": "sha512-hnP1WpjN4KbGEK4dLayul6lgtys6FPz0UfxMeMQCv0M+sTnzN3ConfiO72jHgLxl119guHgI8gLqDOrRLsyp2g==", "dev": true, "requires": { - "@types/jest-diff": "*" + "jest-diff": "^24.3.0" } }, - "@types/jest-diff": { - "version": "20.0.1", - "resolved": "https://registry.npmjs.org/@types/jest-diff/-/jest-diff-20.0.1.tgz", - "integrity": "sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA==", - "dev": true - }, "@types/json-schema": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.3.tgz", @@ -560,33 +554,33 @@ } }, "@types/micromatch": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/@types/micromatch/-/micromatch-3.1.0.tgz", - "integrity": "sha512-06uA9V7v68RTOzA3ky1Oi0HmCPa+YJ050vM+sTECwkxnHUQnO17TAcNCGX400QT6bldUiPb7ux5oKy0j8ccEDw==", + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/@types/micromatch/-/micromatch-3.1.1.tgz", + "integrity": "sha512-Wr5y4uv3r7JP4jEUqv7rZeYiMBGRHcbojDVsl11wq6gw1v/ZZQvJexd9rtvVx3EIVqw8dwtcRjSs8m2DV9qHjQ==", "dev": true, "requires": { "@types/braces": "*" } }, "@types/node": { - "version": "11.13.19", - "resolved": "https://registry.npmjs.org/@types/node/-/node-11.13.19.tgz", - "integrity": "sha512-tLRDU1hmcWamtgRT2iVRdraAQVGFQGgtcqracSo9XyMN1VeZLSVGb8RJJxVqab7UGbijoUijGPVFMjmqzyZIUw==", + "version": "11.15.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-11.15.3.tgz", + "integrity": "sha512-5RzvXVietaB8S4dwDjxjltAOHtTO87fiksjqjWGZih97j6KSrdCDaRfmYMNrgrLM87odGBrsTHAl6N3fLraQaw==", "dev": true }, "@types/node-fetch": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.0.tgz", - "integrity": "sha512-TLFRywthBgL68auWj+ziWu+vnmmcHCDFC/sqCOQf1xTz4hRq8cu79z8CtHU9lncExGBsB8fXA4TiLDLt6xvMzw==", + "version": "2.5.4", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.4.tgz", + "integrity": "sha512-Oz6id++2qAOFuOlE1j0ouk1dzl3mmI1+qINPNBhi9nt/gVOz0G+13Ao6qjhdF0Ys+eOkhu6JnFmt38bR3H0POQ==", "dev": true, "requires": { "@types/node": "*" } }, "@types/pino": { - "version": "5.8.9", - "resolved": "https://registry.npmjs.org/@types/pino/-/pino-5.8.9.tgz", - "integrity": "sha512-ybOTk+Ax54aKKHy4ZMDAVYBzuRhHn5NTGVyFTdH6vDrjTj3sQnP+TAlIJaKKbKu9J8NioRrj1kUGKDtvPPesmA==", + "version": "5.15.1", + "resolved": "https://registry.npmjs.org/@types/pino/-/pino-5.15.1.tgz", + "integrity": "sha512-skJZ2VBHUva/dF4b2/3zOYVOZLyeaYT7wtPyf8+aoh0VuqfW8JO7PK9oJY8yRZVkN4K7RDdA5UFuH0QbbENZaw==", "dev": true, "requires": { "@types/node": "*", @@ -595,9 +589,9 @@ } }, "@types/pino-http": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/@types/pino-http/-/pino-http-4.0.3.tgz", - "integrity": "sha512-o0Lc9MrzP6K+tomVog56Cx5uZNUN40FesDM2pN4x5jyDOiilbaKJqOYAevPAVns1LjcO8mB8YrSc4NE4A/Z2Rw==", + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/@types/pino-http/-/pino-http-4.3.2.tgz", + "integrity": "sha512-JFjLqUz6pUXmxLGxQdY2rVH0CzjQ89iDLjKVN55r5tkB2pzCHGxIpsFMBt6wkyWEZJGHYaSQbgUL7rLk9zMyOA==", "dev": true, "requires": { "@types/pino": "*" @@ -649,18 +643,18 @@ "dev": true }, "@types/yargs": { - "version": "13.0.2", - "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-13.0.2.tgz", - "integrity": "sha512-lwwgizwk/bIIU+3ELORkyuOgDjCh7zuWDFqRtPPhhVgq9N1F7CvLNKg1TX4f2duwtKQ0p044Au9r1PLIXHrIzQ==", + "version": "13.0.4", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-13.0.4.tgz", + "integrity": "sha512-Ke1WmBbIkVM8bpvsNEcGgQM70XcEh/nbpxQhW7FhrsbCsXSY9BmLB1+LHtD7r9zrsOcFlLiF+a/UeJsdfw3C5A==", "dev": true, "requires": { "@types/yargs-parser": "*" } }, "@types/yargs-parser": { - "version": "13.0.0", - "resolved": "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-13.0.0.tgz", - "integrity": "sha512-wBlsw+8n21e6eTd4yVv8YD/E3xq0O6nNnJIquutAsFGE7EyMKz7W6RNT6BRu1SmdgmlCZ9tb0X+j+D6HGr8pZw==", + "version": "13.1.0", + "resolved": "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-13.1.0.tgz", + "integrity": "sha512-gCubfBUZ6KxzoibJ+SCUc/57Ms1jz5NjHe4+dI2krNmU5zCPAphyLJYyTOg06ueIyfj+SaCUqmzun7ImlxDcKg==", "dev": true }, "@typescript-eslint/eslint-plugin": { @@ -865,13 +859,67 @@ "dev": true }, "array-includes": { - "version": "3.0.3", - "resolved": "https://registry.npmjs.org/array-includes/-/array-includes-3.0.3.tgz", - "integrity": "sha1-GEtI9i2S10UrsxsyMWXH+L0CJm0=", + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/array-includes/-/array-includes-3.1.1.tgz", + "integrity": "sha512-c2VXaCHl7zPsvpkFsw4nxvFie4fh1ur9bpcgsVkIjqn0H/Xwdg+7fv3n2r/isyS8EBj5b06M9kHyZuIr4El6WQ==", "dev": true, "requires": { - "define-properties": "^1.1.2", - "es-abstract": "^1.7.0" + "define-properties": "^1.1.3", + "es-abstract": "^1.17.0", + "is-string": "^1.0.5" + }, + "dependencies": { + "es-abstract": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.17.0.tgz", + "integrity": "sha512-yYkE07YF+6SIBmg1MsJ9dlub5L48Ek7X0qz+c/CPCHS9EBXfESorzng4cJQjJW5/pB6vDF41u7F8vUhLVDqIug==", + "dev": true, + "requires": { + "es-to-primitive": "^1.2.1", + "function-bind": "^1.1.1", + "has": "^1.0.3", + "has-symbols": "^1.0.1", + "is-callable": "^1.1.5", + "is-regex": "^1.0.5", + "object-inspect": "^1.7.0", + "object-keys": "^1.1.1", + "object.assign": "^4.1.0", + "string.prototype.trimleft": "^2.1.1", + "string.prototype.trimright": "^2.1.1" + } + }, + "es-to-primitive": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz", + "integrity": "sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==", + "dev": true, + "requires": { + "is-callable": "^1.1.4", + "is-date-object": "^1.0.1", + "is-symbol": "^1.0.2" + } + }, + "has-symbols": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz", + "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg==", + "dev": true + }, + "is-callable": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.5.tgz", + "integrity": "sha512-ESKv5sMCJB2jnHTWZ3O5itG+O128Hsus4K4Qh1h2/cgn2vbgnLSVqfV46AeJA9D5EeeLa9w81KUXMtn34zhX+Q==", + "dev": true + }, + "is-regex": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.5.tgz", + "integrity": "sha512-vlKW17SNq44owv5AQR3Cq0bQPEb8+kF3UKZ2fiZNOWtztYE5i0CzCZxFDwO58qAOWtxdBRVO/V5Qin1wjCqFYQ==", + "dev": true, + "requires": { + "has": "^1.0.3" + } + } } }, "array-unique": { @@ -879,6 +927,69 @@ "resolved": "https://registry.npmjs.org/array-unique/-/array-unique-0.3.2.tgz", "integrity": "sha1-qJS3XUvE9s1nnvMkSp/Y9Gri1Cg=" }, + "array.prototype.flat": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/array.prototype.flat/-/array.prototype.flat-1.2.3.tgz", + "integrity": "sha512-gBlRZV0VSmfPIeWfuuy56XZMvbVfbEUnOXUvt3F/eUUUSyzlgLxhEX4YAEpxNAogRGehPSnfXyPtYyKAhkzQhQ==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "es-abstract": "^1.17.0-next.1" + }, + "dependencies": { + "es-abstract": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.17.0.tgz", + "integrity": "sha512-yYkE07YF+6SIBmg1MsJ9dlub5L48Ek7X0qz+c/CPCHS9EBXfESorzng4cJQjJW5/pB6vDF41u7F8vUhLVDqIug==", + "dev": true, + "requires": { + "es-to-primitive": "^1.2.1", + "function-bind": "^1.1.1", + "has": "^1.0.3", + "has-symbols": "^1.0.1", + "is-callable": "^1.1.5", + "is-regex": "^1.0.5", + "object-inspect": "^1.7.0", + "object-keys": "^1.1.1", + "object.assign": "^4.1.0", + "string.prototype.trimleft": "^2.1.1", + "string.prototype.trimright": "^2.1.1" + } + }, + "es-to-primitive": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz", + "integrity": "sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==", + "dev": true, + "requires": { + "is-callable": "^1.1.4", + "is-date-object": "^1.0.1", + "is-symbol": "^1.0.2" + } + }, + "has-symbols": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz", + "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg==", + "dev": true + }, + "is-callable": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.5.tgz", + "integrity": "sha512-ESKv5sMCJB2jnHTWZ3O5itG+O128Hsus4K4Qh1h2/cgn2vbgnLSVqfV46AeJA9D5EeeLa9w81KUXMtn34zhX+Q==", + "dev": true + }, + "is-regex": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.5.tgz", + "integrity": "sha512-vlKW17SNq44owv5AQR3Cq0bQPEb8+kF3UKZ2fiZNOWtztYE5i0CzCZxFDwO58qAOWtxdBRVO/V5Qin1wjCqFYQ==", + "dev": true, + "requires": { + "has": "^1.0.3" + } + } + } + }, "asn1": { "version": "0.2.4", "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz", @@ -1500,17 +1611,16 @@ "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, "coveralls": { - "version": "3.0.6", - "resolved": "https://registry.npmjs.org/coveralls/-/coveralls-3.0.6.tgz", - "integrity": "sha512-Pgh4v3gCI4T/9VijVrm8Ym5v0OgjvGLKj3zTUwkvsCiwqae/p6VLzpsFNjQS2i6ewV7ef+DjFJ5TSKxYt/mCrA==", + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/coveralls/-/coveralls-3.0.9.tgz", + "integrity": "sha512-nNBg3B1+4iDox5A5zqHKzUTiwl2ey4k2o0NEcVZYvl+GOSJdKBj4AJGKLv6h3SvWch7tABHePAQOSZWM9E2hMg==", "dev": true, "requires": { - "growl": "~> 1.10.0", "js-yaml": "^3.13.1", - "lcov-parse": "^0.0.10", + "lcov-parse": "^1.0.0", "log-driver": "^1.2.7", "minimist": "^1.2.0", - "request": "^2.86.0" + "request": "^2.88.0" } }, "cross-spawn": { @@ -1900,12 +2010,12 @@ } }, "eslint-module-utils": { - "version": "2.4.1", - "resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.4.1.tgz", - "integrity": "sha512-H6DOj+ejw7Tesdgbfs4jeS4YMFrT8uI8xwd1gtQqXssaR0EQ26L+2O/w6wkYFy2MymON0fTwHmXBvvfLNZVZEw==", + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.5.0.tgz", + "integrity": "sha512-kCo8pZaNz2dsAW7nCUjuVoI11EBXXpIzfNxmaoLhXoRDOnqXLC4iSGVRdZPhOitfbdEfMEfKOiENaK6wDPZEGw==", "dev": true, "requires": { - "debug": "^2.6.8", + "debug": "^2.6.9", "pkg-dir": "^2.0.0" }, "dependencies": { @@ -1927,22 +2037,23 @@ } }, "eslint-plugin-import": { - "version": "2.18.2", - "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.18.2.tgz", - "integrity": "sha512-5ohpsHAiUBRNaBWAF08izwUGlbrJoJJ+W9/TBwsGoR1MnlgfwMIKrFeSjWbt6moabiXW9xNvtFz+97KHRfI4HQ==", + "version": "2.19.1", + "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.19.1.tgz", + "integrity": "sha512-x68131aKoCZlCae7rDXKSAQmbT5DQuManyXo2sK6fJJ0aK5CWAkv6A6HJZGgqC8IhjQxYPgo6/IY4Oz8AFsbBw==", "dev": true, "requires": { "array-includes": "^3.0.3", + "array.prototype.flat": "^1.2.1", "contains-path": "^0.1.0", "debug": "^2.6.9", "doctrine": "1.5.0", "eslint-import-resolver-node": "^0.3.2", - "eslint-module-utils": "^2.4.0", + "eslint-module-utils": "^2.4.1", "has": "^1.0.3", "minimatch": "^3.0.4", "object.values": "^1.1.0", "read-pkg-up": "^2.0.0", - "resolve": "^1.11.0" + "resolve": "^1.12.0" }, "dependencies": { "debug": { @@ -1971,9 +2082,9 @@ "dev": true }, "resolve": { - "version": "1.12.0", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.12.0.tgz", - "integrity": "sha512-B/dOmuoAik5bKcD6s6nXDCjzUKnaDvdkRyAk6rsmsKLipWj4797iothd7jmmUhWTfinVMU+wc56rYKsit2Qy4w==", + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.14.1.tgz", + "integrity": "sha512-fn5Wobh4cxbLzuHaE+nphztHy43/b++4M6SsGFC2gB8uYwf0C8LcarfCz1un7UTW8OFQg9iNjZ4xpcFVGebDPg==", "dev": true, "requires": { "path-parse": "^1.0.6" @@ -1982,9 +2093,9 @@ } }, "eslint-plugin-jest": { - "version": "22.15.2", - "resolved": "https://registry.npmjs.org/eslint-plugin-jest/-/eslint-plugin-jest-22.15.2.tgz", - "integrity": "sha512-p4NME9TgXIt+KgpxcXyNBvO30ZKxwFAO1dJZBc2OGfDnXVEtPwEyNs95GSr6RIE3xLHdjd8ngDdE2icRRXrbxg==", + "version": "22.21.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-jest/-/eslint-plugin-jest-22.21.0.tgz", + "integrity": "sha512-OaqnSS7uBgcGiqXUiEnjoqxPNKvR4JWG5mSRkzVoR6+vDwlqqp11beeql1hYs0HTbdhiwrxWLxbX0Vx7roG3Ew==", "dev": true, "requires": { "@typescript-eslint/experimental-utils": "^1.13.0" @@ -2280,14 +2391,14 @@ "dev": true }, "fast-redact": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/fast-redact/-/fast-redact-1.5.0.tgz", - "integrity": "sha512-Afo61CgUjkzdvOKDHn08qnZ0kwck38AOGcMlvSGzvJbIab6soAP5rdoQayecGCDsD69AiF9vJBXyq31eoEO2tQ==" + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/fast-redact/-/fast-redact-2.0.0.tgz", + "integrity": "sha512-zxpkULI9W9MNTK2sJ3BpPQrTEXFNESd2X6O1tXMFpK/XM0G5c5Rll2EVYZH2TqI3xRGK/VaJ+eEOt7pnENJpeA==" }, "fast-safe-stringify": { - "version": "2.0.6", - "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.0.6.tgz", - "integrity": "sha512-q8BZ89jjc+mz08rSxROs8VsrBBcn1SIw1kq9NjolL509tkABRk9io01RAjSaEv1Xb2uFLt8VtRiZbGp5H8iDtg==" + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.0.7.tgz", + "integrity": "sha512-Utm6CdzT+6xsDk2m8S6uL8VHxNwI6Jub+e9NYTcAms28T84pTa25GJQV9j0CY0N1rM8hK4x6grpF2BQf+2qwVA==" }, "fb-watchman": { "version": "2.0.0", @@ -2337,9 +2448,9 @@ } }, "find-my-way": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/find-my-way/-/find-my-way-2.1.0.tgz", - "integrity": "sha512-Hdx6ctcrzkZH5y9EREHtXryXAgc5Bc8z5Cvoa61y9kaoYj2KU4yXD6h8b6u0NUkYPVmQQeRdf0AtG1kQxQ+ukQ==", + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/find-my-way/-/find-my-way-2.2.1.tgz", + "integrity": "sha512-pzZA9/PlhDGG5PRzmd4vH4AbKW7FO68RE7q2I3NzjJHcVPukYbDA7bPdArg7ySKfS6pKki+qhrawFoN6aNZfjA==", "requires": { "fast-decode-uri-component": "^1.0.0", "safe-regex2": "^2.0.0", @@ -2978,12 +3089,6 @@ "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz", "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==" }, - "growl": { - "version": "1.10.5", - "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz", - "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==", - "dev": true - }, "growly": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/growly/-/growly-1.3.0.tgz", @@ -3432,6 +3537,12 @@ "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" }, + "is-string": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/is-string/-/is-string-1.0.5.tgz", + "integrity": "sha512-buY6VNRjhQMiF1qWDouloZlQbRhDPCebwxSjxMjxgemYT46YMd2NR0/H+fBhEfWX4A/w9TBJ+ol+okqJKFE6vQ==", + "dev": true + }, "is-symbol": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.2.tgz", @@ -4193,9 +4304,9 @@ "dev": true }, "lcov-parse": { - "version": "0.0.10", - "resolved": "https://registry.npmjs.org/lcov-parse/-/lcov-parse-0.0.10.tgz", - "integrity": "sha1-GwuP+ayceIklBYK3C3ExXZ2m2aM=", + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/lcov-parse/-/lcov-parse-1.0.0.tgz", + "integrity": "sha1-6w1GtUER68VhrLTECO+TY73I9+A=", "dev": true }, "left-pad": { @@ -4252,6 +4363,12 @@ "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", "integrity": "sha1-soqmKIorn8ZRA1x3EfZathkDMaY=" }, + "lodash.memoize": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", + "integrity": "sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=", + "dev": true + }, "lodash.set": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz", @@ -4617,6 +4734,12 @@ } } }, + "object-inspect": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.7.0.tgz", + "integrity": "sha512-a7pEHdh1xKIAgTySUGgLMx/xwDZskN1Ud6egYYN3EdRW4ZMPNEDUTF+hwy2LUC+Bl+SyLXANnwz/jyh/qutKUw==", + "dev": true + }, "object-keys": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", @@ -4674,15 +4797,68 @@ } }, "object.values": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/object.values/-/object.values-1.1.0.tgz", - "integrity": "sha512-8mf0nKLAoFX6VlNVdhGj31SVYpaNFtUnuoOXWyFEstsWRgU837AK+JYM0iAxwkSzGRbwn8cbFmgbyxj1j4VbXg==", + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/object.values/-/object.values-1.1.1.tgz", + "integrity": "sha512-WTa54g2K8iu0kmS/us18jEmdv1a4Wi//BZ/DTVYEcH0XhLM5NYdpDHja3gt57VrZLcNAO2WGA+KpWsDBaHt6eA==", "dev": true, "requires": { "define-properties": "^1.1.3", - "es-abstract": "^1.12.0", + "es-abstract": "^1.17.0-next.1", "function-bind": "^1.1.1", "has": "^1.0.3" + }, + "dependencies": { + "es-abstract": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.17.0.tgz", + "integrity": "sha512-yYkE07YF+6SIBmg1MsJ9dlub5L48Ek7X0qz+c/CPCHS9EBXfESorzng4cJQjJW5/pB6vDF41u7F8vUhLVDqIug==", + "dev": true, + "requires": { + "es-to-primitive": "^1.2.1", + "function-bind": "^1.1.1", + "has": "^1.0.3", + "has-symbols": "^1.0.1", + "is-callable": "^1.1.5", + "is-regex": "^1.0.5", + "object-inspect": "^1.7.0", + "object-keys": "^1.1.1", + "object.assign": "^4.1.0", + "string.prototype.trimleft": "^2.1.1", + "string.prototype.trimright": "^2.1.1" + } + }, + "es-to-primitive": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz", + "integrity": "sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==", + "dev": true, + "requires": { + "is-callable": "^1.1.4", + "is-date-object": "^1.0.1", + "is-symbol": "^1.0.2" + } + }, + "has-symbols": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz", + "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg==", + "dev": true + }, + "is-callable": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.5.tgz", + "integrity": "sha512-ESKv5sMCJB2jnHTWZ3O5itG+O128Hsus4K4Qh1h2/cgn2vbgnLSVqfV46AeJA9D5EeeLa9w81KUXMtn34zhX+Q==", + "dev": true + }, + "is-regex": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.5.tgz", + "integrity": "sha512-vlKW17SNq44owv5AQR3Cq0bQPEb8+kF3UKZ2fiZNOWtztYE5i0CzCZxFDwO58qAOWtxdBRVO/V5Qin1wjCqFYQ==", + "dev": true, + "requires": { + "has": "^1.0.3" + } + } } }, "once": { @@ -4906,31 +5082,31 @@ "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=" }, "pino": { - "version": "5.13.2", - "resolved": "https://registry.npmjs.org/pino/-/pino-5.13.2.tgz", - "integrity": "sha512-WwOSCy36/gWhinsqWqAnuwIi2WtcH+jvoyeLm3bjUALIrzWIst0AovQjK4jVvSN2l64KFPfi3gd2fjsTovjdLQ==", - "requires": { - "fast-redact": "^1.4.4", - "fast-safe-stringify": "^2.0.6", - "flatstr": "^1.0.9", - "pino-std-serializers": "^2.3.0", - "quick-format-unescaped": "^3.0.2", + "version": "5.15.0", + "resolved": "https://registry.npmjs.org/pino/-/pino-5.15.0.tgz", + "integrity": "sha512-7+FXMTA3H3sNP5+2miY2K9JKnAAW5GKuhHfNWsukFCsPprGQY3ctqpwbV74wAHW3Nl93cEEQ1G82MgOLM8P7TQ==", + "requires": { + "fast-redact": "^2.0.0", + "fast-safe-stringify": "^2.0.7", + "flatstr": "^1.0.12", + "pino-std-serializers": "^2.4.2", + "quick-format-unescaped": "^3.0.3", "sonic-boom": "^0.7.5" } }, "pino-http": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/pino-http/-/pino-http-4.2.0.tgz", - "integrity": "sha512-yLOpH8fwnUJ3n++QmjS9HtxooJN8OKKcbbW+deRh7GqNHyY5+M9ehmH1X69pY+vuCxBY6hKGJGO2wmHG6OEmDQ==", + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/pino-http/-/pino-http-4.3.0.tgz", + "integrity": "sha512-9QrA/5KykS5TLqJPzFQY08bLA3iwHrLEVeM6PYKU95VqB54X/eJKev4A6sagf5UUxeUimVDcWt5wcp5SJHHSvg==", "requires": { "pino": "^5.0.0", "pino-std-serializers": "^2.4.0" } }, "pino-std-serializers": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/pino-std-serializers/-/pino-std-serializers-2.4.0.tgz", - "integrity": "sha512-ysT2ylXu1aEec9k8cm/lz7emBcfpdxFWHqvHeGXf1wvfw7TKPMGhLWwS+ciHw6u4ffnmV+pkAMF4MUIZmZZdSg==" + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/pino-std-serializers/-/pino-std-serializers-2.4.2.tgz", + "integrity": "sha512-WaL504dO8eGs+vrK+j4BuQQq6GLKeCCcHaMB2ItygzVURcL1CycwNEUHTD/lHFHs/NL5qAz2UKrjYWXKSf4aMQ==" }, "pirates": { "version": "4.0.1", @@ -5083,9 +5259,9 @@ "dev": true }, "quick-format-unescaped": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/quick-format-unescaped/-/quick-format-unescaped-3.0.2.tgz", - "integrity": "sha512-FXTaCkwvpIlkdKeGDNgcq07SXWS383noQUuZjvdE1QcTt+eLuqof6/BDiEPqB59FWLie/l91+HtlJSw7iCViSA==" + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/quick-format-unescaped/-/quick-format-unescaped-3.0.3.tgz", + "integrity": "sha512-dy1yjycmn9blucmJLXOfZDx1ikZJUi6E8bBZLnhPG5gBrVhHXx2xVyqqgKBubVNEXmx51dBACMHpoMQK/N/AXQ==" }, "r-json": { "version": "1.2.9", @@ -5718,9 +5894,9 @@ } }, "sonic-boom": { - "version": "0.7.5", - "resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-0.7.5.tgz", - "integrity": "sha512-1pKrnAV6RfvntPnarY71tpthFTM3pWZWWQdghZY8ARjtDPGzG/inxqSuRwQY/7V1woUjfyxPb437zn4p5phgnQ==", + "version": "0.7.6", + "resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-0.7.6.tgz", + "integrity": "sha512-k9E2QQ4zxuVRLDW+ZW6ISzJs3wlEorVdmM7ApDgor7wsGKSDG5YGHsGmgLY4XYh4DMlr/2ap2BWAE7yTFJtWnQ==", "requires": { "flatstr": "^1.0.12" } @@ -5923,6 +6099,26 @@ } } }, + "string.prototype.trimleft": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string.prototype.trimleft/-/string.prototype.trimleft-2.1.1.tgz", + "integrity": "sha512-iu2AGd3PuP5Rp7x2kEZCrB2Nf41ehzh+goo8TV7z8/XDBbsvc6HQIlUl9RjkZ4oyrW1XM5UwlGl1oVEaDjg6Ag==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "function-bind": "^1.1.1" + } + }, + "string.prototype.trimright": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string.prototype.trimright/-/string.prototype.trimright-2.1.1.tgz", + "integrity": "sha512-qFvWL3/+QIgZXVmJBfpHmxLB7xsUXz6HsUmP8+5dRaC3Q7oKUv9Vo6aMCRZC1smrtyECFsIT30PqBJ1gTjAs+g==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "function-bind": "^1.1.1" + } + }, "string_decoder": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", @@ -6198,15 +6394,16 @@ "dev": true }, "ts-jest": { - "version": "24.0.2", - "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-24.0.2.tgz", - "integrity": "sha512-h6ZCZiA1EQgjczxq+uGLXQlNgeg02WWJBbeT8j6nyIBRQdglqbvzDoHahTEIiS6Eor6x8mK6PfZ7brQ9Q6tzHw==", + "version": "24.2.0", + "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-24.2.0.tgz", + "integrity": "sha512-Yc+HLyldlIC9iIK8xEN7tV960Or56N49MDP7hubCZUeI7EbIOTsas6rXCMB4kQjLACJ7eDOF4xWEO5qumpKsag==", "dev": true, "requires": { "bs-logger": "0.x", "buffer-from": "1.x", "fast-json-stable-stringify": "2.x", "json5": "2.x", + "lodash.memoize": "4.x", "make-error": "1.x", "mkdirp": "0.x", "resolve": "1.x", @@ -6271,9 +6468,9 @@ } }, "typescript": { - "version": "3.5.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.5.3.tgz", - "integrity": "sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==", + "version": "3.7.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.4.tgz", + "integrity": "sha512-A25xv5XCtarLwXpcDNZzCGvW2D1S3/bACratYBx2sax8PefsFhlYmkQicKHvpYflFS8if4zne5zT5kpJ7pzuvw==", "dev": true }, "uc-first-array": { diff --git a/package.json b/package.json index dfe0e1b6..30b41a90 100644 --- a/package.json +++ b/package.json @@ -42,15 +42,15 @@ "chokidar": "^2.1.8", "decache": "^4.5.1", "emojic": "^1.1.15", - "find-my-way": "^2.1.0", + "find-my-way": "^2.2.1", "lodash.camelcase": "^4.3.0", "lodash.set": "^4.3.2", "micro": "^9.3.4", "micro-boom": "^1.2.0", "micromatch": "^4.0.2", "path-parser": "^4.2.0", - "pino": "^5.13.2", - "pino-http": "^4.2.0", + "pino": "^5.15.0", + "pino-http": "^4.3.0", "repl.history": "^0.1.4", "signale": "^1.4.0", "test-listen": "^1.1.0", @@ -60,29 +60,29 @@ "youch-terminal": "^1.0.0" }, "devDependencies": { - "@types/jest": "^24.0.18", + "@types/jest": "^24.0.25", "@types/lodash.camelcase": "^4.3.6", "@types/lodash.set": "^4.3.6", "@types/micro": "^7.3.3", - "@types/micromatch": "^3.1.0", - "@types/node": "^11.13.19", - "@types/node-fetch": "^2.5.0", - "@types/pino-http": "^4.0.3", + "@types/micromatch": "^3.1.1", + "@types/node": "^11.15.3", + "@types/node-fetch": "^2.5.4", + "@types/pino-http": "^4.3.2", "@types/signale": "^1.2.1", "@types/test-listen": "^1.1.0", "@types/url-join": "^4.0.0", - "@types/yargs": "^13.0.2", + "@types/yargs": "^13.0.4", "@typescript-eslint/eslint-plugin": "^1.13.0", "@typescript-eslint/parser": "^1.13.0", - "coveralls": "^3.0.6", + "coveralls": "^3.0.9", "eslint": "^5.16.0", "eslint-config-airbnb-base": "^13.2.0", - "eslint-plugin-import": "^2.18.2", - "eslint-plugin-jest": "^22.15.2", + "eslint-plugin-import": "^2.19.1", + "eslint-plugin-jest": "^22.21.0", "jest": "^24.9.0", "node-fetch": "^2.6.0", "strip-ansi": "^5.2.0", - "ts-jest": "^24.0.2", - "typescript": "^3.5.3" + "ts-jest": "^24.2.0", + "typescript": "^3.7.4" } } diff --git a/src/index.ts b/src/index.ts index 2f67cf03..0287059f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,6 @@ // exports = light.default; // export { default, default as light } from './light'; -// TODO: Ensure micro and micro boom are updated export { default as server } from './server'; export { default as params } from './params'; export { default as query } from './query'; From b6bb953b5e8b62420613bee0e3b79a39e18fcac7 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Sun, 29 Dec 2019 01:33:38 -0800 Subject: [PATCH 03/15] clean up types --- src/cli/commands/dev.ts | 6 +-- src/route.ts | 21 ++-------- src/server.ts | 42 ++++++++----------- src/types/{ => external}/async-repl.d.ts | 0 .../{ => external}/aws-serverless-micro.d.ts | 0 src/types/{ => external}/emojic.d.ts | 0 src/types/{ => external}/is-class.d.ts | 0 src/types/{ => external}/micro-boom.d.ts | 0 src/types/{ => external}/repl-history.d.ts | 0 src/types/{ => external}/youch.d.ts | 0 src/types/http.ts | 12 ++++++ src/types/route.ts | 30 +++++++++++-- src/types/server.ts | 10 +++++ src/utils/add-route.ts | 15 ++++--- src/utils/find-routes.ts | 15 ++++++- src/utils/import-routes.ts | 12 ++++-- 16 files changed, 100 insertions(+), 63 deletions(-) rename src/types/{ => external}/async-repl.d.ts (100%) rename src/types/{ => external}/aws-serverless-micro.d.ts (100%) rename src/types/{ => external}/emojic.d.ts (100%) rename src/types/{ => external}/is-class.d.ts (100%) rename src/types/{ => external}/micro-boom.d.ts (100%) rename src/types/{ => external}/repl-history.d.ts (100%) rename src/types/{ => external}/youch.d.ts (100%) create mode 100644 src/types/http.ts create mode 100644 src/types/server.ts diff --git a/src/cli/commands/dev.ts b/src/cli/commands/dev.ts index 71c12f3d..6b102bd3 100644 --- a/src/cli/commands/dev.ts +++ b/src/cli/commands/dev.ts @@ -5,7 +5,7 @@ import chalk from 'chalk'; import decache from 'decache'; import logger from '../../utils/logger'; -import Route from '../../types/route'; +import { RouteObject } from '../../types/route'; import { server } from '../../index'; import findRoutes from '../../utils/find-routes'; import addRoute from '../../utils/add-route'; @@ -46,7 +46,7 @@ const handle = async (argv: Args): Promise => { const routesPath = join(cwd, './routes'); const opts = { - isDev: true, + dev: true, }; const app = server({ @@ -87,7 +87,7 @@ const handle = async (argv: Args): Promise => { decache(f.handler); }); const routeObjs = importRoutes(files, routesPath, true); - routeObjs.forEach((route: Route): void => { + routeObjs.forEach((route: RouteObject): void => { addRoute(app.router, route, opts); }); }); diff --git a/src/route.ts b/src/route.ts index 50f60b0b..d55668c0 100644 --- a/src/route.ts +++ b/src/route.ts @@ -1,4 +1,3 @@ -import { IncomingMessage, ServerResponse } from 'http'; import AWSServerlessMicro from 'aws-serverless-micro'; import { run } from 'micro'; import { handleErrors } from 'micro-boom'; @@ -7,27 +6,13 @@ import pinoHTTP from 'pino-http'; import Youch from 'youch'; import forTerminal from 'youch-terminal'; +import { IM, SR, AP } from './types/http'; +import { Options, Route } from './types/route'; + import pinoPretty from './helpers/pino-pretty'; -// TODO: clean up interfaces // TODO: abstract out more stuff -type IM = IncomingMessage; -type SR = ServerResponse; -type AP = Promise; - -interface Route { - handler: (fn: (req: IM, res: SR) => {} | any) => (req: IM, res: SR) => {}; - middleware: (fn: any) => void; - plugin: (fn: any) => void; -} - -interface Options { - dev?: boolean; - requestLogger?: boolean; - errorHandler?: boolean; -} - const { LIGHT_ENV } = process.env; // TODO: extra to a different folder diff --git a/src/server.ts b/src/server.ts index efb3c704..429c5813 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,45 +1,41 @@ import micro from 'micro'; import Router from 'find-my-way'; -import { IncomingMessage, ServerResponse, Server } from 'http'; + +import { IM, SR } from './types/http'; +import { LightServer } from './types/server'; +import { RouteObject, Options } from './types/route'; import findRoutes from './utils/find-routes'; -import RouteType from './types/route'; import importRoutes from './utils/import-routes'; import addRoute from './utils/add-route'; -import glob from './global'; - -// TODO: move interfaces to global file -// TODO: clean up -interface Light { - server: Server; - router: any; -} - -type IM = IncomingMessage; -type SR = ServerResponse; +import globalRegister from './global'; // TODO: change opts type to the Options type in route.ts -const app = ({ + +export default ({ routes, opts, }: { - routes: string | RouteType[]; - opts?: any; -}): Light => { - const g = glob(); + routes: string | RouteObject[]; // TODO: check this + opts?: Options; +}): LightServer => { + // register global variables + const g = globalRegister(); (global as any).light = g; + + // create find-my-way router with default 404 handler const router = Router({ ignoreTrailingSlash: true, - defaultRoute: (req: IncomingMessage, res: ServerResponse): void => { + defaultRoute: (_: IM, res: SR): void => { res.statusCode = 404; res.end('Not Found'); }, }); - let routeObjs: RouteType[] = []; + let routeObjs: RouteObject[] = []; if (typeof routes === 'string') { - const files: any[] = findRoutes(routes); + const files: RouteObject[] = findRoutes(routes); routeObjs = importRoutes(files, routes); } else { routeObjs = routes; @@ -47,7 +43,7 @@ const app = ({ const server = micro(async (req: IM, res: SR): Promise => router.lookup(req, res)); - routeObjs.forEach((route: RouteType): void => { + routeObjs.forEach((route: RouteObject): void => { addRoute(router, route, opts); }); @@ -56,5 +52,3 @@ const app = ({ server, }; }; - -export default app; diff --git a/src/types/async-repl.d.ts b/src/types/external/async-repl.d.ts similarity index 100% rename from src/types/async-repl.d.ts rename to src/types/external/async-repl.d.ts diff --git a/src/types/aws-serverless-micro.d.ts b/src/types/external/aws-serverless-micro.d.ts similarity index 100% rename from src/types/aws-serverless-micro.d.ts rename to src/types/external/aws-serverless-micro.d.ts diff --git a/src/types/emojic.d.ts b/src/types/external/emojic.d.ts similarity index 100% rename from src/types/emojic.d.ts rename to src/types/external/emojic.d.ts diff --git a/src/types/is-class.d.ts b/src/types/external/is-class.d.ts similarity index 100% rename from src/types/is-class.d.ts rename to src/types/external/is-class.d.ts diff --git a/src/types/micro-boom.d.ts b/src/types/external/micro-boom.d.ts similarity index 100% rename from src/types/micro-boom.d.ts rename to src/types/external/micro-boom.d.ts diff --git a/src/types/repl-history.d.ts b/src/types/external/repl-history.d.ts similarity index 100% rename from src/types/repl-history.d.ts rename to src/types/external/repl-history.d.ts diff --git a/src/types/youch.d.ts b/src/types/external/youch.d.ts similarity index 100% rename from src/types/youch.d.ts rename to src/types/external/youch.d.ts diff --git a/src/types/http.ts b/src/types/http.ts new file mode 100644 index 00000000..4bb1e209 --- /dev/null +++ b/src/types/http.ts @@ -0,0 +1,12 @@ +import { IncomingMessage, ServerResponse, Server } from 'http'; + +type IM = IncomingMessage; +type SR = ServerResponse; +type AP = Promise; + +export { + IM, + SR, + AP, + Server, +}; diff --git a/src/types/route.ts b/src/types/route.ts index 92ea7282..6a9c229f 100644 --- a/src/types/route.ts +++ b/src/types/route.ts @@ -1,6 +1,28 @@ -export default interface Route { - path: string | string[]; +import { IM, SR } from './http'; + +// the route definition that will be ingested +interface RouteObject { + path?: string | string[]; handler: any; - method: string[] | string; + method?: string[] | string; file?: string; -}; // eslint-disable-line +} + +// options passed to the route +interface Options { + dev?: boolean; + requestLogger?: boolean; + errorHandler?: boolean; +} + +interface Route { + handler: (fn: (req: IM, res: SR) => {} | any) => (req: IM, res: SR) => {}; + middleware: (fn: any) => void; + plugin: (fn: any) => void; +} + +export { + RouteObject, + Options, + Route +}; diff --git a/src/types/server.ts b/src/types/server.ts new file mode 100644 index 00000000..1d8a3207 --- /dev/null +++ b/src/types/server.ts @@ -0,0 +1,10 @@ +import { Server } from './http'; + +interface LightServer { + server: Server; + router: any; +} + +export { + LightServer, +}; diff --git a/src/utils/add-route.ts b/src/utils/add-route.ts index baca878f..d8b2bcb6 100644 --- a/src/utils/add-route.ts +++ b/src/utils/add-route.ts @@ -1,20 +1,19 @@ import { join, parse } from 'path'; -import { IncomingMessage, ServerResponse } from 'http'; +import { IM, SR, AP } from '../types/http'; -import Route from '../types/route'; +import { RouteObject, Options } from '../types/route'; -type IM = IncomingMessage; -type SR = ServerResponse; -type AP = Promise; - -export default (router: any, route: Route, opts?: any): void => { +/** + * adds route to the router + */ +export default (router: any, route: RouteObject, opts?: Options): void => { const endpoint = route; if (typeof endpoint.path === 'string') { endpoint.path = [endpoint.path]; } - endpoint.path = endpoint.path.map((p: string): string => { + endpoint.path = (endpoint.path || []).map((p: string): string => { const { name, dir } = parse(p); return join('/', dir, name); }); diff --git a/src/utils/find-routes.ts b/src/utils/find-routes.ts index 5b7f1251..91fc68a9 100644 --- a/src/utils/find-routes.ts +++ b/src/utils/find-routes.ts @@ -1,9 +1,20 @@ import { join } from 'path'; import { existsSync } from 'fs'; + +import { RouteObject } from '../types/route'; import glob from './glob'; -export default (routesPath: string): any[] => { +/** + * Find all of the routes from either the routes file or the fs + * + * @param {string} routesPath - the absolute path to the routes + * @returns {RouteObject} an array of objects with handler + */ +export default (routesPath: string): RouteObject[] => { + // try to find the routes file const routesFilePath = join(routesPath, '../', 'routes.js'); + + // if it exists then use the routes from there if (existsSync(routesFilePath)) { const routerRoutes = require(routesFilePath) || []; // eslint-disable-line const mappedRoutes = routerRoutes.map((r: any): any => ({ @@ -20,5 +31,5 @@ export default (routesPath: string): any[] => { addRoutes(routesPath); - return routes.map((s: string): any => ({ handler: s })); + return routes.map((s: string): RouteObject => ({ handler: s })); }; diff --git a/src/utils/import-routes.ts b/src/utils/import-routes.ts index 096e7d4b..8975612d 100644 --- a/src/utils/import-routes.ts +++ b/src/utils/import-routes.ts @@ -4,14 +4,18 @@ import { relative } from 'path'; import Youch from 'youch'; import forTerminal from 'youch-terminal'; -import RouteType from '../types/route'; +import { RouteObject } from '../types/route'; import { route } from '../index'; -export default (routes: any[], routesPath: string, safe: boolean = false): RouteType[] => { - let results: RouteType[] = []; +export default ( + routes: RouteObject[], + routesPath: string, + safe: boolean = false, +): RouteObject[] => { + let results: RouteObject[] = []; try { - results = routes.map((r: any): RouteType => { + results = routes.map((r: any): RouteObject => { let handler; handler = require(r.handler); // eslint-disable-line if (handler.default) { From 1473827052a5a341cc31a9ba6e3bf4a97692cb33 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Sun, 29 Dec 2019 14:48:28 -0800 Subject: [PATCH 04/15] improve test --- src/global.ts | 2 +- src/params.ts | 1 - src/query.ts | 1 - src/server.ts | 4 +--- src/test.ts | 26 ++++++++++++++------------ src/types/route.ts | 9 +++++++++ tests/404.ts | 17 +++++++++-------- tests/middleware.ts | 10 ++++++---- tests/plugins.ts | 6 ++++-- tests/plugins/error-handler.ts | 10 ++++++---- tests/plugins/request-logger.ts | 21 +++++++++++---------- tests/plugins/youch.ts | 19 ++++++++++--------- tests/route.ts | 14 +++++++------- 13 files changed, 78 insertions(+), 62 deletions(-) diff --git a/src/global.ts b/src/global.ts index 0d843564..536ae8bf 100644 --- a/src/global.ts +++ b/src/global.ts @@ -1,4 +1,4 @@ -// TODO: properly take into account the routes folder +// TODO: properly take into account the src folder import { existsSync } from 'fs'; import { join } from 'path'; diff --git a/src/params.ts b/src/params.ts index 74c28a88..bcbd4b36 100644 --- a/src/params.ts +++ b/src/params.ts @@ -1,6 +1,5 @@ import Path from 'path-parser'; -// TODO: ensure tests cover this export default async (path: string, url: string): Promise => { const parser = new Path(path); return parser.test(url); diff --git a/src/query.ts b/src/query.ts index 8374c778..0abc70ae 100644 --- a/src/query.ts +++ b/src/query.ts @@ -1,6 +1,5 @@ import { URL } from 'url'; -// TODO: ensure tests cover this export default async (url: string): Promise => { const { searchParams } = new URL(url, 'http://localhost'); diff --git a/src/server.ts b/src/server.ts index 429c5813..4627700d 100644 --- a/src/server.ts +++ b/src/server.ts @@ -10,13 +10,11 @@ import importRoutes from './utils/import-routes'; import addRoute from './utils/add-route'; import globalRegister from './global'; -// TODO: change opts type to the Options type in route.ts - export default ({ routes, opts, }: { - routes: string | RouteObject[]; // TODO: check this + routes: string | RouteObject[]; opts?: Options; }): LightServer => { // register global variables diff --git a/src/test.ts b/src/test.ts index 33b557b6..e442b13f 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,9 +1,11 @@ import listen from 'test-listen'; +import { METHODS } from 'http'; + +import { TestOptions } from './types/route'; + import { server } from './index'; -// TODO: redo with hooks -// TODO: support multiple routes with a given route object -export default async (route: any, opts?: any): Promise => { +export default (route: any, opts?: TestOptions): any => { // generate a server with only the route provided const options = { requestLogger: false, @@ -15,20 +17,20 @@ export default async (route: any, opts?: any): Promise => { routes: [ { handler: async (req: any, res: any): Promise => route(req, res, options), - method: 'GET', - path: '/', + method: options.method || METHODS, + path: options.path || '/', }, ], }); - - const url = await listen(app.server); + const srvr = app.server; return { - url, - app, - server: app.server, - close(): void { - this.server.close(); + async listen(): Promise { + return listen(srvr); + }, + + close(): any { + srvr.close(); }, }; }; diff --git a/src/types/route.ts b/src/types/route.ts index 6a9c229f..87023a06 100644 --- a/src/types/route.ts +++ b/src/types/route.ts @@ -15,6 +15,14 @@ interface Options { errorHandler?: boolean; } +interface TestOptions { + dev?: boolean; + requestLogger?: boolean; + errorHandler?: boolean; + method?: string | string[]; + path?: string | string[]; +} + interface Route { handler: (fn: (req: IM, res: SR) => {} | any) => (req: IM, res: SR) => {}; middleware: (fn: any) => void; @@ -24,5 +32,6 @@ interface Route { export { RouteObject, Options, + TestOptions, Route }; diff --git a/tests/404.ts b/tests/404.ts index ba6e3831..9c819d56 100644 --- a/tests/404.ts +++ b/tests/404.ts @@ -5,24 +5,25 @@ import { test, route, } from '../src/index'; -let server: any; +const { handler } = route(); +const { listen, close } = test(handler(() => ({ + hello: 'world', +}))) +let url: any; beforeEach(async () => { - const { handler } = route(); - server = await test(handler(() => ({ - hello: 'world', - }))); + url = await listen(); }); afterEach(async () => { - server.close(); + close(); }); describe('404', () => { describe('with correct route', () => { it('returns data', async () => { expect.assertions(2); - const req = await fetch(server.url); + const req = await fetch(url); const res = await req.json(); expect(req.status).toStrictEqual(200); expect(res).toMatchObject({ hello: 'world' }); @@ -32,7 +33,7 @@ describe('404', () => { describe('with incorrect route', () => { it('returns a 404 error', async () => { expect.assertions(2); - const req = await fetch(join(server.url, 'hello')); + const req = await fetch(join(url, 'hello')); const res = await req.text(); expect(req.status).toStrictEqual(404); expect(res).toBe('Not Found'); diff --git a/tests/middleware.ts b/tests/middleware.ts index f2b5cd67..161e6146 100644 --- a/tests/middleware.ts +++ b/tests/middleware.ts @@ -5,14 +5,16 @@ import { } from '../src/index'; let mw: any = () => {}; -let server: any; +let server: any; +let url: string; beforeEach(async () => { const { handler, middleware } = route(); middleware(mw); - server = await test(handler((req: any): any => ({ + server = test(handler((req: any): any => ({ hello: req.message, }))); + url = await server.listen(); }); afterEach(async () => { @@ -27,7 +29,7 @@ describe('middleware', () => { it('returns data from a middleware', async () => { expect.assertions(2); - const req = await fetch(server.url); + const req = await fetch(url); const res = await req.json(); expect(req.status).toStrictEqual(200); expect(res).toMatchObject({ hello: 'passed a message' }); @@ -41,7 +43,7 @@ describe('middleware', () => { it('returns early from a middleware', async () => { expect.assertions(2); - const req = await fetch(server.url); + const req = await fetch(url); const res = await req.text(); expect(req.status).toStrictEqual(200); expect(res).toBe('middleware!!!'); diff --git a/tests/plugins.ts b/tests/plugins.ts index 9dd44b5d..b8b4e4e9 100644 --- a/tests/plugins.ts +++ b/tests/plugins.ts @@ -4,13 +4,15 @@ import { test, route } from '../src/index'; let plug: any = () => {}; let server: any; +let url: string; beforeEach(async () => { const { handler, plugin } = route(); plugin(plug); - server = await test(handler((req: any) => ({ + server = test(handler((req: any) => ({ hello: req.message, }))); + url = await server.listen(); }); afterEach(async () => { @@ -27,7 +29,7 @@ describe('plugins', () => { it('returns data from a plugin', async () => { expect.assertions(2); - const req = await fetch(server.url); + const req = await fetch(url); const res = await req.json(); expect(req.status).toStrictEqual(200); expect(res).toMatchObject({ hello: 'plugin!!!' }); diff --git a/tests/plugins/error-handler.ts b/tests/plugins/error-handler.ts index 45487434..9a082ab5 100644 --- a/tests/plugins/error-handler.ts +++ b/tests/plugins/error-handler.ts @@ -7,10 +7,12 @@ import { let server: any; let errorHandler = true; let error: any = () => {}; +let url: string; beforeEach(async () => { const { handler } = route(); - server = await test(handler(() => error()), { errorHandler }); + server = test(handler(() => error()), { errorHandler }); + url = await server.listen(); }); afterEach(async () => { @@ -27,7 +29,7 @@ describe('plugins', () => { it('returns a 500 error', async () => { expect.assertions(3); const spy = jest.spyOn(process.stdout, 'write').mockImplementation(); - const req = await fetch(server.url); + const req = await fetch(url); const res = await req.json(); expect(req.status).toStrictEqual(500); expect(res).toMatchObject({ @@ -48,7 +50,7 @@ describe('plugins', () => { it('returns a 500 error', async () => { expect.assertions(3); const spy = jest.spyOn(process.stdout, 'write').mockImplementation(); - const req = await fetch(server.url); + const req = await fetch(url); const res = await req.json(); expect(req.status).toStrictEqual(400); expect(res).toMatchObject({ @@ -70,7 +72,7 @@ describe('plugins', () => { it('returns an internal server error in text', async () => { expect.assertions(2); const spy = jest.spyOn(console, 'error').mockImplementation(); - const req = await fetch(server.url); + const req = await fetch(url); const res = await req.text(); expect(req.status).toStrictEqual(500); expect(res).toBe('Internal Server Error'); diff --git a/tests/plugins/request-logger.ts b/tests/plugins/request-logger.ts index 57a6230d..1cf09246 100644 --- a/tests/plugins/request-logger.ts +++ b/tests/plugins/request-logger.ts @@ -2,20 +2,21 @@ import fetch from 'node-fetch'; import { test, route } from '../../src/index'; -let server: any; +const { handler } = route(); +const { listen, close } = test(handler(() => ({ + hello: 'world', +})), { + dev: false, + requestLogger: true, +}); +let url: string; beforeEach(async () => { - const { handler } = route(); - server = await test(handler(() => ({ - hello: 'world', - })), { - dev: false, - requestLogger: true, - }); + url = await listen(); }); afterEach(async () => { - server.close(); + close(); }); describe('plugins', () => { @@ -24,7 +25,7 @@ describe('plugins', () => { it('logs', async () => { expect.assertions(6); const spy = jest.spyOn(process.stdout, 'write').mockImplementation(); - const req = await fetch(server.url); + const req = await fetch(url); const res = await req.json(); expect(req.status).toStrictEqual(200); expect(res).toMatchObject({ hello: 'world' }); diff --git a/tests/plugins/youch.ts b/tests/plugins/youch.ts index 461b624b..b4cee432 100644 --- a/tests/plugins/youch.ts +++ b/tests/plugins/youch.ts @@ -2,19 +2,20 @@ import fetch from 'node-fetch'; import { test, route } from '../../src/index'; -let server: any; +const { handler } = route(); +const { listen, close } = test(handler(() => { + throw new Error('hi'); +}), { + dev: true, +}); +let url: any; beforeEach(async () => { - const { handler } = route(); - server = await test(handler(() => { - throw new Error('hi'); - }), { - dev: true, - }); + url = await listen(); }); afterEach(async () => { - server.close(); + close(); }); describe('plugins', () => { @@ -23,7 +24,7 @@ describe('plugins', () => { it('youches the error', async () => { expect.assertions(3); const spy = jest.spyOn(console, 'log').mockImplementation(); - const req = await fetch(server.url); + const req = await fetch(url); const res = await req.text(); expect(req.status).toStrictEqual(200); expect(res).toContain('html'); diff --git a/tests/route.ts b/tests/route.ts index d9cdae30..d9f472b6 100644 --- a/tests/route.ts +++ b/tests/route.ts @@ -5,23 +5,23 @@ import { test, route } from '../src/index'; let handler: any = (): any => ({ hello: 'world', }); +const { handler: fn } = route(); +const { listen, close } = test(fn(handler)); -let server: any; - +let url: any; beforeEach(async () => { - const { handler: fn } = route(); - server = await test(fn(handler)); + url = await listen(); }); afterEach(async () => { - server.close(); + close(); }); describe('route', () => { describe('with regular function', () => { it('returns object properly', async () => { expect.assertions(2); - const req = await fetch(server.url); + const req = await fetch(url); const res = await req.json(); expect(req.status).toStrictEqual(200); expect(res).toMatchObject({ hello: 'world' }); @@ -38,7 +38,7 @@ describe('route', () => { it('returns object properly', async () => { expect.assertions(2); - const req = await fetch(server.url); + const req = await fetch(url); const res = await req.json(); expect(req.status).toStrictEqual(200); expect(res).toMatchObject({ hello: 'world' }); From d3f114f1fb50845ad97cb3b4b6e74dbff55da0fa Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Sun, 29 Dec 2019 14:50:33 -0800 Subject: [PATCH 05/15] lint --- .eslintrc.json | 3 ++- src/types/http.ts | 1 + src/types/route.ts | 3 ++- src/types/server.ts | 1 + tests/404.ts | 2 +- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 071caa9a..4154ec0c 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -25,7 +25,8 @@ "@typescript-eslint/indent": ["error", 2], "@typescript-eslint/no-explicit-any": "off", "jest/no-disabled-tests": "off", - "no-underscore-dangle": "off" + "no-underscore-dangle": "off", + "import/extensions": "off" }, "settings": { "import/resolver": { diff --git a/src/types/http.ts b/src/types/http.ts index 4bb1e209..39eba916 100644 --- a/src/types/http.ts +++ b/src/types/http.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-undef */ import { IncomingMessage, ServerResponse, Server } from 'http'; type IM = IncomingMessage; diff --git a/src/types/route.ts b/src/types/route.ts index 87023a06..22ab6e64 100644 --- a/src/types/route.ts +++ b/src/types/route.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-undef */ import { IM, SR } from './http'; // the route definition that will be ingested @@ -33,5 +34,5 @@ export { RouteObject, Options, TestOptions, - Route + Route, }; diff --git a/src/types/server.ts b/src/types/server.ts index 1d8a3207..9b9f2d5a 100644 --- a/src/types/server.ts +++ b/src/types/server.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-undef, import/prefer-default-export */ import { Server } from './http'; interface LightServer { diff --git a/tests/404.ts b/tests/404.ts index 9c819d56..2dfff1b2 100644 --- a/tests/404.ts +++ b/tests/404.ts @@ -8,7 +8,7 @@ import { const { handler } = route(); const { listen, close } = test(handler(() => ({ hello: 'world', -}))) +}))); let url: any; beforeEach(async () => { From 51bd6ff625e8da1a8bb0e58aec9f23af8e7054b0 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Sun, 29 Dec 2019 14:53:58 -0800 Subject: [PATCH 06/15] exclude __tests__ folder --- src/utils/find-routes.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/find-routes.ts b/src/utils/find-routes.ts index 91fc68a9..822c25ed 100644 --- a/src/utils/find-routes.ts +++ b/src/utils/find-routes.ts @@ -26,7 +26,9 @@ export default (routesPath: string): RouteObject[] => { const routes: string[] = []; const addRoutes = (path: any): number => { const files: string[] = glob(join('/', path), '**/*.{js,ts}'); - return routes.push(...files); + const filteredFiles = files.filter((x: string): boolean => !x.includes('__tests__')); + console.log(filteredFiles) + return routes.push(...filteredFiles); }; addRoutes(routesPath); From f653aa8573d2eafe64a6728766d277484931e8c0 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Sun, 29 Dec 2019 14:55:00 -0800 Subject: [PATCH 07/15] lint --- src/utils/find-routes.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/find-routes.ts b/src/utils/find-routes.ts index 822c25ed..a88e562c 100644 --- a/src/utils/find-routes.ts +++ b/src/utils/find-routes.ts @@ -27,7 +27,6 @@ export default (routesPath: string): RouteObject[] => { const addRoutes = (path: any): number => { const files: string[] = glob(join('/', path), '**/*.{js,ts}'); const filteredFiles = files.filter((x: string): boolean => !x.includes('__tests__')); - console.log(filteredFiles) return routes.push(...filteredFiles); }; From 70289cdbc1b65d6939fe9e9843751f181846add7 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Mon, 30 Dec 2019 00:55:15 -0800 Subject: [PATCH 08/15] add tests --- src/router.ts | 13 ++-- src/utils/add-route.ts | 1 + src/utils/find-routes.ts | 13 +++- tests/route.ts | 9 +-- tests/router.ts | 102 +++++++++++++++++++++++++++ tests/seeds/router/complex-routes.ts | 18 +++++ tests/seeds/router/routes.ts | 8 +++ tests/seeds/router/routes/index.ts | 7 ++ 8 files changed, 156 insertions(+), 15 deletions(-) create mode 100644 tests/router.ts create mode 100644 tests/seeds/router/complex-routes.ts create mode 100644 tests/seeds/router/routes.ts create mode 100644 tests/seeds/router/routes/index.ts diff --git a/src/router.ts b/src/router.ts index 387d7736..caa318c3 100644 --- a/src/router.ts +++ b/src/router.ts @@ -2,17 +2,14 @@ import { METHODS } from 'http'; import join from 'url-join'; import camelCase from 'lodash.camelcase'; +import { RouteObject } from './types/route'; + // TODO: clean up interfaces // TODO: write tests -interface RouterRouteType { - method: string; - path: string; - handler: string; -} export default (): any => { // closures - const _routes: RouterRouteType[] = []; + const _routes: RouteObject[] = []; const getRouterObj = (namespace: string): any => { const obj: any = { @@ -21,7 +18,7 @@ export default (): any => { }, }; - METHODS.forEach((method: string): void => { + [...METHODS, 'all'].forEach((method: string): void => { const name = camelCase(method); obj[name] = (rawPath: string | string[], handler: string): void => { let path: any = rawPath; @@ -30,7 +27,7 @@ export default (): any => { } path = path.map((r: string): string => join('/', namespace, r)); _routes.push({ - method, + method: (method === 'all') ? METHODS : method, path, handler: join('/', handler), }); diff --git a/src/utils/add-route.ts b/src/utils/add-route.ts index d8b2bcb6..f1182633 100644 --- a/src/utils/add-route.ts +++ b/src/utils/add-route.ts @@ -13,6 +13,7 @@ export default (router: any, route: RouteObject, opts?: Options): void => { endpoint.path = [endpoint.path]; } + /* istanbul ignore next */ endpoint.path = (endpoint.path || []).map((p: string): string => { const { name, dir } = parse(p); return join('/', dir, name); diff --git a/src/utils/find-routes.ts b/src/utils/find-routes.ts index a88e562c..a392cadc 100644 --- a/src/utils/find-routes.ts +++ b/src/utils/find-routes.ts @@ -12,11 +12,18 @@ import glob from './glob'; */ export default (routesPath: string): RouteObject[] => { // try to find the routes file - const routesFilePath = join(routesPath, '../', 'routes.js'); + const routesFilePathJS = join(routesPath, '../', 'routes.js'); + const routesFilePathTS = join(routesPath, '../', 'routes.ts'); // if it exists then use the routes from there - if (existsSync(routesFilePath)) { - const routerRoutes = require(routesFilePath) || []; // eslint-disable-line + if (existsSync(routesFilePathJS) || existsSync(routesFilePathTS)) { + let routerRoutes; + /* istanbul ignore next */ + if (existsSync(routesFilePathJS)) { + routerRoutes = require(routesFilePathJS) || []; // eslint-disable-line + } else { + routerRoutes = require(routesFilePathTS) || []; // eslint-disable-line + } const mappedRoutes = routerRoutes.map((r: any): any => ({ ...r, handler: join(routesPath, r.handler), diff --git a/tests/route.ts b/tests/route.ts index d9f472b6..31909658 100644 --- a/tests/route.ts +++ b/tests/route.ts @@ -5,16 +5,17 @@ import { test, route } from '../src/index'; let handler: any = (): any => ({ hello: 'world', }); -const { handler: fn } = route(); -const { listen, close } = test(fn(handler)); let url: any; +let server: any; beforeEach(async () => { - url = await listen(); + const { handler: fn } = route(); + server = test(fn(handler)); + url = await server.listen(); }); afterEach(async () => { - close(); + server.close(); }); describe('route', () => { diff --git a/tests/router.ts b/tests/router.ts new file mode 100644 index 00000000..c95953b1 --- /dev/null +++ b/tests/router.ts @@ -0,0 +1,102 @@ +import fetch from 'node-fetch'; +import listen from 'test-listen'; +import { join } from 'path'; + +import complexRouter from './seeds/router/complex-routes'; + +import { + server, +} from '../src/index'; + +let app: any; +let url: any; + +beforeEach(async () => { + url = await listen(app.server); +}); + +afterEach(async () => { + app.server.close(); +}); + +describe('router', () => { + describe('with router file', () => { + beforeAll(() => { + app = server({ + routes: join(__dirname, './seeds/router/routes'), + opts: { requestLogger: false }, + }); + }); + + it('works with root path', async () => { + expect.assertions(2); + const req = await fetch(url); + const res = await req.json(); + expect(req.status).toStrictEqual(200); + expect(res).toMatchObject({ hello: 'router' }); + }); + + it('works with non-file related path', async () => { + expect.assertions(2); + const req = await fetch(`${url}/testing`); + const res = await req.json(); + expect(req.status).toStrictEqual(200); + expect(res).toMatchObject({ hello: 'router' }); + }); + }); + + describe('with complex router', () => { + it('returns the correct routes', async () => { + expect(complexRouter).toMatchObject([{ method: 'GET', path: ['/home', '/test'], handler: '/index' }, + { + method: 'POST', + path: ['/testing/index'], + handler: '/testing', + }, + { + method: + ['ACL', + 'BIND', + 'CHECKOUT', + 'CONNECT', + 'COPY', + 'DELETE', + 'GET', + 'HEAD', + 'LINK', + 'LOCK', + 'M-SEARCH', + 'MERGE', + 'MKACTIVITY', + 'MKCALENDAR', + 'MKCOL', + 'MOVE', + 'NOTIFY', + 'OPTIONS', + 'PATCH', + 'POST', + 'PROPFIND', + 'PROPPATCH', + 'PURGE', + 'PUT', + 'REBIND', + 'REPORT', + 'SEARCH', + 'SOURCE', + 'SUBSCRIBE', + 'TRACE', + 'UNBIND', + 'UNLINK', + 'UNLOCK', + 'UNSUBSCRIBE'], + path: ['/graphql'], + handler: '/graphql', + }, + { + method: 'GET', + path: ['/api/v1/test'], + handler: '/api/v1/test', + }]); + }); + }); +}); diff --git a/tests/seeds/router/complex-routes.ts b/tests/seeds/router/complex-routes.ts new file mode 100644 index 00000000..fb9318e3 --- /dev/null +++ b/tests/seeds/router/complex-routes.ts @@ -0,0 +1,18 @@ +/* eslint-disable no-shadow */ +import { router } from '../../../src'; + +const { route, routes } = router(); + +route.get(['/home', '/test'], 'index'); + +route.post('testing/index', 'testing'); + +route.all('/graphql', 'graphql'); + +route.namespace('api', (route: any) => { + route.namespace('v1', (route: any) => { + route.get('/test', 'api/v1/test'); + }); +}); + +export default routes; diff --git a/tests/seeds/router/routes.ts b/tests/seeds/router/routes.ts new file mode 100644 index 00000000..e5c36725 --- /dev/null +++ b/tests/seeds/router/routes.ts @@ -0,0 +1,8 @@ +import { router } from '../../../src'; + +const { route, routes } = router(); + +route.get('/', 'index'); +route.get('/testing', 'index'); + +module.exports = routes; diff --git a/tests/seeds/router/routes/index.ts b/tests/seeds/router/routes/index.ts new file mode 100644 index 00000000..bde41d19 --- /dev/null +++ b/tests/seeds/router/routes/index.ts @@ -0,0 +1,7 @@ +import { route } from '../../../../src/index'; + +const { handler } = route(); + +module.exports = handler(() => ({ + hello: 'router', +})); From 50eccb3541a82bb97cdf44310deb22a74af48bb5 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Wed, 1 Jan 2020 13:14:43 -0800 Subject: [PATCH 09/15] create model wrapper --- src/index.ts | 1 + src/model.ts | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/model.ts diff --git a/src/index.ts b/src/index.ts index 0287059f..c8b4eb64 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ // export { default, default as light } from './light'; export { default as server } from './server'; +export { default as model } from './model'; export { default as params } from './params'; export { default as query } from './query'; export { default as test } from './test'; diff --git a/src/model.ts b/src/model.ts new file mode 100644 index 00000000..444331f6 --- /dev/null +++ b/src/model.ts @@ -0,0 +1,22 @@ +interface ModelOptions { + name?: string; +} + +interface Model { + _name?: string; + model: any; + [key: string]: any; +} + +export default (fn: any, opts: ModelOptions = {}): Model => { + const model = fn(); + const { name } = opts; + + const returnOBJ: Model = { + _name: name, + model, + }; + if (name) returnOBJ[name] = model; + + return returnOBJ; +}; From 6fb2ab25fae75d22abffbafcc5f34833440d13d7 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Sun, 19 Jan 2020 16:23:10 -0800 Subject: [PATCH 10/15] improve models and factories --- src/factory.ts | 24 ++++++++++++++++++++++++ src/index.ts | 1 + src/model.ts | 26 +++++++++++++------------- 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 src/factory.ts diff --git a/src/factory.ts b/src/factory.ts new file mode 100644 index 00000000..25486cba --- /dev/null +++ b/src/factory.ts @@ -0,0 +1,24 @@ +interface Factory { + _name?: string; + [key: string]: any; +} + +export default (name: string): any => { + return { + handler(factoryFn: any): Factory { + const factory = factoryFn(); + const returnOBJ: Factory = { + _name: name, + ...factory, + }; + + if (name) { + Object.keys(factory).forEach((key): void => { + returnOBJ[`${key}${name}`] = factory[key]; + }); + } + + return returnOBJ; + }, + }; +}; diff --git a/src/index.ts b/src/index.ts index c8b4eb64..2843463b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ export { default as server } from './server'; export { default as model } from './model'; +export { default as factory } from './factory'; export { default as params } from './params'; export { default as query } from './query'; export { default as test } from './test'; diff --git a/src/model.ts b/src/model.ts index 444331f6..a1f3f8a1 100644 --- a/src/model.ts +++ b/src/model.ts @@ -1,22 +1,22 @@ -interface ModelOptions { - name?: string; -} - interface Model { _name?: string; model: any; [key: string]: any; } -export default (fn: any, opts: ModelOptions = {}): Model => { - const model = fn(); - const { name } = opts; +export default (name: string): any => { + return { + handler(modelFn: any): Model { + const model = modelFn(); - const returnOBJ: Model = { - _name: name, - model, - }; - if (name) returnOBJ[name] = model; + const returnOBJ: Model = { + _name: name, + model, + }; - return returnOBJ; + if (name) returnOBJ[name] = model; + + return returnOBJ; + }, + }; }; From d25f5cb8e33fd59e458b5d530ef2a7135cc90277 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Mon, 20 Jan 2020 00:55:07 -0800 Subject: [PATCH 11/15] lint --- src/factory.ts | 32 +++++++++++++++----------------- src/model.ts | 24 +++++++++++------------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/factory.ts b/src/factory.ts index 25486cba..5cbe058b 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -3,22 +3,20 @@ interface Factory { [key: string]: any; } -export default (name: string): any => { - return { - handler(factoryFn: any): Factory { - const factory = factoryFn(); - const returnOBJ: Factory = { - _name: name, - ...factory, - }; +export default (name: string): any => ({ + handler(factoryFn: any): Factory { + const factory = factoryFn(); + const returnOBJ: Factory = { + _name: name, + ...factory, + }; - if (name) { - Object.keys(factory).forEach((key): void => { - returnOBJ[`${key}${name}`] = factory[key]; - }); - } + if (name) { + Object.keys(factory).forEach((key): void => { + returnOBJ[`${key}${name}`] = factory[key]; + }); + } - return returnOBJ; - }, - }; -}; + return returnOBJ; + }, +}); diff --git a/src/model.ts b/src/model.ts index a1f3f8a1..ed7e0c9b 100644 --- a/src/model.ts +++ b/src/model.ts @@ -4,19 +4,17 @@ interface Model { [key: string]: any; } -export default (name: string): any => { - return { - handler(modelFn: any): Model { - const model = modelFn(); +export default (name: string): any => ({ + handler(modelFn: any): Model { + const model = modelFn(); - const returnOBJ: Model = { - _name: name, - model, - }; + const returnOBJ: Model = { + _name: name, + model, + }; - if (name) returnOBJ[name] = model; + if (name) returnOBJ[name] = model; - return returnOBJ; - }, - }; -}; + return returnOBJ; + }, +}); From 2ba677bc394777547722701e446db788571ce85d Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Mon, 20 Jan 2020 16:51:07 -0800 Subject: [PATCH 12/15] add tests --- src/factory.ts | 25 +++++++++++++------------ src/model.ts | 25 ++++++++++++++----------- tests/factory.ts | 30 ++++++++++++++++++++++++++++++ tests/model.ts | 30 ++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 23 deletions(-) create mode 100644 tests/factory.ts create mode 100644 tests/model.ts diff --git a/src/factory.ts b/src/factory.ts index 5cbe058b..8345b839 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -3,20 +3,21 @@ interface Factory { [key: string]: any; } -export default (name: string): any => ({ - handler(factoryFn: any): Factory { - const factory = factoryFn(); - const returnOBJ: Factory = { - _name: name, - ...factory, - }; +export default (name: string): any => { + if (!name) throw new Error('factory must have a name'); + return { + handler(factoryFn: any): Factory { + const factory = factoryFn(); + const returnOBJ: Factory = { + _name: name, + ...factory, + }; - if (name) { Object.keys(factory).forEach((key): void => { returnOBJ[`${key}${name}`] = factory[key]; }); - } - return returnOBJ; - }, -}); + return returnOBJ; + }, + } +}; diff --git a/src/model.ts b/src/model.ts index ed7e0c9b..0f26c26f 100644 --- a/src/model.ts +++ b/src/model.ts @@ -4,17 +4,20 @@ interface Model { [key: string]: any; } -export default (name: string): any => ({ - handler(modelFn: any): Model { - const model = modelFn(); +export default (name: string): any => { + if (!name) throw new Error('model must have a name'); + return { + handler(modelFn: any): Model { + const model = modelFn(); - const returnOBJ: Model = { - _name: name, - model, - }; + const returnOBJ: Model = { + _name: name, + model, + }; - if (name) returnOBJ[name] = model; + returnOBJ[name] = model; - return returnOBJ; - }, -}); + return returnOBJ; + }, + }; +}; diff --git a/tests/factory.ts b/tests/factory.ts new file mode 100644 index 00000000..9cef2e92 --- /dev/null +++ b/tests/factory.ts @@ -0,0 +1,30 @@ +import factory from '../src/factory'; + +describe('factory', () => { + describe('with name', () => { + const { handler } = factory('Test'); + const test = handler(() => ({ hello: 'world' })); + + it('exports factory', async () => { + expect.assertions(1); + expect(test.hello).toBe('world'); + }); + + it('exports named factory', async () => { + expect.assertions(1); + expect(test.helloTest).toBe('world'); + }); + + it('has a name', async () => { + expect.assertions(1); + expect(test._name).toBe('Test'); + }); + }); + + describe('without name', () => { + it('throws an error', async () => { + expect.assertions(1); + expect(() => factory('')).toThrow('factory must have a name'); + }); + }); +}); diff --git a/tests/model.ts b/tests/model.ts new file mode 100644 index 00000000..55a5284e --- /dev/null +++ b/tests/model.ts @@ -0,0 +1,30 @@ +import model from '../src/model'; + +describe('model', () => { + describe('with name', () => { + const { handler } = model('Test'); + const test = handler(() => ({ hello: 'world' })); + + it('exports model', async () => { + expect.assertions(1); + expect(test.model).toMatchObject({ hello: 'world' }) + }); + + it('exports named model', async () => { + expect.assertions(1); + expect(test.Test).toMatchObject({ hello: 'world' }) + }); + + it('has a name', async () => { + expect.assertions(1); + expect(test._name).toBe('Test'); + }); + }); + + describe('without name', () => { + it('throws an error', async () => { + expect.assertions(1); + expect(() => model('')).toThrow('model must have a name'); + }); + }); +}); From 34b56ead82483405adabd09eacca093396257952 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Mon, 20 Jan 2020 16:52:42 -0800 Subject: [PATCH 13/15] lint --- src/factory.ts | 2 +- tests/model.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/factory.ts b/src/factory.ts index 8345b839..2fa34f2f 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -19,5 +19,5 @@ export default (name: string): any => { return returnOBJ; }, - } + }; }; diff --git a/tests/model.ts b/tests/model.ts index 55a5284e..9ae6eb0b 100644 --- a/tests/model.ts +++ b/tests/model.ts @@ -7,12 +7,12 @@ describe('model', () => { it('exports model', async () => { expect.assertions(1); - expect(test.model).toMatchObject({ hello: 'world' }) + expect(test.model).toMatchObject({ hello: 'world' }); }); it('exports named model', async () => { expect.assertions(1); - expect(test.Test).toMatchObject({ hello: 'world' }) + expect(test.Test).toMatchObject({ hello: 'world' }); }); it('has a name', async () => { From d2879a2b309660b8d33621b832ec28b3af81cf02 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Mon, 20 Jan 2020 18:43:08 -0800 Subject: [PATCH 14/15] update docs --- website/package-lock.json | 195 ++++++++++++++++++-------- website/package.json | 3 +- website/src/components/Navigation.jsx | 4 +- website/src/next.config.js | 37 +---- website/src/pages/docs.jsx | 142 ------------------- website/src/pages/guides.jsx | 142 ------------------- 6 files changed, 143 insertions(+), 380 deletions(-) delete mode 100644 website/src/pages/docs.jsx delete mode 100644 website/src/pages/guides.jsx diff --git a/website/package-lock.json b/website/package-lock.json index f97f3639..b866eba8 100644 --- a/website/package-lock.json +++ b/website/package-lock.json @@ -1689,24 +1689,45 @@ "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" }, "cacache": { - "version": "11.3.2", - "resolved": "https://registry.npmjs.org/cacache/-/cacache-11.3.2.tgz", - "integrity": "sha512-E0zP4EPGDOaT2chM08Als91eYnf8Z+eH1awwwVsngUmgppfM5jjJ8l3z5vO5p5w/I3LsiXawb1sW0VY65pQABg==", + "version": "12.0.3", + "resolved": "https://registry.npmjs.org/cacache/-/cacache-12.0.3.tgz", + "integrity": "sha512-kqdmfXEGFepesTuROHMs3MpFLWrPkSSpRqOw80RCflZXy/khxaArvFrQ7uJxSUduzAufc6G0g1VUCOZXxWavPw==", "requires": { - "bluebird": "^3.5.3", + "bluebird": "^3.5.5", "chownr": "^1.1.1", "figgy-pudding": "^3.5.1", - "glob": "^7.1.3", + "glob": "^7.1.4", "graceful-fs": "^4.1.15", + "infer-owner": "^1.0.3", "lru-cache": "^5.1.1", "mississippi": "^3.0.0", "mkdirp": "^0.5.1", "move-concurrently": "^1.0.1", "promise-inflight": "^1.0.1", - "rimraf": "^2.6.2", + "rimraf": "^2.6.3", "ssri": "^6.0.1", "unique-filename": "^1.1.1", "y18n": "^4.0.0" + }, + "dependencies": { + "bluebird": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", + "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==" + }, + "glob": { + "version": "7.1.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", + "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + } } }, "cache-base": { @@ -1807,9 +1828,9 @@ } }, "chownr": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.1.tgz", - "integrity": "sha512-j38EvO5+LHX84jlo6h4UzmOwi0UgW61WRyPtJz4qaadK5eY3BTS5TY/S1Stc3Uk2lIM6TPevAlULiEJwie860g==" + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.3.tgz", + "integrity": "sha512-i70fVHhmV3DtTl6nqvZOnIjbY0Pe4kAUjwHj8z0zAdgBtYrJyYwLKCCuRBQ5ppkyL0AkN7HKRnETdmdp1zqNXw==" }, "chrome-trace-event": { "version": "1.0.0", @@ -2253,9 +2274,9 @@ "integrity": "sha512-RGatb1BUwVMBzV8DRo9Kapc55bdGfAxMcukVk+ZzE3Ts8xaTve0GVz730kBDxjhEBU2LK+RPuAcjZb00Q3O24w==" }, "cyclist": { - "version": "0.2.2", - "resolved": "https://registry.npmjs.org/cyclist/-/cyclist-0.2.2.tgz", - "integrity": "sha1-GzN5LhHpFKL9bW7WRHRkRE5fpkA=" + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/cyclist/-/cyclist-1.0.1.tgz", + "integrity": "sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=" }, "date-fns": { "version": "1.30.1", @@ -2942,7 +2963,8 @@ }, "ansi-regex": { "version": "2.1.1", - "bundled": true + "bundled": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -2960,11 +2982,13 @@ }, "balanced-match": { "version": "1.0.0", - "bundled": true + "bundled": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2977,15 +3001,18 @@ }, "code-point-at": { "version": "1.1.0", - "bundled": true + "bundled": true, + "optional": true }, "concat-map": { "version": "0.0.1", - "bundled": true + "bundled": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", - "bundled": true + "bundled": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -3088,7 +3115,8 @@ }, "inherits": { "version": "2.0.3", - "bundled": true + "bundled": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -3098,6 +3126,7 @@ "is-fullwidth-code-point": { "version": "1.0.0", "bundled": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3110,17 +3139,20 @@ "minimatch": { "version": "3.0.4", "bundled": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } }, "minimist": { "version": "0.0.8", - "bundled": true + "bundled": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -3137,6 +3169,7 @@ "mkdirp": { "version": "0.5.1", "bundled": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -3209,7 +3242,8 @@ }, "number-is-nan": { "version": "1.0.1", - "bundled": true + "bundled": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -3219,6 +3253,7 @@ "once": { "version": "1.4.0", "bundled": true, + "optional": true, "requires": { "wrappy": "1" } @@ -3294,7 +3329,8 @@ }, "safe-buffer": { "version": "5.1.2", - "bundled": true + "bundled": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -3324,6 +3360,7 @@ "string-width": { "version": "1.0.2", "bundled": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3341,6 +3378,7 @@ "strip-ansi": { "version": "3.0.1", "bundled": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3379,11 +3417,13 @@ }, "wrappy": { "version": "1.0.2", - "bundled": true + "bundled": true, + "optional": true }, "yallist": { "version": "3.0.3", - "bundled": true + "bundled": true, + "optional": true } } }, @@ -3760,6 +3800,11 @@ "resolved": "https://registry.npmjs.org/indexof/-/indexof-0.0.1.tgz", "integrity": "sha1-gtwzbSMrkGIXnQWrMpOmYFn9Q10=" }, + "infer-owner": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/infer-owner/-/infer-owner-1.0.4.tgz", + "integrity": "sha512-IClj+Xz94+d7irH5qRyfJonOdfTzuDaifE6ZPWfx0N0+/ATZCbuTPq2prFl526urkQd90WyUKIh1DfBQ2hMz9A==" + }, "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", @@ -4011,6 +4056,11 @@ "resolved": "https://registry.npmjs.org/is-word-character/-/is-word-character-1.0.3.tgz", "integrity": "sha512-0wfcrFgOOOBdgRNT9H33xe6Zi6yhX/uoc4U8NBZGeQQB0ctU1dnlNTyL9JM2646bHDTpsDm1Brb3VPoCIMrd/A==" }, + "is-wsl": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-1.1.0.tgz", + "integrity": "sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0=" + }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -4867,11 +4917,11 @@ "integrity": "sha512-0DTvPVU3ed8+HNXOu5Bs+o//Mbdj9VNQMUOe9oKCwh8l0GNwpTDMKCWbRjgtD291AWnkAgkqA/LOnQS8AmS1tw==" }, "parallel-transform": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/parallel-transform/-/parallel-transform-1.1.0.tgz", - "integrity": "sha1-1BDwZbBdojCB/NEPKIVMKb2jOwY=", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/parallel-transform/-/parallel-transform-1.2.0.tgz", + "integrity": "sha512-P2vSmIu38uIlvdcU7fDkyrxj33gTUy/ABO5ZUbGowxNCopBq/OoD42bP4UmMrJoPyk4Uqf0mu3mtWBhHCZD8yg==", "requires": { - "cyclist": "~0.2.2", + "cyclist": "^1.0.1", "inherits": "^2.0.3", "readable-stream": "^2.1.5" } @@ -6315,9 +6365,9 @@ } }, "stream-shift": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/stream-shift/-/stream-shift-1.0.0.tgz", - "integrity": "sha1-1cdSgl5TZ+eG944Y5EXqIjoVWVI=" + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/stream-shift/-/stream-shift-1.0.1.tgz", + "integrity": "sha512-AiisoFqQ0vbGcZgQPY1cdP2I76glaVA/RauYR4G4thNFgkTqr90yXTo4LYX60Jl+sIlPNHHdGSwo01AvbKUSVQ==" }, "string-hash": { "version": "1.1.3", @@ -6510,20 +6560,26 @@ } }, "terser-webpack-plugin": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-1.2.3.tgz", - "integrity": "sha512-GOK7q85oAb/5kE12fMuLdn2btOS9OBZn4VsecpHDywoUC/jLhSAKOiYo0ezx7ss2EXPMzyEWFoE0s1WLE+4+oA==", + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-1.4.3.tgz", + "integrity": "sha512-QMxecFz/gHQwteWwSo5nTc6UaICqN1bMedC5sMtUc7y3Ha3Q8y6ZO0iCR8pq4RJC8Hjf0FEPEHZqcMB/+DFCrA==", "requires": { - "cacache": "^11.0.2", - "find-cache-dir": "^2.0.0", + "cacache": "^12.0.2", + "find-cache-dir": "^2.1.0", + "is-wsl": "^1.1.0", "schema-utils": "^1.0.0", - "serialize-javascript": "^1.4.0", + "serialize-javascript": "^2.1.2", "source-map": "^0.6.1", - "terser": "^3.16.1", - "webpack-sources": "^1.1.0", - "worker-farm": "^1.5.2" + "terser": "^4.1.2", + "webpack-sources": "^1.4.0", + "worker-farm": "^1.7.0" }, "dependencies": { + "commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==" + }, "find-cache-dir": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/find-cache-dir/-/find-cache-dir-2.1.0.tgz", @@ -6561,9 +6617,9 @@ } }, "p-limit": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.0.tgz", - "integrity": "sha512-pZbTJpoUsCzV48Mc9Nh51VbwO0X9cuPFE8gYwx9BTCt9SF8/b7Zljd2fVgOxhIF/HDTKgpVzs+GPhyKfjLLFRQ==", + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.2.tgz", + "integrity": "sha512-WGR+xHecKTr7EbUEhyLSh5Dube9JtdiG78ufaeLxTgpudf/20KqyMioIUZJAezlTIi6evxuoUs9YXc11cU+yzQ==", "requires": { "p-try": "^2.0.0" } @@ -6603,6 +6659,38 @@ "ajv-errors": "^1.0.0", "ajv-keywords": "^3.1.0" } + }, + "serialize-javascript": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-2.1.2.tgz", + "integrity": "sha512-rs9OggEUF0V4jUSecXazOYsLfu7OGK2qIn3c7IPBiffz32XniEp/TX9Xmc9LQfK2nQ2QKHvZ2oygKUGU0lG4jQ==" + }, + "terser": { + "version": "4.6.3", + "resolved": "https://registry.npmjs.org/terser/-/terser-4.6.3.tgz", + "integrity": "sha512-Lw+ieAXmY69d09IIc/yqeBqXpEQIpDGZqT34ui1QWXIUpR2RjbqEkT8X7Lgex19hslSqcWM5iMN2kM11eMsESQ==", + "requires": { + "commander": "^2.20.0", + "source-map": "~0.6.1", + "source-map-support": "~0.5.12" + } + }, + "webpack-sources": { + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/webpack-sources/-/webpack-sources-1.4.3.tgz", + "integrity": "sha512-lgTS3Xhv1lCOKo7SA5TjKXMjpSM4sBjNV5+q2bqesbSPs5FjGmU6jjtBSkX9b4qW87vDIsCIlUPOEhbZrMdjeQ==", + "requires": { + "source-list-map": "^2.0.0", + "source-map": "~0.6.1" + } + }, + "worker-farm": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/worker-farm/-/worker-farm-1.7.0.tgz", + "integrity": "sha512-rvw3QTZc8lAxyVrqcSGVm5yP/IJ2UcB3U0graE3LCFoZ0Yn2x4EoVSqJKdB/T5M+FLcRPjz4TDacRf3OCfNUzw==", + "requires": { + "errno": "~0.1.7" + } } } }, @@ -6678,9 +6766,9 @@ } }, "tree-kill": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/tree-kill/-/tree-kill-1.2.1.tgz", - "integrity": "sha512-4hjqbObwlh2dLyW4tcz0Ymw0ggoaVDMveUB9w8kFSQScdRLo0gxO9J7WFcUBo+W3C1TLdFIEwNOWebgZZ0RH9Q==", + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/tree-kill/-/tree-kill-1.2.2.tgz", + "integrity": "sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A==", "dev": true }, "trim": { @@ -6819,9 +6907,9 @@ } }, "unique-slug": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/unique-slug/-/unique-slug-2.0.1.tgz", - "integrity": "sha512-n9cU6+gITaVu7VGj1Z8feKMmfAjEAQGhwD9fE3zvpRRa0wEIx8ODYkVGfSc94M2OX00tUFV8wH3zYbm1I8mxFg==", + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/unique-slug/-/unique-slug-2.0.2.tgz", + "integrity": "sha512-zoWr9ObaxALD3DOPfjPSqxt4fnZiWblxHIgeWqW8x7UqDzEtHEQLzji2cuJYQFCU6KmoJikOYAZlrTHHebjx2w==", "requires": { "imurmurhash": "^0.1.4" } @@ -6942,11 +7030,6 @@ "querystring": "0.2.0" } }, - "url-join": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/url-join/-/url-join-4.0.1.tgz", - "integrity": "sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA==" - }, "use": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/use/-/use-3.1.1.tgz", @@ -7199,9 +7282,9 @@ "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==" }, "yallist": { - "version": "3.0.3", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.0.3.tgz", - "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==" + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", + "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==" }, "yargs": { "version": "12.0.5", diff --git a/website/package.json b/website/package.json index 30d4f9e3..e76ed1c5 100644 --- a/website/package.json +++ b/website/package.json @@ -44,8 +44,7 @@ "react-runkit": "^0.9.0", "react-syntax-highlighter": "^11.0.2", "react-text-loop": "^2.0.2", - "tailwindcss": "^1.1.2", - "url-join": "^4.0.1" + "tailwindcss": "^1.1.2" }, "devDependencies": { "concurrently": "^4.1.2", diff --git a/website/src/components/Navigation.jsx b/website/src/components/Navigation.jsx index 9d59a364..ff7d0008 100644 --- a/website/src/components/Navigation.jsx +++ b/website/src/components/Navigation.jsx @@ -51,10 +51,10 @@ class Navigation extends React.Component { Home - + Guides - + Docs diff --git a/website/src/next.config.js b/website/src/next.config.js index db2aef89..c207b3d5 100644 --- a/website/src/next.config.js +++ b/website/src/next.config.js @@ -1,38 +1,3 @@ const withCSS = require("@zeit/next-css"); -const join = require('url-join'); -const guides = require('../../guides/guides.json'); -const docs = require('../../docs/docs.json'); -const { NODE_ENV, COMMIT_REF } = process.env; - -const mappedURL = `https://raw.githubusercontent.com/ludicrousxyz/light/${COMMIT_REF || 'master'}`; - -module.exports = withCSS({ - async exportPathMap() { - const routes = { - '/': { page: '/' }, - }; - - const processRoutes = (obj, prefix) => { - Object.keys(obj).forEach((key) => { - const guide = obj[key]; - if (typeof guide !== 'string') { - return processRoutes(guide, prefix); - } - routes[join(prefix, guide)] = { - page: prefix, - query: { title: guide }, - }; - return null; - }); - }; - - processRoutes(guides, '/guides'); - processRoutes(docs, '/docs'); - - return routes; - }, - env: { - BASE_URL: NODE_ENV === 'development' ? 'http://localhost:8080' : mappedURL, - }, -}); +module.exports = withCSS(); diff --git a/website/src/pages/docs.jsx b/website/src/pages/docs.jsx deleted file mode 100644 index 058d125a..00000000 --- a/website/src/pages/docs.jsx +++ /dev/null @@ -1,142 +0,0 @@ -import React from 'react'; -import join from 'url-join'; -import fetch from 'isomorphic-unfetch'; -import Markdown from 'react-markdown'; -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; - -import CodeBlock from '../components/CodeBlock'; -import Sidebar from '../components/Sidebar'; - -export default class Posts extends React.Component { - static async getInitialProps({ query }) { - let { title } = query; - - const fetchPost = await fetch(join(process.env.BASE_URL, `docs/${title}.md`)); - const post = await fetchPost.text(); - const split = post.split('---'); - split.shift(); - const metadata = split.shift().trim(); - const content = split.join('---').trim(); - - const response = {}; - response.content = content; - - metadata.split('\n').forEach((line) => { - const [attr, val] = line.split(':'); - response[attr.trim()] = val.trim(); - }); - - const fetchSidebar = await fetch(join(process.env.BASE_URL, `docs/docs.json`)); - const menu = await fetchSidebar.json(); - - const flatMenu = Object.assign( - {}, - ...function _flatten(o) { - return [].concat(...Object.keys(o) - .map(k => - typeof o[k] === 'object' ? - _flatten(o[k]) : - ({[k]: o[k]}) - ) - ); - }(menu) - ) - - const previous = Object.keys(flatMenu).reverse().reduce((acc, key) => { - const val = flatMenu[key]; - if (val === title) { - return true; - } - if (acc === true) { - return key; - } - return acc; - }, false); - - const next = Object.keys(flatMenu).reduce((acc, key) => { - const val = flatMenu[key]; - if (val === title) { - return true; - } - if (acc === true) { - return key; - } - return acc; - }, false); - - const cont = {}; - - if (previous !== false && previous !== true) { - cont.previous = { - name: previous, - path: flatMenu[previous], - }; - } - - if (next !== false && next !== true) { - cont.next = { - name: next, - path: flatMenu[next], - }; - } - - return { - query, - menu, - cont, - path: title, - ...response, - }; - } - - render() { - const { title, subtitle, content, menu, path, cont } = this.props; - return ( -
-
-

{ title }

-

{ subtitle }

-
-
-
-
- -
-
- -
-
- { cont.previous ? ( - - - - - { cont.previous.name } - - ) : null } -
-
-
- { cont.next ? ( - - { cont.next.name } - - - - - ) : null } -
-
-
-
-
-
- ); - } -} diff --git a/website/src/pages/guides.jsx b/website/src/pages/guides.jsx deleted file mode 100644 index 5a7fd7a1..00000000 --- a/website/src/pages/guides.jsx +++ /dev/null @@ -1,142 +0,0 @@ -import React from 'react'; -import join from 'url-join'; -import fetch from 'isomorphic-unfetch'; -import Markdown from 'react-markdown'; -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; - -import CodeBlock from '../components/CodeBlock'; -import Sidebar from '../components/Sidebar'; - -export default class Posts extends React.Component { - static async getInitialProps({ query }) { - let { title } = query; - - const fetchPost = await fetch(join(process.env.BASE_URL, `guides/${title}.md`)); - const post = await fetchPost.text(); - const split = post.split('---'); - split.shift(); - const metadata = split.shift().trim(); - const content = split.join('---').trim(); - - const response = {}; - response.content = content; - - metadata.split('\n').forEach((line) => { - const [attr, val] = line.split(':'); - response[attr.trim()] = val.trim(); - }); - - const fetchSidebar = await fetch(join(process.env.BASE_URL, `guides/guides.json`)); - const menu = await fetchSidebar.json(); - - const flatMenu = Object.assign( - {}, - ...function _flatten(o) { - return [].concat(...Object.keys(o) - .map(k => - typeof o[k] === 'object' ? - _flatten(o[k]) : - ({[k]: o[k]}) - ) - ); - }(menu) - ) - - const previous = Object.keys(flatMenu).reverse().reduce((acc, key) => { - const val = flatMenu[key]; - if (val === title) { - return true; - } - if (acc === true) { - return key; - } - return acc; - }, false); - - const next = Object.keys(flatMenu).reduce((acc, key) => { - const val = flatMenu[key]; - if (val === title) { - return true; - } - if (acc === true) { - return key; - } - return acc; - }, false); - - const cont = {}; - - if (previous !== false && previous !== true) { - cont.previous = { - name: previous, - path: flatMenu[previous], - }; - } - - if (next !== false && next !== true) { - cont.next = { - name: next, - path: flatMenu[next], - }; - } - - return { - query, - menu, - cont, - path: title, - ...response, - }; - } - - render() { - const { title, subtitle, content, menu, path, cont } = this.props; - return ( -
-
-

{ title }

-

{ subtitle }

-
-
-
-
- -
-
- -
-
- { cont.previous ? ( - - - - - { cont.previous.name } - - ) : null } -
-
-
- { cont.next ? ( - - { cont.next.name } - - - - - ) : null } -
-
-
-
-
-
- ); - } -} From 513f45f76f7ee51f2fa0c8b2509bbfbe2055a071 Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Mon, 20 Jan 2020 18:49:21 -0800 Subject: [PATCH 15/15] add .bookignore --- .bookignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .bookignore diff --git a/.bookignore b/.bookignore new file mode 100644 index 00000000..b43bf86b --- /dev/null +++ b/.bookignore @@ -0,0 +1 @@ +README.md