Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ because this is a development tool, not a production product.
We do try to tell you about such changes in this `CHANGELOG.md`
and we fix bugs as fast as we can.

<a id="0.5.2"></a>
## 0.5.3 (2018-01-06)
Can make use of `HttpParams` which yields a `request.urlWithParams`.
Added supporting `HeroService.searchHeroes(term: string)` and test.

<a id="0.5.2"></a>
## 0.5.2 (2017-12-10)
No longer modify the request data coming from client. Fixes #164
Expand Down
2 changes: 1 addition & 1 deletion backend.service.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend.service.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bundles/in-memory-web-api.umd.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export declare function removeTrailingSlash(path: string): string;
*/
export interface RequestCore {
url: string;
urlWithParams?: string;
}
/**
* Interface for object w/ info about the current request url
Expand Down
2 changes: 1 addition & 1 deletion interfaces.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-in-memory-web-api",
"version": "0.5.2",
"version": "0.5.3",
"description": "An in-memory web api for Angular demos and tests",
"main": "bundles/in-memory-web-api.umd.js",
"module": "index.js",
Expand Down
10 changes: 10 additions & 0 deletions src/app/hero.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ export class HeroServiceCoreSpec {
);
}));

it('can search for heroes by name containing "a"', async(() => {
heroService.searchHeroes('a')
.subscribe(
(heroes: Hero[]) => {
expect(heroes.length).toBe(3, 'should find 3 heroes with letter "a"');
},
failure
);
}));

it('can update existing hero', async(() => {
const id = 1;
heroService.getHero(id)
Expand Down
1 change: 1 addition & 0 deletions src/app/hero.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export abstract class HeroService {
abstract getHero(id: number): Observable<Hero>;
abstract addHero (name: string): Observable<Hero>;
abstract deleteHero (hero: Hero | number): Observable<Hero>;
abstract searchHeroes(term: string): Observable<Hero[]>;
abstract updateHero (hero: Hero): Observable<Hero>;
}
12 changes: 11 additions & 1 deletion src/app/http-client-hero.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable }from '@angular/core';
import { HttpClient, HttpHeaders }from '@angular/common/http';
import { HttpClient, HttpHeaders, HttpParams }from '@angular/common/http';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
Expand Down Expand Up @@ -56,6 +56,16 @@ export class HttpClientHeroService extends HeroService {
.catch(this.handleError);
}

searchHeroes(term: string): Observable<Hero[]> {
term = term.trim();
// add safe, encoded search parameter if term is present
const options = term ?
{ params: new HttpParams().set('name', term) } : {};

return this.http.get<Hero[]>(this.heroesUrl, options)
.catch(this.handleError);
}

updateHero (hero: Hero): Observable<null> {
return this.http.put(this.heroesUrl, hero, cudOptions)
.catch(this.handleError);
Expand Down
10 changes: 10 additions & 0 deletions src/app/http-hero.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ export class HttpHeroService extends HeroService {
.catch(this.handleError);
}


searchHeroes(term: string): Observable<Hero[]> {
term = term.trim();
// NB: not a safe encoded search parameter
const search = term ? '/?name=' + term : '';
return this.http.get(this.heroesUrl + search)
.map(res => res.json())
.catch(this.handleError);
}

updateHero (hero: Hero): Observable<Hero> {
return this.http.put(this.heroesUrl, hero, cudOptions)
.map(res => res.json())
Expand Down
2 changes: 1 addition & 1 deletion src/in-mem/backend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export abstract class BackendService {

protected handleRequest_(req: RequestCore): Observable<any> {

const url = req.url;
const url = req.urlWithParams ? req.urlWithParams : req.url;

// Try override parser
// If no override parser or it returns nothing, use default parser
Expand Down
3 changes: 2 additions & 1 deletion src/in-mem/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ export function removeTrailingSlash(path: string) {
* Minimum definition needed by base class
*/
export interface RequestCore {
url: string;
url: string; // request URL
urlWithParams?: string; // request URL with query parameters added by `HttpParams`
}

/**
Expand Down