@ditojs/server 0.275.0 → 1.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +25 -43
- package/src/app/Application.js +105 -111
- package/src/app/Validator.js +6 -3
- package/src/app/index.js +2 -2
- package/src/cli/db/createMigration.js +1 -1
- package/src/cli/db/index.js +7 -7
- package/src/cli/db/listAssetConfig.js +1 -1
- package/src/cli/db/reset.js +1 -1
- package/src/cli/index.js +14 -5
- package/src/controllers/AdminController.js +164 -158
- package/src/controllers/CollectionController.js +8 -29
- package/src/controllers/Controller.js +71 -76
- package/src/controllers/ControllerAction.js +37 -18
- package/src/controllers/MemberAction.js +2 -2
- package/src/controllers/ModelController.js +4 -4
- package/src/controllers/RelationController.js +3 -3
- package/src/controllers/{UserController.js → UsersController.js} +8 -13
- package/src/controllers/index.js +5 -5
- package/src/decorators/action.js +3 -3
- package/src/decorators/authorize.js +1 -1
- package/src/decorators/index.js +6 -6
- package/src/decorators/parameters.js +1 -1
- package/src/decorators/returns.js +1 -1
- package/src/decorators/scope.js +1 -1
- package/src/decorators/transacted.js +1 -1
- package/src/errors/AssetError.js +1 -1
- package/src/errors/AuthenticationError.js +1 -1
- package/src/errors/AuthorizationError.js +1 -1
- package/src/errors/ControllerError.js +1 -1
- package/src/errors/DatabaseError.js +12 -3
- package/src/errors/GraphError.js +1 -1
- package/src/errors/ModelError.js +1 -1
- package/src/errors/NotFoundError.js +1 -1
- package/src/errors/NotImplementedError.js +1 -1
- package/src/errors/QueryBuilderError.js +1 -1
- package/src/errors/RelationError.js +1 -1
- package/src/errors/ValidationError.js +1 -1
- package/src/errors/WrappedError.js +1 -1
- package/src/errors/index.js +14 -14
- package/src/graph/DitoGraphProcessor.js +2 -1
- package/src/graph/graph.js +1 -1
- package/src/graph/index.js +3 -3
- package/src/index.js +11 -9
- package/src/lib/index.js +2 -2
- package/src/middleware/createTransaction.js +1 -1
- package/src/middleware/findRoute.js +3 -2
- package/src/middleware/handleConnectMiddleware.js +88 -0
- package/src/middleware/handleError.js +1 -1
- package/src/middleware/handleRoute.js +3 -3
- package/src/middleware/index.js +8 -7
- package/src/mixins/AssetMixin.js +1 -1
- package/src/mixins/UserMixin.js +1 -1
- package/src/mixins/index.js +4 -4
- package/src/models/AssetModel.js +2 -2
- package/src/models/Model.js +16 -12
- package/src/models/RelationAccessor.js +1 -1
- package/src/models/SessionModel.js +2 -2
- package/src/models/TimeStampedModel.js +2 -2
- package/src/models/UserModel.js +2 -2
- package/src/models/definitions/assets.js +1 -1
- package/src/models/definitions/filters.js +57 -44
- package/src/models/definitions/hooks.js +1 -1
- package/src/models/definitions/index.js +9 -9
- package/src/models/definitions/modifiers.js +1 -1
- package/src/models/definitions/options.js +1 -1
- package/src/models/definitions/properties.js +2 -2
- package/src/models/definitions/relations.js +1 -1
- package/src/models/definitions/schema.js +1 -1
- package/src/models/definitions/scopes.js +2 -2
- package/src/models/index.js +5 -5
- package/src/query/QueryBuilder.js +5 -5
- package/src/query/QueryFilters.js +50 -50
- package/src/query/QueryParameters.js +2 -2
- package/src/query/index.js +3 -3
- package/src/schema/formats/index.js +2 -2
- package/src/schema/index.js +4 -4
- package/src/schema/keywords/_relate.js +1 -1
- package/src/schema/keywords/index.js +12 -12
- package/src/schema/properties.test.js +1 -1
- package/src/schema/relations.js +1 -1
- package/src/schema/relations.test.js +2 -2
- package/src/services/index.js +1 -1
- package/src/storage/DiskStorage.js +1 -1
- package/src/storage/S3Storage.js +4 -4
- package/src/storage/Storage.js +1 -1
- package/src/storage/index.js +4 -4
- package/src/utils/function.test.js +1 -1
- package/src/utils/handler.js +17 -0
- package/src/utils/index.js +8 -7
- package/src/utils/object.test.js +1 -1
- package/lib/app/Application.js +0 -961
- package/lib/app/SessionStore.js +0 -40
- package/lib/app/Validator.js +0 -355
- package/lib/app/index.js +0 -26
- package/lib/cli/console.js +0 -175
- package/lib/cli/db/createMigration.js +0 -237
- package/lib/cli/db/index.js +0 -66
- package/lib/cli/db/listAssetConfig.js +0 -16
- package/lib/cli/db/migrate.js +0 -15
- package/lib/cli/db/reset.js +0 -27
- package/lib/cli/db/rollback.js +0 -15
- package/lib/cli/db/seed.js +0 -104
- package/lib/cli/db/unlock.js +0 -15
- package/lib/cli/index.js +0 -90
- package/lib/controllers/AdminController.js +0 -258
- package/lib/controllers/CollectionController.js +0 -263
- package/lib/controllers/Controller.js +0 -462
- package/lib/controllers/ControllerAction.js +0 -276
- package/lib/controllers/MemberAction.js +0 -22
- package/lib/controllers/ModelController.js +0 -64
- package/lib/controllers/RelationController.js +0 -82
- package/lib/controllers/UserController.js +0 -98
- package/lib/controllers/index.js +0 -50
- package/lib/decorators/action.js +0 -14
- package/lib/decorators/authorize.js +0 -13
- package/lib/decorators/index.js +0 -58
- package/lib/decorators/parameters.js +0 -35
- package/lib/decorators/returns.js +0 -26
- package/lib/decorators/scope.js +0 -14
- package/lib/decorators/transacted.js +0 -12
- package/lib/errors/AssetError.js +0 -19
- package/lib/errors/AuthenticationError.js +0 -19
- package/lib/errors/AuthorizationError.js +0 -19
- package/lib/errors/ControllerError.js +0 -24
- package/lib/errors/DatabaseError.js +0 -27
- package/lib/errors/GraphError.js +0 -19
- package/lib/errors/ModelError.js +0 -24
- package/lib/errors/NotFoundError.js +0 -19
- package/lib/errors/NotImplementedError.js +0 -19
- package/lib/errors/QueryBuilderError.js +0 -19
- package/lib/errors/RelationError.js +0 -36
- package/lib/errors/ResponseError.js +0 -46
- package/lib/errors/ValidationError.js +0 -19
- package/lib/errors/WrappedError.js +0 -24
- package/lib/errors/index.js +0 -122
- package/lib/graph/DitoGraphProcessor.js +0 -185
- package/lib/graph/expression.js +0 -76
- package/lib/graph/graph.js +0 -300
- package/lib/graph/index.js +0 -34
- package/lib/index.js +0 -82
- package/lib/lib/EventEmitter.js +0 -76
- package/lib/lib/KnexHelper.js +0 -40
- package/lib/lib/index.js +0 -26
- package/lib/middleware/attachLogger.js +0 -16
- package/lib/middleware/createTransaction.js +0 -36
- package/lib/middleware/findRoute.js +0 -35
- package/lib/middleware/handleError.js +0 -26
- package/lib/middleware/handleRoute.js +0 -29
- package/lib/middleware/handleUser.js +0 -36
- package/lib/middleware/index.js +0 -66
- package/lib/middleware/logRequests.js +0 -122
- package/lib/mixins/AssetMixin.js +0 -81
- package/lib/mixins/SessionMixin.js +0 -22
- package/lib/mixins/TimeStampedMixin.js +0 -47
- package/lib/mixins/UserMixin.js +0 -151
- package/lib/mixins/index.js +0 -42
- package/lib/models/AssetModel.js +0 -12
- package/lib/models/Model.js +0 -953
- package/lib/models/RelationAccessor.js +0 -41
- package/lib/models/SessionModel.js +0 -12
- package/lib/models/TimeStampedModel.js +0 -12
- package/lib/models/UserModel.js +0 -12
- package/lib/models/definitions/assets.js +0 -11
- package/lib/models/definitions/filters.js +0 -101
- package/lib/models/definitions/hooks.js +0 -11
- package/lib/models/definitions/index.js +0 -38
- package/lib/models/definitions/modifiers.js +0 -11
- package/lib/models/definitions/options.js +0 -11
- package/lib/models/definitions/properties.js +0 -87
- package/lib/models/definitions/relations.js +0 -11
- package/lib/models/definitions/schema.js +0 -11
- package/lib/models/definitions/scopes.js +0 -51
- package/lib/models/index.js +0 -50
- package/lib/query/QueryBuilder.js +0 -745
- package/lib/query/QueryFilters.js +0 -82
- package/lib/query/QueryParameters.js +0 -77
- package/lib/query/Registry.js +0 -40
- package/lib/query/index.js +0 -34
- package/lib/schema/formats/_empty.js +0 -10
- package/lib/schema/formats/_required.js +0 -10
- package/lib/schema/formats/index.js +0 -26
- package/lib/schema/index.js +0 -49
- package/lib/schema/keywords/_computed.js +0 -11
- package/lib/schema/keywords/_foreign.js +0 -11
- package/lib/schema/keywords/_hidden.js +0 -11
- package/lib/schema/keywords/_index.js +0 -11
- package/lib/schema/keywords/_instanceof.js +0 -49
- package/lib/schema/keywords/_primary.js +0 -11
- package/lib/schema/keywords/_range.js +0 -26
- package/lib/schema/keywords/_relate.js +0 -19
- package/lib/schema/keywords/_specificType.js +0 -11
- package/lib/schema/keywords/_unique.js +0 -11
- package/lib/schema/keywords/_unsigned.js +0 -11
- package/lib/schema/keywords/_validate.js +0 -80
- package/lib/schema/keywords/index.js +0 -106
- package/lib/schema/properties.js +0 -227
- package/lib/schema/properties.test.js +0 -573
- package/lib/schema/relations.js +0 -274
- package/lib/schema/relations.test.js +0 -155
- package/lib/services/Service.js +0 -34
- package/lib/services/index.js +0 -18
- package/lib/storage/AssetFile.js +0 -97
- package/lib/storage/DiskStorage.js +0 -125
- package/lib/storage/S3Storage.js +0 -171
- package/lib/storage/Storage.js +0 -209
- package/lib/storage/index.js +0 -34
- package/lib/utils/decorator.js +0 -16
- package/lib/utils/deprecate.js +0 -46
- package/lib/utils/emitter.js +0 -13
- package/lib/utils/function.js +0 -14
- package/lib/utils/function.test.js +0 -49
- package/lib/utils/index.js +0 -66
- package/lib/utils/json.js +0 -9
- package/lib/utils/object.js +0 -92
- package/lib/utils/object.test.js +0 -65
- package/lib/utils/scope.js +0 -14
package/package.json
CHANGED
|
@@ -1,24 +1,17 @@
|
|
|
1
1
|
{
|
|
2
|
+
"type": "module",
|
|
2
3
|
"name": "@ditojs/server",
|
|
3
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0-rc.0",
|
|
4
5
|
"description": "Dito.js Server – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js",
|
|
5
|
-
"main": "lib/index.js",
|
|
6
6
|
"repository": "https://github.com/ditojs/dito/tree/master/packages/server",
|
|
7
7
|
"author": "Jürg Lehni <juerg@scratchdisk.com> (http://scratchdisk.com)",
|
|
8
8
|
"license": "MIT",
|
|
9
|
+
"main": "./src/index.js",
|
|
9
10
|
"files": [
|
|
10
|
-
"src/"
|
|
11
|
-
"lib/"
|
|
11
|
+
"src/"
|
|
12
12
|
],
|
|
13
|
-
"scripts": {
|
|
14
|
-
"knex": "babel-node node_modules/.bin/knex",
|
|
15
|
-
"clean": "rimraf lib",
|
|
16
|
-
"build": "babel src --out-dir lib --copy-files",
|
|
17
|
-
"watch": "yarn build --watch",
|
|
18
|
-
"prepare": "yarn clean && yarn build"
|
|
19
|
-
},
|
|
20
13
|
"bin": {
|
|
21
|
-
"dito": "./
|
|
14
|
+
"dito": "./src/cli/index.js"
|
|
22
15
|
},
|
|
23
16
|
"engines": {
|
|
24
17
|
"node": ">= 14.0.0",
|
|
@@ -28,27 +21,23 @@
|
|
|
28
21
|
"node >= 14"
|
|
29
22
|
],
|
|
30
23
|
"dependencies": {
|
|
31
|
-
"@
|
|
32
|
-
"@
|
|
33
|
-
"@ditojs/
|
|
34
|
-
"@
|
|
35
|
-
"@ditojs/utils": "^0.275.0",
|
|
36
|
-
"@koa/cors": "^3.1.0",
|
|
24
|
+
"@ditojs/admin": "^1.0.0-rc.0",
|
|
25
|
+
"@ditojs/router": "^1.0.0-rc.0",
|
|
26
|
+
"@ditojs/utils": "^1.0.0-rc.0",
|
|
27
|
+
"@koa/cors": "^3.2.0",
|
|
37
28
|
"@koa/multer": "^3.0.0",
|
|
38
|
-
"@
|
|
29
|
+
"@originjs/vite-plugin-commonjs": "^1.0.3",
|
|
39
30
|
"ajv": "^7.2.4",
|
|
40
31
|
"ajv-formats": "^1.6.1",
|
|
41
|
-
"aws-sdk": "^2.
|
|
42
|
-
"axios": "^0.26.
|
|
43
|
-
"babel-loader": "^8.2.3",
|
|
32
|
+
"aws-sdk": "^2.1093.0",
|
|
33
|
+
"axios": "^0.26.1",
|
|
44
34
|
"bcryptjs": "^2.4.3",
|
|
45
35
|
"bytes": "^3.1.2",
|
|
46
|
-
"
|
|
47
|
-
"data-uri-to-buffer": "^3.0.1",
|
|
36
|
+
"data-uri-to-buffer": "^4.0.0",
|
|
48
37
|
"eventemitter2": "^6.4.5",
|
|
49
|
-
"file-type": "^
|
|
38
|
+
"file-type": "^17.1.1",
|
|
39
|
+
"find-up": "^6.3.0",
|
|
50
40
|
"fs-extra": "^10.0.1",
|
|
51
|
-
"html-webpack-tags-plugin": "^2.0.17",
|
|
52
41
|
"image-size": "^1.0.1",
|
|
53
42
|
"is-svg": "^4.3.2",
|
|
54
43
|
"koa": "^2.13.4",
|
|
@@ -56,7 +45,6 @@
|
|
|
56
45
|
"koa-compose": "^4.1.0",
|
|
57
46
|
"koa-compress": "^5.1.0",
|
|
58
47
|
"koa-conditional-get": "^3.0.0",
|
|
59
|
-
"koa-connect-history-api-fallback": "^0.3.1",
|
|
60
48
|
"koa-etag": "^4.0.0",
|
|
61
49
|
"koa-helmet": "^6.1.0",
|
|
62
50
|
"koa-mount": "^4.0.0",
|
|
@@ -64,8 +52,8 @@
|
|
|
64
52
|
"koa-response-time": "^2.1.0",
|
|
65
53
|
"koa-session": "^6.2.0",
|
|
66
54
|
"koa-static": "^5.0.0",
|
|
67
|
-
"
|
|
68
|
-
"
|
|
55
|
+
"mime-types": "^2.1.35",
|
|
56
|
+
"minimatch": "^5.0.1",
|
|
69
57
|
"multer": "^1.4.4",
|
|
70
58
|
"multer-s3": "^2.10.0",
|
|
71
59
|
"nanoid": "^3.3.1",
|
|
@@ -73,32 +61,26 @@
|
|
|
73
61
|
"passport-local": "^1.0.0",
|
|
74
62
|
"passthrough-counter": "^1.0.0",
|
|
75
63
|
"picocolors": "^1.0.0",
|
|
76
|
-
"
|
|
77
|
-
"pino
|
|
64
|
+
"picomatch": "^2.3.1",
|
|
65
|
+
"pino": "^7.8.1",
|
|
66
|
+
"pino-pretty": "^7.5.3",
|
|
78
67
|
"pluralize": "^8.0.0",
|
|
79
68
|
"repl": "^0.1.3",
|
|
80
|
-
"source-map-loader": "1.1.3",
|
|
81
69
|
"uuid": "^8.3.2",
|
|
70
|
+
"vite": "^2.8.6",
|
|
71
|
+
"vite-plugin-vue2": "^1.9.3",
|
|
82
72
|
"vue": "^2.6.14",
|
|
83
|
-
"vue-template-compiler": "^2.6.14"
|
|
84
|
-
"webpack": "^4.46.0",
|
|
85
|
-
"webpack-hot-client": "^4.2.0"
|
|
73
|
+
"vue-template-compiler": "^2.6.14"
|
|
86
74
|
},
|
|
87
75
|
"peerDependencies": {
|
|
88
76
|
"knex": "^0.21.0",
|
|
89
77
|
"objection": "^2.2.0"
|
|
90
78
|
},
|
|
91
79
|
"devDependencies": {
|
|
92
|
-
"
|
|
93
|
-
"@babel/preset-env": "^7.16.11",
|
|
94
|
-
"@ditojs/babel-preset": "^0.275.0",
|
|
95
|
-
"babel-plugin-dynamic-import-node": "^2.3.3",
|
|
96
|
-
"babel-plugin-module-resolver": "^4.1.0",
|
|
97
|
-
"knex": "^0.21.21",
|
|
80
|
+
"knex": "^0.21.17",
|
|
98
81
|
"objection": "^2.2.18",
|
|
99
82
|
"pg": "^8.7.3",
|
|
100
|
-
"rimraf": "^3.0.2",
|
|
101
83
|
"sqlite3": "^5.0.2"
|
|
102
84
|
},
|
|
103
|
-
"gitHead": "
|
|
85
|
+
"gitHead": "e643586e4b26a850f54c52a1f1bf8f0421c05d31"
|
|
104
86
|
}
|
package/src/app/Application.js
CHANGED
|
@@ -12,26 +12,31 @@ import cors from '@koa/cors'
|
|
|
12
12
|
import compose from 'koa-compose'
|
|
13
13
|
import compress from 'koa-compress'
|
|
14
14
|
import conditional from 'koa-conditional-get'
|
|
15
|
+
import mount from 'koa-mount'
|
|
15
16
|
import passport from 'koa-passport'
|
|
16
17
|
import session from 'koa-session'
|
|
17
18
|
import etag from 'koa-etag'
|
|
18
19
|
import helmet from 'koa-helmet'
|
|
19
20
|
import responseTime from 'koa-response-time'
|
|
20
21
|
import Router from '@ditojs/router'
|
|
21
|
-
import {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
import
|
|
26
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
isArray, isObject, isString, asArray, isPlainObject, hyphenate, clone, merge,
|
|
24
|
+
parseDataPath, normalizeDataPath, isModule
|
|
25
|
+
} from '@ditojs/utils'
|
|
26
|
+
import SessionStore from './SessionStore.js'
|
|
27
|
+
import { Validator } from './Validator.js'
|
|
28
|
+
import { EventEmitter } from '../lib/index.js'
|
|
29
|
+
import { Controller, AdminController } from '../controllers/index.js'
|
|
30
|
+
import { Service } from '../services/index.js'
|
|
31
|
+
import { Storage } from '../storage/index.js'
|
|
32
|
+
import { convertSchema } from '../schema/index.js'
|
|
33
|
+
import { formatJson } from '../utils/index.js'
|
|
27
34
|
import {
|
|
28
35
|
ResponseError,
|
|
29
36
|
ValidationError,
|
|
30
37
|
DatabaseError,
|
|
31
38
|
AssetError
|
|
32
|
-
} from '
|
|
33
|
-
import SessionStore from './SessionStore'
|
|
34
|
-
import { Validator } from './Validator'
|
|
39
|
+
} from '../errors/index.js'
|
|
35
40
|
import {
|
|
36
41
|
attachLogger,
|
|
37
42
|
createTransaction,
|
|
@@ -40,15 +45,12 @@ import {
|
|
|
40
45
|
handleRoute,
|
|
41
46
|
handleUser,
|
|
42
47
|
logRequests
|
|
43
|
-
} from '
|
|
44
|
-
import {
|
|
45
|
-
isArray, isObject, isString, asArray, isPlainObject, hyphenate, clone, merge,
|
|
46
|
-
parseDataPath, normalizeDataPath
|
|
47
|
-
} from '@ditojs/utils'
|
|
48
|
-
import {
|
|
48
|
+
} from '../middleware/index.js'
|
|
49
|
+
import objection, {
|
|
49
50
|
Model,
|
|
50
51
|
BelongsToOneRelation,
|
|
51
|
-
|
|
52
|
+
// TODO: Import directly once we can move to Objection 3
|
|
53
|
+
// knexSnakeCaseMappers,
|
|
52
54
|
ref
|
|
53
55
|
} from 'objection'
|
|
54
56
|
|
|
@@ -85,13 +87,10 @@ export class Application extends Koa {
|
|
|
85
87
|
this.models = Object.create(null)
|
|
86
88
|
this.services = Object.create(null)
|
|
87
89
|
this.controllers = Object.create(null)
|
|
88
|
-
this.hasControllerMiddleware = false
|
|
89
90
|
this.setupLogger()
|
|
90
91
|
this.setupKnex()
|
|
91
|
-
this.
|
|
92
|
-
|
|
93
|
-
this.use(middleware)
|
|
94
|
-
}
|
|
92
|
+
this.setupMiddleware(middleware)
|
|
93
|
+
|
|
95
94
|
if (config.storages) {
|
|
96
95
|
this.addStorages(config.storages)
|
|
97
96
|
}
|
|
@@ -106,20 +105,24 @@ export class Application extends Koa {
|
|
|
106
105
|
}
|
|
107
106
|
}
|
|
108
107
|
|
|
109
|
-
addRoute(
|
|
110
|
-
|
|
111
|
-
|
|
108
|
+
addRoute(
|
|
109
|
+
method, path, transacted, middlewares, controller = null, action = null
|
|
110
|
+
) {
|
|
111
|
+
middlewares = asArray(middlewares)
|
|
112
|
+
const middleware = middlewares.length > 1
|
|
113
|
+
? compose(middlewares)
|
|
114
|
+
: middlewares[0]
|
|
112
115
|
// Instead of directly passing `handler`, pass a `route` object that also
|
|
113
116
|
// will be exposed through `ctx.route`, see `routerHandler()`:
|
|
114
117
|
const route = {
|
|
115
|
-
|
|
118
|
+
method,
|
|
116
119
|
path,
|
|
117
120
|
transacted,
|
|
118
|
-
|
|
121
|
+
middleware,
|
|
119
122
|
controller,
|
|
120
123
|
action
|
|
121
124
|
}
|
|
122
|
-
this.router[
|
|
125
|
+
this.router[method](path, route)
|
|
123
126
|
}
|
|
124
127
|
|
|
125
128
|
addModels(models) {
|
|
@@ -235,12 +238,7 @@ export class Application extends Koa {
|
|
|
235
238
|
|
|
236
239
|
addServices(services) {
|
|
237
240
|
for (const [name, service] of Object.entries(services)) {
|
|
238
|
-
|
|
239
|
-
if (name === 'default' && isPlainObject(service)) {
|
|
240
|
-
this.addServices(service)
|
|
241
|
-
} else {
|
|
242
|
-
this.addService(service, name)
|
|
243
|
-
}
|
|
241
|
+
this.addService(service, name)
|
|
244
242
|
}
|
|
245
243
|
}
|
|
246
244
|
|
|
@@ -285,7 +283,7 @@ export class Application extends Koa {
|
|
|
285
283
|
|
|
286
284
|
addControllers(controllers, namespace) {
|
|
287
285
|
for (const [key, value] of Object.entries(controllers)) {
|
|
288
|
-
if (isPlainObject(value)) {
|
|
286
|
+
if (isModule(value) || isPlainObject(value)) {
|
|
289
287
|
this.addControllers(value, namespace ? `${namespace}/${key}` : key)
|
|
290
288
|
} else {
|
|
291
289
|
this.addController(value, namespace)
|
|
@@ -294,8 +292,6 @@ export class Application extends Koa {
|
|
|
294
292
|
}
|
|
295
293
|
|
|
296
294
|
addController(controller, namespace) {
|
|
297
|
-
// Controllers require additional middleware to be installed once.
|
|
298
|
-
this.setupControllerMiddleware()
|
|
299
295
|
// Auto-instantiate controller classes:
|
|
300
296
|
if (Controller.isPrototypeOf(controller)) {
|
|
301
297
|
// eslint-disable-next-line new-cap
|
|
@@ -311,11 +307,11 @@ export class Application extends Koa {
|
|
|
311
307
|
// Now that the controller is set up, call `initialize()` which can be
|
|
312
308
|
// overridden by controllers.
|
|
313
309
|
controller.initialize()
|
|
314
|
-
// Each controller can also
|
|
315
|
-
// `AdminController`:
|
|
316
|
-
const
|
|
317
|
-
if (
|
|
318
|
-
this.use(
|
|
310
|
+
// Each controller can also compose their own middleware (or app), e.g. as
|
|
311
|
+
// used in `AdminController`:
|
|
312
|
+
const composed = controller.compose()
|
|
313
|
+
if (composed) {
|
|
314
|
+
this.use(mount(controller.url, composed))
|
|
319
315
|
}
|
|
320
316
|
}
|
|
321
317
|
|
|
@@ -333,8 +329,8 @@ export class Application extends Koa {
|
|
|
333
329
|
)
|
|
334
330
|
}
|
|
335
331
|
|
|
336
|
-
|
|
337
|
-
return this.getAdminController()?.
|
|
332
|
+
getAdminViteConfig(config) {
|
|
333
|
+
return this.getAdminController()?.getViteConfig(config) || null
|
|
338
334
|
}
|
|
339
335
|
|
|
340
336
|
getAssetConfig({
|
|
@@ -501,18 +497,19 @@ export class Application extends Koa {
|
|
|
501
497
|
})
|
|
502
498
|
}
|
|
503
499
|
|
|
504
|
-
|
|
500
|
+
setupMiddleware(middleware) {
|
|
505
501
|
const { app, log } = this.config
|
|
506
502
|
|
|
507
|
-
|
|
503
|
+
// Setup global middleware
|
|
508
504
|
|
|
505
|
+
this.use(attachLogger(this.logger))
|
|
509
506
|
if (app.responseTime !== false) {
|
|
510
507
|
this.use(responseTime(getOptions(app.responseTime)))
|
|
511
508
|
}
|
|
512
509
|
if (log.requests) {
|
|
513
510
|
this.use(logRequests())
|
|
514
511
|
}
|
|
515
|
-
//
|
|
512
|
+
// This needs to be positioned after the request logger to log the correct
|
|
516
513
|
// response status.
|
|
517
514
|
this.use(handleError())
|
|
518
515
|
if (app.helmet !== false) {
|
|
@@ -539,50 +536,44 @@ export class Application extends Koa {
|
|
|
539
536
|
this.use(conditional())
|
|
540
537
|
this.use(etag())
|
|
541
538
|
}
|
|
542
|
-
}
|
|
543
539
|
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
//
|
|
547
|
-
|
|
548
|
-
//
|
|
549
|
-
if (
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
// eslint-disable-next-line new-cap
|
|
569
|
-
options.ContextStore = SessionStore(modelClass)
|
|
570
|
-
}
|
|
571
|
-
this.use(session(options, this))
|
|
540
|
+
// Controller-specific middleware
|
|
541
|
+
|
|
542
|
+
// 1. Find route from routes installed by controllers.
|
|
543
|
+
this.use(findRoute(this.router))
|
|
544
|
+
// 2. Additional, user-space application-level middleware.
|
|
545
|
+
if (middleware) {
|
|
546
|
+
this.use(middleware)
|
|
547
|
+
}
|
|
548
|
+
// 3. body parser
|
|
549
|
+
this.use(bodyParser(getOptions(app.bodyParser)))
|
|
550
|
+
// 4. respect transacted settings, create and handle transactions.
|
|
551
|
+
this.use(createTransaction())
|
|
552
|
+
// 5. session
|
|
553
|
+
if (app.session) {
|
|
554
|
+
const {
|
|
555
|
+
modelClass,
|
|
556
|
+
...options
|
|
557
|
+
} = getOptions(app.session)
|
|
558
|
+
if (modelClass) {
|
|
559
|
+
// Create a ContextStore that resolved the specified model class,
|
|
560
|
+
// uses it to persist and retrieve the session, and automatically
|
|
561
|
+
// binds all db operations to `ctx.transaction`, if it is set.
|
|
562
|
+
// eslint-disable-next-line new-cap
|
|
563
|
+
options.ContextStore = SessionStore(modelClass)
|
|
572
564
|
}
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
this.use(
|
|
565
|
+
this.use(session(options, this))
|
|
566
|
+
}
|
|
567
|
+
// 6. passport
|
|
568
|
+
if (app.passport) {
|
|
569
|
+
this.use(passport.initialize())
|
|
570
|
+
if (app.session) {
|
|
571
|
+
this.use(passport.session())
|
|
580
572
|
}
|
|
581
|
-
|
|
582
|
-
// 6. finally handle the found route, or set status / allow accordingly.
|
|
583
|
-
this.use(handleRoute())
|
|
584
|
-
this.hasControllerMiddleware = true
|
|
573
|
+
this.use(handleUser())
|
|
585
574
|
}
|
|
575
|
+
// 6. finally handle the found route, or set status / allow accordingly.
|
|
576
|
+
this.use(handleRoute())
|
|
586
577
|
}
|
|
587
578
|
|
|
588
579
|
setupLogger() {
|
|
@@ -591,26 +582,32 @@ export class Application extends Koa {
|
|
|
591
582
|
const user = user => ({ id: user.id })
|
|
592
583
|
const serializers = { err, req, res, user }
|
|
593
584
|
|
|
594
|
-
const
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
},
|
|
604
|
-
// Redact common sensitive headers.
|
|
605
|
-
redact: [
|
|
606
|
-
'*.headers["cookie"]',
|
|
607
|
-
'*.headers["set-cookie"]',
|
|
608
|
-
'*.headers["authorization"]'
|
|
609
|
-
],
|
|
610
|
-
base: null // no pid,hostname,name
|
|
585
|
+
const { prettyPrint, ...options } = merge({
|
|
586
|
+
level: 'info',
|
|
587
|
+
serializers,
|
|
588
|
+
prettyPrint: {
|
|
589
|
+
colorize: true,
|
|
590
|
+
// List of keys to ignore in pretty mode.
|
|
591
|
+
ignore: 'req,res,durationMs,user,requestId',
|
|
592
|
+
// SYS to use system time and not UTC.
|
|
593
|
+
translateTime: 'SYS:HH:MM:ss.l'
|
|
611
594
|
},
|
|
612
|
-
|
|
613
|
-
|
|
595
|
+
// Redact common sensitive headers.
|
|
596
|
+
redact: [
|
|
597
|
+
'*.headers["cookie"]',
|
|
598
|
+
'*.headers["set-cookie"]',
|
|
599
|
+
'*.headers["authorization"]'
|
|
600
|
+
],
|
|
601
|
+
base: null // no pid,hostname,name
|
|
602
|
+
}, getOptions(this.config.logger))
|
|
603
|
+
|
|
604
|
+
const transport = prettyPrint
|
|
605
|
+
? pino.transport({
|
|
606
|
+
target: 'pino-pretty',
|
|
607
|
+
options: prettyPrint
|
|
608
|
+
}) : null
|
|
609
|
+
|
|
610
|
+
const logger = pino(options, transport)
|
|
614
611
|
|
|
615
612
|
this.logger = logger.child({ name: 'app' })
|
|
616
613
|
}
|
|
@@ -624,7 +621,7 @@ export class Application extends Koa {
|
|
|
624
621
|
if (snakeCaseOptions) {
|
|
625
622
|
knex = {
|
|
626
623
|
...knex,
|
|
627
|
-
...knexSnakeCaseMappers(snakeCaseOptions)
|
|
624
|
+
...objection.knexSnakeCaseMappers(snakeCaseOptions)
|
|
628
625
|
}
|
|
629
626
|
}
|
|
630
627
|
this.knex = Knex(knex)
|
|
@@ -716,15 +713,12 @@ export class Application extends Koa {
|
|
|
716
713
|
}
|
|
717
714
|
await this.emit('before:start')
|
|
718
715
|
await this.forEachService(service => service.start())
|
|
719
|
-
const {
|
|
720
|
-
server: { host, port },
|
|
721
|
-
env
|
|
722
|
-
} = this.config
|
|
716
|
+
const { server: { host, port } } = this.config
|
|
723
717
|
this.server = await new Promise((resolve, reject) => {
|
|
724
718
|
const server = this.listen(port, host, () => {
|
|
725
719
|
const { port } = server.address()
|
|
726
720
|
console.info(
|
|
727
|
-
|
|
721
|
+
`Dito server started at http://${host}:${port}`
|
|
728
722
|
)
|
|
729
723
|
resolve(server)
|
|
730
724
|
})
|
package/src/app/Validator.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import objection from 'objection'
|
|
2
|
-
import
|
|
2
|
+
import ajv from 'ajv'
|
|
3
3
|
import addFormats from 'ajv-formats'
|
|
4
4
|
import { isArray, isObject, clone, isAsync, isPromise } from '@ditojs/utils'
|
|
5
|
-
import { formatJson } from '
|
|
6
|
-
import * as schema from '
|
|
5
|
+
import { formatJson } from '../utils/index.js'
|
|
6
|
+
import * as schema from '../schema/index.js'
|
|
7
|
+
|
|
8
|
+
// TODO: Switch to direct import once Ajv is updated / supports ESM.
|
|
9
|
+
const Ajv = ajv.default
|
|
7
10
|
|
|
8
11
|
// Dito does not rely on objection.AjvValidator but instead implements its own
|
|
9
12
|
// validator instance that is shared across the whole app and handles schema
|
package/src/app/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './Application'
|
|
2
|
-
export * from './Validator'
|
|
1
|
+
export * from './Application.js'
|
|
2
|
+
export * from './Validator.js'
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from 'path'
|
|
2
2
|
import fs from 'fs-extra'
|
|
3
3
|
import pico from 'picocolors'
|
|
4
|
-
import { getRelationClass, isThroughRelationClass } from '
|
|
4
|
+
import { getRelationClass, isThroughRelationClass } from '@ditojs/server'
|
|
5
5
|
import {
|
|
6
6
|
isObject, isArray, isString, deindent, capitalize
|
|
7
7
|
} from '@ditojs/utils'
|
package/src/cli/db/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from './createMigration'
|
|
2
|
-
export * from './listAssetConfig'
|
|
3
|
-
export * from './migrate'
|
|
4
|
-
export * from './rollback'
|
|
5
|
-
export * from './reset'
|
|
6
|
-
export * from './seed'
|
|
7
|
-
export * from './unlock'
|
|
1
|
+
export * from './createMigration.js'
|
|
2
|
+
export * from './listAssetConfig.js'
|
|
3
|
+
export * from './migrate.js'
|
|
4
|
+
export * from './rollback.js'
|
|
5
|
+
export * from './reset.js'
|
|
6
|
+
export * from './seed.js'
|
|
7
|
+
export * from './unlock.js'
|
package/src/cli/db/reset.js
CHANGED
package/src/cli/index.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
#!/usr/bin/env
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import path from 'path'
|
|
4
|
+
import fs from 'fs'
|
|
4
5
|
import pico from 'picocolors'
|
|
5
6
|
import Knex from 'knex'
|
|
6
7
|
import { isPlainObject, isFunction, camelize } from '@ditojs/utils'
|
|
7
|
-
import * as db from './db'
|
|
8
|
-
import startConsole from './console'
|
|
8
|
+
import * as db from './db/index.js'
|
|
9
|
+
import startConsole from './console.js'
|
|
10
|
+
|
|
11
|
+
// Tell Knex to use ESM. TODO: Fix this properly in Knex itself, so it works
|
|
12
|
+
// with Yarn too.
|
|
13
|
+
process.env.npm_package_type = 'module'
|
|
9
14
|
|
|
10
15
|
const commands = { db, console: startConsole }
|
|
11
16
|
|
|
@@ -56,8 +61,12 @@ async function execute() {
|
|
|
56
61
|
}
|
|
57
62
|
}
|
|
58
63
|
|
|
59
|
-
// Start the console if `node ./
|
|
60
|
-
|
|
64
|
+
// Start the console if `node ./cli/index.js`
|
|
65
|
+
|
|
66
|
+
// See module was not imported but called directly
|
|
67
|
+
const path1 = fs.realpathSync(import.meta.url.replace(/^file:\/\//, ''))
|
|
68
|
+
const path2 = fs.realpathSync(process.argv[1])
|
|
69
|
+
if (path1 === path2) {
|
|
61
70
|
execute()
|
|
62
71
|
}
|
|
63
72
|
|