Skip to content

Commit cc5f7c4

Browse files
committed
Merge branch 'feature/class-transformer' into develop
2 parents 4f790ee + 94d4d0e commit cc5f7c4

File tree

10 files changed

+58
-39
lines changed

10 files changed

+58
-39
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.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,13 @@
151151
"aurelia-validation": "^1.1.1",
152152
"bluebird": "^3.5.0",
153153
"bootstrap": "^4.0.0",
154+
"class-transformer": "^0.1.9",
154155
"i18next-browser-languagedetector": "^2.0.0",
155156
"isomorphic-fetch": "^2.2.1",
156157
"jquery": "^3.2.1",
157158
"moment": "^2.18.1",
158159
"normalize.css": "^8.0.0",
159-
"popper.js": "^1.12.9"
160+
"popper.js": "^1.12.9",
161+
"reflect-metadata": "^0.1.12"
160162
}
161163
}

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
}

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,10 @@ clap@^1.0.9:
17451745
dependencies:
17461746
chalk "^1.1.3"
17471747

1748+
class-transformer@^0.1.9:
1749+
version "0.1.9"
1750+
resolved "https://registry.yarnpkg.com/class-transformer/-/class-transformer-0.1.9.tgz#29977c528233ca014e6fd9523327ebd31d11ca54"
1751+
17481752
class-utils@^0.3.5:
17491753
version "0.3.6"
17501754
resolved "https://registry.yarnpkg.com/class-utils/-/class-utils-0.3.6.tgz#f93369ae8b9a7ce02fd41faad0ca83033190c463"
@@ -8188,6 +8192,10 @@ reduce-function-call@^1.0.1:
81888192
dependencies:
81898193
balanced-match "^0.4.2"
81908194

8195+
reflect-metadata@^0.1.12:
8196+
version "0.1.12"
8197+
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.12.tgz#311bf0c6b63cd782f228a81abe146a2bfa9c56f2"
8198+
81918199
regenerate@^1.2.1:
81928200
version "1.3.3"
81938201
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.3.tgz#0c336d3980553d755c39b586ae3b20aa49c82b7f"

0 commit comments

Comments
 (0)