@microsoft/paris
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -6,2 +6,3 @@ export interface DataSet<T> { | ||
previous?: string; | ||
meta?: object; | ||
} |
@@ -6,6 +6,6 @@ import { EntityModelConfigBase } from "../models/entity-config-base.interface"; | ||
import { EntityId } from "../models/entity-id.type"; | ||
export interface DataEntityConstructor<TEntity extends ModelBase, TRawData = any, TId extends EntityId = string> extends DataEntityType<TEntity, TRawData, TId> { | ||
export interface DataEntityConstructor<TEntity extends ModelBase, TRawData = any, TId extends EntityId = string, TDataSet = any> extends DataEntityType<TEntity, TRawData, TId, TDataSet> { | ||
new (data?: any, rawData?: TRawData): TEntity; | ||
} | ||
export interface DataEntityType<TEntity extends ModelBase = any, TRawData = any, TId extends EntityId = string> { | ||
export interface DataEntityType<TEntity extends ModelBase = any, TRawData = any, TId extends EntityId = string, TDataSet = any> { | ||
new (data?: EntityModelConfigBase, rawData?: TRawData): TEntity; | ||
@@ -12,0 +12,0 @@ singularName?: string; |
@@ -9,3 +9,4 @@ import { EntityConfigBase, IEntityConfigBase } from "./entity-config.base"; | ||
import { EntityId } from "../models/entity-id.type"; | ||
export declare class ModelEntity<TEntity extends ModelBase = any, TRawData = any, TId extends EntityId = string> extends EntityConfigBase<TEntity, TRawData, TId> implements EntityConfig<TEntity, TRawData, TId> { | ||
import { DataSet } from "../dataset/dataset"; | ||
export declare class ModelEntity<TEntity extends ModelBase = any, TRawData = any, TId extends EntityId = string, TDataSet = any> extends EntityConfigBase<TEntity, TRawData, TId> implements EntityConfig<TEntity, TRawData, TId> { | ||
endpoint: EntityConfigFunctionOrValue; | ||
@@ -21,2 +22,3 @@ loadAll?: boolean; | ||
}; | ||
parseDataSet?: (dataSet: TDataSet) => DataSet<TRawData>; | ||
parseItemQuery?: (itemId: EntityId, entity?: IEntityConfigBase<TEntity, TRawData, TId>, config?: ParisConfig, params?: { | ||
@@ -31,5 +33,5 @@ [index: string]: any; | ||
} | ||
export interface EntityConfig<TEntity extends ModelBase, TRawData = any, TId extends EntityId = string> extends IEntityConfigBase<TEntity, TRawData, TId>, EntityBackendConfig<TEntity, TRawData, TId> { | ||
export interface EntityConfig<TEntity extends ModelBase, TRawData = any, TId extends EntityId = string, TDataSet = any> extends IEntityConfigBase<TEntity, TRawData, TId>, EntityBackendConfig<TEntity, TRawData, TId, TDataSet> { | ||
} | ||
export interface EntityBackendConfig<TEntity extends ModelBase, TRawData = any, TId extends EntityId = string> extends ApiCallBackendConfigInterface { | ||
export interface EntityBackendConfig<TEntity extends ModelBase, TRawData = any, TId extends EntityId = string, TDataSet = any> extends ApiCallBackendConfigInterface { | ||
/** | ||
@@ -93,2 +95,13 @@ * If true, all the Entity's items are fetched whenever any is needed, and then cached so subsequent requests are retrieved from cache rather than backend. | ||
/** | ||
* For query results, Paris accepts either an array of items or an object. That object may contain properties such as 'count', 'next' and 'previous'. | ||
* `parseDataSet`, if available, receives the object as it was returned from the API and parses it to a DataSet interface, so the original properties are available in the DataSet. | ||
* | ||
* @example <caption>Parsing a DataSet from a raw object returned by the backend</caption> | ||
* ```typescript | ||
* parseDataSet: (rawDataSet:TodoRawDataSet) => ({ items: rawDataSet.todoItems, next: rawDataSet.$nextPage, count: rawDataSet.total }) | ||
* ``` | ||
* @param dataSet | ||
*/ | ||
parseDataSet?: (dataSet: TDataSet) => DataSet<TRawData>; | ||
/** | ||
* When getting an Entity from backend (when calling repository.getItemById), Paris follows the REST standard and fetches it by GET from /{the Entity's endpoint}/{ID}. | ||
@@ -95,0 +108,0 @@ * `parseItemQuery` allows to specify a different URL. This is useful if your API doesn't follow the REST standard. |
import { EntityConfig } from "./entity.config"; | ||
import { DataEntityType } from "./data-entity.base"; | ||
export declare function Entity(config: EntityConfig<any, any, any>): (target: DataEntityType<any, any, any>) => void; | ||
export declare function Entity(config: EntityConfig<any, any, any, any>): (target: DataEntityType<any, any, any, any>) => void; |
import { DataEntityType } from "./data-entity.base"; | ||
import { IEntityConfigBase } from "./entity-config.base"; | ||
export declare function ValueObject(config: IEntityConfigBase): (target: DataEntityType<any, any, string>) => void; | ||
export declare function ValueObject(config: IEntityConfigBase): (target: DataEntityType<any, any, string, any>) => void; |
@@ -8,4 +8,5 @@ import { ModelBase } from "../models/model.base"; | ||
import { DataSet } from "../dataset/dataset"; | ||
export declare function rawDataToDataSet<T extends ModelBase, R = any>(rawDataSet: any, entityConstructor: DataEntityConstructor<T>, allItemsProperty: string, paris: Paris, dataOptions?: DataOptions, query?: DataQuery): Observable<DataSet<T>>; | ||
export declare function modelArray<T extends ModelBase, R = any>(data: Array<any>, entityConstructor: DataEntityConstructor<T>, paris: Paris, dataOptions?: DataOptions, query?: DataQuery): Observable<Array<T>>; | ||
export declare function modelItem<T extends ModelBase, R = any>(entityConstructor: DataEntityConstructor<T>, data: R, paris: Paris, dataOptions?: DataOptions, query?: DataQuery): Observable<T>; | ||
export declare function rawDataToDataSet<TEntity extends ModelBase, TRawData = any, TDataSet extends any = any>(rawDataSet: TDataSet, entityConstructor: DataEntityConstructor<TEntity>, allItemsProperty: string, paris: Paris, dataOptions?: DataOptions, query?: DataQuery): Observable<DataSet<TEntity>>; | ||
export declare function parseDataSet<TRawData = any, TDataSet extends any = any>(rawDataSet: TDataSet, allItemsProperty?: string, parseDataSet?: (rawDataSet: TDataSet) => DataSet<TRawData>): DataSet<TRawData>; | ||
export declare function modelArray<TEntity extends ModelBase, TRawData = any>(rawData: Array<TRawData>, entityConstructor: DataEntityConstructor<TEntity>, paris: Paris, dataOptions?: DataOptions, query?: DataQuery): Observable<Array<TEntity>>; | ||
export declare function modelItem<TEntity extends ModelBase, TRawData = any>(entityConstructor: DataEntityConstructor<TEntity>, rawData: TRawData, paris: Paris, dataOptions?: DataOptions, query?: DataQuery): Observable<TEntity>; |
@@ -5,22 +5,28 @@ import { defaultDataOptions } from "../dataset/data.options"; | ||
import { map } from "rxjs/operators"; | ||
var DEFAULT_ALL_ITEMS_PROPERTY = 'items'; | ||
export function rawDataToDataSet(rawDataSet, entityConstructor, allItemsProperty, paris, dataOptions, query) { | ||
if (dataOptions === void 0) { dataOptions = defaultDataOptions; } | ||
var rawItems = rawDataSet instanceof Array ? rawDataSet : rawDataSet[allItemsProperty]; | ||
if (!rawItems || !rawItems.length) | ||
var dataSet = parseDataSet(rawDataSet, allItemsProperty, entityConstructor.entityConfig.parseDataSet); | ||
if (!dataSet.items || !dataSet.items.length) | ||
return of({ count: 0, items: [] }); | ||
return modelArray(rawItems, entityConstructor, paris, dataOptions, query).pipe(map(function (items) { | ||
return Object.freeze({ | ||
count: rawDataSet.count, | ||
return modelArray(dataSet.items, entityConstructor, paris, dataOptions, query).pipe(map(function (items) { | ||
return Object.freeze(Object.assign(dataSet, { | ||
items: items, | ||
next: rawDataSet.next, | ||
previous: rawDataSet.previous | ||
}); | ||
})); | ||
})); | ||
} | ||
export function modelArray(data, entityConstructor, paris, dataOptions, query) { | ||
export function parseDataSet(rawDataSet, allItemsProperty, parseDataSet) { | ||
if (allItemsProperty === void 0) { allItemsProperty = DEFAULT_ALL_ITEMS_PROPERTY; } | ||
return rawDataSet instanceof Array | ||
? { count: 0, items: rawDataSet } | ||
: parseDataSet | ||
? parseDataSet(rawDataSet) || { count: 0, items: [] } | ||
: { count: rawDataSet.count, items: rawDataSet[allItemsProperty] }; | ||
} | ||
export function modelArray(rawData, entityConstructor, paris, dataOptions, query) { | ||
if (dataOptions === void 0) { dataOptions = defaultDataOptions; } | ||
if (!data.length) | ||
if (!rawData.length) | ||
return of([]); | ||
else { | ||
var itemCreators = data.map(function (itemData) { | ||
var itemCreators = rawData.map(function (itemData) { | ||
return modelItem(entityConstructor, itemData, paris, dataOptions, query); | ||
@@ -31,5 +37,5 @@ }); | ||
} | ||
export function modelItem(entityConstructor, data, paris, dataOptions, query) { | ||
export function modelItem(entityConstructor, rawData, paris, dataOptions, query) { | ||
if (dataOptions === void 0) { dataOptions = defaultDataOptions; } | ||
return ReadonlyRepository.getModelData(data, entityConstructor.entityConfig || entityConstructor.valueObjectConfig, paris.config, paris, dataOptions, query); | ||
return ReadonlyRepository.getModelData(rawData, entityConstructor.entityConfig || entityConstructor.valueObjectConfig, paris.config, paris, dataOptions, query); | ||
} |
{ | ||
"name": "@microsoft/paris", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Library for the implementation of Domain Driven Design in Angular/TypeScript apps", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://microsoft.visualstudio.com/DefaultCollection/WDATP/_git/Paris" | ||
"url": "https://github.com/Microsoft/paris" | ||
}, | ||
@@ -15,3 +15,3 @@ "main": "dist/index.js", | ||
"prepublishOnly": "npm run build", | ||
"test": "mocha -r ts-node/register lib/**/*.spec.ts", | ||
"test": "mocha --opts ./test/mocha.opts", | ||
"docs": "typedoc --options typedocconfig.ts" | ||
@@ -54,3 +54,3 @@ }, | ||
"intl": "^1.2.5", | ||
"lodash.get": "^4.4.2", | ||
"lodash-es": "4.17.10", | ||
"merge2": "^1.0.2", | ||
@@ -57,0 +57,0 @@ "mocha": "^5.2.0", |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
472856
135
4076