API Reference
Overview of the public API provided by vitek-plugin. Import from vitek-plugin in your project.
Plugin
vitek(options?)- Returns a Vite plugin. Call fromvite.config.tsin thepluginsarray.VitekOptions- Type for plugin options:apiDir,apiBasePath,openApi,enableValidation,logging,sockets,plugins,alias,onGenerationError, etc. See Configuration.
Plugin API (Extensibility)
VitekPlugin- Type for external plugins with hooks.afterTypesGenerated(ctx)- Hook called after types/services/OpenAPI are generated.beforeApiRequest(ctx)- Hook called before each API request; callnext()to continue or send a response to short-circuit.AfterTypesGeneratedContext- Context:root,schema,sockets,apiBasePath,socketBasePath.BeforeApiRequestContext- Context:req,res,path,method,next.
Pass plugins via vitek({ plugins: [myPlugin] }). See Plugin API for usage.
Introspection
getManifest(root, apiDir)- Returns{ routes, middlewares, sockets }with metadata. Paths are relative to root.getRoutes(root, apiDir)- ReturnsParsedRoute[]:{ method, pattern, params, file }.getSockets(root, apiDir)- ReturnsParsedSocket[]:{ pattern, params, file }.writeManifest(root, apiDir, outDir)- Writesvitek-manifest.jsonto outDir.VitekManifest- Type for the manifest object.ParsedRoute- Type for parsed route metadata.ParsedSocket- Type for parsed socket metadata.
See Introspection API for the manifest format and use cases.
Documentation (OpenAPI / AsyncAPI)
When using openApi: true (or an options object) in plugin options:
OpenApiOptions- Options for OpenAPI generation:info,servers,apiBasePath.OpenApiInfo- API info:title,version,description.OpenApiServer- Server entry:url,description.AsyncApiOptions- Options for AsyncAPI generation (used internally when sockets exist):info,serverUrl.AsyncApiInfo- AsyncAPI info:title,version,description.
See OpenAPI / Swagger + AsyncAPI for the full guide.
Types
VitekContext- Context passed to route handlers (method, path, params, query, body, etc.).VitekRequest- Internal request representation.Route- Describes a registered route.RouteHandler- Type of the default export of a route file:(context: VitekContext) => Promise<any> | any.Middleware- Type of middleware functions:(context, next) => Promise<void>.RouteMatch- Result of matching a request to a route.
Response Helpers
Use these to return custom status codes and headers from handlers:
json(body, options?)- Custom JSON response with status and headersok(body, headers?)- 200 OKcreated(body, headers?)- 201 CreatednoContent(headers?)- 204 No ContentbadRequest(body?, headers?)- 400unauthorized(body?, headers?)- 401forbidden(body?, headers?)- 403notFound(body?, headers?)- 404conflict(body?, headers?)- 409unprocessableEntity(body?, headers?)- 422tooManyRequests(body?, headers?)- 429internalServerError(body?, headers?)- 500redirect(url, permanent?, preserveMethod?)- 301/302/307/308
See Response Handling for usage.
Error Classes
Throw these in handlers for automatic status code and JSON error response:
VitekError- Base errorHttpError- Base for HTTP errors (hasstatusCode)BadRequestError- 400UnauthorizedError- 401ForbiddenError- 403NotFoundError- 404ConflictError- 409ValidationError- 422 (supports field errors object)TooManyRequestsError- 429InternalServerError- 500
See Error Handling for usage.
Validation
validate(data, schema)- Returns validation result without throwing.validateOrThrow(data, schema)- ThrowsValidationErrorif invalid.validateBody(body, schema)- Validates request body.validateQuery(query, schema)- Validates query parameters.ValidationSchema- Type:Record<string, ValidationRule>.ValidationRule- Type:{ type, required?, min?, max?, pattern?, custom? }.ValidationResult- Type returned byvalidate.
See Request Validation for usage.
WebSockets
- Socket handler context – The argument to
*.socket.ts/*.socket.jshandlers:socket,req,params,path, and optionallyapi(internal API client). See WebSockets. ctx.api– When present, providesfetch(path, options?)to call REST endpoints internally from a socket handler. Path is relative to the API base path.context.sockets– In HTTP route handlers, when provided:emit(pattern, data)to push data to WebSocket clients connected to that socket path (pattern=''for root,'chat'for/api/ws/chat, etc.).
