UNPKG

@avonjs/avonjs

Version:

A fluent Node.js API generator.

182 lines (181 loc) 5.36 kB
import express, { type Request, type Response } from 'express'; import { type Params } from 'express-jwt'; import { type SignOptions } from 'jsonwebtoken'; import type { OpenAPIV3 } from 'openapi-types'; import FieldCollection from './Collections/FieldCollection'; import { ResourceCollection } from './Collections/ResourceCollection'; import type { AttemptCallback, Auth, Dictionary, ErrorHandler, Optional, PrimaryKey, UserResolver } from './Contracts'; import { type Field } from './Fields'; import LoginRequest from './Http/Requests/Auth/LoginRequest'; import type AvonRequest from './Http/Requests/AvonRequest'; import type { AvonResponse } from './Http/Responses'; import Resource from './Resource'; export default class Avon { /** * Indicates application current version. */ protected static VERSION: string; /** * Array of available resources. */ protected static resourceInstances: Resource[]; /** * Map of available resources. */ protected static resourceMap: Dictionary<Resource>; /** * The error handler callback. */ protected static errorHandler: ErrorHandler; /** * The user resolver callback. */ protected static userResolver: UserResolver; /** * Extended swagger paths. */ protected static paths: OpenAPIV3.PathsObject; /** * List of routes without authorization. */ protected static excepts: Array<string | RegExp>; /** * The login attempt callback. */ protected static attemptCallback: AttemptCallback; /** * Set application secret key. */ protected static appKey: string; /** * Indicates JWT params. */ protected static jwtSignOptions: SignOptions; /** * Indicates JWT verify params. */ protected static jwtVerifyOptions: Params; /** * The login attempt callback. */ protected static authFields: Field[]; /** * Extended swagger info. */ protected static info: OpenAPIV3.InfoObject; /** * Get the Avon version. */ static version(): string; /** * Register array of new resources. */ static resources(resources?: Resource[]): Avon; /** * Find resource for given uriKey. */ static resourceForKey(key: string): Optional<Resource>; /** * Get collection of available resources. */ static resourceCollection(): ResourceCollection; /** * Get express instance. */ static routes(withAuthentication?: boolean): import("express-serve-static-core").Express; /** * Get express instance. */ static express(withAuthentication?: boolean, middlewares?: Array<express.RequestHandler>): import("express-serve-static-core").Express; /** * Get JWT middleware. */ static expressjwt(): { (req: express.Request, res: express.Response, next: express.NextFunction): Promise<void>; unless: typeof import("express-unless").unless; }; /** * Handle the given error. */ static handleError(error: Error): Avon; /** * Handle the given error. */ static handleErrorUsing(errorHandler: ErrorHandler): Avon; /** * Get the user id. */ static userId(request: AvonRequest): Optional<PrimaryKey>; /** * Resolve the user for incoming request to share in the app. */ static resolveUser(request: AvonRequest): import("./Contracts").Deferred<import("./Contracts").Nullable<import("./Contracts").Model>>; /** * Set the user resolver callback. */ static resolveUserUsing(userResolver: UserResolver): typeof Avon; /** * Register resource from given path. */ static resourceIn(path: string): typeof Avon; /** * Set login fields. */ static credentials(authFields: Field[]): typeof Avon; /** * Get login fields. */ static fieldsForLogin(): FieldCollection<Field>; /** * Set JWT secret. */ static key(appKey: string): typeof Avon; /** * Extend swagger paths. */ static extend(paths: OpenAPIV3.PathsObject): typeof Avon; /** * Extend swagger paths. */ static describe(info: OpenAPIV3.InfoObject): typeof Avon; /** * Set the JWT sign options. */ static signOptions(signOptions: SignOptions): typeof Avon; /** * Set the JWT verify options. */ static verifyOptions(verifyOptions: Params): typeof Avon; /** * Set the JWT options. */ static except(path: string | RegExp): typeof Avon; /** * Set attempt callback. */ static attemptUsing(attemptCallback: AttemptCallback): typeof Avon; /** * Handle login request. */ static login(req: Request, res: Response): Promise<void>; /** * Perform login request validation. */ static performValidation(request: LoginRequest): Promise<void>; /** * Set attempt callback. */ static attempt(payload: Dictionary<unknown>): Promise<AvonResponse>; /** * Make JWT token. */ static sign(payload: Auth): string; /** * Get the schema for open API. */ static schema(request: AvonRequest): OpenAPIV3.Document; /** * Get the login swagger schema. */ static loginSchema(request: AvonRequest): OpenAPIV3.PathsObject; }