Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
Closed
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,15 @@ The `InMemoryDbService` method name must be the same as the HTTP method name but
This service calls it with an `HttpMethodInterceptorArgs` object.
For example, your HTTP GET interceptor would be called like this:
e.g., `yourInMemDbService["get"](interceptorArgs)`.
Your method must **return an `Observable<Response>`** which _should be "cold"_.

Your method can have two possible returns:
* `null` - the service will continue on with its default processing (checking the collection or
falling back to a different backend). This is useful if you only wish to handle processing
for certain paths in the given HTTP method.
* `Observable<Response>` - your code has handled the request and the response is available from this
observable. It _should be "cold"_.

Your method must **return one of these two values**.

The `HttpMethodInterceptorArgs` (as of this writing) are:
```ts
Expand Down
16 changes: 12 additions & 4 deletions src/in-memory-backend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,10 @@ export class InMemoryBackendService {
if (/commands\/$/i.test(reqInfo.base)) {
return this.commands(reqInfo);

} else if (this.inMemDbService[reqMethodName]) {
}

// Don't do an else if to allow interceptor to fall back to default behavior below.
if (this.inMemDbService[reqMethodName]) {
// InMemoryDbService has an overriding interceptor for this HTTP method; call it
// The interceptor result must be an Observable<Response>
const interceptorArgs: HttpMethodInterceptorArgs = {
Expand All @@ -352,10 +355,15 @@ export class InMemoryBackendService {
config: this.config,
passThruBackend: this.passThruBackend
};
const interceptorResponse = this.inMemDbService[reqMethodName](interceptorArgs) as Observable<Response>;
return this.addDelay(interceptorResponse);

} else if (reqInfo.collection) {
const interceptorResponse = this.inMemDbService[reqMethodName](interceptorArgs);
if (interceptorResponse !== null) {
return this.addDelay(interceptorResponse as Observable<Response>);
}
}

// Standard behavior without interceptors
if (reqInfo.collection) {
// request is for a collection created by the InMemoryDbService
return this.addDelay(this.collectionHandler(reqInfo));

Expand Down