-
Notifications
You must be signed in to change notification settings - Fork 643
/
action-decorator.ts
359 lines (328 loc) · 10 KB
/
action-decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import { DEFAULT_DRAWER_WIDTH, VariantType } from '@admin-bro/design-system'
import ConfigurationError from '../../utils/errors/configuration-error'
import ViewHelpers from '../../utils/view-helpers/view-helpers'
import AdminBro from '../../../admin-bro'
import BaseResource from '../../adapters/resource/base-resource'
import {
Action,
IsFunction,
ActionContext,
ActionRequest,
ActionResponse,
After,
Before,
ActionHandler,
} from '../../actions/action.interface'
import { CurrentAdmin } from '../../../current-admin.interface'
import { ActionJSON } from '../../../frontend/interfaces/action/action-json.interface'
import BaseRecord from '../../adapters/record/base-record'
import actionErrorHandler from '../../services/action-error-handler/action-error-handler'
import ForbiddenError from '../../utils/errors/forbidden-error'
import {
ParsedLayoutElement,
LayoutElement,
layoutElementParser,
} from '../../utils/layout-element-parser'
const DEFAULT_VARIANT: VariantType = 'default'
/**
* Decorates an action
*
* @category Decorators
*/
class ActionDecorator {
public name: string
private _admin: AdminBro
private _resource: BaseResource
private h: ViewHelpers
private action: Action<ActionResponse>
/**
* @param {Object} params
* @param {Action} params.action
* @param {BaseResource} params.resource
* @param {AdminBro} params.admin current instance of AdminBro
*/
constructor({ action, admin, resource }: {
action: Action<ActionResponse>;
admin: AdminBro;
resource: BaseResource;
}) {
if (!action.actionType) {
throw new ConfigurationError(
`action: "${action.name}" does not have an "actionType" property`,
'Action',
)
}
this.name = action.name
this._admin = admin
this._resource = resource
this.h = new ViewHelpers({ options: admin.options })
/**
* Original action object
* @type {Action}
*/
this.action = action
}
/**
* Original handler wrapped with the hook `before` and `after` methods.
*
* @param {ActionRequest} request
* @param {any} response
* @param {ActionContext} context
*
* @return {Promise<any>}
*/
async handler(
request: ActionRequest,
response: any,
context: ActionContext,
): Promise<any> {
try {
const modifiedRequest = await this.invokeBeforeHook(request, context)
this.canInvokeAction(context)
const res = await this.invokeHandler(modifiedRequest, response, context)
return this.invokeAfterHook(res, modifiedRequest, context)
} catch (error) {
return actionErrorHandler(error, context)
}
}
/**
* Invokes before action hooks if there are any
*
* @param {ActionRequest} request
* @param {ActionContext} context
*
* @return {Promise<ActionRequest>}
*/
async invokeBeforeHook(request: ActionRequest, context: ActionContext): Promise<ActionRequest> {
if (!this.action.before) {
return request
}
if (typeof this.action.before === 'function') {
return this.action.before(request, context)
}
if (Array.isArray(this.action.before)) {
return (this.action.before as Array<Before>).reduce((prevPromise, hook) => (
prevPromise.then(modifiedRequest => (
hook(modifiedRequest, context)
))
), Promise.resolve(request))
}
throw new ConfigurationError(
'Before action hook has to be either function or Array<function>',
'Action#Before',
)
}
/**
* Invokes action handler if there is any
*
* @param {ActionRequest} request
* @param {any} response
* @param {ActionContext} context
*
* @return {Promise<ActionResponse>}
*/
async invokeHandler(
request: ActionRequest,
response: any,
context: ActionContext,
): Promise<ActionResponse> {
if (typeof this.action.handler === 'function') {
return this.action.handler(request, response, context)
}
if (Array.isArray(this.action.handler)) {
return (this.action.handler as Array<ActionHandler<ActionResponse>>).reduce(
(prevPromise, handler) => (
prevPromise.then(() => (
handler(request, response, context)
))
), Promise.resolve({}),
)
}
throw new ConfigurationError(
'Action handler has to be either function or Array<function>',
'Action#Before',
)
}
/**
* Invokes after action hooks if there are any
*
* @param {ActionResponse} response
* @param {ActionRequest} request
* @param {ActionContext} context
*
* @return {Promise<ActionResponse>}
*/
async invokeAfterHook(
response: ActionResponse,
request: ActionRequest,
context: ActionContext,
): Promise<ActionResponse> {
if (!this.action.after) {
return response
}
if (typeof this.action.after === 'function') {
return this.action.after(response, request, context)
}
if (Array.isArray(this.action.after)) {
return (this.action.after as Array<After<ActionResponse>>).reduce((prevPromise, hook) => (
prevPromise.then(modifiedResponse => (
hook(modifiedResponse, request, context)
))
), Promise.resolve(response))
}
throw new ConfigurationError(
'After action hook has to be either function or Array<function>',
'Action#After',
)
}
/**
* Returns true when action can be performed on a record
*
* @return {Boolean}
*/
isRecordType(): boolean {
return this.action.actionType.includes('record')
}
/**
* Returns true when action can be performed on an entire resource
*
* @return {Boolean}
*/
isResourceType(): boolean {
return this.action.actionType.includes('resource')
}
/**
* Returns true when action can be performed on selected records
*
* @return {Boolean}
*/
isBulkType(): boolean {
return this.action.actionType.includes('bulk')
}
is(what: 'isAccessible' | 'isVisible', currentAdmin?: CurrentAdmin, record?: BaseRecord): boolean {
if (!['isAccessible', 'isVisible'].includes(what)) {
throw new Error(`'what' has to be either "isAccessible" or "isVisible". You gave ${what}`)
}
let isAction
if (typeof this.action[what] === 'function') {
isAction = (this.action[what] as IsFunction)({
resource: this._resource,
record,
action: this,
h: this.h,
currentAdmin,
_admin: this._admin,
} as unknown as ActionContext)
} else if (typeof this.action[what] === 'undefined') {
isAction = true
} else {
isAction = this.action[what]
}
return isAction
}
/**
* Is action visible in the UI
* @param {CurrentAdmin} [currentAdmin] currently logged in admin user
* @param {BaseRecord} [record]
*
* @return {Boolean}
*/
isVisible(currentAdmin?: CurrentAdmin, record?: BaseRecord): boolean {
return this.is('isVisible', currentAdmin, record)
}
/**
* Is action accessible
*
* @param {CurrentAdmin} [currentAdmin] currently logged in admin user
* @param {BaseRecord} [record]
* @return {Boolean}
*/
isAccessible(currentAdmin?: CurrentAdmin, record?: BaseRecord): boolean {
return this.is('isAccessible', currentAdmin, record)
}
/**
* Indicates if user can invoke given action
*
* @param {ActionContext} context passed action context
*
* @return {boolean} true given user has rights to the action
* @throws {ForbiddenError} when user cannot perform given action
*/
canInvokeAction(context: ActionContext): boolean {
const { record, records, currentAdmin, resource } = context
if (record && this.isAccessible(currentAdmin, record)) {
return true
}
if (records && !records.find(bulkRecord => !this.isAccessible(currentAdmin, bulkRecord))) {
return true
}
if (!record && !records && this.isAccessible(currentAdmin)) {
return true
}
throw new ForbiddenError(this._admin.translateMessage('forbiddenError', resource.id(), {
actionName: this.name,
resourceId: resource.id(),
}))
}
containerWidth(): ActionJSON['containerWidth'] {
if (typeof this.action.containerWidth === 'undefined') {
return this.action.showInDrawer
? DEFAULT_DRAWER_WIDTH
: 1 // 100% for a regular action
}
return this.action.containerWidth
}
layout(currentAdmin?: CurrentAdmin): Array<ParsedLayoutElement> | null {
if (this.action.layout) {
let layoutConfig: Array<LayoutElement>
if (typeof this.action.layout === 'function') {
layoutConfig = this.action.layout(currentAdmin) as Array<LayoutElement>
} else {
layoutConfig = this.action.layout
}
return layoutConfig.map(element => layoutElementParser(element))
}
return null
}
variant(): VariantType {
return this.action.variant || DEFAULT_VARIANT
}
parent(): string | null {
return this.action.parent || null
}
custom(): Record<string, any> {
return this.action.custom || {}
}
hasHandler(): boolean {
return !!this.action.handler
}
/**
* Serializes action to JSON format
*
* @param {CurrentAdmin} [currentAdmin]
*
* @return {ActionJSON} serialized action
*/
toJSON(currentAdmin?: CurrentAdmin): ActionJSON {
const resourceId = this._resource._decorated?.id() || this._resource.id()
return {
name: this.action.name,
actionType: this.action.actionType,
icon: this.action.icon,
label: this._admin.translateAction(this.action.name, resourceId),
resourceId,
guard: this.action.guard ? this._admin.translateMessage(this.action.guard, resourceId) : '',
showFilter: !!this.action.showFilter,
component: this.action.component,
showInDrawer: !!this.action.showInDrawer,
hideActionHeader: !!this.action.hideActionHeader,
containerWidth: this.containerWidth(),
layout: this.layout(currentAdmin),
variant: this.variant(),
parent: this.parent(),
hasHandler: this.hasHandler(),
custom: this.custom(),
}
}
}
export default ActionDecorator