UNPKG

backend.in

Version:

A npm module that could help node.js servers in one line of code

175 lines (172 loc) 5.15 kB
import { Request, Response, NextFunction, Application } from 'express'; import mongoose, { Document, Model } from 'mongoose'; import session from 'express-session'; import multer from 'multer'; import cors from 'cors'; import nodemailer from 'nodemailer'; import { createClient } from 'redis'; /** * NOTE: Install these before using: * npm i express mongoose bcryptjs jsonwebtoken express-session multer helmet cors morgan express-rate-limit nodemailer redis uuid axios * npm i -D @types/express @types/mongoose @types/bcryptjs @types/jsonwebtoken @types/express-session @types/multer @types/helmet @types/cors @types/morgan @types/express-rate-limit @types/redis @types/uuid @types/axios */ type AnyObject = { [k: string]: any; }; type Tier = "starter" | "basic" | "premium" | "advanced"; interface AuthRoutesPaths { signup?: string; login?: string; logout?: string; me?: string; refresh?: string; update?: string; changePassword?: string; forgotPassword?: string; resetPassword?: string; verifyEmail?: string; } interface JWTConfig { accessTokenSecret: string; refreshTokenSecret?: string; accessTokenExpiresIn?: string | number; refreshTokenExpiresIn?: string | number; } interface EmailConfig { transporterOptions: nodemailer.TransportOptions; from?: string; sendVerificationEmail?: (to: string, token: string) => Promise<void>; sendResetEmail?: (to: string, token: string) => Promise<void>; } interface RateLimitConfig { windowMs?: number; max?: number; message?: string; tierOverrides?: Partial<Record<Tier, { max: number; }>>; } interface StorageConfig { uploadDir?: string; multerStorage?: multer.StorageEngine; staticRoute?: string; maxFileSize?: number; allowedMimeTypes?: string[]; } interface ProjectConfig { id: string; name: string; tier: Tier; maxRoutes: number; maxLibraries: number; maxUsers?: number; maxRequestsPerMonth?: number; features: { database: boolean; advancedAuth: boolean; storage: boolean; email: boolean; redis: boolean; rateLimiting: boolean; realtime: boolean; analytics: boolean; }; } interface UsageMetrics { requests: number; lastReset: Date; users?: number; storage?: number; } interface ServerOptions<T extends Document> { apiKey: string; secret: string; port: number; projectId: string; tier: Tier; usage?: UsageMetrics; validationService?: { url: string; validateEndpoint: string; reportUsageEndpoint: string; }; libraries?: Array<(req: Request, res: Response, next: NextFunction) => void>; routes?: Array<{ method: "get" | "post" | "put" | "delete" | "patch"; path: string; handlers: any[]; }>; credentials?: { apiKey?: string; secret?: string; }; mongoUri?: string; baseRoute?: string; auth?: { enabled?: boolean; model?: Model<T>; jwt?: JWTConfig; advanced?: boolean; routes?: AuthRoutesPaths; requireEmailVerification?: boolean; roleField?: string; oauth?: { google?: { clientId: string; clientSecret: string; }; github?: { clientId: string; clientSecret: string; }; }; }; session?: { enabled?: boolean; options?: session.SessionOptions; redis?: boolean; }; storage?: { enabled?: boolean; config?: StorageConfig; }; enableHelmet?: boolean; enableCors?: boolean | cors.CorsOptions; enableLogging?: boolean; rateLimit?: RateLimitConfig | false; email?: EmailConfig | false; redis?: { enabled?: boolean; url?: string; }; validators?: { signup?: (body: any) => { ok: boolean; message?: string; }; login?: (body: any) => { ok: boolean; message?: string; }; resetPassword?: (body: any) => { ok: boolean; message?: string; }; }; webhooks?: { onUserSignup?: (user: any) => Promise<void>; onUserLogin?: (user: any) => Promise<void>; onError?: (error: any, context: string) => Promise<void>; }; analytics?: { enabled?: boolean; ignorePaths?: string[]; }; } declare let DB: typeof mongoose | null; declare let appInstance: Application | null; declare let redisClient: ReturnType<typeof createClient> | null; declare let projectConfig: ProjectConfig | null; declare function startServer<T extends Document>(options: ServerOptions<T>): Promise<Application>; declare function stopServer(): Promise<void>; export { type AnyObject, type AuthRoutesPaths, DB, type EmailConfig, type JWTConfig, type ProjectConfig, type RateLimitConfig, type ServerOptions, type StorageConfig, type Tier, type UsageMetrics, appInstance, projectConfig, redisClient, startServer, stopServer };