@datorama/akita
Version:
State Management Tailored-Made for JS Applications
749 lines (738 loc) • 21.5 kB
JavaScript
import { HttpClient } from '@angular/common/http';
import { Subject, throwError } from 'rxjs';
import { filter, map, catchError, finalize, tap } from 'rxjs/operators';
import { __spread, __extends } from 'tslib';
import { Injectable, InjectionToken, defineInjectable, inject } from '@angular/core';
import { isFunction, isObject, isNumber, isString, EntityService, isDefined } from '@datorama/akita';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @enum {string} */
var HttpMethod = {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
PATCH: 'PATCH',
DELETE: 'DELETE',
};
/** @type {?} */
var ofType = (/**
* @param {?} type
* @return {?}
*/
function (type) { return filter((/**
* @param {?} action
* @return {?}
*/
function (action) { return action.type === type; })); });
/** @type {?} */
var filterMethod = (/**
* @param {?} method
* @return {?}
*/
function (method) {
return filter((/**
* @param {?} action
* @return {?}
*/
function (action) { return action.method === method; }));
});
/** @type {?} */
var filterStore = (/**
* @param {?} name
* @return {?}
*/
function (name) { return filter((/**
* @param {?} action
* @return {?}
*/
function (action) { return action.storeName === name; })); });
var NgEntityServiceNotifier = /** @class */ (function () {
function NgEntityServiceNotifier() {
this.dispatcher = new Subject();
this.action$ = this.dispatcher.asObservable();
}
/**
* @param {?} event
* @return {?}
*/
NgEntityServiceNotifier.prototype.dispatch = /**
* @param {?} event
* @return {?}
*/
function (event) {
this.dispatcher.next(event);
};
NgEntityServiceNotifier.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] }
];
/** @nocollapse */ NgEntityServiceNotifier.ngInjectableDef = defineInjectable({ factory: function NgEntityServiceNotifier_Factory() { return new NgEntityServiceNotifier(); }, token: NgEntityServiceNotifier, providedIn: "root" });
return NgEntityServiceNotifier;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var NgEntityServiceLoader = /** @class */ (function () {
function NgEntityServiceLoader() {
this.dispatcher = new Subject();
this.loading$ = this.dispatcher.asObservable();
}
/**
* @param {?} event
* @return {?}
*/
NgEntityServiceLoader.prototype.dispatch = /**
* @param {?} event
* @return {?}
*/
function (event) {
this.dispatcher.next(event);
};
/**
* @param {?=} name
* @return {?}
*/
NgEntityServiceLoader.prototype.loadersFor = /**
* @param {?=} name
* @return {?}
*/
function (name) {
var _this = this;
/** @type {?} */
var filterStore$$1 = filter((/**
* @param {?} __0
* @return {?}
*/
function (_a) {
var storeName = _a.storeName;
return (name ? storeName === name : true);
}));
/** @type {?} */
var filterMethod$$1 = (/**
* @param {?} mthd
* @return {?}
*/
function (mthd) {
return filter((/**
* @param {?} __0
* @return {?}
*/
function (_a) {
var method = _a.method;
return isFunction(mthd) ? mthd(method) : method === mthd;
}));
});
/** @type {?} */
var actionBased = (/**
* @param {?} current
* @return {?}
*/
function (current) {
return _this.loading$.pipe(filterStore$$1, filterMethod$$1(current), map((/**
* @param {?} action
* @return {?}
*/
function (action) { return action.loading; })));
});
/** @type {?} */
var idBased = (/**
* @param {?} id
* @param {?} mthd
* @return {?}
*/
function (id, mthd) {
return _this.loading$.pipe(filterStore$$1, filterMethod$$1(mthd), filter((/**
* @param {?} action
* @return {?}
*/
function (action) { return action.entityId === id; })), map((/**
* @param {?} action
* @return {?}
*/
function (action) { return action.loading; })));
});
return {
get$: actionBased(HttpMethod.GET),
add$: actionBased(HttpMethod.POST),
update$: actionBased((/**
* @param {?} method
* @return {?}
*/
function (method) { return method === HttpMethod.PUT || method === HttpMethod.PATCH; })),
delete$: actionBased(HttpMethod.DELETE),
getEntity: (/**
* @param {?} id
* @return {?}
*/
function (id) { return idBased(id, HttpMethod.GET); }),
updateEntity: (/**
* @param {?} id
* @return {?}
*/
function (id) { return idBased(id, (/**
* @param {?} method
* @return {?}
*/
function (method) { return method === HttpMethod.PUT || method === HttpMethod.PATCH; })); }),
deleteEntity: (/**
* @param {?} id
* @return {?}
*/
function (id) { return idBased(id, HttpMethod.DELETE); })
};
};
NgEntityServiceLoader.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] }
];
/** @nocollapse */ NgEntityServiceLoader.ngInjectableDef = defineInjectable({ factory: function NgEntityServiceLoader_Factory() { return new NgEntityServiceLoader(); }, token: NgEntityServiceLoader, providedIn: "root" });
return NgEntityServiceLoader;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var NG_ENTITY_SERVICE_CONFIG = new InjectionToken('NgEntityServiceGlobalConfig');
/** @type {?} */
var defaultConfig = {
httpMethods: {
GET: HttpMethod.GET,
POST: HttpMethod.POST,
PATCH: HttpMethod.PATCH,
PUT: HttpMethod.PUT,
DELETE: HttpMethod.DELETE
}
};
/**
* @param {?} target
* @param {...?} sources
* @return {?}
*/
function mergeDeep(target) {
var sources = [];
for (var _i = 1; _i < arguments.length; _i++) {
sources[_i - 1] = arguments[_i];
}
var _a, _b;
if (!sources.length)
return target;
/** @type {?} */
var source = sources.shift();
if (isObject(target) && isObject(source)) {
for (var key in source) {
if (isObject(source[key])) {
if (!target[key])
Object.assign(target, (_a = {}, _a[key] = {}, _a));
mergeDeep(target[key], source[key]);
}
else {
Object.assign(target, (_b = {}, _b[key] = source[key], _b));
}
}
}
return mergeDeep.apply(void 0, __spread([target], sources));
}
/**
* @param {?=} config
* @return {?}
*/
function NgEntityServiceConfig(config) {
if (config === void 0) { config = {}; }
return (/**
* @param {?} constructor
* @return {?}
*/
function (constructor) {
if (config.baseUrl) {
constructor['baseUrl'] = config.baseUrl;
}
if (config.resourceName) {
constructor['resourceName'] = config.resourceName;
}
});
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @param {?} idOrConfig
* @return {?}
*/
function isID(idOrConfig) {
return isNumber(idOrConfig) || isString(idOrConfig);
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @param {?} storeName
* @param {?} notifier
* @return {?}
*/
function successAction(storeName, notifier) {
return (/**
* @param {?} __0
* @return {?}
*/
function (_a) {
var payload = _a.payload, method = _a.method, successMsg = _a.successMsg;
notifier.dispatch({
type: 'success',
storeName: storeName,
payload: payload,
method: method,
successMsg: successMsg
});
});
}
/**
* @param {?} storeName
* @param {?} notifier
* @return {?}
*/
function errorAction(storeName, notifier) {
return (/**
* @param {?} __0
* @return {?}
*/
function (_a) {
var payload = _a.payload, method = _a.method, errorMsg = _a.errorMsg;
notifier.dispatch({
type: 'error',
storeName: storeName,
payload: payload,
method: method,
errorMsg: errorMsg
});
});
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var mapResponse = (/**
* @param {?} config
* @return {?}
*/
function (config) {
return map((/**
* @param {?} res
* @return {?}
*/
function (res) { return ((config || {}).mapResponseFn ? config.mapResponseFn(res) : res); }));
});
/**
* @template S
*/
var /**
* @template S
*/
NgEntityService = /** @class */ (function (_super) {
__extends(NgEntityService, _super);
function NgEntityService(store, config) {
if (config === void 0) { config = {}; }
var _this = _super.call(this) || this;
_this.store = store;
_this.config = config;
_this.globalConfig = {};
_this.http = inject(HttpClient);
_this.loader = inject(NgEntityServiceLoader);
_this.notifier = inject(NgEntityServiceNotifier);
_this.globalConfig = inject(NG_ENTITY_SERVICE_CONFIG);
_this.mergedConfig = mergeDeep(defaultConfig, _this.globalConfig, config);
_this.dispatchSuccess = successAction(_this.store.storeName, _this.notifier);
_this.dispatchError = errorAction(_this.store.storeName, _this.notifier);
return _this;
}
Object.defineProperty(NgEntityService.prototype, "api", {
get: /**
* @return {?}
*/
function () {
return (this.baseUrl || this.getConfigValue('baseUrl')) + "/" + this.resourceName;
},
enumerable: true,
configurable: true
});
Object.defineProperty(NgEntityService.prototype, "resourceName", {
get: /**
* @return {?}
*/
function () {
return this.getConfigValue('resourceName') || this.store.storeName;
},
enumerable: true,
configurable: true
});
/**
* @param {?} api
* @return {?}
*/
NgEntityService.prototype.setBaseUrl = /**
* @param {?} api
* @return {?}
*/
function (api) {
this.baseUrl = api;
};
/**
* @template T
* @param {?=} idOrConfig
* @param {?=} config
* @return {?}
*/
NgEntityService.prototype.get = /**
* @template T
* @param {?=} idOrConfig
* @param {?=} config
* @return {?}
*/
function (idOrConfig, config) {
var _this = this;
/** @type {?} */
var url;
/** @type {?} */
var isSingle = isID(idOrConfig);
/** @type {?} */
var _config = (isSingle ? config : idOrConfig) || {};
/** @type {?} */
var method = this.getHttpMethod(HttpMethod.GET);
if (_config.url) {
url = _config.url;
}
else {
url = isSingle ? this.api + "/" + idOrConfig : this.api;
}
this.loader.dispatch({
method: method,
loading: true,
entityId: isSingle ? idOrConfig : null,
storeName: this.store.storeName
});
return (/** @type {?} */ (this.http[method.toLowerCase()](url, _config).pipe(mapResponse(_config), tap((/**
* @param {?} data
* @return {?}
*/
function (data) {
if (isSingle) {
_this.store.upsert((/** @type {?} */ (idOrConfig)), data);
}
else {
_config.append ? _this.store.add(data) : _this.store.set(data);
}
_this.dispatchSuccess({
method: method,
payload: data,
successMsg: _config.successMsg
});
})), catchError((/**
* @param {?} error
* @return {?}
*/
function (error) { return _this.handleError(method, error, _config.errorMsg); })), finalize((/**
* @return {?}
*/
function () {
_this.loader.dispatch({
method: method,
loading: false,
storeName: _this.store.storeName
});
})))));
};
/**
*
* Add a new entity - Creates a POST request
*
* service.add(entity)
* service.add(entity, config)
*
*/
/**
*
* Add a new entity - Creates a POST request
*
* service.add(entity)
* service.add(entity, config)
*
* @template T
* @param {?} entity
* @param {?=} config
* @return {?}
*/
NgEntityService.prototype.add = /**
*
* Add a new entity - Creates a POST request
*
* service.add(entity)
* service.add(entity, config)
*
* @template T
* @param {?} entity
* @param {?=} config
* @return {?}
*/
function (entity, config) {
var _this = this;
/** @type {?} */
var method = this.getHttpMethod(HttpMethod.POST);
this.loader.dispatch({
method: method,
loading: true,
storeName: this.store.storeName
});
return (/** @type {?} */ (this.http[method.toLowerCase()](this.resolveUrl(config), entity, config).pipe(mapResponse(config), tap((/**
* @param {?} entity
* @return {?}
*/
function (entity) {
_this.store.add(entity, config);
_this.dispatchSuccess({
method: method,
payload: entity,
successMsg: config && config.successMsg
});
})), catchError((/**
* @param {?} error
* @return {?}
*/
function (error) { return _this.handleError(method, error, config && config.errorMsg); })), finalize((/**
* @return {?}
*/
function () {
_this.loader.dispatch({
method: method,
loading: false,
storeName: _this.store.storeName
});
})))));
};
/**
*
* Update an entity - Creates a PUT/PATCH request
*
* service.update(id, entity)
* service.update(id, entity, config)
*
*/
/**
*
* Update an entity - Creates a PUT/PATCH request
*
* service.update(id, entity)
* service.update(id, entity, config)
*
* @template T
* @param {?} id
* @param {?} entity
* @param {?=} config
* @return {?}
*/
NgEntityService.prototype.update = /**
*
* Update an entity - Creates a PUT/PATCH request
*
* service.update(id, entity)
* service.update(id, entity, config)
*
* @template T
* @param {?} id
* @param {?} entity
* @param {?=} config
* @return {?}
*/
function (id, entity, config) {
var _this = this;
/** @type {?} */
var method = config && config.method ? config.method : this.getHttpMethod(HttpMethod.PUT);
this.loader.dispatch({
method: method,
loading: true,
entityId: id,
storeName: this.store.storeName
});
return (/** @type {?} */ (this.http[method.toLocaleLowerCase()](this.resolveUrl(config, id), entity, config).pipe(mapResponse(config), tap((/**
* @param {?} entity
* @return {?}
*/
function (entity) {
_this.store.update(id, (/** @type {?} */ (entity)));
_this.dispatchSuccess({
method: method,
payload: entity,
successMsg: config && config.successMsg
});
})), catchError((/**
* @param {?} error
* @return {?}
*/
function (error) { return _this.handleError(method, error, config && config.errorMsg); })), finalize((/**
* @return {?}
*/
function () {
_this.loader.dispatch({
method: method,
loading: false,
entityId: id,
storeName: _this.store.storeName
});
})))));
};
/**
*
* Delete an entity - Creates a DELETE request
*
* service.delete(id)
* service.delete(id, config)
*
*/
/**
*
* Delete an entity - Creates a DELETE request
*
* service.delete(id)
* service.delete(id, config)
*
* @template T
* @param {?} id
* @param {?=} config
* @return {?}
*/
NgEntityService.prototype.delete = /**
*
* Delete an entity - Creates a DELETE request
*
* service.delete(id)
* service.delete(id, config)
*
* @template T
* @param {?} id
* @param {?=} config
* @return {?}
*/
function (id, config) {
var _this = this;
/** @type {?} */
var method = this.getHttpMethod(HttpMethod.DELETE);
this.loader.dispatch({
method: method,
loading: true,
entityId: id,
storeName: this.store.storeName
});
return (/** @type {?} */ (this.http[method.toLowerCase()](this.resolveUrl(config, id), config).pipe(mapResponse(config), tap((/**
* @param {?} res
* @return {?}
*/
function (res) {
_this.store.remove(id);
_this.dispatchSuccess({
method: method,
payload: res,
successMsg: config && config.successMsg
});
})), catchError((/**
* @param {?} error
* @return {?}
*/
function (error) { return _this.handleError(method, error, config && config.errorMsg); })), finalize((/**
* @return {?}
*/
function () {
_this.loader.dispatch({
method: method,
loading: false,
entityId: id,
storeName: _this.store.storeName
});
})))));
};
/**
* @private
* @param {?} type
* @return {?}
*/
NgEntityService.prototype.getHttpMethod = /**
* @private
* @param {?} type
* @return {?}
*/
function (type) {
return this.mergedConfig.httpMethods[type];
};
/**
* @private
* @param {?} key
* @return {?}
*/
NgEntityService.prototype.getConfigValue = /**
* @private
* @param {?} key
* @return {?}
*/
function (key) {
return this.constructor[key] || this.mergedConfig[key];
};
/**
* @private
* @param {?} config
* @param {?=} id
* @return {?}
*/
NgEntityService.prototype.resolveUrl = /**
* @private
* @param {?} config
* @param {?=} id
* @return {?}
*/
function (config, id) {
/** @type {?} */
var customUrl = (config || {}).url;
if (isDefined(id)) {
return customUrl || this.api + "/" + id;
}
return customUrl || this.api;
};
/**
* @private
* @param {?} method
* @param {?} error
* @param {?} errorMsg
* @return {?}
*/
NgEntityService.prototype.handleError = /**
* @private
* @param {?} method
* @param {?} error
* @param {?} errorMsg
* @return {?}
*/
function (method, error, errorMsg) {
this.dispatchError({
method: method,
errorMsg: errorMsg,
payload: error
});
return throwError(error);
};
return NgEntityService;
}(EntityService));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { mapResponse, NgEntityService, mergeDeep, NgEntityServiceConfig, NG_ENTITY_SERVICE_CONFIG, defaultConfig, HttpMethod, ofType, filterMethod, filterStore, NgEntityServiceNotifier, isID, NgEntityServiceLoader, successAction, errorAction };
//# sourceMappingURL=datorama-akita-angular-playground-projects-ng-entity-service.js.map