Skip to content

Commit 94d4d0e

Browse files
committed
Implmenet class transformer
1 parent d1928ac commit 94d4d0e

File tree

8 files changed

+47
-38
lines changed

8 files changed

+47
-38
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ this.routeGeneratorService.navigateByRouteNames(
313313
```
314314

315315
## Class transfomer (model handling)
316+
We have included the [class transformer](https://github.com/typestack/class-transformer) which helps creating models (`src/app/models/*`). This transformation can be done
317+
in both direction (rest to model, model to rest).
316318

317319
## Dialog service
318320
There is a custom dialog implementation for simpler useage of elements in dialogs.

src/app/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import LanguageDetector from 'i18next-browser-languagedetector';
3434
/**
3535
* Polyfills
3636
*/
37+
import 'reflect-metadata';
3738
import 'utils/polyfills.utils';
3839

3940
// Fontawesome setup

src/app/models/user.model.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11

2+
import { Expose } from 'class-transformer';
3+
24
export class UserModel {
3-
constructor(
4-
public avatarUrl: string,
5-
public login: string,
6-
public htmlUrl: string
7-
) { }
5+
6+
@Expose({ name: 'avatar_url' })
7+
public avatarUrl: string = '';
8+
9+
public login: string = '';
10+
11+
@Expose({ name: 'html_url' })
12+
public htmlUrl: string = '';
813

914
public getLogin(): string {
1015
return this.login;

src/app/modules/users/users.vm.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { autoinject } from 'aurelia-framework';
2-
import { UserService } from './../../services/user.service';
2+
import { UserRestService } from './../../services/rest/user.service';
33
import { UserModel } from './../../models/user.model';
44

55
@autoinject
@@ -8,10 +8,10 @@ export class UsersViewModel {
88
public users: Array<UserModel> = [];
99

1010
constructor(
11-
private userService: UserService
11+
private userRestService: UserRestService
1212
) { }
1313

1414
public async activate(): Promise<void> {
15-
this.users = await this.userService.getUsers();
15+
this.users = await this.userRestService.getUsers();
1616
}
1717
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { autoinject } from 'aurelia-framework';
2+
import { plainToClass } from 'class-transformer';
3+
4+
import { RestClient } from './rest-client.service';
5+
import { UserModel } from './../../models/user.model';
6+
7+
export interface IUser {
8+
avatar_url: string;
9+
login: string;
10+
html_url: string;
11+
}
12+
13+
@autoinject
14+
export class UserRestService {
15+
16+
constructor(
17+
private restClient: RestClient,
18+
) {
19+
this.restClient
20+
.useAPI()
21+
.withResource('users');
22+
}
23+
24+
public async getUsers(): Promise<UserModel[]> {
25+
const users = await this.restClient.findAll<IUser[]>();
26+
27+
return plainToClass(UserModel, users);
28+
}
29+
}

src/app/services/user.service.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/config/development.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"NAME": "Development",
3-
"API_MAIN_URL": "https://api",
3+
"API_MAIN_URL": "https://api.github.com/",
44
"API_UAM_URL": "https://api",
55
"LOG_LEVEL": "debug"
66
}

src/config/production.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"NAME": "Production",
3-
"API_MAIN_URL": "https://api",
3+
"API_MAIN_URL": "https://api.github.com/",
44
"API_UAM_URL": "https://api",
55
"LOG_LEVEL": "error"
66
}

0 commit comments

Comments
 (0)