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

Commit e423d57

Browse files
committed
feature: add responseInterceptor (#61)
1 parent 9189edd commit e423d57

10 files changed

+70
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# "angular-in-memory-web-api" versions
22

3+
<a name="0.1.14"></a>
4+
## 0.1.14 (2016-10-29)
5+
* Add `responseInterceptor` for [issue #61](https://github.com/angular/in-memory-web-api/issues/61)
6+
37
<a name="0.1.13"></a>
48
## 0.1.13 (2016-10-20)
59
* Update README for 0.1.11 breaking change: npm publish as `esm` and a `umd` bundle

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ Request URLs for your api may not match the api imagined by the default `parseUr
118118
You can override the default by implementing a `parseUrl` method in your `InMemoryDbService`.
119119
Such a method must take the incoming request URL string and return a `ParsedUrl` object.
120120
121+
## _responseInterceptor_
122+
123+
You can morph the response returned by the default HTTP methods, called by `collectionHandler`,
124+
to suit your needs by adding a `responseInterceptor` method to your `InMemoryDbService` class.
125+
The `collectionHandler` calls your interceptor like this:
126+
```ts
127+
responseOptions = this.responseInterceptor(responseOptions, requestInfo);
128+
```
129+
121130
## HTTP method interceptors
122131
123132
If you make requests this service can't handle but still want an in-memory database to hold values,

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,14 @@ var InMemoryBackendConfig = (function () {
530530
*/
531531
function isSuccess(status) { return status >= 200 && status < 300; }
532532

533+
/**
534+
* The `responseInterceptor` can morph the response from `collectionHandler`
535+
* Default just returns the response.
536+
* Override with an `responseInterceptor` method in your `inMemDbService`
537+
*/
538+
function responseInterceptor(res, ri) {
539+
return res;
540+
}
533541
/**
534542
* Set the status text in a response:
535543
*/
@@ -584,6 +592,7 @@ var InMemoryBackendService = (function () {
584592
this.config.host = loc.host;
585593
this.config.rootPath = loc.pathname;
586594
Object.assign(this.config, config || {});
595+
this.responseInterceptor = inMemDbService["responseInterceptor"] || responseInterceptor;
587596
this.setPassThruBackend();
588597
}
589598
InMemoryBackendService.prototype.createConnection = function (req) {
@@ -731,6 +740,7 @@ var InMemoryBackendService = (function () {
731740
resOptions = createErrorResponse(STATUS.METHOD_NOT_ALLOWED, 'Method not allowed');
732741
break;
733742
}
743+
resOptions = this.responseInterceptor(resOptions, reqInfo);
734744
return this.createDelayedObservableResponse(resOptions);
735745
};
736746
/**
@@ -1003,6 +1013,7 @@ exports.createObservableResponse = createObservableResponse;
10031013
exports.InMemoryDbService = InMemoryDbService;
10041014
exports.InMemoryBackendConfig = InMemoryBackendConfig;
10051015
exports.isSuccess = isSuccess;
1016+
exports.responseInterceptor = responseInterceptor;
10061017
exports.setStatusText = setStatusText;
10071018
exports.InMemoryBackendService = InMemoryBackendService;
10081019
exports.inMemoryBackendServiceFactory = inMemoryBackendServiceFactory;

examples/hero-data.service.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
*/
77
import {
88
InMemoryDbService,
9-
createErrorResponse, createObservableResponse, HttpMethodInterceptorArgs, ParsedUrl, STATUS
9+
createErrorResponse, createObservableResponse, HttpMethodInterceptorArgs,
10+
ParsedUrl, RequestInfo, STATUS
1011
} from 'angular-in-memory-web-api';
1112

12-
import { ResponseOptions, URLSearchParams } from '@angular/http';
13+
import { RequestMethod, ResponseOptions, URLSearchParams } from '@angular/http';
1314

1415
// For AoT compile
1516
/* tslint:disable:no-unused-variable */
@@ -61,6 +62,14 @@ export class HeroDataOverrideService extends HeroDataService {
6162
}
6263
}
6364

65+
// intercept response from the default HTTP method handlers
66+
responseInterceptor(response: ResponseOptions, reqInfo: RequestInfo) {
67+
const method = RequestMethod[reqInfo.req.method].toUpperCase();
68+
const body = JSON.stringify(response.body);
69+
console.log(`responseInterceptor: ${method} ${reqInfo.req.url}: \n${body}`);
70+
return response;
71+
}
72+
6473
// HTTP GET interceptor
6574
protected get(interceptorArgs: HttpMethodInterceptorArgs) {
6675

in-memory-backend.service.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ export interface RequestInfo {
104104
query: URLSearchParams;
105105
resourceUrl: string;
106106
}
107+
/**
108+
* The `responseInterceptor` can morph the response from `collectionHandler`
109+
* Default just returns the response.
110+
* Override with an `responseInterceptor` method in your `inMemDbService`
111+
*/
112+
export declare function responseInterceptor(res: ResponseOptions, ri: RequestInfo): ResponseOptions;
107113
/**
108114
* Set the status text in a response:
109115
*/
@@ -158,6 +164,7 @@ export declare class InMemoryBackendService {
158164
protected passThruBackend: ConnectionBackend;
159165
protected config: InMemoryBackendConfigArgs;
160166
protected db: Object;
167+
private responseInterceptor;
161168
constructor(injector: Injector, inMemDbService: InMemoryDbService, config: InMemoryBackendConfigArgs);
162169
createConnection(req: Request): Connection;
163170
/**

in-memory-backend.service.js

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

in-memory-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.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"__symbolic":"module","version":1,"metadata":{"createErrorResponse":{"__symbolic":"function","parameters":["status","message"],"value":{"__symbolic":"error","message":"Expression form not supported","line":20,"character":21}},"isSuccess":{"__symbolic":"function","parameters":["status"],"value":{"__symbolic":"binop","operator":"&&","left":{"__symbolic":"binop","operator":">=","left":{"__symbolic":"reference","name":"status"},"right":200},"right":{"__symbolic":"binop","operator":"<","left":{"__symbolic":"reference","name":"status"},"right":300}}}}}
1+
{"__symbolic":"module","version":1,"metadata":{"createErrorResponse":{"__symbolic":"function","parameters":["status","message"],"value":{"__symbolic":"error","message":"Expression form not supported","line":20,"character":21}},"isSuccess":{"__symbolic":"function","parameters":["status"],"value":{"__symbolic":"binop","operator":"&&","left":{"__symbolic":"binop","operator":">=","left":{"__symbolic":"reference","name":"status"},"right":200},"right":{"__symbolic":"binop","operator":"<","left":{"__symbolic":"reference","name":"status"},"right":300}}},"responseInterceptor":{"__symbolic":"function","parameters":["res","ri"],"value":{"__symbolic":"reference","name":"res"}}}}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-in-memory-web-api",
3-
"version": "0.1.13",
3+
"version": "0.1.14",
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",
@@ -36,7 +36,7 @@
3636
"@angular/http": "^2.0.0",
3737
"reflect-metadata": "~0.1.8",
3838
"rxjs": "5.0.0-beta.12",
39-
"zone.js": "^0.6.25"
39+
"zone.js": "^0.6.26"
4040
},
4141
"devDependencies": {
4242
"@angular/common": "~2.1.0",
@@ -49,7 +49,7 @@
4949
"@types/core-js": "^0.9.34",
5050
"@types/jasmine": "^2.5.36",
5151
"@types/node": "^6.0.46",
52-
"angular-in-memory-web-api": "~0.1.12",
52+
"angular-in-memory-web-api": "~0.1.13",
5353
"canonical-path": "0.0.2",
5454
"concurrently": "^3.0.0",
5555
"core-js": "^2.4.1",

src/in-memory-backend.service.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ export interface RequestInfo {
153153
resourceUrl: string;
154154
}
155155

156+
/**
157+
* The `responseInterceptor` can morph the response from `collectionHandler`
158+
* Default just returns the response.
159+
* Override with an `responseInterceptor` method in your `inMemDbService`
160+
*/
161+
export function responseInterceptor(res: ResponseOptions, ri: RequestInfo): ResponseOptions {
162+
return res;
163+
}
164+
156165
/**
157166
* Set the status text in a response:
158167
*/
@@ -219,6 +228,7 @@ export class InMemoryBackendService {
219228
protected passThruBackend: ConnectionBackend;
220229
protected config: InMemoryBackendConfigArgs = new InMemoryBackendConfig();
221230
protected db: Object;
231+
private responseInterceptor: typeof responseInterceptor;
222232

223233
constructor(
224234
private injector: Injector,
@@ -232,6 +242,7 @@ export class InMemoryBackendService {
232242
this.config.rootPath = loc.pathname;
233243
Object.assign(this.config, config || {});
234244

245+
this.responseInterceptor = inMemDbService[`responseInterceptor`] || responseInterceptor;
235246
this.setPassThruBackend();
236247
}
237248

@@ -390,6 +401,8 @@ export class InMemoryBackendService {
390401
resOptions = createErrorResponse(STATUS.METHOD_NOT_ALLOWED, 'Method not allowed');
391402
break;
392403
}
404+
405+
resOptions = this.responseInterceptor(resOptions, reqInfo);
393406
return this.createDelayedObservableResponse(resOptions);
394407
}
395408

0 commit comments

Comments
 (0)