@ditojs/server 1.2.1 → 1.4.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ditojs/server",
3
- "version": "1.2.1",
3
+ "version": "1.4.1",
4
4
  "type": "module",
5
5
  "description": "Dito.js Server – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js",
6
6
  "repository": "https://github.com/ditojs/dito/tree/master/packages/server",
@@ -14,22 +14,22 @@
14
14
  "dito": "./src/cli/index.js"
15
15
  },
16
16
  "engines": {
17
- "node": ">= 14.0.0",
17
+ "node": ">= 16.0.0",
18
18
  "yarn": ">= 1.0.0"
19
19
  },
20
20
  "browserslist": [
21
- "node >= 14"
21
+ "node >= 16"
22
22
  ],
23
23
  "dependencies": {
24
- "@ditojs/admin": "^1.2.0",
25
- "@ditojs/router": "^1.2.0",
26
- "@ditojs/utils": "^1.2.0",
24
+ "@ditojs/admin": "^1.4.1",
25
+ "@ditojs/router": "^1.4.1",
26
+ "@ditojs/utils": "^1.4.1",
27
27
  "@koa/cors": "^3.2.0",
28
28
  "@koa/multer": "^3.0.0",
29
29
  "@originjs/vite-plugin-commonjs": "^1.0.3",
30
- "ajv": "^8.10.0",
30
+ "ajv": "^8.11.0",
31
31
  "ajv-formats": "^2.1.1",
32
- "aws-sdk": "^2.1095.0",
32
+ "aws-sdk": "^2.1098.0",
33
33
  "axios": "^0.26.1",
34
34
  "bcryptjs": "^2.4.3",
35
35
  "bytes": "^3.1.2",
@@ -53,7 +53,6 @@
53
53
  "koa-session": "^6.2.0",
54
54
  "koa-static": "^5.0.0",
55
55
  "mime-types": "^2.1.35",
56
- "minimatch": "^5.0.1",
57
56
  "multer": "^1.4.4",
58
57
  "multer-s3": "^2.10.0",
59
58
  "nanoid": "^3.3.1",
@@ -62,7 +61,7 @@
62
61
  "passthrough-counter": "^1.0.0",
63
62
  "picocolors": "^1.0.0",
64
63
  "picomatch": "^2.3.1",
65
- "pino": "^7.9.1",
64
+ "pino": "^7.9.2",
66
65
  "pino-pretty": "^7.5.4",
67
66
  "pluralize": "^8.0.0",
68
67
  "repl": "^0.1.3",
@@ -82,5 +81,5 @@
82
81
  "pg": "^8.7.3",
83
82
  "sqlite3": "^5.0.2"
84
83
  },
85
- "gitHead": "14339419ebec275f8e611092d399acff706b9322"
84
+ "gitHead": "0e8a22ea0cb68eec3df1b6236cf77a85f5a2c836"
86
85
  }
@@ -171,7 +171,7 @@ export class Application extends Koa {
171
171
  }
172
172
  if (Object.keys(data).length > 0) {
173
173
  console.info(
174
- pico.yellow.bold(`\n${modelClass.name}:\n`),
174
+ pico.yellow(pico.bold(`\n${modelClass.name}:\n`)),
175
175
  util.inspect(data, {
176
176
  colors: true,
177
177
  depth: null,
@@ -13,7 +13,7 @@ import { merge } from '@ditojs/utils'
13
13
  import { Controller } from './Controller.js'
14
14
  import { handleConnectMiddleware } from '../middleware/index.js'
15
15
  import { ControllerError } from '../errors/index.js'
16
- import { formatJson } from '../utils/index.js'
16
+ import { formatJson, deprecate } from '../utils/index.js'
17
17
 
18
18
  export class AdminController extends Controller {
19
19
  // @override
@@ -34,11 +34,19 @@ export class AdminController extends Controller {
34
34
  }
35
35
 
36
36
  getPath(name) {
37
- const str = this.config[name]?.path
37
+ const { config } = this
38
+ let str = config[name]
39
+ if (config.build?.path || config.dist?.path) {
40
+ deprecate(`config.admin.build.path and config.admin.dist.path are deprecated. Use config.admin.root and config.admin.dist instead.`)
41
+
42
+ str = name === 'root' ? config.build?.path
43
+ : name === 'dist' ? config.dist?.path
44
+ : null
45
+ }
38
46
  if (!str) {
39
47
  throw new ControllerError(
40
48
  this,
41
- `Missing admin.${name}.path configuration.`
49
+ `Missing \`config.admin.${name}\` configuration.`
42
50
  )
43
51
  }
44
52
  return path.resolve(str)
@@ -85,8 +93,8 @@ export class AdminController extends Controller {
85
93
  this.koa = new Koa()
86
94
  this.koa.use(this.middleware())
87
95
  if (this.mode === 'development') {
88
- // Calling getPath() throws exception if admin.build.path is not defined:
89
- if (this.getPath('build')) {
96
+ // Calling getPath() throws exception if config.admin.root is not defined:
97
+ if (this.getPath('root')) {
90
98
  this.app.once('after:start', () => this.setupViteServer())
91
99
  }
92
100
  } else {
@@ -128,14 +136,14 @@ export class AdminController extends Controller {
128
136
  const development = this.mode === 'development'
129
137
 
130
138
  const cwd = path.resolve('.')
131
- const root = this.getPath('build')
139
+ const root = this.getPath('root')
132
140
  const base = `${this.url}/`
133
141
  const views = path.join(root, 'views')
134
142
 
135
143
  // Read `package.json` from the closest package.json, so we can emulate
136
144
  // ESM-style imports mappings in rollup / vite.
137
145
  const pkg = findUpSync('package.json', { cwd: root })
138
- const { imports } = JSON.parse(fs.readFileSync(pkg, 'utf8'))
146
+ const { imports = {} } = JSON.parse(fs.readFileSync(pkg, 'utf8'))
139
147
 
140
148
  return defineConfig(merge({
141
149
  root,
@@ -209,7 +217,7 @@ export class AdminController extends Controller {
209
217
  replacement: '#',
210
218
  customResolver(id) {
211
219
  for (const [find, replacement] of Object.entries(imports)) {
212
- picomatch.isMatch(id, find, {
220
+ picomatch.isMatch(id, find.replace('*', '**'), {
213
221
  capture: true,
214
222
  onMatch({ input, regex }) {
215
223
  const replacementPath = path.resolve(replacement)
@@ -120,6 +120,15 @@ export class Controller {
120
120
  type, actions, name, action, authorize[name]
121
121
  )
122
122
  }
123
+ // Expose a direct reference to the controller on the action object, but
124
+ // also make it inherit from the controller so that all its public fields
125
+ // and functions (`app`, `query()`, `execute()`, etc.) can be accessed
126
+ // directly through `this` from actions.
127
+ // NOTE: Inheritance is also set up by `inheritValues()` so that from inside
128
+ // the handlers, `super` points to the parent controller's actions object,
129
+ // so that calling `super.patch()` from a patch handler magically works.
130
+ actions.controller = this
131
+ Object.setPrototypeOf(actions, this)
123
132
  return actions
124
133
  }
125
134
 
@@ -139,7 +148,9 @@ export class Controller {
139
148
  this.setupActionRoute(
140
149
  type,
141
150
  // eslint-disable-next-line new-cap
142
- new actionClass(this, handler, type, name, method, path, authorize)
151
+ new actionClass(
152
+ this, actions, handler, type, name, method, path, authorize
153
+ )
143
154
  )
144
155
  return handler
145
156
  }
@@ -559,5 +570,5 @@ function isMethodAction(name) {
559
570
  options: true,
560
571
  trace: true,
561
572
  connect: true
562
- }[name]
573
+ }[name] || false
563
574
  }
@@ -1,7 +1,9 @@
1
1
  import { isString, isObject, asArray, clone } from '@ditojs/utils'
2
2
 
3
3
  export default class ControllerAction {
4
- constructor(controller, handler, type, name, _method, _path, _authorize) {
4
+ constructor(
5
+ controller, actions, handler, type, name, _method, _path, _authorize
6
+ ) {
5
7
  const {
6
8
  core = false,
7
9
  // Allow decorators on actions to override the predetermined defaults for
@@ -20,6 +22,7 @@ export default class ControllerAction {
20
22
  } = handler
21
23
 
22
24
  this.controller = controller
25
+ this.actions = actions
23
26
  this.handler = handler
24
27
  this.type = type
25
28
  this.name = name
@@ -96,7 +99,7 @@ export default class ControllerAction {
96
99
  }
97
100
 
98
101
  async callHandler(ctx, ...args) {
99
- return this.handler.call(this.controller, ctx, ...args)
102
+ return this.handler.call(this.actions, ctx, ...args)
100
103
  }
101
104
 
102
105
  createValidationError(options) {
@@ -216,7 +216,7 @@ export class QueryBuilder extends objection.QueryBuilder {
216
216
  this._allowScopes = query._allowScopes ? { ...query._allowScopes } : null
217
217
  this._ignoreScopes = { ...query._ignoreScopes }
218
218
  }
219
- // If he target is a child query of a graph query, copy all scopes, graph
219
+ // If the target is a child query of a graph query, copy all scopes, graph
220
220
  // and non-graph. If it is a child query of a related or eager query,
221
221
  // copy only the graph scopes.
222
222
  const copyAllScopes =
@@ -226,15 +226,9 @@ export class QueryBuilder extends objection.QueryBuilder {
226
226
  }
227
227
 
228
228
  _filterScopes(scopes, callback) {
229
- return Object.entries(scopes).reduce(
230
- (scopes, [scope, graph]) => {
231
- if (callback(scope, graph)) {
232
- scopes[scope] = graph
233
- }
234
- return scopes
235
- },
236
- {}
237
- )
229
+ return Object.fromEntries(Object.entries(scopes).filter(
230
+ ([scope, graph]) => callback(scope, graph)
231
+ ))
238
232
  }
239
233
 
240
234
  _applyScope(scope, graph) {
@@ -255,12 +249,12 @@ export class QueryBuilder extends objection.QueryBuilder {
255
249
  const expr = this.graphExpressionObject()
256
250
  if (expr) {
257
251
  // Add a new modifier to the existing graph expression that
258
- // recursively applies the graph-scope to the resulting queries.
259
- // This even works if nested scopes expand the graph expression,
260
- // because it re-applies itself to the result.
252
+ // recursively adds the graph-scope to the resulting queries. This
253
+ // even works if nested scopes expand the graph expression, because it
254
+ // re-applies itself to the result.
261
255
  const name = `^${scope}`
262
256
  const modifiers = {
263
- [name]: query => query._applyScope(scope, graph)
257
+ [name]: query => query.withScope(name)
264
258
  }
265
259
  this.withGraph(
266
260
  addGraphScope(this.modelClass(), expr, [name], modifiers, true)
@@ -26,6 +26,7 @@ export class Service {
26
26
  }
27
27
 
28
28
  getLogger(ctx) {
29
- return ctx.logger.child({ name: this.name })
29
+ const logger = ctx?.logger ?? this.app.logger
30
+ return logger.child({ name: this.name })
30
31
  }
31
32
  }