diff --git a/app/controllers/index.ts b/app/controllers/index.ts new file mode 100644 index 0000000..d2a1c4f --- /dev/null +++ b/app/controllers/index.ts @@ -0,0 +1 @@ +export * from './sessions.controller' diff --git a/configs/customs.ts b/configs/customs.ts index 83d381e..56a9f40 100644 --- a/configs/customs.ts +++ b/configs/customs.ts @@ -1,12 +1,12 @@ import { RoutingControllersOptions } from 'routing-controllers' -import * as path from 'path' import * as interceptors from './interceptors' +import * as controllers from '../app/controllers' const objectToArray = (dict: object): Array => Object.keys(dict).map(name => dict[name]) export const routingConfigs: RoutingControllersOptions = { - controllers: [path.join(__dirname, '../app/controllers/**/*.ts')], + controllers: objectToArray(controllers), // global interceptors interceptors: objectToArray(interceptors), diff --git a/configs/middlewares.ts b/configs/middlewares.ts index c8f57e9..d1520bf 100644 --- a/configs/middlewares.ts +++ b/configs/middlewares.ts @@ -1,11 +1,12 @@ import * as Koa from 'koa' -import * as kcors from 'kcors' +import * as kcors from '@koa/cors' import * as logger from 'koa-logger' import * as bodyParser from 'koa-bodyparser' import { Environment } from './environments' export const useMiddlewares = (app: T): T => { + // cors options: https://github.com/koajs/cors#corsoptions app.use(kcors()) Environment.identity !== 'test' && app.use(logger()) diff --git a/package.json b/package.json index 342922b..0574f94 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "koa2-typescript-guide", - "version": "0.2.0", + "version": "0.2.1", "description": "the best practice of building Koa2 with TypeScript", "main": "app.ts", "scripts": { "test": "mocha", "lint": "tslint -c tslint.yml -p tsconfig.json", "start": "export NODE_ENV=development; nodemon --config nodemon.json", - "build": "npm run lint && tsc", + "build": "yarn run clean && ncc build app.ts", "mongo": "docker-compose -f compose.yml up -d", "clean": "rm -rf dist" }, @@ -25,7 +25,6 @@ "@haifeng-fe/tslint-config": "^1.0.0", "@types/chai": "^4.1.2", "@types/chai-http": "^3.0.4", - "@types/kcors": "^2.2.2", "@types/koa": "^2.0.48", "@types/koa-bodyparser": "^4.2.0", "@types/mocha": "^2.2.48", @@ -33,13 +32,14 @@ "chai": "^4.1.2", "chai-http": "^3.0.0", "mocha": "^5.0.0", + "ncc": "^0.3.6", "nodemon": "^1.18.10", "pre-commit": "^1.2.2", "ts-node": "^8.0.2", - "typescript": "^3.3.3333" + "typescript": "^3.3.4000" }, "dependencies": { - "kcors": "^2.2.1", + "@koa/cors": "^3.0.0", "koa": "^2.7.0", "koa-bodyparser": "^4.2.0", "koa-logger": "^3.1.0", diff --git a/yarn.lock b/yarn.lock index 73da7ce..3154065 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7,6 +7,13 @@ resolved "https://registry.yarnpkg.com/@haifeng-fe/tslint-config/-/tslint-config-1.0.0.tgz#074900b4abcfa25d092d019b811fa4350a7c24e7" integrity sha512-j36k3+dP5vc7jGkdZhJ75gnv4w9/q1EW00obf5tOOzPir1zNyinFo1r9RCQMT8NFvZT+rdzpBVsvIF2lqFj2tg== +"@koa/cors@^3.0.0": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@koa/cors/-/cors-3.0.0.tgz#df021b4df2dadf1e2b04d7c8ddf93ba2d42519cb" + integrity sha512-hDp+cXj6vTYSwHRJfiSpnf5dTMv3FmqNKh1or9BPJk4SHOviHnK9GoL9dT0ypt/E+hlkRkZ9edHylcosW3Ghrw== + dependencies: + vary "^1.1.2" + "@types/accepts@*": version "1.3.5" resolved "https://registry.yarnpkg.com/@types/accepts/-/accepts-1.3.5.tgz#c34bec115cfc746e04fe5a059df4ce7e7b391575" @@ -80,13 +87,6 @@ resolved "https://registry.yarnpkg.com/@types/http-assert/-/http-assert-1.4.0.tgz#41d173466e396e99a14d75f7160cc997f2f9ed8b" integrity sha512-TZDqvFW4nQwL9DVSNJIJu4lPLttKgzRF58COa7Vs42Ki/MrhIqUbeIw0MWn4kGLiZLXB7oCBibm7nkSjPkzfKQ== -"@types/kcors@^2.2.2": - version "2.2.3" - resolved "https://registry.yarnpkg.com/@types/kcors/-/kcors-2.2.3.tgz#e6532132492173a8681dd85774f380834b96f571" - integrity sha512-ICwth6ZODezC22Yvkk5fMIESkAHhbjoqNn+yIBPJPKfG2sP9+fPmMWGBUrI3h1wjTBmbrQ1fBE6qoUMmG7yo9w== - dependencies: - "@types/koa" "*" - "@types/keygrip@*": version "1.0.1" resolved "https://registry.yarnpkg.com/@types/keygrip/-/keygrip-1.0.1.tgz#ff540462d2fb4d0a88441ceaf27d287b01c3d878" @@ -602,6 +602,11 @@ color-name@1.1.3: resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25" integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU= +colors@1.2.3: + version "1.2.3" + resolved "https://registry.yarnpkg.com/colors/-/colors-1.2.3.tgz#1b152a9c4f6c9f74bc4bb96233ad0b7983b79744" + integrity sha512-qTfM2pNFeMZcLvf/RbrVAzDEVttZjFhaApfx9dplNjvHSX88Ui66zBRb/4YGob/xUWxDceirgoC1lT676asfCQ== + combined-stream@^1.0.5: version "1.0.7" resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.7.tgz#2d1d24317afb8abe95d6d2c0b07b57813539d828" @@ -733,6 +738,11 @@ crypto-random-string@^1.0.0: resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e" integrity sha1-ojD2T1aDEOFJgAmUB5DsmVRbyn4= +dateformat@3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae" + integrity sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q== + debug@3.1.0, debug@~3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" @@ -1535,11 +1545,6 @@ js-yaml@^3.11.0: argparse "^1.0.7" esprima "^4.0.0" -kcors@^2.2.1: - version "2.2.2" - resolved "https://registry.yarnpkg.com/kcors/-/kcors-2.2.2.tgz#b6250e7a4f0a33c8f477b7fd0dfa11a3f3ca518d" - integrity sha512-rIqbKa2S0gT0wC/790jsQM6hNpABHBNWQ7+XYS1xJV6zOGxlanW+RtCmlDn6wPZsGpRk371yy8abfBgl2OTavg== - keygrip@~1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/keygrip/-/keygrip-1.0.3.tgz#399d709f0aed2bab0a059e0cdd3a5023a053e1dc" @@ -1952,6 +1957,16 @@ nanomatch@^1.2.9: snapdragon "^0.8.1" to-regex "^3.0.1" +ncc@^0.3.6: + version "0.3.6" + resolved "https://registry.yarnpkg.com/ncc/-/ncc-0.3.6.tgz#11b47af973d7f2374af46bc0d8533366acc908eb" + integrity sha1-EbR6+XPX8jdK9GvA2FMzZqzJCOs= + dependencies: + mkdirp "^0.5.1" + rimraf "^2.6.1" + tracer "^0.8.7" + ws "^2.3.1" + needle@^2.2.1: version "2.2.4" resolved "https://registry.yarnpkg.com/needle/-/needle-2.2.4.tgz#51931bff82533b1928b7d1d69e01f1b00ffd2a4e" @@ -2494,6 +2509,11 @@ safe-buffer@5.1.2, safe-buffer@^5.0.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, s resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g== +safe-buffer@~5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.0.1.tgz#d263ca54696cd8a306b5ca6551e92de57918fbe7" + integrity sha1-0mPKVGls2KMGtcplUekt5XkY++c= + safe-regex@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/safe-regex/-/safe-regex-1.1.0.tgz#40a3669f3b077d1e943d44629e157dd48023bf2e" @@ -2823,6 +2843,11 @@ timed-out@^4.0.0: resolved "https://registry.yarnpkg.com/timed-out/-/timed-out-4.0.1.tgz#f32eacac5a175bea25d7fab565ab3ed8741ef56f" integrity sha1-8y6srFoXW+ol1/q1Zas+2HQe9W8= +tinytim@0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/tinytim/-/tinytim-0.1.1.tgz#c968a1e5559ad9553224ef7627bab34e3caef8a8" + integrity sha1-yWih5VWa2VUyJO92J7qzTjyu+Kg= + to-object-path@^0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/to-object-path/-/to-object-path-0.3.0.tgz#297588b7b0e7e0ac08e04e672f85c1f4999e17af" @@ -2860,6 +2885,16 @@ touch@^3.1.0: dependencies: nopt "~1.0.10" +tracer@^0.8.7: + version "0.8.15" + resolved "https://registry.yarnpkg.com/tracer/-/tracer-0.8.15.tgz#c4bb5b8c788ed3d7106c081288e22fd5a5abc474" + integrity sha512-ZQzlhd6zZFIpAhACiZkxLjl65XqVwi8t8UEBVGRIHAQN6nj55ftJWiFell+WSqWCP/vEycrIbUSuiyMwul+TFw== + dependencies: + colors "1.2.3" + dateformat "3.0.3" + mkdirp "^0.5.1" + tinytim "0.1.1" + ts-node@^8.0.2: version "8.0.2" resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.0.2.tgz#9ecdf8d782a0ca4c80d1d641cbb236af4ac1b756" @@ -2919,10 +2954,15 @@ typeorm@^0.2.13: yargonaut "^1.1.2" yargs "^12.0.5" -typescript@^3.3.3333: - version "3.3.3333" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.3.3333.tgz#171b2c5af66c59e9431199117a3bcadc66fdcfd6" - integrity sha512-JjSKsAfuHBE/fB2oZ8NxtRTk5iGcg6hkYXMnZ3Wc+b2RSqejEqTaem11mHASMnFilHrax3sLK0GDzcJrekZYLw== +typescript@^3.3.4000: + version "3.3.4000" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.3.4000.tgz#76b0f89cfdbf97827e1112d64f283f1151d6adf0" + integrity sha512-jjOcCZvpkl2+z7JFn0yBOoLQyLoIkNZAs/fYJkUG6VKy6zLPHJGfQJYFHzibB6GJaF/8QrcECtlQ5cpvRHSMEA== + +ultron@~1.1.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.1.1.tgz#9fe1536a10a664a65266a1e3ccf85fd36302bc9c" + integrity sha512-UIEXBNeYmKptWH6z8ZnqTeS8fV74zG0/eRU9VGkpzz+LIJNs8W/zM/L+7ctCkRrgbNnnR0xxw4bKOr0cW0N0Og== undefsafe@^2.0.2: version "2.0.2" @@ -3079,6 +3119,14 @@ write-file-atomic@^2.0.0: imurmurhash "^0.1.4" signal-exit "^3.0.2" +ws@^2.3.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/ws/-/ws-2.3.1.tgz#6b94b3e447cb6a363f785eaf94af6359e8e81c80" + integrity sha1-a5Sz5EfLajY/eF6vlK9jWejoHIA= + dependencies: + safe-buffer "~5.0.1" + ultron "~1.1.0" + xdg-basedir@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-3.0.0.tgz#496b2cc109eca8dbacfe2dc72b603c17c5870ad4"