| |
| |
| import populator from '../utils/populator/populator.js' |
| import ViewHelpers from '../utils/view-helpers/view-helpers.js' |
| import { CurrentAdmin } from '../../current-admin.interface.js' |
| import AdminJS from '../../adminjs.js' |
| import { ActionContext, ActionRequest, RecordActionResponse, ActionResponse, BulkActionResponse } from '../actions/action.interface.js' |
| import ConfigurationError from '../utils/errors/configuration-error.js' |
| import NotFoundError from '../utils/errors/not-found-error.js' |
| import ForbiddenError from '../utils/errors/forbidden-error.js' |
| import { requestParser } from '../utils/request-parser/index.js' |
| import { SearchActionResponse } from '../actions/search/search-action.js' |
| import actionErrorHandler from '../services/action-error-handler/action-error-handler.js' |
| import { validateParam } from '../../utils/param-converter/validate-param.js' |
| import { DecoratedProperties } from '../decorators/resource/utils/decorate-properties.js' |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class ApiController { |
| private _admin: AdminJS |
|
|
| private currentAdmin: CurrentAdmin |
|
|
| |
| |
| |
| |
| |
| constructor({ admin }, currentAdmin) { |
| this._admin = admin |
| this.currentAdmin = currentAdmin |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| async getActionContext(request: ActionRequest): Promise<ActionContext> { |
| const { resourceId, action: actionName } = request.params |
| const h = new ViewHelpers(this._admin) |
| const resource = this._admin.findResource(resourceId) |
| const action = resource.decorate().actions[actionName] |
| return { |
| resource, |
| action, |
| h, |
| currentAdmin: this.currentAdmin, |
| _admin: this._admin, |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async search(request: ActionRequest, response): Promise<SearchActionResponse> { |
| request.params.action = 'search' |
| |
| console.log([ |
| 'Using ApiController#search is deprecated in favour of resourceAction', |
| 'It will be removed in the next version', |
| ].join('\n')) |
| return this.resourceAction(request, response) as Promise<SearchActionResponse> |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async resourceAction(originalRequest: ActionRequest, response: any): Promise<ActionResponse> { |
| const actionContext = await this.getActionContext(originalRequest) |
| const request = requestParser(originalRequest, actionContext.resource) |
| return actionContext.action.handler(request, response, actionContext) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async recordAction(originalRequest: ActionRequest, response: any): Promise<RecordActionResponse> { |
| const { recordId, resourceId } = originalRequest.params |
| const actionContext = await this.getActionContext(originalRequest) |
| const request = requestParser(originalRequest, actionContext.resource) |
|
|
| if (!recordId) { |
| throw new NotFoundError([ |
| 'You have to pass recordId to the recordAction', |
| ].join('\n'), 'Action#handler') |
| } |
|
|
| const idProperty = Object.values(actionContext.resource.decorate()?.properties as DecoratedProperties) |
| .find((p) => p.isId()) |
| if (!idProperty || !validateParam(recordId, idProperty)) { |
| const invalidRecordError = actionErrorHandler( |
| new ForbiddenError([ |
| 'You have to pass a valid recordId to the recordAction', |
| ].join('\n')), |
| actionContext, |
| ) |
|
|
| return invalidRecordError as RecordActionResponse |
| } |
|
|
| let record = await actionContext.resource.findOne(recordId, actionContext) |
|
|
| if (!record) { |
| const missingRecordError = actionErrorHandler( |
| new NotFoundError([ |
| `Record with given id: "${recordId}" cannot be found in resource "${resourceId}"`, |
| ].join('\n'), 'Action#handler'), |
| actionContext, |
| ) |
|
|
| return missingRecordError as RecordActionResponse |
| } |
|
|
| [record] = await populator([record], actionContext) |
|
|
| actionContext.record = record |
| const jsonWithRecord = await actionContext.action.handler(request, response, actionContext) |
|
|
| const isValidRecord = !!(jsonWithRecord && jsonWithRecord.record && jsonWithRecord.record.recordActions) |
| const anErrorWasHandled = jsonWithRecord && jsonWithRecord.notice && jsonWithRecord.notice.type === 'error' |
|
|
| if (isValidRecord || anErrorWasHandled) { |
| return jsonWithRecord |
| } |
|
|
| throw new ConfigurationError( |
| 'handler of a recordAction should return a RecordJSON object', |
| 'Action#handler', |
| ) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async bulkAction(originalRequest: ActionRequest, response: any): Promise<BulkActionResponse> { |
| const { resourceId } = originalRequest.params |
| const { recordIds } = originalRequest.query || {} |
| const actionContext = await this.getActionContext(originalRequest) |
| const request = requestParser(originalRequest, actionContext.resource) |
|
|
| if (!recordIds) { |
| throw new NotFoundError([ |
| 'You have to pass "recordIds" to the bulkAction via search params: ?recordIds=...', |
| ].join('\n'), 'Action#handler') |
| } |
|
|
| let records = await actionContext.resource.findMany(recordIds.split(','), actionContext) |
|
|
| if (!records || !records.length) { |
| throw new NotFoundError([ |
| `record with given id: "${recordIds}" cannot be found in resource "${resourceId}"`, |
| ].join('\n'), 'Action#handler') |
| } |
| records = await populator(records, actionContext) |
| const jsonWithRecord = await actionContext.action.handler(request, response, { ...actionContext, records }) |
|
|
| if (jsonWithRecord && jsonWithRecord.records) { |
| return jsonWithRecord |
| } |
| throw new ConfigurationError( |
| 'handler of a bulkAction should return an Array of RecordJSON object', |
| 'Action#handler', |
| ) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async dashboard(request: any, response: any): Promise<any> { |
| const h = new ViewHelpers(this._admin) |
| const handler = this._admin.options.dashboard && this._admin.options.dashboard.handler |
| if (handler) { |
| return handler(request, response, { |
| h, |
| currentAdmin: this.currentAdmin, |
| _admin: this._admin, |
| }) |
| } |
| return { |
| message: [ |
| 'You can override this method by setting up dashboard.handler', |
| 'function in AdminJS options', |
| ].join('\n'), |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async page(request: any, response: any): Promise<any> { |
| const h = new ViewHelpers(this._admin) |
| const { pages = {} } = this._admin.options |
|
|
| const { pageName } = request.params |
| const { handler } = (pages[pageName] || {}) |
|
|
| if (handler) { |
| return handler(request, response, { |
| h, |
| currentAdmin: this.currentAdmin, |
| _admin: this._admin, |
| }) |
| } |
| return { |
| message: [ |
| 'You can override this method by setting up pages[pageName].handler', |
| 'function in AdminJS options', |
| ].join('\n'), |
| } |
| } |
| } |
|
|
| export default ApiController |
|
|