Skip to content

Commit de9dfe4

Browse files
committed
Remove deprecated decorators and class-level action methods
1 parent 2d02f8b commit de9dfe4

12 files changed

+39
-147
lines changed

packages/server/src/controllers/CollectionController.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,7 @@ export class CollectionController extends Controller {
182182
)
183183
}
184184

185-
convertToCoreActions(actions) {
186-
// Mark action object and methods as core, so `Controller.processValues()`
187-
// can filter correctly.
188-
for (const action of Object.values(actions)) {
189-
// Mark action functions also, so ControllerAction can use it to determine
190-
// value for `transacted`.
191-
action.core = true
192-
}
193-
actions.$core = true
194-
return actions
195-
}
196-
197-
collection = this.convertToCoreActions({
185+
collection = this.markAsCoreActions({
198186
async get(ctx, modify) {
199187
const result = await this.execute(ctx, (query, trx) => {
200188
query
@@ -245,7 +233,7 @@ export class CollectionController extends Controller {
245233
}
246234
})
247235

248-
member = this.convertToCoreActions({
236+
member = this.markAsCoreActions({
249237
async get(ctx, modify) {
250238
return this.execute(ctx, (query, trx) =>
251239
query

packages/server/src/controllers/Controller.js

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export class Controller {
3232
name = null
3333
path = null
3434
url = null
35-
actions = null
3635
assets = null
36+
actions = null
3737
transacted = null
3838
initialized = false
3939

@@ -80,7 +80,6 @@ export class Controller {
8080
// @overridable
8181
setup() {
8282
this.logController()
83-
this.setProperty('actions', this.actions || this.reflectActionsObject())
8483
// Now that the instance fields are reflected in the `controller` object
8584
// we can use the normal inheritance mechanism through `setupActions()`:
8685
this.setProperty('actions', this.setupActions('actions'))
@@ -142,30 +141,15 @@ export class Controller {
142141
return value
143142
}
144143

145-
reflectActionsObject() {
146-
// On base controllers, the actions can be defined directly in the class
147-
// instead of inside an actions object, as is done with model and relation
148-
// controllers. But in order to use the same structure for inheritance as
149-
// these other controllers, we reflect these instance fields in a separate
150-
// `actions` object.
151-
const { allow } = this
152-
const actions = allow ? { allow } : {}
153-
154-
const addAction = key => {
155-
const value = this[key]
156-
// NOTE: Only add instance methods that have a @action() decorator, which
157-
// in turn sets the `method` property on the method, as well as action
158-
// objects which provide the `method` property:
159-
if (value?.method) {
160-
actions[key] = value
161-
}
144+
markAsCoreActions(actions) {
145+
// Mark action object and methods as core, so `Controller.processValues()`
146+
// can filter correctly.
147+
for (const action of Object.values(actions)) {
148+
// Mark action functions also, so ControllerAction can use it to determine
149+
// value for `transacted`.
150+
action.core = true
162151
}
163-
// Use `Object.getOwnPropertyNames()` to get the fields, in order to
164-
// not also receive values from parents (those are fetched later in
165-
// `inheritValues()`, see `getParentValues()`).
166-
const proto = Object.getPrototypeOf(this)
167-
Object.getOwnPropertyNames(proto).forEach(addAction)
168-
Object.getOwnPropertyNames(this).forEach(addAction)
152+
actions.$core = true
169153
return actions
170154
}
171155

@@ -190,26 +174,28 @@ export class Controller {
190174
values: actions,
191175
authorize
192176
} = this.processValues(this.inheritValues(type))
193-
for (const [name, action] of Object.entries(actions)) {
194-
// Replace the action object with the converted action handler, so they
195-
// too can benefit from prototypal inheritance:
196-
actions[name] = this.setupAction(
197-
type,
198-
actions,
199-
name,
200-
action,
201-
authorize[name]
202-
)
177+
if (actions) {
178+
for (const [name, action] of Object.entries(actions)) {
179+
// Replace the action object with the converted action handler, so they
180+
// too can benefit from prototypal inheritance:
181+
actions[name] = this.setupAction(
182+
type,
183+
actions,
184+
name,
185+
action,
186+
authorize[name]
187+
)
188+
}
189+
// Expose a direct reference to the controller on the action object, but
190+
// also make it inherit from the controller so that all its public fields
191+
// and functions (`app`, `query()`, `execute()`, etc.) can be accessed
192+
// directly through `this` from actions.
193+
// NOTE: Inheritance is also set up by `inheritValues()` so that from the
194+
// handlers, `super` points to the parent controller's actions object, so
195+
// that calling `super.patch()` from a patch handler magically works.
196+
actions.controller = this
197+
Object.setPrototypeOf(actions, this)
203198
}
204-
// Expose a direct reference to the controller on the action object, but
205-
// also make it inherit from the controller so that all its public fields
206-
// and functions (`app`, `query()`, `execute()`, etc.) can be accessed
207-
// directly through `this` from actions.
208-
// NOTE: Inheritance is also set up by `inheritValues()` so that from inside
209-
// the handlers, `super` points to the parent controller's actions object,
210-
// so that calling `super.patch()` from a patch handler magically works.
211-
actions.controller = this
212-
Object.setPrototypeOf(actions, this)
213199
return actions
214200
}
215201

packages/server/src/controllers/ControllerAction.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,12 @@ export default class ControllerAction {
77
handler,
88
type,
99
name,
10-
_method,
11-
_path,
10+
method,
11+
path,
1212
_authorize
1313
) {
1414
const {
1515
core = false,
16-
// Allow decorators on actions to override the predetermined defaults for
17-
// `method`, `path` and `authorize`:
18-
// TODO: `handler.method` and `handler.path` were deprecated in March
19-
// 2022, remove later and only set the valued passed to constructor then.
20-
method = _method,
21-
path = _path,
2216
scope,
2317
authorize,
2418
transacted,
@@ -28,6 +22,7 @@ export default class ControllerAction {
2822
...additional
2923
} = handler
3024

25+
this.app = controller.app
3126
this.controller = controller
3227
this.actions = actions
3328
this.handler = handler
@@ -37,6 +32,8 @@ export default class ControllerAction {
3732
this.method = method
3833
this.path = path
3934
this.scope = scope
35+
// Allow action handlers to override the predetermined defaults for
36+
// `authorize`:
4037
this.authorize = authorize || _authorize
4138
this.transacted = !!(
4239
transacted ??
@@ -52,7 +49,6 @@ export default class ControllerAction {
5249
)
5350
)
5451
this.authorization = controller.processAuthorize(this.authorize)
55-
this.app = controller.app
5652
this.paramsName = ['post', 'put', 'patch'].includes(this.method)
5753
? 'body'
5854
: 'query'

packages/server/src/controllers/RelationController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class RelationController extends CollectionController {
8787
})
8888
}
8989

90-
collection = this.convertToCoreActions({})
90+
collection = this.markAsCoreActions({})
9191

92-
member = this.convertToCoreActions({})
92+
member = this.markAsCoreActions({})
9393
}

packages/server/src/decorators/action.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/server/src/decorators/authorize.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/server/src/decorators/index.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/server/src/decorators/parameters.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/server/src/decorators/returns.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/server/src/decorators/scope.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/server/src/decorators/transacted.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/server/src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ export * from './mixins/index.js'
66
export * from './models/index.js'
77
export * from './controllers/index.js'
88
export * from './services/index.js'
9-
export * from './decorators/index.js'
109
export * from './storage/index.js'

0 commit comments

Comments
 (0)