Typetron is a batteries-included framework, meaning that it provides tools to not only create backend app but also help you develop and link the backend with your frontend as easy as possible. In this section we will create a blank app that uses Typetron on the backend and Angular on the frontend.
These two apps will share source code between them, things like Models, used to typehint the backend responses and Forms, used to typehint and build forms with their validation.
In order to get started with Typetron and Angular make sure you have the necessary prerequisites on your machine:
Once you have all these installed, we can start creating our full-stack project. First, create a new Typetron project:
$ typetron new MyProject
$ cd MyProject
Once you’ve created that, create a new Angular project inside MyProject:
$ ng new frontend
We now have both Typetron and Angular app inside the MyProject directory. This is a typical file structure:
+-- MyProject
| +-- config
| +-- Controllers
| +-- Entities
| +-- Forms
| +-- frontend <-- This is the Angular app
| -- (Angular files and folders)
| +-- migrations
| +-- Models
| +-- node_modules
| +-- Providers
| +-- public
| +-- Services
| +-- test
The only thing we need to do is to link them. Why do we want to link them? Because we want to use Models and Forms from within the Typetron app inside the Angular app. To do so, we need to add a few packages to the Angular app:
import { AppConfig } from '@Typetron/Framework'
import { CorsMiddleware } from '@Typetron/Framework/Middleware'
export default new AppConfig({
// ...
middleware: [
CorsMiddleware
],
providers: [
// ...
]
})
{
"compilerOptions": {
"other configs": "...",
"baseUrl": "./",
"paths": {
"@Typetron/*": [
"../node_modules/@typetron/framework/*"
],
"@Data/Models/*": [
"../Models/*"
],
"@Data/Forms/*": [
"../Forms/*"
]
}
}
}
src/main.ts
add the reflect-metadata package. This package is used internally by Typetron to get
information from decorators in our Models and Forms:
import 'reflect-metadata'
import { enableProdMode } from '@angular/core'
// ...
From this point we can start using Models in our Angular app. This way we don’t have to write interfaces for responses we get from the backend side. For example, we can type-hint the http response:
import {Component, OnInit} from '@angular/core'
import {User} from '@Data/Models/User'
import {HttpClient} from '@angular/common/http'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
users: User[] = []
constructor(private http: HttpClient) {}
async ngOnInit(): Promise<void> {
this.users = await this.http.get<User[]>('http://localhost:8000/users').toPromise()
}
}
$ npm install @typetron/angular
You can use this package as follows:
import { Component } from '@angular/core'
import { RegisterForm } from '@Data/Forms/RegisterForm'
import { FormBuilder } from '@typetron/angular'
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.scss']
})
export class TypetronFormBuilderComponent {
registerForm = FormBuilder.build(RegisterForm)
}
With this configuration you are now ready to take advantage of the entire power of Typetron.
Check the tutorials and documentation pages for more insights on Typetron.