UNPKG

easy-api.ts

Version:

A powerful library to create your own API with ease.

99 lines (98 loc) 2.79 kB
import { type APIFunctionLike, FunctionManager } from "../managers/FunctionManager"; import { RouteCompilationManager } from "../managers/RouteCompilationManager"; import { BaseDatabase } from "../managers/DatabaseManager"; import { Data } from "../../main"; import { TypedEmitter } from "tiny-typed-emitter"; import { Addon } from "./Addon"; import * as F from "fastify"; export interface EATS_Events { error: (exception: Error) => any; request: (data: Data) => any; ready: (data: Data) => any; } export interface EATS_API_OPTIONS extends F.FastifyServerOptions { addons?: Addon[]; dots?: boolean; reverse?: boolean; debug?: boolean; omitFunctions?: string[]; databaseOptions?: { disableDefault?: boolean; customOptions?: unknown; overrideDatabaseWith?: () => typeof BaseDatabase; }; } /** * Represents an easy-api.ts route. */ export interface EATS_ROUTE { method: F.HTTPMethods; url: string; code: string; comments?: boolean; reverse?: boolean; /** * Additional information about this route. */ extraData?: Record<string, any>; } export interface EATS_EVENT { name: keyof EATS_Events; code: string; comments?: boolean; reverse?: boolean; } /** * The main hub of your API. */ export declare class API extends TypedEmitter<EATS_Events> { #private; options: EATS_API_OPTIONS; uptime: number; app: F.FastifyInstance; functions: FunctionManager; private interpreter; compilationManager: RouteCompilationManager; db: BaseDatabase<unknown, unknown>; constructor(options: EATS_API_OPTIONS); /** * Add the given functions to the function manager. * @param functions - The functions to be added. * @returns {void} */ addFunction(...functions: APIFunctionLike[]): void; /** * Adds a route to the API. * @param data - Object structure of the route. * @returns {void} */ route(data: EATS_ROUTE): void; /** * Run code when events gets emitted. * @param data - Object structure of the event. * @returns {void} */ event(data: EATS_EVENT): void; /** * Loads routes and events using this method. * @param dir - Files directory to be loaded. * @returns {void} */ load(dir: string): void; /** * Set the not found (404) error handler. * @param code - Code to be executed when error handler is triggered. * @returns {void} */ setNotFoundHandler(code: string): void; /** * Connects the API. * @param options - Connection options. * @returns {void} */ connect(options: F.FastifyListenOptions): void; /** * Whether "rs" syntax is enabled. */ get rs(): boolean; }