UNPKG

@avonjs/avonjs

Version:

A fluent Node.js API generator.

210 lines (209 loc) 6.42 kB
import Joi, { type AnySchema } from 'joi'; import type { OpenAPIV3 } from 'openapi-types'; import FieldCollection from '../Collections/FieldCollection'; import type { AnyValue, BulkActionResult, HasSchema, Model, Nullable, OpenApiSchema, Optional, Rules, RunCallback, SeeCallback, SerializedAction, UnknownRecord } from '../Contracts'; import type { Field } from '../Fields'; import type ActionRequest from '../Http/Requests/ActionRequest'; import type AvonRequest from '../Http/Requests/AvonRequest'; import { AvonResponse } from '../Http/Responses'; import { Fluent } from '../Models'; declare const Action_base: (abstract new (...args: import("../Contracts").Args) => { seeCallback: SeeCallback; authorizedToSee(request: AvonRequest): boolean; canSee(callback: SeeCallback): any; }) & { new (): {}; }; export default abstract class Action extends Action_base implements HasSchema { /** * The callback used to authorize running the action. */ runCallback?: RunCallback; /** * Indicates if the action can be run without any models. */ standaloneAction: boolean; /** * Indicates if the action can be run on a single model. */ inlineAction: boolean; /** * Indicates if the action destroy some resources. */ destructiveAction: boolean; /** * Indicates the response status code. */ protected responseCode: number; /** * Indicates the response content type. */ protected responseType: string; /** * Execute the action for the given request. */ handleRequest(request: ActionRequest): Promise<AvonResponse>; /** * Get models for incoming action. */ protected resolveModels(request: ActionRequest): Promise<never[] | Model[]>; /** * Authorize models before running action. */ protected authorizeAction(request: ActionRequest, models: Model[]): Promise<void>; /** * Authorize a standalone action. */ protected authorizeStandaloneAction(request: ActionRequest): Promise<void>; /** * Authorize an inline action. */ protected authorizeInlineAction(request: ActionRequest, model: Model): Promise<void>; /** * Authorize a bulk action. */ protected authorizeBulkAction(request: ActionRequest, models: Model[]): Promise<void>; /** * Handle authorization errors. */ protected handleAuthorizationError(error: unknown): Error; /** * Make Joi validator to authorize the models. */ protected authorizationValidator(request: ActionRequest): Joi.ArraySchema<any[]>; /** * Get unauthorized to run message. */ protected unauthorizedMessage(model?: Nullable<Model>): string; /** * Prepare change log for incoming action. */ protected getChanges(request: ActionRequest, resources: Model[]): BulkActionResult; /** * Resolve the creation fields. */ resolveFields(request: AvonRequest): Fluent; /** * Store changes for incoming action. */ protected recordChanges(request: ActionRequest, changes?: BulkActionResult): Promise<void>; /** * Perform the action on the given models. */ protected abstract handle(fields: Fluent, models: Model[]): Promise<Optional<AvonResponse>>; /** * Determine if the action is executable for the given request. */ authorizedToRun(request: AvonRequest, model?: Nullable<Model>): Promise<boolean>; /** * Validate an action for incoming request. * * @throws {ValidationException} */ validate(request: AvonRequest): Promise<void>; /** * Create a validator instance for a resource creation request. */ validator(request: AvonRequest): Joi.AnySchema; /** * Get the validation rules for a resource creation request. */ rules(request: AvonRequest): AnySchema[]; /** * Prepare given rules for validator. */ prepareRulesForValidator(rules: Rules[]): AnySchema[]; /** * Perform any final formatting of the given validation rules. */ protected formatRules(request: AvonRequest, rules: AnySchema[]): AnySchema[]; /** * Prepare given rules for validator. */ dataForValidation(request: AvonRequest): import("../Contracts").AnyRecord; /** * Handle any post-validation processing. */ protected afterValidation(request: AvonRequest, validator: AnyValue): AnyValue; /** * Get the fields that are available for the given request. */ availableFields(request: AvonRequest): FieldCollection; /** * Get the fields available on the action. */ fields(request: AvonRequest): Field[]; /** * Set the callback to be run to authorize running the action. */ canRun(callback?: RunCallback): this; /** * Set the callback to be run to authorize viewing the filter or action. */ canSee(callback: SeeCallback): this; /** * Get the displayable name of the action. */ name(): string; /** * Get the URI key for the action. */ uriKey(): string; /** * Mark the action as a standalone action. * * @return this */ standalone(): this; /** * Determine if the action is a "standalone" action. * * @return bool */ isStandalone(): boolean; /** * Mark the action as a "inline" action. * * @return this */ inline(): this; /** * Determine if the action is a "inline" action. * * @return bool */ isInline(): boolean; /** * Mark the action as a "destructive" action. * * @return this */ destructive(): this; /** * Determine if the action is a "destructive" action. * * @return bool */ isDestructive(): boolean; /** * Prepare the action for JSON serialization. */ serializeForIndex(request: ActionRequest): SerializedAction; /** * Get successful response. */ respondSuccess(data?: UnknownRecord): AvonResponse; /** * Get the swagger-ui response schema. */ responseSchema(request: AvonRequest): OpenAPIV3.ResponsesObject; /** * Get the swagger-ui schema. */ schema(request: AvonRequest): OpenApiSchema; /** * Get the swagger-ui possible request body contents. */ accepts(): string[]; } export {};