Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit f7e216d

Browse files
authored
feature: support HttpParams and test with searchHeroes (#170)
1 parent cb4e06f commit f7e216d

13 files changed

+47
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ because this is a development tool, not a production product.
88
We do try to tell you about such changes in this `CHANGELOG.md`
99
and we fix bugs as fast as we can.
1010

11+
<a id="0.5.2"></a>
12+
## 0.5.3 (2018-01-06)
13+
Can make use of `HttpParams` which yields a `request.urlWithParams`.
14+
Added supporting `HeroService.searchHeroes(term: string)` and test.
15+
1116
<a id="0.5.2"></a>
1217
## 0.5.2 (2017-12-10)
1318
No longer modify the request data coming from client. Fixes #164

backend.service.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend.service.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundles/in-memory-web-api.umd.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interfaces.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export declare function removeTrailingSlash(path: string): string;
131131
*/
132132
export interface RequestCore {
133133
url: string;
134+
urlWithParams?: string;
134135
}
135136
/**
136137
* Interface for object w/ info about the current request url

interfaces.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-in-memory-web-api",
3-
"version": "0.5.2",
3+
"version": "0.5.3",
44
"description": "An in-memory web api for Angular demos and tests",
55
"main": "bundles/in-memory-web-api.umd.js",
66
"module": "index.js",

src/app/hero.service.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ export class HeroServiceCoreSpec {
9999
);
100100
}));
101101

102+
it('can search for heroes by name containing "a"', async(() => {
103+
heroService.searchHeroes('a')
104+
.subscribe(
105+
(heroes: Hero[]) => {
106+
expect(heroes.length).toBe(3, 'should find 3 heroes with letter "a"');
107+
},
108+
failure
109+
);
110+
}));
111+
102112
it('can update existing hero', async(() => {
103113
const id = 1;
104114
heroService.getHero(id)

src/app/hero.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export abstract class HeroService {
88
abstract getHero(id: number): Observable<Hero>;
99
abstract addHero (name: string): Observable<Hero>;
1010
abstract deleteHero (hero: Hero | number): Observable<Hero>;
11+
abstract searchHeroes(term: string): Observable<Hero[]>;
1112
abstract updateHero (hero: Hero): Observable<Hero>;
1213
}

src/app/http-client-hero.service.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable }from '@angular/core';
2-
import { HttpClient, HttpHeaders }from '@angular/common/http';
2+
import { HttpClient, HttpHeaders, HttpParams }from '@angular/common/http';
33

44
import { Observable } from 'rxjs/Observable';
55
import 'rxjs/add/observable/throw';
@@ -56,6 +56,16 @@ export class HttpClientHeroService extends HeroService {
5656
.catch(this.handleError);
5757
}
5858

59+
searchHeroes(term: string): Observable<Hero[]> {
60+
term = term.trim();
61+
// add safe, encoded search parameter if term is present
62+
const options = term ?
63+
{ params: new HttpParams().set('name', term) } : {};
64+
65+
return this.http.get<Hero[]>(this.heroesUrl, options)
66+
.catch(this.handleError);
67+
}
68+
5969
updateHero (hero: Hero): Observable<null> {
6070
return this.http.put(this.heroesUrl, hero, cudOptions)
6171
.catch(this.handleError);

0 commit comments

Comments
 (0)