texdec
v0.0.18
Published
A typescript web framework that I actually like to use
Downloads
6
Maintainers
Readme
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/node
go to tsconfig.json and set
experimentalDecorators
andemitDecoratorMetadata
totrue
target
toes2019
Set 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
controllerDir
texDecSettings.set('controllerDir', path.join(__dirname, 'controllers'))
TExDec will search in the given path for files with name matching
*.controller
and 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.init
returns 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/cats
Example controller:
inside
controllers/cats
create the filescats.controller.ts
cats.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
baseRoute
the main route of the controllercontrollerConfig
the 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
route
the route of the methodvalidationObj
the 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 tofalse
the incoming variable will be cast to the type that is declared
Our Controller config
inside
controllers/cats
create the filecats.config.ts
import {ControllerConfig} from 'texdec' export class CatsConfig extends ControllerConfig { constructor() { super(); this.middleware([]).include('getMany') this.middleware([]).exclude('getOne') this.baseRoute() } }
class methods
middleware
accepts 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 methodexcude
or use the middleware in a specific set of methods with the chain methodinclude
. Bothinclude
andexclude
accept either a string or an array of strings.
baseRoute
if you want to overide the base route that you set ontexdecSettings
TExDec Options
via the
texDecSettings
we 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
routeParamHelper
You can create custom controller method params with this option...
baseRoute
base route is the route all the controllers will be under