restifyx.js
Version:
Advanced API endpoint handler system with automatic documentation
187 lines (186 loc) • 5.95 kB
TypeScript
import type { Request, Response, NextFunction } from "express";
import type { HttpMethod, EndpointMetadata, MiddlewareFunction } from "../types/endpoint";
export type ValidationSchemaType = "string" | "number" | "boolean" | "date" | "decimal" | "object" | "array";
export type ValidationSchemaObject = {
type: ValidationSchemaType;
required?: boolean;
min?: number;
max?: number;
pattern?: RegExp;
default?: any;
allow?: any[];
};
export type ValidationSchema = Record<string, string | ValidationSchemaObject>;
export type ResponseDefinition = Record<string | number, any>;
export type RateLimitOptions = {
windowMs: number;
max: number;
message?: string;
};
export type EndpointContext = {
req: Request;
res: Response;
next: NextFunction;
};
export type EndpointHandler = (header: Record<string, any>, body: Record<string, any>, query: Record<string, any>, cookie: Record<string, any>, params: Record<string, any>, context?: EndpointContext) => Promise<void> | void;
/**
* Fluent Function Code for defining endpoints
*
* @example
* \`\`\`typescript
* const endpoint = new Endpoint()
* .setName('Get User')
* .setDescription('Get user by ID')
* .setPath('/api/users/:id')
* .setMethod('GET')
* .setTags(['users'])
* .addResponse({
* 200: { message: 'Success' },
* 404: 'User not found'
* })
* .addQuery({
* include: 'string'
* })
* .setHandler(async (header, body, query, params) => {
* // Implementation
* SendData.json({ user: { id: params.id } });
* })
* \`\`\`
*/
export declare class Endpoint {
private metadata;
private querySchema;
private bodySchema;
private headerSchema;
private cookieSchema;
private paramsSchema;
private responses;
private rateLimit?;
private cacheOptions?;
private handler?;
/**
* Set the name of the endpoint
*
* @param name - The name of the endpoint
* @returns The Endpoint instance for chaining
*/
setName(name: string): Endpoint;
/**
* Set the description of the endpoint
*
* @param description - The description of the endpoint
* @returns The Endpoint instance for chaining
*/
setDescription(description: string): Endpoint;
/**
* Set the path of the endpoint
*
* @param path - The path of the endpoint (e.g., '/api/users/:id')
* @returns The Endpoint instance for chaining
*/
setPath(path: string): Endpoint;
/**
* Set the HTTP method of the endpoint
*
* @param method - The HTTP method (GET, POST, PUT, DELETE, etc.)
* @returns The Endpoint instance for chaining
*/
setMethod(method: HttpMethod): Endpoint;
/**
* Set tags for the endpoint (used for Swagger documentation)
*
* @param tags - Array of tags
* @returns The Endpoint instance for chaining
*/
setTags(tags: string[]): Endpoint;
/**
* Add middleware to the endpoint
*
* @param middleware - Express middleware function(s)
* @returns The Endpoint instance for chaining
*/
addMiddleware(...middleware: MiddlewareFunction[]): Endpoint;
/**
* Set rate limiting options for the endpoint
*
* @param options - Rate limiting options
* @returns The Endpoint instance for chaining
*/
setRateLimit(options: RateLimitOptions): Endpoint;
/**
* Set caching options for the endpoint
*
* @param duration - Cache duration in seconds
* @param keyGenerator - Optional function to generate cache key
* @returns The Endpoint instance for chaining
*/
setCache(duration: number, keyGenerator?: (req: Request) => string): Endpoint;
/**
* Define expected query parameters and their types
*
* @param schema - Schema definition for query parameters
* @returns The Endpoint instance for chaining
*/
addQuery(schema: ValidationSchema): Endpoint;
/**
* Define expected body parameters and their types
*
* @param schema - Schema definition for request body
* @returns The Endpoint instance for chaining
*/
addBody(schema: ValidationSchema): Endpoint;
/**
* Define expected header parameters and their types
*
* @param schema - Schema definition for request headers
* @returns The Endpoint instance for chaining
*/
addHeader(schema: ValidationSchema): Endpoint;
/**
* Define expected cookie parameters and their types
*
* @param schema - Schema definition for cookies
* @returns The Endpoint instance for chaining
*/
addCookie(schema: ValidationSchema): Endpoint;
/**
* Define expected path parameters and their types
*
* @param schema - Schema definition for path parameters
* @returns The Endpoint instance for chaining
*/
addParams(schema: ValidationSchema): Endpoint;
/**
* Define response schemas for different status codes
*
* @param responses - Object mapping status codes to response schemas
* @returns The Endpoint instance for chaining
*/
addResponse(responses: ResponseDefinition): Endpoint;
/**
* Set the handler function for the endpoint
*
* @param handler - The function that handles the endpoint request
* @returns The Endpoint instance for chaining
*/
setHandler(handler: EndpointHandler): Endpoint;
/**
* Build the endpoint metadata and handler
*
* @returns The endpoint metadata
* @internal
*/
build(): EndpointMetadata;
private createJoiSchema;
private createJoiSchemaForType;
private buildResponsesMetadata;
private buildParametersMetadata;
private buildRequestBodyMetadata;
private buildParameterSchema;
}
/**
* Create a new endpoint with a fluent function
*
* @returns A new Endpoint instance
*/
export declare function CreateEndpoint(): Endpoint;