Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angular-in-memory-web-api

Package Overview
Dependencies
Maintainers
3
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-in-memory-web-api - npm Package Compare versions

Comparing version 0.4.1 to 0.4.2

40

backend.service.d.ts
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/delay';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { HeadersCore, RequestInfoUtilities, InMemoryDbService, InMemoryBackendConfigArgs, ParsedRequestUrl, PassThruBackend, RequestCore, RequestInfo, ResponseOptions, UriInfo } from './interfaces';

@@ -13,6 +13,8 @@ /**

protected inMemDbService: InMemoryDbService;
protected passThruBackend: PassThruBackend;
protected config: InMemoryBackendConfigArgs;
protected db: Object;
protected dbReadySubject: BehaviorSubject<boolean>;
private passThruBackend;
constructor(inMemDbService: InMemoryDbService, config?: InMemoryBackendConfigArgs);
protected readonly dbReady: Observable<boolean>;
/**

@@ -43,2 +45,3 @@ * Process Request and return an Observable of Http Response object

protected handleRequest(req: RequestCore): Observable<any>;
protected handleRequest_(req: RequestCore): Observable<any>;
/**

@@ -86,2 +89,6 @@ * Add configured delay to response observable unless delay === 0

/**
* create the function that passes unhandled requests through to the "real" backend.
*/
protected abstract createPassThruBackend(): PassThruBackend;
/**
* return a search map from a location query/search string

@@ -105,3 +112,3 @@ */

protected createResponseOptions$(resOptionsFactory: () => ResponseOptions): Observable<ResponseOptions>;
protected delete({id, collection, collectionName, headers, url}: RequestInfo): ResponseOptions;
protected delete(collection: any[], {id, collectionName, headers, url}: RequestInfo): ResponseOptions;
/**

@@ -132,3 +139,3 @@ * Find first instance of item in collection by `item.id`

}>(collection: T[]): any;
protected get({id, query, collection, collectionName, headers, url}: RequestInfo): ResponseOptions;
protected get(collection: any[], {id, query, collectionName, headers, url}: RequestInfo): ResponseOptions;
/** Get JSON body from the request object */

@@ -141,2 +148,7 @@ protected abstract getJsonBody(req: any): any;

/**
* get or create the function that passes unhandled requests
* through to the "real" backend.
*/
protected getPassThruBackend(): PassThruBackend;
/**
* return canonical HTTP method name (lowercase) from the request object

@@ -149,5 +161,4 @@ * e.g. (req.method || 'get').toLowerCase();

protected indexOf(collection: any[], id: number): number;
protected parseId(collection: {
id: any;
}[], id: string): any;
/** Parse the id as a number. Return original value if not a number. */
protected parseId(id: string): any;
/**

@@ -171,16 +182,11 @@ * Parses the request URL into a `ParsedRequestUrl` object.

protected parseRequestUrl(url: string): ParsedRequestUrl;
protected post({collection, headers, id, req, resourceUrl, url}: RequestInfo): ResponseOptions;
protected put({id, collection, collectionName, headers, req, url}: RequestInfo): ResponseOptions;
protected post(collection: any[], {id, collectionName, headers, req, resourceUrl, url}: RequestInfo): ResponseOptions;
protected put(collection: any[], {id, collectionName, headers, req, url}: RequestInfo): ResponseOptions;
protected removeById(collection: any[], id: number): boolean;
protected readonly requestInfoUtils: RequestInfoUtilities;
/**
* Reset the "database" to its original state
* Tell your in-mem "database" to reset.
* returns Observable of the database because resetting it could be async
*/
protected resetDb(reqInfo?: RequestInfo): void;
/**
* Sets the function that passes unhandled requests
* through to the "real" backend if
* config.passThruUnknownUrl is true.
*/
protected abstract setPassThruBackend(): void;
protected resetDb(reqInfo?: RequestInfo): Observable<boolean>;
}
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/delay';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { of } from 'rxjs/observable/of';
import { fromPromise } from 'rxjs/observable/fromPromise';
import { isPromise } from 'rxjs/util/isPromise';
import { delay } from 'rxjs/operator/delay';
import { filter } from 'rxjs/operator/filter';
import { first } from 'rxjs/operator/first';
import { switchMap } from 'rxjs/operator/switchMap';
import { getStatusText, isSuccess, STATUS } from './http-status-codes';

@@ -17,3 +24,2 @@ import { InMemoryBackendConfig, parseUri, removeTrailingSlash } from './interfaces';

this.config = new InMemoryBackendConfig();
this.resetDb();
var loc = this.getLocation('/');

@@ -24,3 +30,15 @@ this.config.host = loc.host; // default to app web server host

}
//// protected /////
Object.defineProperty(BackendService.prototype, "dbReady", {
//// protected /////
get: function () {
if (!this.dbReadySubject) {
// first time the service is called.
this.dbReadySubject = new BehaviorSubject(false);
this.resetDb();
}
return first.call(filter.call(this.dbReadySubject.asObservable(), function (r) { return r; }));
},
enumerable: true,
configurable: true
});
/**

@@ -52,2 +70,7 @@ * Process Request and return an Observable of Http Response object

var _this = this;
// handle the request when there is an in-memory database
return switchMap.call(this.dbReady, function () { return _this.handleRequest_(req); });
};
BackendService.prototype.handleRequest_ = function (req) {
var _this = this;
var url = req.url;

@@ -60,10 +83,8 @@ // Try override parser

var collectionName = parsed.collectionName;
var collection = this.db[collectionName];
var reqInfo = {
req: req,
apiBase: parsed.apiBase,
collection: collection,
collectionName: collectionName,
headers: this.createHeaders({ 'Content-Type': 'application/json' }),
id: this.parseId(collection, parsed.id),
id: this.parseId(parsed.id),
method: this.getRequestMethod(req),

@@ -81,3 +102,3 @@ query: parsed.query,

if (methodInterceptor) {
// Call the InMemoryDbService interceptor for this HTTP method.
// InMemoryDbService intercepts this HTTP method.
// if interceptor produced a response, return it.

@@ -91,18 +112,13 @@ // else InMemoryDbService chose not to intercept; continue processing.

}
if (reqInfo.collection) {
// request is for a collection created by the InMemoryDbService
if (this.db[collectionName]) {
// request is for a known collection of the InMemoryDbService
return this.createResponse$(function () { return _this.collectionHandler(reqInfo); });
}
else if (this.config.passThruUnknownUrl) {
// Passes request thru to a "real" backend.
if (!this.passThruBackend) {
this.setPassThruBackend();
}
return this.passThruBackend.handle(req);
if (this.config.passThruUnknownUrl) {
// unknown collection; pass request thru to a "real" backend.
return this.getPassThruBackend().handle(req);
}
else {
// can't handle this request
resOptions = this.createErrorResponseOptions(url, STATUS.NOT_FOUND, "Collection '" + collectionName + "' not found");
return this.createResponse$(function () { return resOptions; });
}
// 404 - can't handle this request
resOptions = this.createErrorResponseOptions(url, STATUS.NOT_FOUND, "Collection '" + collectionName + "' not found");
return this.createResponse$(function () { return resOptions; });
};

@@ -113,4 +129,4 @@ /**

BackendService.prototype.addDelay = function (response) {
var delay = this.config.delay;
return delay === 0 ? response : response.delay(delay || 500);
var d = this.config.delay;
return d === 0 ? response : delay.call(response, d || 500);
};

@@ -160,2 +176,3 @@ /**

BackendService.prototype.collectionHandler = function (reqInfo) {
var collection = this.db[reqInfo.collectionName];
// const req = reqInfo.req;

@@ -165,12 +182,12 @@ var resOptions;

case 'get':
resOptions = this.get(reqInfo);
resOptions = this.get(collection, reqInfo);
break;
case 'post':
resOptions = this.post(reqInfo);
resOptions = this.post(collection, reqInfo);
break;
case 'put':
resOptions = this.put(reqInfo);
resOptions = this.put(collection, reqInfo);
break;
case 'delete':
resOptions = this.delete(reqInfo);
resOptions = this.delete(collection, reqInfo);
break;

@@ -201,23 +218,22 @@ default:

BackendService.prototype.commands = function (reqInfo) {
var _this = this;
var command = reqInfo.collectionName.toLowerCase();
var method = reqInfo.method;
var resOptions;
var resOptions = {
status: STATUS.OK,
url: reqInfo.url
};
switch (command) {
case 'resetdb':
this.resetDb(reqInfo);
resOptions = { status: STATUS.OK };
break;
return switchMap.call(this.resetDb(reqInfo), function () { return _this.createResponse$(function () { return resOptions; }, false /* no delay */); });
case 'config':
if (method === 'get') {
resOptions = {
body: this.clone(this.config),
status: STATUS.OK
};
resOptions.body = this.clone(this.config);
}
else {
// any other HTTP method is assumed to be a config update
resOptions.status = STATUS.NO_CONTENT;
var body = this.getJsonBody(reqInfo.req);
Object.assign(this.config, body);
this.setPassThruBackend();
resOptions = { status: STATUS.NO_CONTENT };
this.passThruBackend = undefined; // re-create next time
}

@@ -228,3 +244,2 @@ break;

}
resOptions.url = reqInfo.url;
return this.createResponse$(function () { return resOptions; }, false /* no delay */);

@@ -273,4 +288,4 @@ };

};
BackendService.prototype.delete = function (_a) {
var id = _a.id, collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers, url = _a.url;
BackendService.prototype.delete = function (collection, _a) {
var id = _a.id, collectionName = _a.collectionName, headers = _a.headers, url = _a.url;
// tslint:disable-next-line:triple-equals

@@ -323,4 +338,4 @@ if (id == undefined) {

};
BackendService.prototype.get = function (_a) {
var id = _a.id, query = _a.query, collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers, url = _a.url;
BackendService.prototype.get = function (collection, _a) {
var id = _a.id, query = _a.query, collectionName = _a.collectionName, headers = _a.headers, url = _a.url;
var data = collection;

@@ -357,13 +372,18 @@ // tslint:disable-next-line:triple-equals

;
/**
* get or create the function that passes unhandled requests
* through to the "real" backend.
*/
BackendService.prototype.getPassThruBackend = function () {
return this.passThruBackend ?
this.passThruBackend :
this.passThruBackend = this.createPassThruBackend();
};
BackendService.prototype.indexOf = function (collection, id) {
return collection.findIndex(function (item) { return item.id === id; });
};
// tries to parse id as number if collection item.id is a number.
// returns the original param id otherwise.
BackendService.prototype.parseId = function (collection, id) {
if (collection && collection[0] && typeof collection[0].id === 'number') {
var idNum = parseFloat(id);
return isNaN(idNum) ? id : idNum;
}
return id;
/** Parse the id as a number. Return original value if not a number. */
BackendService.prototype.parseId = function (id) {
var idNum = parseFloat(id);
return isNaN(idNum) ? id : idNum;
};

@@ -435,4 +455,4 @@ /**

// Can update an existing entity too if post409 is false.
BackendService.prototype.post = function (_a) {
var collection = _a.collection, /* collectionName, */ headers = _a.headers, id = _a.id, req = _a.req, resourceUrl = _a.resourceUrl, url = _a.url;
BackendService.prototype.post = function (collection, _a) {
var id = _a.id, collectionName = _a.collectionName, headers = _a.headers, req = _a.req, resourceUrl = _a.resourceUrl, url = _a.url;
var item = this.getJsonBody(req);

@@ -457,3 +477,3 @@ // tslint:disable-next-line:triple-equals

else if (this.config.post409) {
return this.createErrorResponseOptions(url, STATUS.CONFLICT, "item with id='" + id + " exists and may not be updated with PUT; use POST instead.");
return this.createErrorResponseOptions(url, STATUS.CONFLICT, "'" + collectionName + "' item with id='" + id + " exists and may not be updated with POST; use PUT instead.");
}

@@ -469,4 +489,4 @@ else {

// Can create an entity too if put404 is false.
BackendService.prototype.put = function (_a) {
var id = _a.id, collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers, req = _a.req, url = _a.url;
BackendService.prototype.put = function (collection, _a) {
var id = _a.id, collectionName = _a.collectionName, headers = _a.headers, req = _a.req, url = _a.url;
var item = this.getJsonBody(req);

@@ -478,3 +498,3 @@ // tslint:disable-next-line:triple-equals

if (id && id !== item.id) {
return this.createErrorResponseOptions(url, STATUS.BAD_REQUEST, "Request id does not match item.id");
return this.createErrorResponseOptions(url, STATUS.BAD_REQUEST, "Request for '" + collectionName + "' id does not match item.id");
}

@@ -494,3 +514,3 @@ else {

// item to update not found; use POST to create new item for this id.
return this.createErrorResponseOptions(url, STATUS.NOT_FOUND, "id='" + id + " not found");
return this.createErrorResponseOptions(url, STATUS.NOT_FOUND, "'" + collectionName + "' item with id='" + id + " not found and may not be created with PUT; use POST instead.");
}

@@ -513,2 +533,3 @@ else {

get: function () {
var _this = this;
return {

@@ -518,6 +539,7 @@ config: this.config,

findById: this.findById.bind(this),
getDb: function () { return _this.db; },
getJsonBody: this.getJsonBody.bind(this),
getLocation: this.getLocation.bind(this),
getPassThruBackend: this.getPassThruBackend.bind(this),
parseRequestUrl: this.parseRequestUrl.bind(this),
passThruBackend: this.passThruBackend
};

@@ -529,6 +551,17 @@ },

/**
* Reset the "database" to its original state
* Tell your in-mem "database" to reset.
* returns Observable of the database because resetting it could be async
*/
BackendService.prototype.resetDb = function (reqInfo) {
this.db = this.inMemDbService.createDb(reqInfo);
var _this = this;
this.dbReadySubject.next(false);
var db = this.inMemDbService.createDb(reqInfo);
var db$ = db instanceof Observable ? db :
isPromise(db) ? fromPromise(db) :
of(db);
first.call(db$).subscribe(function (d) {
_this.db = d;
_this.dbReadySubject.next(true);
});
return this.dbReady;
};

@@ -535,0 +568,0 @@ return BackendService;

@@ -1,1 +0,1 @@

[{"__symbolic":"module","version":3,"metadata":{"BackendService":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"handleRequest":[{"__symbolic":"method"}],"addDelay":[{"__symbolic":"method"}],"applyQuery":[{"__symbolic":"method"}],"bind":[{"__symbolic":"method"}],"bodify":[{"__symbolic":"method"}],"clone":[{"__symbolic":"method"}],"collectionHandler":[{"__symbolic":"method"}],"commands":[{"__symbolic":"method"}],"createErrorResponseOptions":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createResponseOptions$":[{"__symbolic":"method"}],"delete":[{"__symbolic":"method"}],"findById":[{"__symbolic":"method"}],"genId":[{"__symbolic":"method"}],"genIdDefault":[{"__symbolic":"method"}],"get":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getLocation":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"indexOf":[{"__symbolic":"method"}],"parseId":[{"__symbolic":"method"}],"parseRequestUrl":[{"__symbolic":"method"}],"post":[{"__symbolic":"method"}],"put":[{"__symbolic":"method"}],"removeById":[{"__symbolic":"method"}],"resetDb":[{"__symbolic":"method"}],"setPassThruBackend":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"BackendService":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"handleRequest":[{"__symbolic":"method"}],"addDelay":[{"__symbolic":"method"}],"applyQuery":[{"__symbolic":"method"}],"bind":[{"__symbolic":"method"}],"bodify":[{"__symbolic":"method"}],"clone":[{"__symbolic":"method"}],"collectionHandler":[{"__symbolic":"method"}],"commands":[{"__symbolic":"method"}],"createErrorResponseOptions":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createResponseOptions$":[{"__symbolic":"method"}],"delete":[{"__symbolic":"method"}],"findById":[{"__symbolic":"method"}],"genId":[{"__symbolic":"method"}],"genIdDefault":[{"__symbolic":"method"}],"get":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getLocation":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"indexOf":[{"__symbolic":"method"}],"parseId":[{"__symbolic":"method"}],"parseRequestUrl":[{"__symbolic":"method"}],"post":[{"__symbolic":"method"}],"put":[{"__symbolic":"method"}],"removeById":[{"__symbolic":"method"}],"resetDb":[{"__symbolic":"method"}],"setPassThruBackend":[{"__symbolic":"method"}]}}}}]
[{"__symbolic":"module","version":3,"metadata":{"BackendService":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"handleRequest":[{"__symbolic":"method"}],"handleRequest_":[{"__symbolic":"method"}],"addDelay":[{"__symbolic":"method"}],"applyQuery":[{"__symbolic":"method"}],"bind":[{"__symbolic":"method"}],"bodify":[{"__symbolic":"method"}],"clone":[{"__symbolic":"method"}],"collectionHandler":[{"__symbolic":"method"}],"commands":[{"__symbolic":"method"}],"createErrorResponseOptions":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createPassThruBackend":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createResponseOptions$":[{"__symbolic":"method"}],"delete":[{"__symbolic":"method"}],"findById":[{"__symbolic":"method"}],"genId":[{"__symbolic":"method"}],"genIdDefault":[{"__symbolic":"method"}],"get":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getLocation":[{"__symbolic":"method"}],"getPassThruBackend":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"indexOf":[{"__symbolic":"method"}],"parseId":[{"__symbolic":"method"}],"parseRequestUrl":[{"__symbolic":"method"}],"post":[{"__symbolic":"method"}],"put":[{"__symbolic":"method"}],"removeById":[{"__symbolic":"method"}],"resetDb":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"BackendService":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"handleRequest":[{"__symbolic":"method"}],"handleRequest_":[{"__symbolic":"method"}],"addDelay":[{"__symbolic":"method"}],"applyQuery":[{"__symbolic":"method"}],"bind":[{"__symbolic":"method"}],"bodify":[{"__symbolic":"method"}],"clone":[{"__symbolic":"method"}],"collectionHandler":[{"__symbolic":"method"}],"commands":[{"__symbolic":"method"}],"createErrorResponseOptions":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createPassThruBackend":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createResponseOptions$":[{"__symbolic":"method"}],"delete":[{"__symbolic":"method"}],"findById":[{"__symbolic":"method"}],"genId":[{"__symbolic":"method"}],"genIdDefault":[{"__symbolic":"method"}],"get":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getLocation":[{"__symbolic":"method"}],"getPassThruBackend":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"indexOf":[{"__symbolic":"method"}],"parseId":[{"__symbolic":"method"}],"parseRequestUrl":[{"__symbolic":"method"}],"post":[{"__symbolic":"method"}],"put":[{"__symbolic":"method"}],"removeById":[{"__symbolic":"method"}],"resetDb":[{"__symbolic":"method"}]}}}}]
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rxjs/Observable'), require('rxjs/add/operator/delay'), require('@angular/core'), require('@angular/http'), require('rxjs/add/operator/map'), require('@angular/common/http')) :
typeof define === 'function' && define.amd ? define(['exports', 'rxjs/Observable', 'rxjs/add/operator/delay', '@angular/core', '@angular/http', 'rxjs/add/operator/map', '@angular/common/http'], factory) :
(factory((global.ng = global.ng || {}, global.ng.inMemoryWebApi = {}),global.Rx,global.Rx,global.ng.core,global.ng.http,null,global._angular_common_http));
}(this, (function (exports,rxjs_Observable,rxjs_add_operator_delay,_angular_core,_angular_http,rxjs_add_operator_map,_angular_common_http) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rxjs/Observable'), require('rxjs/BehaviorSubject'), require('rxjs/observable/of'), require('rxjs/observable/fromPromise'), require('rxjs/util/isPromise'), require('rxjs/operator/delay'), require('rxjs/operator/filter'), require('rxjs/operator/first'), require('rxjs/operator/switchMap'), require('@angular/core'), require('@angular/http'), require('rxjs/operator/map'), require('@angular/common/http')) :
typeof define === 'function' && define.amd ? define(['exports', 'rxjs/Observable', 'rxjs/BehaviorSubject', 'rxjs/observable/of', 'rxjs/observable/fromPromise', 'rxjs/util/isPromise', 'rxjs/operator/delay', 'rxjs/operator/filter', 'rxjs/operator/first', 'rxjs/operator/switchMap', '@angular/core', '@angular/http', 'rxjs/operator/map', '@angular/common/http'], factory) :
(factory((global.ng = global.ng || {}, global.ng.inMemoryWebApi = {}),global.Rx,global.rxjs_BehaviorSubject,global.rxjs_observable_of,global.rxjs_observable_fromPromise,global.rxjs_util_isPromise,global.rxjs_operator_delay,global.rxjs_operator_filter,global.rxjs_operator_first,global.rxjs_operator_switchMap,global.ng.core,global.ng.http,global.rxjs_operator_map,global._angular_common_http));
}(this, (function (exports,rxjs_Observable,rxjs_BehaviorSubject,rxjs_observable_of,rxjs_observable_fromPromise,rxjs_util_isPromise,rxjs_operator_delay,rxjs_operator_filter,rxjs_operator_first,rxjs_operator_switchMap,_angular_core,_angular_http,rxjs_operator_map,_angular_common_http) { 'use strict';

@@ -576,3 +576,2 @@ var STATUS = {

this.config = new InMemoryBackendConfig();
this.resetDb();
var loc = this.getLocation('/');

@@ -583,3 +582,15 @@ this.config.host = loc.host; // default to app web server host

}
//// protected /////
Object.defineProperty(BackendService.prototype, "dbReady", {
//// protected /////
get: function () {
if (!this.dbReadySubject) {
// first time the service is called.
this.dbReadySubject = new rxjs_BehaviorSubject.BehaviorSubject(false);
this.resetDb();
}
return rxjs_operator_first.first.call(rxjs_operator_filter.filter.call(this.dbReadySubject.asObservable(), function (r) { return r; }));
},
enumerable: true,
configurable: true
});
/**

@@ -611,2 +622,7 @@ * Process Request and return an Observable of Http Response object

var _this = this;
// handle the request when there is an in-memory database
return rxjs_operator_switchMap.switchMap.call(this.dbReady, function () { return _this.handleRequest_(req); });
};
BackendService.prototype.handleRequest_ = function (req) {
var _this = this;
var url = req.url;

@@ -619,10 +635,8 @@ // Try override parser

var collectionName = parsed.collectionName;
var collection = this.db[collectionName];
var reqInfo = {
req: req,
apiBase: parsed.apiBase,
collection: collection,
collectionName: collectionName,
headers: this.createHeaders({ 'Content-Type': 'application/json' }),
id: this.parseId(collection, parsed.id),
id: this.parseId(parsed.id),
method: this.getRequestMethod(req),

@@ -640,3 +654,3 @@ query: parsed.query,

if (methodInterceptor) {
// Call the InMemoryDbService interceptor for this HTTP method.
// InMemoryDbService intercepts this HTTP method.
// if interceptor produced a response, return it.

@@ -650,18 +664,13 @@ // else InMemoryDbService chose not to intercept; continue processing.

}
if (reqInfo.collection) {
// request is for a collection created by the InMemoryDbService
if (this.db[collectionName]) {
// request is for a known collection of the InMemoryDbService
return this.createResponse$(function () { return _this.collectionHandler(reqInfo); });
}
else if (this.config.passThruUnknownUrl) {
// Passes request thru to a "real" backend.
if (!this.passThruBackend) {
this.setPassThruBackend();
}
return this.passThruBackend.handle(req);
if (this.config.passThruUnknownUrl) {
// unknown collection; pass request thru to a "real" backend.
return this.getPassThruBackend().handle(req);
}
else {
// can't handle this request
resOptions = this.createErrorResponseOptions(url, STATUS.NOT_FOUND, "Collection '" + collectionName + "' not found");
return this.createResponse$(function () { return resOptions; });
}
// 404 - can't handle this request
resOptions = this.createErrorResponseOptions(url, STATUS.NOT_FOUND, "Collection '" + collectionName + "' not found");
return this.createResponse$(function () { return resOptions; });
};

@@ -672,4 +681,4 @@ /**

BackendService.prototype.addDelay = function (response) {
var delay = this.config.delay;
return delay === 0 ? response : response.delay(delay || 500);
var d = this.config.delay;
return d === 0 ? response : rxjs_operator_delay.delay.call(response, d || 500);
};

@@ -719,2 +728,3 @@ /**

BackendService.prototype.collectionHandler = function (reqInfo) {
var collection = this.db[reqInfo.collectionName];
// const req = reqInfo.req;

@@ -724,12 +734,12 @@ var resOptions;

case 'get':
resOptions = this.get(reqInfo);
resOptions = this.get(collection, reqInfo);
break;
case 'post':
resOptions = this.post(reqInfo);
resOptions = this.post(collection, reqInfo);
break;
case 'put':
resOptions = this.put(reqInfo);
resOptions = this.put(collection, reqInfo);
break;
case 'delete':
resOptions = this.delete(reqInfo);
resOptions = this.delete(collection, reqInfo);
break;

@@ -760,23 +770,22 @@ default:

BackendService.prototype.commands = function (reqInfo) {
var _this = this;
var command = reqInfo.collectionName.toLowerCase();
var method = reqInfo.method;
var resOptions;
var resOptions = {
status: STATUS.OK,
url: reqInfo.url
};
switch (command) {
case 'resetdb':
this.resetDb(reqInfo);
resOptions = { status: STATUS.OK };
break;
return rxjs_operator_switchMap.switchMap.call(this.resetDb(reqInfo), function () { return _this.createResponse$(function () { return resOptions; }, false /* no delay */); });
case 'config':
if (method === 'get') {
resOptions = {
body: this.clone(this.config),
status: STATUS.OK
};
resOptions.body = this.clone(this.config);
}
else {
// any other HTTP method is assumed to be a config update
resOptions.status = STATUS.NO_CONTENT;
var body = this.getJsonBody(reqInfo.req);
Object.assign(this.config, body);
this.setPassThruBackend();
resOptions = { status: STATUS.NO_CONTENT };
this.passThruBackend = undefined; // re-create next time
}

@@ -787,3 +796,2 @@ break;

}
resOptions.url = reqInfo.url;
return this.createResponse$(function () { return resOptions; }, false /* no delay */);

@@ -832,4 +840,4 @@ };

};
BackendService.prototype.delete = function (_a) {
var id = _a.id, collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers, url = _a.url;
BackendService.prototype.delete = function (collection, _a) {
var id = _a.id, collectionName = _a.collectionName, headers = _a.headers, url = _a.url;
// tslint:disable-next-line:triple-equals

@@ -882,4 +890,4 @@ if (id == undefined) {

};
BackendService.prototype.get = function (_a) {
var id = _a.id, query = _a.query, collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers, url = _a.url;
BackendService.prototype.get = function (collection, _a) {
var id = _a.id, query = _a.query, collectionName = _a.collectionName, headers = _a.headers, url = _a.url;
var data = collection;

@@ -916,13 +924,18 @@ // tslint:disable-next-line:triple-equals

/**
* get or create the function that passes unhandled requests
* through to the "real" backend.
*/
BackendService.prototype.getPassThruBackend = function () {
return this.passThruBackend ?
this.passThruBackend :
this.passThruBackend = this.createPassThruBackend();
};
BackendService.prototype.indexOf = function (collection, id) {
return collection.findIndex(function (item) { return item.id === id; });
};
// tries to parse id as number if collection item.id is a number.
// returns the original param id otherwise.
BackendService.prototype.parseId = function (collection, id) {
if (collection && collection[0] && typeof collection[0].id === 'number') {
var idNum = parseFloat(id);
return isNaN(idNum) ? id : idNum;
}
return id;
/** Parse the id as a number. Return original value if not a number. */
BackendService.prototype.parseId = function (id) {
var idNum = parseFloat(id);
return isNaN(idNum) ? id : idNum;
};

@@ -994,4 +1007,4 @@ /**

// Can update an existing entity too if post409 is false.
BackendService.prototype.post = function (_a) {
var collection = _a.collection, /* collectionName, */ headers = _a.headers, id = _a.id, req = _a.req, resourceUrl = _a.resourceUrl, url = _a.url;
BackendService.prototype.post = function (collection, _a) {
var id = _a.id, collectionName = _a.collectionName, headers = _a.headers, req = _a.req, resourceUrl = _a.resourceUrl, url = _a.url;
var item = this.getJsonBody(req);

@@ -1016,3 +1029,3 @@ // tslint:disable-next-line:triple-equals

else if (this.config.post409) {
return this.createErrorResponseOptions(url, STATUS.CONFLICT, "item with id='" + id + " exists and may not be updated with PUT; use POST instead.");
return this.createErrorResponseOptions(url, STATUS.CONFLICT, "'" + collectionName + "' item with id='" + id + " exists and may not be updated with POST; use PUT instead.");
}

@@ -1028,4 +1041,4 @@ else {

// Can create an entity too if put404 is false.
BackendService.prototype.put = function (_a) {
var id = _a.id, collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers, req = _a.req, url = _a.url;
BackendService.prototype.put = function (collection, _a) {
var id = _a.id, collectionName = _a.collectionName, headers = _a.headers, req = _a.req, url = _a.url;
var item = this.getJsonBody(req);

@@ -1037,3 +1050,3 @@ // tslint:disable-next-line:triple-equals

if (id && id !== item.id) {
return this.createErrorResponseOptions(url, STATUS.BAD_REQUEST, "Request id does not match item.id");
return this.createErrorResponseOptions(url, STATUS.BAD_REQUEST, "Request for '" + collectionName + "' id does not match item.id");
}

@@ -1053,3 +1066,3 @@ else {

// item to update not found; use POST to create new item for this id.
return this.createErrorResponseOptions(url, STATUS.NOT_FOUND, "id='" + id + " not found");
return this.createErrorResponseOptions(url, STATUS.NOT_FOUND, "'" + collectionName + "' item with id='" + id + " not found and may not be created with PUT; use POST instead.");
}

@@ -1072,2 +1085,3 @@ else {

get: function () {
var _this = this;
return {

@@ -1077,6 +1091,7 @@ config: this.config,

findById: this.findById.bind(this),
getDb: function () { return _this.db; },
getJsonBody: this.getJsonBody.bind(this),
getLocation: this.getLocation.bind(this),
getPassThruBackend: this.getPassThruBackend.bind(this),
parseRequestUrl: this.parseRequestUrl.bind(this),
passThruBackend: this.passThruBackend
};

@@ -1088,6 +1103,17 @@ },

/**
* Reset the "database" to its original state
* Tell your in-mem "database" to reset.
* returns Observable of the database because resetting it could be async
*/
BackendService.prototype.resetDb = function (reqInfo) {
this.db = this.inMemDbService.createDb(reqInfo);
var _this = this;
this.dbReadySubject.next(false);
var db = this.inMemDbService.createDb(reqInfo);
var db$ = db instanceof rxjs_Observable.Observable ? db :
rxjs_util_isPromise.isPromise(db) ? rxjs_observable_fromPromise.fromPromise(db) :
rxjs_observable_of.of(db);
rxjs_operator_first.first.call(db$).subscribe(function (d) {
_this.db = d;
_this.dbReadySubject.next(true);
});
return this.dbReady;
};

@@ -1177,8 +1203,7 @@ return BackendService;

HttpBackendService.prototype.createResponse$fromResponseOptions$ = function (resOptions$) {
return resOptions$.map(function (opts) {
var options = opts;
return new _angular_http.Response(new _angular_http.ResponseOptions(options));
return rxjs_operator_map.map.call(resOptions$, function (opts) {
return new _angular_http.Response(new _angular_http.ResponseOptions(opts));
});
};
HttpBackendService.prototype.setPassThruBackend = function () {
HttpBackendService.prototype.createPassThruBackend = function () {
try {

@@ -1190,3 +1215,3 @@ // copied from @angular/http/backends/xhr_backend

var xhrBackend_1 = new _angular_http.XHRBackend(browserXhr, baseResponseOptions, xsrfStrategy);
this.passThruBackend = {
return {
handle: function (req) { return xhrBackend_1.createConnection(req).response; }

@@ -1277,15 +1302,15 @@ };

HttpClientBackendService.prototype.createQueryMap = function (search) {
var map = new Map();
var map$$1 = new Map();
if (search) {
var params_1 = new _angular_common_http.HttpParams({ fromString: search });
params_1.keys().forEach(function (p) { return map.set(p, params_1.getAll(p)); });
params_1.keys().forEach(function (p) { return map$$1.set(p, params_1.getAll(p)); });
}
return map;
return map$$1;
};
HttpClientBackendService.prototype.createResponse$fromResponseOptions$ = function (resOptions$) {
return resOptions$.map(function (opts) { return new _angular_common_http.HttpResponse(opts); });
return rxjs_operator_map.map.call(resOptions$, function (opts) { return new _angular_common_http.HttpResponse(opts); });
};
HttpClientBackendService.prototype.setPassThruBackend = function () {
HttpClientBackendService.prototype.createPassThruBackend = function () {
try {
this.passThruBackend = new _angular_common_http.HttpXhrBackend(this.xhrFactory);
return new _angular_common_http.HttpXhrBackend(this.xhrFactory);
}

@@ -1292,0 +1317,0 @@ catch (ex) {

@@ -9,5 +9,13 @@ # "angular-in-memory-web-api" versions

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.4.2"></a>
## 0.4.2 (2017-09-08)
- Postpones the in-memory database initialization (via `resetDb`) until the first HTTP request.
- Your `createDb` method _can_ be asynchronous.
You may return the database object (synchronous), an observable of it, or a promise of it. Issue #113.
- fixed some rare race conditions.
<a id="0.4.1"></a>

@@ -14,0 +22,0 @@ ## 0.4.1 (2017-09-08)

import { Injector } from '@angular/core';
import { Connection, ConnectionBackend, Headers, Request, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import { InMemoryBackendConfigArgs, InMemoryDbService, ResponseOptions } from './interfaces';

@@ -45,3 +44,5 @@ import { BackendService } from './backend.service';

protected createResponse$fromResponseOptions$(resOptions$: Observable<ResponseOptions>): Observable<Response>;
protected setPassThruBackend(): void;
protected createPassThruBackend(): {
handle: (req: Request) => Observable<Response>;
};
}

@@ -13,3 +13,3 @@ var __extends = (this && this.__extends) || (function () {

import { BrowserXhr, Headers, ReadyState, RequestMethod, Response, ResponseOptions as HttpResponseOptions, URLSearchParams, XHRBackend, XSRFStrategy } from '@angular/http';
import 'rxjs/add/operator/map';
import { map } from 'rxjs/operator/map';
import { STATUS } from './http-status-codes';

@@ -88,8 +88,7 @@ import { InMemoryBackendConfig, InMemoryBackendConfigArgs, InMemoryDbService } from './interfaces';

HttpBackendService.prototype.createResponse$fromResponseOptions$ = function (resOptions$) {
return resOptions$.map(function (opts) {
var options = opts;
return new Response(new HttpResponseOptions(options));
return map.call(resOptions$, function (opts) {
return new Response(new HttpResponseOptions(opts));
});
};
HttpBackendService.prototype.setPassThruBackend = function () {
HttpBackendService.prototype.createPassThruBackend = function () {
try {

@@ -101,3 +100,3 @@ // copied from @angular/http/backends/xhr_backend

var xhrBackend_1 = new XHRBackend(browserXhr, baseResponseOptions, xsrfStrategy);
this.passThruBackend = {
return {
handle: function (req) { return xhrBackend_1.createConnection(req).response; }

@@ -104,0 +103,0 @@ };

@@ -1,1 +0,1 @@

[{"__symbolic":"module","version":3,"metadata":{"HttpBackendService":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"./backend.service","name":"BackendService"},"decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[null,null,[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfig"}]},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}}]],"parameters":[{"__symbolic":"reference","module":"@angular/core","name":"Injector"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"createConnection":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"setPassThruBackend":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"HttpBackendService":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"./backend.service","name":"BackendService"},"decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[null,null,[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfig"}]},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}}]],"parameters":[{"__symbolic":"reference","module":"@angular/core","name":"Injector"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"createConnection":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"setPassThruBackend":[{"__symbolic":"method"}]}}}}]
[{"__symbolic":"module","version":3,"metadata":{"HttpBackendService":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"./backend.service","name":"BackendService"},"decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[null,null,[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfig"}]},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}}]],"parameters":[{"__symbolic":"reference","module":"@angular/core","name":"Injector"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"createConnection":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createPassThruBackend":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"HttpBackendService":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"./backend.service","name":"BackendService"},"decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[null,null,[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfig"}]},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}}]],"parameters":[{"__symbolic":"reference","module":"@angular/core","name":"Injector"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"createConnection":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createPassThruBackend":[{"__symbolic":"method"}]}}}}]

@@ -1,4 +0,3 @@

import { HttpBackend, HttpHeaders, HttpRequest, HttpResponse, XhrFactory } from '@angular/common/http';
import { HttpBackend, HttpHeaders, HttpRequest, HttpResponse, HttpXhrBackend, XhrFactory } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import { InMemoryBackendConfigArgs, InMemoryDbService, ResponseOptions } from './interfaces';

@@ -44,3 +43,3 @@ import { BackendService } from './backend.service';

protected createResponse$fromResponseOptions$(resOptions$: Observable<ResponseOptions>): Observable<HttpResponse<any>>;
protected setPassThruBackend(): void;
protected createPassThruBackend(): HttpXhrBackend;
}

@@ -13,3 +13,3 @@ var __extends = (this && this.__extends) || (function () {

import { HttpHeaders, HttpParams, HttpResponse, HttpXhrBackend, XhrFactory } from '@angular/common/http';
import 'rxjs/add/operator/map';
import { map } from 'rxjs/operator/map';
import { STATUS } from './http-status-codes';

@@ -81,7 +81,7 @@ import { InMemoryBackendConfig, InMemoryBackendConfigArgs, InMemoryDbService } from './interfaces';

HttpClientBackendService.prototype.createResponse$fromResponseOptions$ = function (resOptions$) {
return resOptions$.map(function (opts) { return new HttpResponse(opts); });
return map.call(resOptions$, function (opts) { return new HttpResponse(opts); });
};
HttpClientBackendService.prototype.setPassThruBackend = function () {
HttpClientBackendService.prototype.createPassThruBackend = function () {
try {
this.passThruBackend = new HttpXhrBackend(this.xhrFactory);
return new HttpXhrBackend(this.xhrFactory);
}

@@ -88,0 +88,0 @@ catch (ex) {

@@ -1,1 +0,1 @@

[{"__symbolic":"module","version":3,"metadata":{"HttpClientBackendService":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"./backend.service","name":"BackendService"},"decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[null,[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfig"}]},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}}],null],"parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"},{"__symbolic":"reference","module":"@angular/common/http","name":"XhrFactory"}]}],"handle":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"setPassThruBackend":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"HttpClientBackendService":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"./backend.service","name":"BackendService"},"decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[null,[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfig"}]},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}}],null],"parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"},{"__symbolic":"reference","module":"@angular/common/http","name":"XhrFactory"}]}],"handle":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"setPassThruBackend":[{"__symbolic":"method"}]}}}}]
[{"__symbolic":"module","version":3,"metadata":{"HttpClientBackendService":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"./backend.service","name":"BackendService"},"decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[null,[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfig"}]},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}}],null],"parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"},{"__symbolic":"reference","module":"@angular/common/http","name":"XhrFactory"}]}],"handle":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createPassThruBackend":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"HttpClientBackendService":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"./backend.service","name":"BackendService"},"decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[null,[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfig"}]},{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Optional"}}],null],"parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"},{"__symbolic":"reference","module":"@angular/common/http","name":"XhrFactory"}]}],"handle":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createPassThruBackend":[{"__symbolic":"method"}]}}}}]

@@ -23,2 +23,4 @@ import { Observable } from 'rxjs/Observable';

*
* returns Observable of the database because could have to create it asynchronously.
*
* This method must be safe to call repeatedly.

@@ -29,7 +31,7 @@ * Each time it should return a new object with new arrays containing new item objects.

*
* When constructed the in-mem backend service calls this method without a value.
* The service calls it with the RequestInfo when it receives a POST `commands/resetDb` request.
* The in-mem backend service calls this method without a value the first time.
* The service calls it with the `RequestInfo` when it receives a POST `commands/resetDb` request.
* Your InMemoryDbService can adjust its behavior accordingly.
*/
abstract createDb(reqInfo?: RequestInfo): {};
abstract createDb(reqInfo?: RequestInfo): {} | Observable<{}> | Promise<{}>;
}

@@ -159,3 +161,2 @@ /**

apiBase: string;
collection: any[];
collectionName: string;

@@ -184,2 +185,4 @@ headers: HeadersCore;

createResponse$: (resOptionsFactory: () => ResponseOptions) => Observable<any>;
/** Get the in-mem service's copy of the "database" */
getDb(): {};
/** Get JSON body from the request object */

@@ -189,2 +192,4 @@ getJsonBody(req: any): any;

getLocation(url: string): UriInfo;
/** Get (or create) the "real" backend */
getPassThruBackend: PassThruBackend;
/**

@@ -202,4 +207,2 @@ * Parses the request URL into a `ParsedRequestUrl` object.

}>(collection: T[], id: any): T;
/** pass through backend, if it exists */
passThruBackend: PassThruBackend;
}

@@ -206,0 +209,0 @@ /**

@@ -1,1 +0,1 @@

[{"__symbolic":"module","version":3,"metadata":{"HeadersCore":{"__symbolic":"interface"},"InMemoryDbService":{"__symbolic":"class","members":{"createDb":[{"__symbolic":"method"}]}},"InMemoryBackendConfigArgs":{"__symbolic":"class"},"InMemoryBackendConfig":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"InMemoryBackendConfigArgs"}]}]}},"UriInfo":{"__symbolic":"interface"},"parseUri":{"__symbolic":"function"},"ParsedRequestUrl":{"__symbolic":"interface"},"PassThruBackend":{"__symbolic":"interface"},"removeTrailingSlash":{"__symbolic":"function","parameters":["path"],"value":{"__symbolic":"error","message":"Expression form not supported","line":196,"character":22}},"RequestCore":{"__symbolic":"interface"},"RequestInfo":{"__symbolic":"interface"},"RequestInfoUtilities":{"__symbolic":"interface"},"ResponseOptions":{"__symbolic":"interface"}}},{"__symbolic":"module","version":1,"metadata":{"HeadersCore":{"__symbolic":"interface"},"InMemoryDbService":{"__symbolic":"class","members":{"createDb":[{"__symbolic":"method"}]}},"InMemoryBackendConfigArgs":{"__symbolic":"class"},"InMemoryBackendConfig":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"InMemoryBackendConfigArgs"}]}]}},"UriInfo":{"__symbolic":"interface"},"parseUri":{"__symbolic":"function"},"ParsedRequestUrl":{"__symbolic":"interface"},"PassThruBackend":{"__symbolic":"interface"},"removeTrailingSlash":{"__symbolic":"function","parameters":["path"],"value":{"__symbolic":"error","message":"Expression form not supported","line":196,"character":22}},"RequestCore":{"__symbolic":"interface"},"RequestInfo":{"__symbolic":"interface"},"RequestInfoUtilities":{"__symbolic":"interface"},"ResponseOptions":{"__symbolic":"interface"}}}]
[{"__symbolic":"module","version":3,"metadata":{"HeadersCore":{"__symbolic":"interface"},"InMemoryDbService":{"__symbolic":"class","members":{"createDb":[{"__symbolic":"method"}]}},"InMemoryBackendConfigArgs":{"__symbolic":"class"},"InMemoryBackendConfig":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"InMemoryBackendConfigArgs"}]}]}},"UriInfo":{"__symbolic":"interface"},"parseUri":{"__symbolic":"function"},"ParsedRequestUrl":{"__symbolic":"interface"},"PassThruBackend":{"__symbolic":"interface"},"removeTrailingSlash":{"__symbolic":"function","parameters":["path"],"value":{"__symbolic":"error","message":"Expression form not supported","line":198,"character":22}},"RequestCore":{"__symbolic":"interface"},"RequestInfo":{"__symbolic":"interface"},"RequestInfoUtilities":{"__symbolic":"interface"},"ResponseOptions":{"__symbolic":"interface"}}},{"__symbolic":"module","version":1,"metadata":{"HeadersCore":{"__symbolic":"interface"},"InMemoryDbService":{"__symbolic":"class","members":{"createDb":[{"__symbolic":"method"}]}},"InMemoryBackendConfigArgs":{"__symbolic":"class"},"InMemoryBackendConfig":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"InMemoryBackendConfigArgs"}]}]}},"UriInfo":{"__symbolic":"interface"},"parseUri":{"__symbolic":"function"},"ParsedRequestUrl":{"__symbolic":"interface"},"PassThruBackend":{"__symbolic":"interface"},"removeTrailingSlash":{"__symbolic":"function","parameters":["path"],"value":{"__symbolic":"error","message":"Expression form not supported","line":198,"character":22}},"RequestCore":{"__symbolic":"interface"},"RequestInfo":{"__symbolic":"interface"},"RequestInfoUtilities":{"__symbolic":"interface"},"ResponseOptions":{"__symbolic":"interface"}}}]
{
"name": "angular-in-memory-web-api",
"version": "0.4.1",
"version": "0.4.2",
"description": "An in-memory web api for Angular demos and tests",

@@ -5,0 +5,0 @@ "main": "bundles/in-memory-web-api.umd.js",

@@ -175,3 +175,8 @@ # Angular in-memory-web-api

### Examples
* The `createDb` method can be synchronous or asynchronous.
so you can initialize your in-memory database service from a JSON file.
Return the database object, an observable of that object, or a promise of that object.
The in-mem web api service calls `createDb` (a) when it handles the _first_ `HttpClient` (or `Http`) request and (b) when it receives a `POST resetdb` request.
## In-memory web api examples
The tests (`src/app/*.spec.ts` files) in the [github repo](https://github.com/angular/in-memory-web-api/tree/master/src/app) are a good place to learn how to setup and use this in-memory web api library.

@@ -183,3 +188,3 @@

# Bonus Features
# Advanced Features
Some features are not readily apparent in the basic usage example.

@@ -199,3 +204,3 @@

1. If it looks like a [command](#commands), process as a command
2. If the [HTTP method is overridden](#method-override)
2. If the [HTTP method is overridden](#method-override), try the override.
3. If the resource name (after the api base path) matches one of the configured collections, process that

@@ -202,0 +207,0 @@ 4. If not but the `Config.passThruUnknownUrl` flag is `true`, try to [pass the request along to a real _XHR_](#passthru).

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc