UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

101 lines (95 loc) 3.26 kB
import { L as LucidErrorData, E as ErrorResult, R as ResponseBody, P as Permission, a as RouteController, S as ServiceFn, b as ServiceWrapperConfig, c as ServiceContext, d as ServiceResponse } from './adapter-CEgX2yQ4.js'; import zod__default from 'zod'; import { FastifyRequest, FastifyInstance } from 'fastify'; import 'kysely'; import 'kysely/helpers/sqlite'; import 'http'; import 'node:stream'; import 'fs'; import 'stream'; import 'vite'; /** * The LucidAPIError class should be used to throw errors within the API request lifecycle. This will be caught by Fastify's error handler and will return a formatted error response. If the error is a Zod error, it will be formatted into a more readable format. * @class * @extends Error * @param {LucidErrorData} error * @returns {void} * @example * throw new LucidAPIError({ * type: "basic", * name: "Fetch User Error", * message: "Error while fetching user data", * status: 500, * }); * @example * throw new LucidAPIError({ * type: "validation", * name: "Validation Error", * message: "Validation error occurred", * status: 400, * errorResponse: { * body: { * email: { * code: "invalid_email", * message: "Invalid email address", * }, * }, * }, * }); */ declare class LucidAPIError extends Error { error: LucidErrorData; constructor(error: LucidErrorData); static formatZodErrors(error: zod__default.ZodIssue[]): ErrorResult; } interface BuildResponseParams { data: unknown; pagination?: { count: number; page: number; perPage: number; }; } type FormatAPIResponse = (request: FastifyRequest, params: BuildResponseParams) => ResponseBody; declare const formatAPIResponse: FormatAPIResponse; type Route = <ParamsT extends zod__default.ZodTypeAny | undefined, BodyT extends zod__default.ZodTypeAny | undefined, QueryT extends zod__default.ZodTypeAny | undefined>(fastify: FastifyInstance, opts: { method: "get" | "post" | "put" | "delete" | "patch"; url: string; permissions?: Permission[]; middleware?: { authenticate?: boolean; validateCSRF?: boolean; contentLocale?: boolean; clientAuthentication?: boolean; }; zodSchema?: { params?: ParamsT; body?: BodyT; query?: QueryT; }; swaggerSchema?: { description?: string; tags?: string[]; summary?: string; response?: Record<string | number, { description?: string; type?: string; properties?: unknown; }>; body?: { type?: string; properties?: unknown; required?: string[]; }; headers?: { type: string; properties: unknown; required: string[]; }; }; bodyLimit?: number; controller: RouteController<ParamsT, BodyT, QueryT>; }) => void; declare const route: Route; declare const serviceWrapper: <T extends unknown[], R>(fn: ServiceFn<T, R>, wrapperConfig: ServiceWrapperConfig) => (service: ServiceContext, ...args: T) => ServiceResponse<R>; export { LucidAPIError, formatAPIResponse, route, serviceWrapper };