Skip to content
This repository has been archived by the owner on Jul 12, 2019. It is now read-only.

Commit

Permalink
fix: serialization and deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorniv committed Aug 25, 2018
1 parent 78020d5 commit d5c7e92
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
23 changes: 11 additions & 12 deletions lib/ngx-transfer-http/src/transfer-http.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,9 @@ export class TransferHttpService {
}

const tempKey = url + (options ? JSON.stringify(options) : '');
const key = makeStateKey<string>(tempKey);

const key = makeStateKey<T>(tempKey);
try {
return this.resolveData(key);
return this.resolveData<T>(key);
} catch (e) {
return callback(method, uri, options)
.pipe(tap((data: T) => {
Expand All @@ -206,7 +205,7 @@ export class TransferHttpService {
// nothing;
}
if (isPlatformServer(this.platformId)) {
this.setCache(key, data);
this.setCache<T>(key, data);
}
}));
}
Expand All @@ -225,10 +224,10 @@ export class TransferHttpService {
}

const tempKey = url + (body ? JSON.stringify(body) : '') + (options ? JSON.stringify(options) : '');
const key = makeStateKey<string>(tempKey);
const key = makeStateKey<T>(tempKey);

try {
return this.resolveData(key);
return this.resolveData<T>(key);
} catch (e) {
return callback(uri, body, options)
.pipe(tap((data: T) => {
Expand All @@ -237,13 +236,13 @@ export class TransferHttpService {
// nothing;
}
if (isPlatformServer(this.platformId)) {
this.setCache(key, data);
this.setCache<T>(key, data);
}
}));
}
}

private resolveData<T>(key: StateKey<string>): Observable<T> {
private resolveData<T>(key: StateKey<T>): Observable<T> {
const data = this.getFromCache<T>(key);

if (!data) {
Expand All @@ -258,14 +257,14 @@ export class TransferHttpService {
// Server only code.
}

return from(Promise.resolve(data));
return from(Promise.resolve<T>(data));
}

private setCache<T>(key: StateKey<string>, data: T): void {
return this.transferState.set<string>(key, JSON.stringify(data));
private setCache<T>(key: StateKey<T>, data: T): void {
return this.transferState.set<T>(key, data);
}

private getFromCache<T>(key: StateKey<string>): T {
private getFromCache<T>(key: StateKey<T>): T {
return this.transferState.get<T>(key, null);
}
}
6 changes: 6 additions & 0 deletions src/app/transfer-back/Datum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Datum {
id: number;
first_name: string;
last_name: string;
avatar: string;
}
8 changes: 8 additions & 0 deletions src/app/transfer-back/RootObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Datum } from './Datum';
export interface RootObject {
page: number;
per_page: number;
total: number;
total_pages: number;
data: Datum[];
}
11 changes: 8 additions & 3 deletions src/app/transfer-back/transfer-back.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Component, OnInit, Inject } from '@angular/core';
import { TransferHttpService } from '@gorniv/ngx-transfer-http';

import { RootObject } from './RootObject';

@Component({
selector: 'app-transfer-back',
templateUrl: './transfer-back.component.html'
Expand All @@ -18,14 +20,17 @@ export class TransferBackComponent implements OnInit {
}

ngOnInit(): void {
this.http.get('https://reqres.in/api/users?delay=3').subscribe(result => {

this.http.get<RootObject>('https://reqres.in/api/users?delay=1').subscribe(result => {
console.log(result);
this.resultGet = result;
});

this.http.post('https://reqres.in/api/users', {
'name': 'morpheus',
'job': 'leader'
}).subscribe(result => {
console.log(result);
this.resultPost = result;
});

Expand All @@ -36,14 +41,14 @@ export class TransferBackComponent implements OnInit {
this.resultPut = result;
});

this.http.patch('https://reqres.in/api/users/2', {
this.http.patch('https://reqres.in/api/users/3', {
'name': 'morpheus',
'job': 'zion resident'
}).subscribe(result => {
this.resultPatch = result;
});

this.http.delete('https://reqres.in/api/users/2').subscribe(result => {
this.http.delete('https://reqres.in/api/users/4').subscribe(result => {
this.resultDelete = result;
});

Expand Down

0 comments on commit d5c7e92

Please sign in to comment.