texdec v0.0.18
TExDec
Yet another typescript web framework
Installation
mkdir mywebproject cd mywebproject npm init npm i typescript ./node_modules/.bin/tsc --init npm i texdec express @types/express @types/nodego to tsconfig.json and set
experimentalDecoratorsandemitDecoratorMetadatatotruetargettoes2019Set up entry file
on entry file (e.x
server.ts) the usual express stuffexport const app: Express = express() app.use(express.urlencoded()) app.use(express.json())next thing is to get the TExDecSettings class
import {TExDecSettings} from 'texdec' const texDecSettings = TExDecSettings.getInstance()the only option we have to set for TExDec to work is the
controllerDirtexDecSettings.set('controllerDir', path.join(__dirname, 'controllers'))TExDec will search in the given path for files with name matching
*.controllerand load themand then we execute
TExDec.init(app)in order to find all the controller files and load the routes.import {TExDec} from 'texdec' import {Server} from 'http' TExDec.init(app).then(() => { const http = new Server(app) http.listen(3000, () => { console.info(`server started at http://localhost:${3000}`) }) })
TExDec.initreturns a promise which is resolved when all the routes from the controller files have been loaded. When the routes are loaded we can instantiate the http server.Our first controller
create the directory structure
controllers/catsExample controller:
inside
controllers/catscreate the filescats.controller.tscats.controller.ts
import {Controller, Get, Param, Post, Query, Body} from 'texdec' import {CatsConfig} from './cats.config' @Controller('cats', CatsConfig) class CatsController { public cats: string[] @Get('/') getMany( @Query() search: string, ) { if (search) return this.cats.filter(cat => (cat.indexOf(search) > -1)) else return cats } @Get('/:id') getOne( @Param('id') index: number, ) { return this.cats[index] } @Post('/') insertOne( @Body() name: string, ) { this.cats.push(name) return this.cats } }Class decorator
@Controller(baseRoute: string, controllerConfig?: IControllerConfig)Params
baseRoutethe main route of the controllercontrollerConfigthe configuration class of the controllerMethod Decorators:
@Get(route: string, validationObj?: IValidationObj)@Post(route: string, validationObj?: IValidationObj)@Put(route: string, validationObj?: IValidationObj)@Delete(route: string, validationObj?: IValidationObj)@Patch(route: string, validationObj?: IValidationObj)Params
routethe route of the methodvalidationObjthe validation object of the incoming parametersMethod Parameter Decorators
Match Query parameters
@Query(parameterName?: string | null | undefined, castToType?: boolean)Match url parameters
@Param(parameterName?: string | null | undefined, castToType?: boolean)Match body parameters (only if incomming body is json format)
@Body(parameterName?: string | null | undefined, castToType?: boolean)The Response Object
@Res()The Request Object
@Req()Params
parameterName(Optional) the name of the incoming key - if not set the name of the variable will be used.castToType(Optional) defaults totrue. If set tofalsethe incoming variable will be cast to the type that is declaredOur Controller config
inside
controllers/catscreate the filecats.config.tsimport {ControllerConfig} from 'texdec' export class CatsConfig extends ControllerConfig { constructor() { super(); this.middleware([]).include('getMany') this.middleware([]).exclude('getOne') this.baseRoute() } }class methods
middlewareaccepts and array of function (expressjs middlewares) that will be used in all the controllers methods. you can excude specific methods from using the middleware with the chain methodexcudeor use the middleware in a specific set of methods with the chain methodinclude. Bothincludeandexcludeaccept either a string or an array of strings.
baseRouteif you want to overide the base route that you set ontexdecSettingsTExDec Options
via the
texDecSettingswe can configure some options.available options are:
castHelper webLogger routerLogger routeParamHelper baseRoute controllerDir
castHelper(Default:Class: CastHelper) is the class that contains the methods that casts the variables in the controllers. You can extend the default CastHelper class and write your own cast functions
webLogger(Default:console) the web logger
routerLogger(Default:console) the router logger
routeParamHelperYou can create custom controller method params with this option...
baseRoutebase route is the route all the controllers will be under